-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
261 lines (227 loc) · 6.41 KB
/
index.js
File metadata and controls
261 lines (227 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const {app, BrowserWindow, ipcMain, Menu, screen, utilityProcess, session} = require('electron');
const { autoUpdater } = require("electron-updater");
const path = require('node:path');
const fs = require('node:fs');
const userDataPath = app.getPath("userData");
const childProcess = require('child_process');
// Remove menu bar
Menu.setApplicationMenu(null);
// Create browser window test
let splashWindow;
function createSplashWindow() {
splashWindow = new BrowserWindow({
width: 880,
height: 560,
frame: false,
transparent: true,
resizable: false,
//alwaysOnTop: true
});
splashWindow.loadFile("pages/splash.html");
}
function createBrowserWindow() {
// Check if first use
let first_use = !fs.existsSync(path.join(userDataPath, 'user-data.json'));
first_use = false;
let primaryDisplay = screen.getPrimaryDisplay();
let {
width,
height
} = primaryDisplay.workAreaSize;
const win = new BrowserWindow({
title: 'YAB',
show: false,
width: Math.floor(width * (first_use ? 0.5 : 0.7)),
height: Math.floor(height * (first_use ? 0.6 : 0.8)),
autoHideMenuBar: true,
titleBarStyle: 'hidden',
webPreferences: {
sandbox: false,
preload: path.join(__dirname, 'js', (first_use ? 'setup' : 'index'), 'preload.js')
}
})
if (!first_use) win.maximize();
// TODO: Remove traffic lights in macos
//win.setWindowButtonVisibility(false)
if (first_use) {
// Setup Screen
win.loadFile('pages/setup.html');
} else {
// Open normal browser
win.loadFile('pages/index.html');
}
win.webContents.openDevTools();
function sendState() {
win.webContents.send('window-state', {
maximized: win.isMaximized()
});
}
win.on('maximize', sendState);
win.on('unmaximize', sendState);
win.show();
session.defaultSession.on('will-download', async (event, item) => {
const fileName = item.getFilename();
const filePath = path.join(app.getPath('downloads'), fileName);
item.setSavePath(filePath);
const tempExt = path.extname(fileName);
const tempPath = path.join(app.getPath('temp'), `temp_icon${tempExt}`);
try {
fs.writeFileSync(tempPath, '');
const icon = await app.getFileIcon(tempPath, { size:'large' });
// send icon to main
fs.unlinkSync(tempPath);
} catch (err) {
// cant get icon put a placeholder or nothing
}
});
app.on('child-process-gone', (event, details) => {
console.log(`Child process of type ${details.type} with name ${details.name} and service name ${details.serviceName} has exited.`);
console.log(`Reason: ${details.reason}, Exit Code: ${details.exitCode}`);
win.webContents.send('process-unexpected-terminated', { pid: details.name });
});
}
app.whenReady().then(() => {
createSplashWindow()
autoUpdater.checkForUpdates();
setTimeout(() => {
splashWindow.hide();
setTimeout(() => {
createBrowserWindow();
splashWindow.destroy();
}, 1000);
}, 3000);
autoUpdater.on("checking-for-update", () => {
console.log("checking for updates");
splashWindow.destroy();
createBrowserWindow();
});
autoUpdater.on("update-not-available", (info) => {
console.log("not available");
createBrowserWindow();
});
autoUpdater.on("error", (err) => {
//splashWindow.webContents.send("update-status", `Error: ${err.message}`);
console.log("error occurred", err.message);
createBrowserWindow();
});
ipcMain.on('window-action', (event, data) => {
let win = BrowserWindow.fromWebContents(event.sender);
if (!win) return;
switch (data.action) {
case 'minimize':
win.minimize();
break;
case 'maximize':
if (win.isMaximized()) {
win.unmaximize();
} else {
win.maximize();
}
break;
case 'close':
win.close();
break;
case 'getwindowdata':
win.webContents.send('window-data', {
mouse: screen.getCursorScreenPoint(),
bounds: win.getBounds()
});
break;
default:
// Invalid action
break;
}
});
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
});
ipcMain.on('close-request', () => {
app.quit();
});
// SUBPROCESSES
// Store child processes with custom PIDs
const subprocesses = {};
// generate pid
function generateUniquePid() {
let pid;
do {
pid = Math.floor(10000000 + Math.random() * 9000000).toString();
} while (subprocesses[pid]);
return pid;
}
// spawning
ipcMain.handle('spawn-process', async (event) => {
const pid = generateUniquePid();
const child = utilityProcess.fork(path.join(__dirname, 'subwasmoon.js'), [], {
serviceName: pid,
stdio: 'pipe'
});
child.on('spawn', () => {
console.log("spawned process with fpid: " + pid + " and tpid: " + child.pid)
subprocesses[pid] = {
child: child,
tpid: child.pid
}
});
child.on('error', (error) => {
console.error('Utility process encountered an error:', error);
});
child.stdout.on('data', (data) => {
console.log(`Utility stdout: ${data.toString()}`);
});
child.stderr.on('data', (data) => {
console.error(`Utility stderr: ${data.toString()}`);
});
child.on('message', (message) => {
/*if (message.type.startsWith('editRquest/legacy/')) {
} else if (message.type.startsWith('editRequest/v2/')) {
}*/
})
return pid;
});
ipcMain.on('terminate-process', (event, pid) => {
const child = subprocesses[pid].child;
child.kill();
});
ipcMain.on('kill-process', (event, pid) => {
const child = subprocesses[pid].child;
child.kill();
});
ipcMain.on('reset-process', (event, pid) => {
const old_child = subprocesses[pid].child;
console.log(subprocesses[pid]);
old_child.kill();
const new_child = utilityProcess.fork(path.join(__dirname, 'sub_wasmoon.js'), [], {
serviceName: pid.toString()
});
new_child.on('spawn', () => {
console.log("Reset process " + pid + " with new tpid: " + new_child.pid)
subprocesses[pid] = {
child: new_child,
tpid: new_child.pid
}
});
});
ipcMain.on("execute-lua", (event, pid, lua, api) => {
setTimeout(function () {
const child = subprocesses[pid].child;
child.postMessage({ type: "code", code: lua, api: api });
}, 250);
});
ipcMain.on("set-html", (event, pid, html) => {
setTimeout(function () {
const child = subprocesses[pid].child;
child.postMessage({ type: "html", html: html })
}, 250);
});
ipcMain.handle('get-memory-mb', (event, pid) => {
const child = subprocesses[pid];
const metric = app.getAppMetrics().find(m => m.pid == child.tpid);
if (!metric) return 0;
if (process.platform === "win32") {
return metric.memory.privateBytes / 1000;
} else {
return metric.memory.workingSetSize / 1000;
}
});