This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
190 lines (168 loc) · 5.09 KB
/
main.js
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
const {app, BrowserWindow, ipcMain} = require('electron');
const {spawn, fork, exec} = require('child_process');
const path = require("path");
const fs = require("fs");
const axios = require("axios");
const asar = require('asar');
let subprocesses = [];
let isLoading = false;
const resourcePath = path.parse(app.getAppPath()).dir;
let logFn = (...args) => {
console.log(...args);
}
const createWindow = () => {
const win = new BrowserWindow({
width: 1000, height: 800, webPreferences: {
preload: path.join(__dirname, './assets/js/preload.js'), nodeIntegrationInWorker: true
}
})
logFn = (...args) => {
if (!win.isDestroyed()) win.webContents.send('process-log', ...args);
console.log(...args);
}
win.loadFile('./assets/index.html');
}
app.whenReady().then(() => {
if (app.isPackaged && !fs.existsSync(`${resourcePath}/release`)) asar.extractAll(`${resourcePath}/app.asar`, `${resourcePath}/release`);
ipcMain.handle('start', handleStart)
ipcMain.handle('updateApp', updateApp)
ipcMain.handle('get-rec-info', getRecInfo)
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', () => {
_closeChildProcess()
if (process.platform !== 'darwin') app.quit()
})
app.on('before-quit', () => {
if (isLoading) _removeFile(`${resourcePath}/app.asar-new`);
})
app.on('quit', () => {
if (fs.existsSync(`${resourcePath}/updater.exe`) && fs.existsSync(`${resourcePath}/app.asar-new`)) {
logFn("开始替换资源...");
const child = spawn(`"${resourcePath}/updater.exe"`, {
detached: true,
shell: true,
cwd: resourcePath,
stdio: 'ignore'
});
child.unref();
}
})
async function handleStart(e, account, password, platform, isFillAP, chromeUrl) {
try {
fs.writeFile(app.isPackaged ? `${resourcePath}/data.rec` : './data.rec', JSON.stringify({
account,
password,
platform,
isFillAP,
chromeUrl
}), {encoding: 'utf8'}, () => {
})
subprocesses.push(createChildProcess({account, password, platform, isFillAP, chromeUrl}, subprocesses.length));
} catch (err) {
logFn('createWorkerError:', err);
return -1;
}
return 1;
}
function getRecInfo() {
try {
return fs.readFileSync(app.isPackaged ? `${resourcePath}/data.rec` : './data.rec', {encoding: 'utf8'});
} catch (err) {
return "{}";
}
}
async function updateApp() {
if (!app.isPackaged || isLoading) return;
_closeChildProcess();
logFn("开始更新...");
try {
logFn("创建更新脚本...");
if (!fs.existsSync(`${resourcePath}/updater.exe`)) _copyFile(app.getAppPath() + '/updater.exe', `${resourcePath}/updater.exe`);
logFn("开始下载最新版本资源...");
isLoading = true;
if (await _downloadFile('https://github.com/Samuel-luo/network_class_Ganker/releases/latest/download/app.asar', `${resourcePath}/app.asar-new`).then(() => 1).catch(() => 0)) {
_removeDir(`${resourcePath}/release`);
app.exit(0)
} else {
logFn("下载失败!");
_removeFile(`${resourcePath}/app.asar-new`);
isLoading = false;
}
} catch (err) {
logFn(err);
}
}
function createChildProcess(data, index) {
let subprocess = fork(app.isPackaged ? path.join(resourcePath, 'release', 'index.js') : './index.js', {
cwd: app.isPackaged ? `${resourcePath}/release` : __dirname, stdio: ['pipe', 'pipe', 'pipe', 'ipc'], env: {
account: data.account, password: data.password, platform: data.platform, isFillAP: data.isFillAP, chromeUrl: data.chromeUrl
}
});
subprocess.stdout.on('data', (msg) => {
logFn('child_processStdOut:', msg.toString());
})
subprocess.stderr.on('data', (msg) => {
logFn('child_processStdErr:', msg.toString());
})
subprocess.on('close', (...args) => {
logFn('child_processClose:', ...args);
subprocesses[index] = undefined;
})
return subprocess;
}
process.on('SIGINT', (...args) => {
logFn('main_processExitSIGINT:', ...args);
_closeChildProcess()
})
process.on('exit', (...args) => {
logFn('main_processExit:', ...args);
_closeChildProcess()
})
function _closeChildProcess() {
let subprocess;
while (subprocess = subprocesses.shift()) {
try {
subprocess.send('exit');
} catch (err) {
// 静默处理 err
}
}
}
async function _downloadFile(url, filepath) {
const response = await axios({
url,
method: "GET",
responseType: "arraybuffer",
});
fs.writeFileSync(filepath, response.data, {encoding: "binary"})
}
function _copyFile(from, to) {
fs.copyFileSync(from, to);
}
function _removeFile(filePath) {
while (fs.existsSync(filePath)) {
fs.rmSync(filePath);
}
}
function _removeDir(filePath) {
const files = []
if (fs.existsSync(filePath)) {
const files = fs.readdirSync(filePath)
files.forEach((file) => {
const nextFilePath = `${filePath}/${file}`
const states = fs.statSync(nextFilePath)
if (states.isDirectory()) {
// recurse
_removeDir(nextFilePath)
} else {
// delete file
fs.unlinkSync(nextFilePath)
}
})
fs.rmdirSync(filePath)
}
}