Skip to content

Commit

Permalink
ghuh
Browse files Browse the repository at this point in the history
  • Loading branch information
Nintendoboi22 authored Dec 9, 2024
1 parent 4aca557 commit df345ea
Show file tree
Hide file tree
Showing 97 changed files with 4,780 additions and 0 deletions.
1 change: 1 addition & 0 deletions shapez-io/electron_wegame/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
To build, place the lib64 folder from the wegame sdk for electron 13 in `wegame_sdk` and run the `wegame.main.standalone` gulp task.
13 changes: 13 additions & 0 deletions shapez-io/electron_wegame/electron_wegame.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.exclude": {
"**/node_modules": true,
"**/typedefs_gen": true
}
}
}
Binary file added shapez-io/electron_wegame/favicon.icns
Binary file not shown.
Binary file added shapez-io/electron_wegame/favicon.ico
Binary file not shown.
Binary file added shapez-io/electron_wegame/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
253 changes: 253 additions & 0 deletions shapez-io/electron_wegame/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/* eslint-disable quotes,no-undef */

const { app, BrowserWindow, Menu, MenuItem, ipcMain, shell } = require("electron");

app.commandLine.appendSwitch("in-process-gpu");

const path = require("path");
const url = require("url");
const fs = require("fs");
const wegame = require("./wegame");
const asyncLock = require("async-lock");

const isDev = process.argv.indexOf("--dev") >= 0;
const isLocal = process.argv.indexOf("--local") >= 0;

const roamingFolder =
process.env.APPDATA ||
(process.platform == "darwin"
? process.env.HOME + "/Library/Preferences"
: process.env.HOME + "/.local/share");
let storePath = path.join(roamingFolder, "shapez.io", "saves");

if (!fs.existsSync(storePath)) {
// No try-catch by design
fs.mkdirSync(storePath, { recursive: true });
}

/** @type {BrowserWindow} */
let win = null;
let menu = null;

function createWindow() {
let faviconExtension = ".png";
if (process.platform === "win32") {
faviconExtension = ".ico";
}

win = new BrowserWindow({
width: 1280,
height: 800,
show: false,
backgroundColor: "#222428",
useContentSize: true,
minWidth: 800,
minHeight: 600,
title: "图形工厂",
transparent: false,
icon: path.join(__dirname, "favicon" + faviconExtension),
// fullscreen: true,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
webSecurity: true,
sandbox: true,

contextIsolation: true,
preload: path.join(__dirname, "preload.js"),
},
allowRunningInsecureContent: false,
});

if (isLocal) {
win.loadURL("http://localhost:3005");
} else {
win.loadURL(
url.format({
pathname: path.join(__dirname, "index.html"),
protocol: "file:",
slashes: true,
})
);
}
win.webContents.session.clearCache(() => null);
win.webContents.session.clearStorageData();

win.webContents.on("new-window", (event, pth) => {
event.preventDefault();
shell.openExternal(pth);
});

win.on("closed", () => {
console.log("Window closed");
win = null;
});

if (isDev) {
menu = new Menu();

const mainItem = new MenuItem({
label: "Toggle Dev Tools",
click: () => win.webContents.toggleDevTools(),
accelerator: "F12",
});
menu.append(mainItem);

const reloadItem = new MenuItem({
label: "Restart",
click: () => win.reload(),
accelerator: "F5",
});
menu.append(reloadItem);

const fullscreenItem = new MenuItem({
label: "Fullscreen",
click: () => win.setFullScreen(!win.isFullScreen()),
accelerator: "F11",
});
menu.append(fullscreenItem);

Menu.setApplicationMenu(menu);
} else {
Menu.setApplicationMenu(null);
}

win.once("ready-to-show", () => {
win.show();
win.focus();
});
}

if (!app.requestSingleInstanceLock()) {
app.exit(0);
} else {
app.on("second-instance", (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus
if (win) {
if (win.isMinimized()) {
win.restore();
}
win.focus();
}
});
}

app.on("ready", createWindow);

app.on("window-all-closed", () => {
console.log("All windows closed");
app.quit();
});

ipcMain.on("set-fullscreen", (event, flag) => {
win.setFullScreen(flag);
});

ipcMain.on("exit-app", (event, flag) => {
win.close();
app.quit();
});

let renameCounter = 1;

const fileLock = new asyncLock({
timeout: 30000,
maxPending: 1000,
});

function niceFileName(filename) {
return filename.replace(storePath, "@");
}

async function writeFileSafe(filename, contents) {
++renameCounter;
const prefix = "[ " + renameCounter + ":" + niceFileName(filename) + " ] ";
const transactionId = String(new Date().getTime()) + "." + renameCounter;

if (fileLock.isBusy()) {
console.warn(prefix, "Concurrent write process on", filename);
}

fileLock.acquire(filename, async () => {
console.log(prefix, "Starting write on", niceFileName(filename), "in transaction", transactionId);

if (!fs.existsSync(filename)) {
// this one is easy
console.log(prefix, "Writing file instantly because it does not exist:", niceFileName(filename));
await fs.promises.writeFile(filename, contents, "utf8");
return;
}

// first, write a temporary file (.tmp-XXX)
const tempName = filename + ".tmp-" + transactionId;
console.log(prefix, "Writing temporary file", niceFileName(tempName));
await fs.promises.writeFile(tempName, contents, "utf8");

// now, rename the original file to (.backup-XXX)
const oldTemporaryName = filename + ".backup-" + transactionId;
console.log(
prefix,
"Renaming old file",
niceFileName(filename),
"to",
niceFileName(oldTemporaryName)
);
await fs.promises.rename(filename, oldTemporaryName);

// now, rename the temporary file (.tmp-XXX) to the target
console.log(
prefix,
"Renaming the temporary file",
niceFileName(tempName),
"to the original",
niceFileName(filename)
);
await fs.promises.rename(tempName, filename);

// we are done now, try to create a backup, but don't fail if the backup fails
try {
// check if there is an old backup file
const backupFileName = filename + ".backup";
if (fs.existsSync(backupFileName)) {
console.log(prefix, "Deleting old backup file", niceFileName(backupFileName));
// delete the old backup
await fs.promises.unlink(backupFileName);
}

// rename the old file to the new backup file
console.log(prefix, "Moving", niceFileName(oldTemporaryName), "to the backup file location");
await fs.promises.rename(oldTemporaryName, backupFileName);
} catch (ex) {
console.error(prefix, "Failed to switch backup files:", ex);
}
});
}

ipcMain.handle("fs-job", async (event, job) => {
const filenameSafe = job.filename.replace(/[^a-z\.\-_0-9]/i, "");
const fname = path.join(storePath, filenameSafe);
switch (job.type) {
case "read": {
if (!fs.existsSync(fname)) {
// Special FILE_NOT_FOUND error code
return { error: "file_not_found" };
}
return await fs.promises.readFile(fname, "utf8");
}
case "write": {
await writeFileSafe(fname, job.contents);
return job.contents;
}

case "delete": {
await fs.promises.unlink(fname);
return;
}

default:
throw new Error("Unknown fs job: " + job.type);
}
});

wegame.init(isDev);
wegame.listen();
18 changes: 18 additions & 0 deletions shapez-io/electron_wegame/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "electron",
"version": "1.0.0",
"main": "index.js",
"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 ."
},
"devDependencies": {
"electron": "^13.1.6"
},
"dependencies": {
"async-lock": "^1.2.8"
}
}
Loading

0 comments on commit df345ea

Please sign in to comment.