cables_dev/cables_electron/src_client/cables_electron.js
- import { Logger } from "cables-shared-client";
- import ElectronEditor from "./electron_editor.js";
- import electronCommands from "./cmd_electron.js";
- export default class CablesElectron
- {
- constructor()
- {
- this._logger = new Logger("electron");
- this._electron = window.nodeRequire("electron");
- this._importSync = window.nodeRequire("import-sync");
- window.ipcRenderer = this._electron.ipcRenderer;
- this._settings = this._electron.ipcRenderer.sendSync("platformSettings") || {};
- this._usersettings = this._settings.userSettings;
- delete this._settings.userSettings;
- this._config = this._electron.ipcRenderer.sendSync("cablesConfig") || {};
- this.editorIframe = null;
- this._startUpLogItems = this._electron.ipcRenderer.sendSync("getStartupLog") || [];
- if (!this._config.isPackaged) window.ELECTRON_DISABLE_SECURITY_WARNINGS = true;
- this._loadedModules = {};
- }
-
- get gui()
- {
- return this.editorWindow ? this.editorWindow.gui : null;
- }
-
- get editorWindow()
- {
- return this.editorIframe.contentWindow;
- }
-
- get CABLES()
- {
- return this.editorWindow ? this.editorWindow.CABLES : null;
- }
-
- get _log()
- {
- CABLES.UI = this.CABLES.UI;
- return this._logger;
- }
-
- init()
- {
- this.editorIframe = document.getElementById("editorIframe");
- let src = this._config.uiIndexHtml + window.location.search;
- if (window.location.hash)
- {
- src += window.location.hash;
- }
- this.editorIframe.src = src;
- this.editorIframe.onload = () =>
- {
- if (this.editorWindow)
- {
- const waitForUi = this.editorWindow.waitForAce;
- this.editorWindow.waitForAce = () =>
- {
- this._logStartup("loading", this._settings.patchFile);
- this._incrementStartup();
- this._logStartup("checking/installing op dependencies...");
- this._electron.ipcRenderer.invoke("talkerMessage", "installProjectDependencies").then((npmResult) =>
- {
- this.editorWindow.CABLESUILOADER.cfg.patchConfig.onError = (...args) =>
- {
-
- if (args && args[0] === "core_patch" && args[2] && args[2].message && args[2].message.includes("was compiled against a different Node.js version"))
- {
- const dirParts = args[2].message.split("/");
- const opNameIndex = dirParts.findIndex((part) => { return part.startsWith("Ops."); });
- const opName = dirParts[opNameIndex];
- const packageName = dirParts[opNameIndex + 2];
- const onClick = "CABLES.CMD.ELECTRON.openOpDir('', '" + opName + "');";
- const msg = "try running this <a onclick=\"" + onClick + "\" > in the op dir</a>:";
- this._log.error(msg);
- this._log.error("`npm --prefix ./ install " + packageName + "`");
- this._log.error("`npx \"@electron/rebuild\" -v " + process.versions.electron);
- }
- };
- waitForUi();
- if (this.gui)
- {
- this.gui.on("uiloaded", () =>
- {
- if (this.editor && this.editor.config && !this.editor.config.patchFile) this.gui.setStateUnsaved();
- });
- const corePatch = this.gui.corePatch();
- corePatch.on("onOpAdd", (op) =>
- {
- this._electron.ipcRenderer.invoke("documentChanged");
- });
- }
- if (npmResult.error && npmResult.data && npmResult.msg !== "UNSAVED_PROJECT")
- {
- npmResult.data.forEach((msg) =>
- {
- const opName = msg.opName ? " for " + msg.opName : "";
- this._log.error("failed dependency" + opName + ": " + msg.stderr);
- });
- }
- else if (npmResult.msg !== "EMPTY" && npmResult.msg !== "UNSAVED_PROJECT")
- {
- npmResult.data.forEach((result) =>
- {
- const npmText = result.stderr || result.stdout;
- this._logStartup(result.opName + ": " + npmText);
- });
- }
- if (this.gui)
- {
- this.gui.on("uiloaded", () =>
- {
- if (this._settings.maximizeRenderer) this.gui.toggleMaximizeCanvas();
- });
- }
- });
- };
- if (this._settings.uiLoadStart) this.editorWindow.CABLESUILOADER.uiLoadStart -= this._settings.uiLoadStart;
- this._startUpLogItems.forEach((logEntry) =>
- {
- this._logStartup(logEntry.title);
- });
- if (this.editorWindow.loadjs)
- {
- this.editorWindow.loadjs.ready("cablesuinew", this._uiReady.bind(this));
- }
- }
- };
- window.addEventListener("message", (event) =>
- {
- if (event.data && event.data.type === "hashchange")
- {
- window.location.hash = event.data.data;
- }
- }, false);
- window.addEventListener("hashchange", () =>
- {
- if (this.editorWindow)
- {
- this.editorWindow.postMessage({ "type": "hashchange", "data": window.location.hash }, "*");
- }
- }, false);
- this.editor = new ElectronEditor({
- "config": {
- ...this._settings,
- "isTrustedPatch": true,
- "platformClass": "PlatformElectron",
- "urlCables": "cables://",
- "urlSandbox": "cables://",
- "communityUrl": this._config.communityUrl,
- "user": this._settings.currentUser,
- "usersettings": { "settings": this._usersettings },
- "isDevEnv": !this._config.isPackaged,
- "env": this._config.env,
- "patchId": this._settings.patchId,
- "patchVersion": "",
- "socketcluster": {},
- "remoteClient": false,
- "buildInfo": this._settings.buildInfo,
- "patchConfig": {
- "allowEdit": true,
- "prefixAssetPath": this._settings.currentPatchDir,
- "assetPath": this._settings.paths.assetPath,
- "paths": this._settings.paths
- },
- }
- });
- }
- openOpDirsTab()
- {
- if (this.CABLES) this.CABLES.platform.openOpDirsTab();
- }
- _uiReady()
- {
- if (this.CABLES)
- {
- if (this.CABLES.Op)
- {
- const cablesElectron = this;
- this.CABLES.Op.prototype.require = function (moduleName)
- {
- return cablesElectron._opRequire(moduleName, this, cablesElectron);
- };
- }
- this.CABLES.UI.DEFAULTOPNAMES.defaultOpFallback = this.CABLES.UI.DEFAULTOPNAMES.HttpRequest;
- this.CABLES.CMD.ELECTRON = electronCommands.functions;
- this.CABLES.CMD.commands = this.CABLES.CMD.commands.concat(electronCommands.commands);
- Object.assign(this.CABLES.CMD.UI, electronCommands.functionOverrides.UI);
- Object.assign(this.CABLES.CMD.PATCH, electronCommands.functionOverrides.PATCH);
- Object.assign(this.CABLES.CMD.RENDERER, electronCommands.functionOverrides.RENDERER);
- const commandOverrides = electronCommands.commandOverrides;
- this.CABLES.CMD.commands.forEach((command) =>
- {
- const commandOverride = commandOverrides.find((override) => { return override.cmd === command.cmd; });
- if (commandOverride)
- {
- Object.assign(command, commandOverride);
- }
- });
- }
- }
- _opRequire(moduleName, op, thisClass)
- {
- if (op) op.setUiError("oprequire", null);
- if (moduleName === "electron") return thisClass._electron;
- if (this._loadedModules[moduleName]) return this._loadedModules[moduleName];
- let modulePath = null;
- let moduleFile = null;
- try
- {
-
- modulePath = window.ipcRenderer.sendSync("getOpModuleDir", { "opName": op.objName, "opId": op.opId, "moduleName": moduleName });
- this._loadedModules[moduleName] = window.nodeRequire(modulePath);
- return this._loadedModules[moduleName];
- }
- catch (ePath)
- {
- try
- {
-
- moduleFile = window.ipcRenderer.sendSync("getOpModuleLocation", { "opName": op.objName, "opId": op.opId, "moduleName": moduleName });
- this._loadedModules[moduleName] = window.nodeRequire(moduleFile);
- return this._loadedModules[moduleName];
- }
- catch (eFile)
- {
- try
- {
-
- this._loadedModules[moduleName] = window.nodeRequire(moduleName);
- return this._loadedModules[moduleName];
- }
- catch (eName)
- {
- try
- {
- moduleFile = window.ipcRenderer.sendSync("getOpModuleLocation", { "opName": op.objName || op.name, "opId": op.opId, "moduleName": moduleName, });
- this._loadedModules[moduleName] = this._importSync(moduleFile);
- return this._loadedModules[moduleName];
- }
- catch (eImport)
- {
- let errorMessage = "failed to load node module: " + moduleName + "\n\n";
- if (op) op.setUiError("oprequire", errorMessage);
- this._log.error(errorMessage, modulePath, moduleFile);
- return { };
- }
- }
- }
- }
- }
- _logStartup(title)
- {
- if (this.editorWindow && this.editorWindow.logStartup) this.editorWindow.logStartup(title);
- }
- _incrementStartup()
- {
- if (this.editorWindow && this.editorWindow.logStartup) this.editorWindow.incrementStartup();
- }
- }