Skip to content

Commit

Permalink
Modify Electron wrapper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dengr1065 committed Jul 27, 2024
1 parent 7bc48c1 commit a2e192f
Showing 1 changed file with 8 additions and 52 deletions.
60 changes: 8 additions & 52 deletions electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit a2e192f

Please sign in to comment.