forked from DogReactor/AigisPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
289 lines (275 loc) · 8.78 KB
/
main.ts
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { app, BrowserWindow, session, ipcMain, Menu, protocol } from 'electron';
import * as path from 'path';
import { RequestHandler } from './requestHandler';
let win: BrowserWindow, serve;
const args = process.argv.slice(1);
serve = args.some(val => val === '--serve');
import * as log from 'electron-log';
import * as unzip from 'unzipper';
import * as request from 'request';
import * as Config from 'electron-store';
import * as url from 'url';
import { autoUpdater } from 'electron-updater';
const config = new Config();
require('@electron/remote/main').initialize()
// app.commandLine.appendSwitch('--enable-npapi');
app.commandLine.appendSwitch('disable-site-isolation-trials');
app.commandLine.appendSwitch('js-flags', '--max-old-space-size=8192');
app.commandLine.appendSwitch('lang', 'ja-jp')
// app.commandLine.appendSwitch('ignore-certificate-errors');
if (config.get('disable-hardware-acceleration')) {
app.disableHardwareAcceleration();
}
autoUpdater.logger = log;
autoUpdater.autoInstallOnAppQuit = false;
log.transports.file.level = 'info';
log.info('App starting...');
function sendStatusToWindow(text, obj?) {
log.info(text, obj);
win.webContents.send('update-message', text, obj);
}
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('UPDATE.CHECK');
});
autoUpdater.on('update-available', (ev, info) => {
sendStatusToWindow('UPDATE.AVB');
});
autoUpdater.on('update-not-available', (ev, info) => {
sendStatusToWindow('UPDATE.NOTAVB');
});
autoUpdater.on('error', (ev, err) => {
sendStatusToWindow('UPDATE.ERROR');
});
autoUpdater.on('download-progress', (ev, progressObj) => {
sendStatusToWindow('UPDATE.PROGRESS', progressObj);
});
autoUpdater.on('update-downloaded', (ev, info) => {
sendStatusToWindow('UPDATE.DOWNLOADED');
ipcMain.once('updateNow', (e, arg) => {
autoUpdater.quitAndInstall();
});
});
let fileList = {};
function createWindow() {
win = new BrowserWindow({
width: 960,
height: 694,
frame: false,
resizable: false,
// transparent: true,
useContentSize: true,
webPreferences: {
webSecurity: false,
plugins: true,
nodeIntegration: true,
contextIsolation: false,
nodeIntegrationInSubFrames: true,
webviewTag: true,
partition: 'persist:main',
enableRemoteModule: true
}
});
RequestHandler.setWin(win);
// and load the index.html of the app.
if (serve) {
// require('electron-reload')(__dirname, {
// electron: require(`${__dirname}/node_modules/electron`)
// });
win.loadURL('http://localhost:4200');
} else {
win.loadURL(
url.format({
pathname: path.join(__dirname, 'ng/index.html'),
protocol: 'file:',
slashes: true
})
);
}
win.webContents.on('render-process-gone', (e, details) => {
console.log('render-process-gone', details);
});
// Open the DevTools.
// win.webContents.openDevTools();
if (serve) {
win.webContents.openDevTools();
}
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store window
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
});
}
try {
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
const gameSession = session.fromPartition('persist:game', { cache: true });
app['RequestHandler'] = RequestHandler;
app['dirname'] = __dirname;
const filterData = [
'https://millennium-war.net/*',
'https://all.millennium-war.net/*',
'https://drc1bk94f7rq8.cloudfront.net/*',
'http://millennium-war.net/*',
'http://all.millennium-war.net/*'
];
// 统计
gameSession.webRequest.onCompleted({ urls: ['http://*/*', 'https://*/*'] }, () => {
if (win) {
win.webContents.send('response-incoming');
}
});
// TODO: 筛掉Aborted的请求
gameSession.webRequest.onErrorOccurred(({ url, error }) => {
if (win) {
win.webContents.send('error-incoming', url, error);
}
});
// 游戏数据的拦截
// 自定义协议的注册
gameSession.protocol.registerStreamProtocol('http', RequestHandler.handleData);
gameSession.protocol.registerStreamProtocol('https', RequestHandler.handleData);
createWindow();
// menu
if (process.platform === 'darwin') {
// Create our menu entries so that we can use MAC shortcuts
Menu.setApplicationMenu(
Menu.buildFromTemplate([
{
label: 'Edit'
}
])
);
}
ipcMain.on('fileList', (_, arg) => {
fileList = arg;
RequestHandler.setFileList(fileList);
let fontPath = Object.keys(fileList).find(v => {
return fileList[v] === 'MainFont.aft';
});
if (!fontPath) {
return;
}
fontPath = fontPath.split('/')[2];
if (config.get('fontPath') !== fontPath) {
config.set('fontPath', fontPath);
RequestHandler.setFontPath(fontPath);
console.log('get new FontPath', fontPath);
}
});
ipcMain.on('proxyStatusUpdate', (_, proxyRule: string) => {
const requestSession = session.fromPartition('persist:request', { cache: true });
const gameSession = session.fromPartition('persist:game', { cache: true });
const rule = {
proxyRules: proxyRule,
proxyBypassRules: '127.0.0.1 player.pigtv.moe',
pacScript: ''
};
gameSession.setProxy(rule);
requestSession.setProxy(rule);
// proxyServer.setProxy(arg.Enabled, arg.Socks5, arg.Host, arg.Port);
});
ipcMain.on('checkForUpdates', () => {
autoUpdater.setFeedURL('http://player.pigtv.moe/assets/aigisplayer');
autoUpdater.checkForUpdates();
});
ipcMain.on('installPlugin', (_, arg) => {
const url = arg.url;
const pluginPath = arg.pluginPath;
const salt = arg.salt;
request
.get(url)
.on('error', e => {
win.webContents.send(`plugin-install-error-${salt}`, e);
})
.pipe(unzip.Extract({ path: pluginPath }))
.on('close', () => {
win.webContents.send(`plugin-install-success-${salt}`);
})
.on('error', e => {
win.webContents.send(`plugin-install-error-${salt}`, e);
});
});
ipcMain.on('prompt', (event, message, text) => {
let promptResponse = null;
let promptWindow = new BrowserWindow({
width: 350,
height: 120,
show: false,
resizable: false,
movable: false,
alwaysOnTop: true,
frame: false,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInSubFrames: true,
webviewTag: true
}
});
const html = `
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div>${message}</div>
<input type="text" id="inputbox" value="${text}" style="width:100%"/>
<div style="margin-top: 10px;">
<button style="float:right" onclick="ok()">OK</button>
<button style="float:right;margin-right: 10px;" onclick="cancel()">Cancel</button>
</div>
<script>
const ipcRenderer = require('electron').ipcRenderer;
const inputbox = document.getElementById('inputbox');
function cancel(){
ipcRenderer.send('prompt-response','',true);
window.close();
}
function ok(){
ipcRenderer.send('prompt-response',inputbox.value,false);
window.close();
}
</script>
</body>
</html>
`;
promptWindow.loadURL('data:text/html,' + html);
promptWindow.show();
ipcMain.on('prompt-response', function (event, value, cancel) {
if (cancel) {
value = null;
}
promptResponse = value;
});
promptWindow.on('close', () => {
promptWindow = null;
event.returnValue = promptResponse;
});
});
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
win = null;
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
});
} catch (e) {
// Catch Error
// throw e;
}
process.on('unhandledRejection', (reason, promise) => {
console.error(`Uncaught error in`, promise);
});