From 7bc48c1f28f8e269ea05c663e93221047304f7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=97=D0=BB=20=D0=93=D1=80=D0=B8?= =?UTF-8?q?=D0=B3=D0=BE=D1=80=27=D1=94=D0=B2?= Date: Sat, 27 Jul 2024 22:32:52 +0300 Subject: [PATCH 1/2] Update Electron --- electron/package.json | 10 +++++----- electron/yarn.lock | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/electron/package.json b/electron/package.json index 2d65a371..3371a784 100644 --- a/electron/package.json +++ b/electron/package.json @@ -5,15 +5,15 @@ "license": "MIT", "private": true, "scripts": { - "startDev": "electron --disable-direct-composition --in-process-gpu . --dev --local", - "startDevGpu": "electron --enable-gpu-rasterization --enable-accelerated-2d-canvas --num-raster-threads=8 --enable-zero-copy . --dev --local", - "start": "electron --disable-direct-composition --in-process-gpu ." + "start": "electron .", + "startDev": "electron . --dev" + }, + "devDependencies": { + "electron": "^31.3.0" }, - "devDependencies": {}, "optionalDependencies": {}, "dependencies": { "async-lock": "^1.4.1", - "electron": "^30.0.0", "electron-window-state": "^5.0.3" } } diff --git a/electron/yarn.lock b/electron/yarn.lock index d664a30f..cb67c4a1 100644 --- a/electron/yarn.lock +++ b/electron/yarn.lock @@ -162,10 +162,10 @@ electron-window-state@^5.0.3: jsonfile "^4.0.0" mkdirp "^0.5.1" -electron@^30.0.0: - version "30.0.1" - resolved "https://registry.yarnpkg.com/electron/-/electron-30.0.1.tgz#2caf0eb7ed591b9b9842b522421bcae3aa8293d6" - integrity sha512-iwxkI/n2wBd29NH7TH0ZY8aWGzCoKpzJz+D10u7aGSJi1TV6d4MSM3rWyKvT/UkAHkTKOEgYfUyCa2vWQm8L0g== +electron@^31.3.0: + version "31.3.0" + resolved "https://registry.yarnpkg.com/electron/-/electron-31.3.0.tgz#4a084a8229d5bd829c33b8b65073381d0e925093" + integrity sha512-3LMRMmK4UK0A+jYSLGLYdfhc20TgY2v5jD3iGmhRZlDYj0gn7xBj/waRjlNalysZ0D2rgPvoes0wHuf5e/Bguw== dependencies: "@electron/get" "^2.0.0" "@types/node" "^20.9.0" From a2e192f610b817fb5e1787717994f5bb48112a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=97=D0=BB=20=D0=93=D1=80=D0=B8?= =?UTF-8?q?=D0=B3=D0=BE=D1=80=27=D1=94=D0=B2?= Date: Sat, 27 Jul 2024 23:08:57 +0300 Subject: [PATCH 2/2] Modify Electron wrapper Minimal set of changes, not a refactor yet. Make shapez store all of its data in a single directory whose path is provided by Electron APIs. Useless checks and web preferences are removed and app.isPackaged is used to detect whether the app should try to load a localhost URL. --- electron/index.js | 60 +++++++---------------------------------------- 1 file changed, 8 insertions(+), 52 deletions(-) diff --git a/electron/index.js b/electron/index.js index 76e10fdf..db60e086 100644 --- a/electron/index.js +++ b/electron/index.js @@ -10,27 +10,17 @@ const windowStateKeeper = require("electron-window-state"); app.commandLine.appendSwitch("disable-features", "HardwareMediaKeyHandling"); const isDev = app.commandLine.hasSwitch("dev"); -const isLocal = app.commandLine.hasSwitch("local"); const safeMode = app.commandLine.hasSwitch("safe-mode"); const externalMod = app.commandLine.getSwitchValue("load-mod"); -const roamingFolder = - process.env.APPDATA || - (process.platform == "darwin" - ? process.env.HOME + "/Library/Preferences" - : process.env.HOME + "/.local/share"); +app.setName("shapez-ce"); +const userData = app.getPath("userData"); -let storePath = path.join(roamingFolder, "shapez.io", "saves"); -let modsPath = path.join(roamingFolder, "shapez.io", "mods"); +const storePath = path.join(userData, "saves"); +const modsPath = path.join(userData, "mods"); -if (!fs.existsSync(storePath)) { - // No try-catch by design - fs.mkdirSync(storePath, { recursive: true }); -} - -if (!fs.existsSync(modsPath)) { - fs.mkdirSync(modsPath, { recursive: true }); -} +fs.mkdirSync(storePath, { recursive: true }); +fs.mkdirSync(modsPath, { recursive: true }); /** @type {BrowserWindow} */ let win = null; @@ -63,24 +53,14 @@ function createWindow() { // fullscreen: true, autoHideMenuBar: !isDev, webPreferences: { - nodeIntegration: false, - nodeIntegrationInWorker: false, - nodeIntegrationInSubFrames: false, - contextIsolation: true, - enableRemoteModule: false, disableBlinkFeatures: "Auxclick", - - webSecurity: true, - sandbox: true, preload: path.join(__dirname, "preload.js"), - experimentalFeatures: false, }, - allowRunningInsecureContent: false, }); mainWindowState.manage(win); - if (isLocal) { + if (!app.isPackaged) { win.loadURL("http://localhost:3005"); } else { win.loadURL( @@ -123,30 +103,6 @@ function createWindow() { contentsEvent.preventDefault(); }); - // Filter loading any module via remote; - // you shouldn't be using remote at all, though - // https://electronjs.org/docs/tutorial/security#16-filter-the-remote-module - app.on("remote-require", (event, webContents, moduleName) => { - event.preventDefault(); - }); - - // built-ins are modules such as "app" - app.on("remote-get-builtin", (event, webContents, moduleName) => { - event.preventDefault(); - }); - - app.on("remote-get-global", (event, webContents, globalName) => { - event.preventDefault(); - }); - - app.on("remote-get-current-window", (event, webContents) => { - event.preventDefault(); - }); - - app.on("remote-get-current-web-contents", (event, webContents) => { - event.preventDefault(); - }); - //// END SECURITY win.webContents.on("will-navigate", (event, pth) => { @@ -313,7 +269,7 @@ async function writeFileSafe(filename, contents) { } ipcMain.handle("fs-job", async (event, job) => { - const filenameSafe = job.filename.replace(/[^a-z\.\-_0-9]/gi, "_"); + const filenameSafe = job.filename.replace(/[^a-z.\-_0-9]/gi, "_"); const fname = path.join(storePath, filenameSafe); switch (job.type) { case "read": {