arcade-machine/ui/main.cjs
2023-03-02 19:21:18 +01:00

155 lines
4.3 KiB
JavaScript

const { app, BrowserWindow, ipcMain } = require("electron");
const { globalShortcut } = require("electron");
const path = require("path");
const vars = require("./vars.json");
const { spawn } = require("child_process");
/** @type {BrowserWindow} */
let mainWindow;
if (!app.requestSingleInstanceLock()) {
app.quit();
} else {
app.on("second-instance", (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
}
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
autoHideMenuBar: true,
});
if (process.env.NODE_ENV !== "development") {
console.log("Production mode");
mainWindow.loadFile(path.join(__dirname, "/renderer/dist/index.html"));
} else {
console.log("Development mode");
mainWindow.loadURL("http://localhost:5173/");
mainWindow.webContents.openDevTools();
}
mainWindow.on("closed", () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
};
app.whenReady().then(() => {
createWindow();
// const {
// ExitKey1,
// ExitKey2,
// GameExe1,
// GameExe2,
// GameExe3,
// GameExe4,
// GameExe5,
// GameExe6,
// } = require("./Vars.js"); //imports exitkeys from vars.js
// const { execFile, exec, spawn } = require("child_process");
// console.log(ExitKey1 + " + " + ExitKey2);
// const ret = globalShortcut.register(ExitKey1 + " + " + ExitKey2, () => {
// //makes globalshortcut from vars in vars.js
// console.log("CommandOrControl+X is pressed");
// //On shortcut kills game exe
// switch (Execounter) {
// case 1:
// exec("taskkill /im " + GameExe1);
// break;
// case 2:
// exec("taskkill /im " + GameExe2);
// break;
// case 3:
// exec("taskkill /im " + GameExe3);
// break;
// case 4:
// exec("taskkill /im " + GameExe4);
// break;
// case 5:
// exec("taskkill /im " + GameExe5);
// break;
// case 6:
// exec("taskkill /im " + GameExe6);
// break;
// default:
// break;
// }
// });
});
/** @type {null|{ id: number, process: ReturnType<spawn>, title: string }} */
let running = null;
const stop = () => {
if (running == null) return;
running.process.kill();
setTimeout(() => {
if (running) {
running.process.kill("SIGKILL");
}
}, 1000);
mainWindow.webContents.send("game-stoping", running.id, running.title);
};
let supporting = [];
ipcMain.on("start", (event, data) => {
if (running) stop();
const game = vars.games[data];
if (!game) return event.reply("error", "Game not found");
if (game.supporting) {
for (const exe of game.supporting) {
if (supporting.includes(exe)) continue;
supporting.push(exe);
spawn(exe.path, {
windowsHide: true
});
if (exe["kill-browser"]) {
setTimeout(() => {
spawn("taskkill", ["/im", vars.browser]);
}, 1200);
}
}
}
running = {
id: data,
process: spawn(game.path, game.args, {
windowsHide: true
}),
title: game.title,
};
event.reply("game-start", data, game.title);
running.process.on("close", () => {
running = null;
event.reply("game-stop", data, game.title);
});
running.process.on("error", (err) => {
console.error(err);
event.reply("game-error", err, data, game.title);
})
});