|
|
@ -1,6 +1,9 @@
|
|
|
|
/* eslint-disable quotes,no-undef */
|
|
|
|
/* eslint-disable quotes,no-undef */
|
|
|
|
|
|
|
|
|
|
|
|
const { app, BrowserWindow, Menu, MenuItem, ipcMain, shell } = require("electron");
|
|
|
|
const { app, BrowserWindow, Menu, MenuItem, ipcMain, shell } = require("electron");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.commandLine.appendSwitch("in-process-gpu");
|
|
|
|
|
|
|
|
|
|
|
|
const path = require("path");
|
|
|
|
const path = require("path");
|
|
|
|
const url = require("url");
|
|
|
|
const url = require("url");
|
|
|
|
const fs = require("fs");
|
|
|
|
const fs = require("fs");
|
|
|
@ -49,7 +52,7 @@ function createWindow() {
|
|
|
|
nodeIntegration: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
webSecurity: false,
|
|
|
|
webSecurity: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
allowRunningInsecureContent: false,
|
|
|
|
// allowRunningInsecureContent: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (isLocal) {
|
|
|
|
if (isLocal) {
|
|
|
@ -63,7 +66,7 @@ function createWindow() {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
win.webContents.session.clearCache();
|
|
|
|
win.webContents.session.clearCache(() => null);
|
|
|
|
win.webContents.session.clearStorageData();
|
|
|
|
win.webContents.session.clearStorageData();
|
|
|
|
|
|
|
|
|
|
|
|
win.webContents.on("new-window", (event, pth) => {
|
|
|
|
win.webContents.on("new-window", (event, pth) => {
|
|
|
@ -81,7 +84,7 @@ function createWindow() {
|
|
|
|
|
|
|
|
|
|
|
|
const mainItem = new MenuItem({
|
|
|
|
const mainItem = new MenuItem({
|
|
|
|
label: "Toggle Dev Tools",
|
|
|
|
label: "Toggle Dev Tools",
|
|
|
|
click: () => win.toggleDevTools(),
|
|
|
|
click: () => win.webContents.toggleDevTools(),
|
|
|
|
accelerator: "F12",
|
|
|
|
accelerator: "F12",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
menu.append(mainItem);
|
|
|
|
menu.append(mainItem);
|
|
|
@ -161,20 +164,20 @@ async function writeFileSafe(filename, contents) {
|
|
|
|
console.warn(prefix, "Concurrent write process on", filename);
|
|
|
|
console.warn(prefix, "Concurrent write process on", filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fileLock.acquire(filename, async () => {
|
|
|
|
await fileLock.acquire(filename, async () => {
|
|
|
|
console.log(prefix, "Starting write on", niceFileName(filename), "in transaction", transactionId);
|
|
|
|
console.log(prefix, "Starting write on", niceFileName(filename), "in transaction", transactionId);
|
|
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(filename)) {
|
|
|
|
if (!fs.existsSync(filename)) {
|
|
|
|
// this one is easy
|
|
|
|
// this one is easy
|
|
|
|
console.log(prefix, "Writing file instantly because it does not exist:", niceFileName(filename));
|
|
|
|
console.log(prefix, "Writing file instantly because it does not exist:", niceFileName(filename));
|
|
|
|
await fs.promises.writeFile(filename, contents, { encoding: "utf8" });
|
|
|
|
fs.writeFileSync(filename, contents, { encoding: "utf8" });
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// first, write a temporary file (.tmp-XXX)
|
|
|
|
// first, write a temporary file (.tmp-XXX)
|
|
|
|
const tempName = filename + ".tmp-" + transactionId;
|
|
|
|
const tempName = filename + ".tmp-" + transactionId;
|
|
|
|
console.log(prefix, "Writing temporary file", niceFileName(tempName));
|
|
|
|
console.log(prefix, "Writing temporary file", niceFileName(tempName));
|
|
|
|
await fs.promises.writeFile(tempName, contents, { encoding: "utf8" });
|
|
|
|
fs.writeFileSync(tempName, contents, { encoding: "utf8" });
|
|
|
|
|
|
|
|
|
|
|
|
// now, rename the original file to (.backup-XXX)
|
|
|
|
// now, rename the original file to (.backup-XXX)
|
|
|
|
const oldTemporaryName = filename + ".backup-" + transactionId;
|
|
|
|
const oldTemporaryName = filename + ".backup-" + transactionId;
|
|
|
@ -185,7 +188,7 @@ async function writeFileSafe(filename, contents) {
|
|
|
|
"to",
|
|
|
|
"to",
|
|
|
|
niceFileName(oldTemporaryName)
|
|
|
|
niceFileName(oldTemporaryName)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
await fs.promises.rename(filename, oldTemporaryName);
|
|
|
|
fs.renameSync(filename, oldTemporaryName);
|
|
|
|
|
|
|
|
|
|
|
|
// now, rename the temporary file (.tmp-XXX) to the target
|
|
|
|
// now, rename the temporary file (.tmp-XXX) to the target
|
|
|
|
console.log(
|
|
|
|
console.log(
|
|
|
@ -195,7 +198,7 @@ async function writeFileSafe(filename, contents) {
|
|
|
|
"to the original",
|
|
|
|
"to the original",
|
|
|
|
niceFileName(filename)
|
|
|
|
niceFileName(filename)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
await fs.promises.rename(tempName, filename);
|
|
|
|
fs.renameSync(tempName, filename);
|
|
|
|
|
|
|
|
|
|
|
|
// we are done now, try to create a backup, but don't fail if the backup fails
|
|
|
|
// we are done now, try to create a backup, but don't fail if the backup fails
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -204,12 +207,12 @@ async function writeFileSafe(filename, contents) {
|
|
|
|
if (fs.existsSync(backupFileName)) {
|
|
|
|
if (fs.existsSync(backupFileName)) {
|
|
|
|
console.log(prefix, "Deleting old backup file", niceFileName(backupFileName));
|
|
|
|
console.log(prefix, "Deleting old backup file", niceFileName(backupFileName));
|
|
|
|
// delete the old backup
|
|
|
|
// delete the old backup
|
|
|
|
await fs.promises.unlink(backupFileName);
|
|
|
|
fs.unlinkSync(backupFileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// rename the old file to the new backup file
|
|
|
|
// rename the old file to the new backup file
|
|
|
|
console.log(prefix, "Moving", niceFileName(oldTemporaryName), "to the backup file location");
|
|
|
|
console.log(prefix, "Moving", niceFileName(oldTemporaryName), "to the backup file location");
|
|
|
|
await fs.promises.rename(oldTemporaryName, backupFileName);
|
|
|
|
fs.renameSync(oldTemporaryName, backupFileName);
|
|
|
|
} catch (ex) {
|
|
|
|
} catch (ex) {
|
|
|
|
console.error(prefix, "Failed to switch backup files:", ex);
|
|
|
|
console.error(prefix, "Failed to switch backup files:", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -229,12 +232,13 @@ async function performFsJob(job) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const data = await fs.promises.readFile(fname, { encoding: "utf8" });
|
|
|
|
const data = fs.readFileSync(fname, { encoding: "utf8" });
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
success: true,
|
|
|
|
data,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} catch (ex) {
|
|
|
|
} catch (ex) {
|
|
|
|
|
|
|
|
console.error(ex);
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
error: ex,
|
|
|
|
error: ex,
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -242,12 +246,13 @@ async function performFsJob(job) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "write": {
|
|
|
|
case "write": {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await writeFileSafe(fname, job.contents);
|
|
|
|
writeFileSafe(fname, job.contents);
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
success: true,
|
|
|
|
data: job.contents,
|
|
|
|
data: job.contents,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} catch (ex) {
|
|
|
|
} catch (ex) {
|
|
|
|
|
|
|
|
console.error(ex);
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
error: ex,
|
|
|
|
error: ex,
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -256,8 +261,9 @@ async function performFsJob(job) {
|
|
|
|
|
|
|
|
|
|
|
|
case "delete": {
|
|
|
|
case "delete": {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await fs.promises.unlink(fname);
|
|
|
|
fs.unlinkSync(fname);
|
|
|
|
} catch (ex) {
|
|
|
|
} catch (ex) {
|
|
|
|
|
|
|
|
console.error(ex);
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
error: ex,
|
|
|
|
error: ex,
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -274,7 +280,9 @@ async function performFsJob(job) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ipcMain.handle("fs-job", (event, arg) => performFsJob(arg));
|
|
|
|
ipcMain.on("fs-job", async (event, arg) => {
|
|
|
|
|
|
|
|
const result = await performFsJob(arg);
|
|
|
|
|
|
|
|
event.sender.send("fs-response", { id: arg.id, result });
|
|
|
|
|
|
|
|
});
|
|
|
|
wegame.init(isDev);
|
|
|
|
wegame.init(isDev);
|
|
|
|
wegame.listen();
|
|
|
|
wegame.listen();
|
|
|
|