This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
485 lines (451 loc) · 15.5 KB
/
index.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
"use strict";
require('./src/core/global');
const {version} = require("./package.json");
const set = require('./src/core/index');
const {app, BrowserWindow, ipcMain, Tray, Menu, shell , clipboard} = require('electron');
const instanceLock = app.requestSingleInstanceLock();
const electronLogger = require('electron-log');
const {autoUpdater} = require("electron-updater");
const exec = require('child_process').exec;
const fs = require('fs-extra');
const path = require('path');
const fsutil = require("nodejs-fs-utils");
// 區域宣告
let isQuiting;
const $ = { // 已完成不需要變更
closeApp: function(){
if(pid){
process.kill(pid);
pid = 0;
}
set.closetimer(); // 遠程呼叫 關閉動態時間 (正確的關閉 避免報錯)
app.quit(); //沒有真的退出
process.exit();
},
taskbar: async function(win){
let type = (process.platform == "darwin")?'png':'ico';
taskbar_tray = new Tray(path.join(__dirname,'./src/resource/img/ml-logo.'+type));
const contextMenu = Menu.buildFromTemplate([
{
icon: path.join(__dirname,'./src/resource/img/ml-logo-taskbar.png'),
label: `Mjolnir League v.${version}`,
enabled: false
},
{
type: 'separator'
},
{
label: 'Github', click: function () {
shell.openExternal("https://github.com/yomisana");
shell.openExternal("https://github.com/Yomisana/Mjolnir-League");
}
},
{
type: 'separator'
},
{
label: `Clean log (${logstorage.storage} ${logstorage.type})`, click: function () {
$.cleanlog();
console.log(`[INFO] 已經清除了 ${logstorage.storage} ${logstorage.type} 記錄檔了`);
}
},
{
type: 'separator'
},
{
label: 'Quit Mjolnir League', click: function () {
$.closeApp();
}
}
]);
//taskbar_tray.setToolTip(`Mjolnir League v.${version}`);
taskbar_tray.setToolTip(`Mjolnir League`);
taskbar_tray.setContextMenu(contextMenu);
taskbar_tray.on('click', async ()=>{
await $.calculatestorage();
const contextMenu = Menu.buildFromTemplate([
{
icon: path.join(__dirname,'./src/resource/img/ml-logo-taskbar.png'), // 16 {右鍵後裡面的}
label: `Mjolnir League Cleaner v.${version}`,
enabled: false
},
{
type: 'separator'
},
{
label: 'Github', click: function () {
shell.openExternal("https://github.com/yomisana");
shell.openExternal("https://github.com/Yomisana/Mjolnir-LeagueCleaner");
}
},
{
type: 'separator'
},
{
label: `Clean log (${logstorage.storage} ${logstorage.type})`, click: function () {
$.cleanlog();
console.log(`[INFO] 已經清除了 ${logstorage.storage} ${logstorage.type} 記錄檔了`);
}
},
{
type: 'separator'
},
{
label: 'Quit Mjolnir League Cleaner', click: function () {
$.closeApp();
}
}
]);
taskbar_tray.setContextMenu(contextMenu);
win.show();
});
},
showMessage: function(window, message, type, title){
dialog.showMessageBoxSync(window, {
message: message,
type: type,
title: title
});
},
calculatestorage:function(){
return new Promise((resolve,reject)=>{
fsutil.fsize(path.join(log_dir), {
countFolders: false
}, function (err, size) {
logstorage.storage = null; logstorage.type = "null";
// console.log(size); // bytes : 1024 bytes = 1KB
logstorage.storage = (size / 1024).toFixed();
logstorage.type = "KB";
if(logstorage.storage >= 1024){ // 1024 KB
let kbtomb = logstorage.storage
logstorage.storage = (kbtomb / 1024).toFixed();
logstorage.type = "MB";
if(logstorage.storage >= 1024){ // 1024 KB
let mbtogb = logstorage.storage
logstorage.storage = (mbtogb / 1024).toFixed();
logstorage.type = "GB";
}
}
// console.log(`${(size / 1024).toFixed()}`);
console.log(`log folder size: ${logstorage.storage} ${logstorage.type}`);
resolve(true);
});
});
},
cleanlog: function(){
fs.readdir(path.join(log_dir), async (err, files) => {
if (err) {
console.error("[ERROR - Clean log] "+err);
}
for (const file of files) {
fs.unlink(path.join(log_dir, file), err => {
if (err) {
console.error("[ERROR - Ready Clean log] "+err);
}
});
}
// update tray contextMenu
await $.calculatestorage();
const contextMenu = Menu.buildFromTemplate([
{
icon: path.join(__dirname,'./src/resource/img/ml-logo-taskbar.png'), // 16 {右鍵後裡面的}
label: `Mjolnir League Cleaner v.${version}`,
enabled: false
},
{
type: 'separator'
},
{
label: 'Github', click: function () {
shell.openExternal("https://github.com/yomisana");
shell.openExternal("https://github.com/Yomisana/Mjolnir-LeagueCleaner");
}
},
{
type: 'separator'
},
{
label: `Clean log (${logstorage.storage} ${logstorage.type})`, click: function () {
$.cleanlog();
console.log(`[INFO] 已經清除了 ${logstorage.storage} ${logstorage.type} 記錄檔了`);
}
},
{
type: 'separator'
},
{
label: 'Quit Mjolnir League Cleaner', click: function () {
$.closeApp();
}
}
]);
taskbar_tray.setContextMenu(contextMenu);
});
}
}
app.whenReady().then(() => {
locate = app.getLocale();
software_version = `${version}`;
// splash
ml_splash = new BrowserWindow({
title: splash_set.title,
icon: window_icon,
autoHideMenuBar: true,
backgroundColor: window_BackgroundColor,
width: splash_set.width, height: splash_set.height,
minWidth: splash_set.min_width, minHeigh: splash_set.min_width,
maxWidth: splash_set.max_width, maxHeight: splash_set.max_height,
transparent: true,
titleBarStyle: 'hiddenInset',
frame: false,
show: false,
webPreferences:{
devTools: false,
fullscreenBoolean: false,
fullscreenableBoolean: false,
simpleFullscreenBoolean: false,
preload: __dirname + "/preload.js"
}
});
ml_splash.loadFile('src/resource/html/ml_splash.html');
ml_splash.setMenu(null);
ml_splash.center();
ml_splash.once('ready-to-show', async () => {
ml_splash.show();
if(app.isPackaged){
autoUpdater.checkForUpdatesAndNotify();
}
else{
let starterTimer = setInterval(()=>{
if(mainWindowReady){
clearInterval(starterTimer);
require('./src/core/index');
ml_main.show();
ml_splash.close();
}
},100);
}
});
//主畫面
ml_main = new BrowserWindow
({
title: main_set.title,
icon: window_icon,
autoHideMenuBar: true,
//resizable: false,
width: main_set.width, height: main_set.height,
minWidth: main_set.min_width,
minHeight: main_set.min_height,
//maxWidth: main_set.max_width, maxHeight: main_set.max_height,
titleBarStyle: 'hiddenInset',
frame: true,
show: false,
webPreferences: {
devTools: false,
fullscreenBoolean: false,
fullscreenableBoolean: false,
simpleFullscreenBoolean: false,
nodeIntegration: true,
contextIsolation: true,
enableRemoteModule: false, // turn off remote
preload: __dirname + "/preload.js" // use a preload script
}
});
ml_main.setMenu(null);
ml_main.loadFile('src/resource/html/ml_main.html');
ml_main.once('ready-to-show', async () => {
console.log("[INFO] 版本: ", software_version);
console.log(`[INFO] 主畫面渲染完畢`);
mainWindowReady = true;
await $.calculatestorage();
$.taskbar(ml_main);
});
// 主畫面最小化時
ml_main.on('minimize', (event) => {
//if(pid)
event.preventDefault();
ml_main.hide();
});
// 主畫面將要關閉時
ml_main.on('close', (event) => {
if (!isQuiting) {
event.preventDefault();
ml_main.hide();
event.returnValue = false;
}
});
// ipcMain
ipcMain.on("toMain", async (event, args) => {
console.log('[ipcMain] ' + args + ' 事件已觸發');
if(args == "closeapp"){
$.closeApp();
}else if(args == "sversion"){
ml_main.webContents.send("software_version", software_version);
}else if(args == "kill_lolrender"){
exec('taskkill /f /im LeagueClientUxRender.exe',function (error, stdout, stderr) {
//console.log(stdout);
console.log("[INFO] 發送結束客戶端渲染畫面程序完畢!");
if(error)
console.error("[ERROR] " + error);
});
}else if(args == "Clean_log"){
fs.readdir(path.join(log_dir), (err, files) => {
if (err) {
console.error("[ERROR - Clean log] "+err);
}
for (const file of files) {
fs.unlink(path.join(log_dir, file), err => {
if (err) {
console.error("[ERROR - Ready Clean log] "+err);
}
});
}
});
}else if(Array.isArray(args)){
if(args[0] == "accept_checkbox"){
if(args[1]){
console.log("啟用自動接受");
settings.accept_checkbox = true;
}else{
console.warn("禁用自動接受");
settings.accept_checkbox = false;
}
}
if(args[0] == "summoner_name_copy"){
if(args[1]){
console.log("複製召喚師名稱成功");
// 複製文字
clipboard.writeText(args[1]);
}else{
console.warn("複製召喚師名稱失敗...");
}
}
if(args[0] == "champselect_chat_copy"){
if(args[1]){
console.log("複製聊天室內容成功");
// 複製聊天室文字
clipboard.writeText(args[1].replace(/^(\r\n|\n|\r|\t| )+/gm, ""));
}else{
clipboard.writeText(args[1]);
}
}
if(args[0] == "api_refresh"){
if(args[1]){
console.log("變更客戶端調度頻率:" + (args[1] * 1000));
refresh_check_path_timer.open = args[1] * 1000;
set.refresh_time();
}else{
console.log("變更客戶端調度頻率: 預設...");
refresh_check_path_timer.open = 1000;
set.refresh_time();
}
}
if(args[0] == "find_refresh"){
if(args[1]){
console.log("變更搜尋客戶端調度頻率:" + (args[1] * 1000));
refresh_check_path_timer.close = args[1] * 1000;
set.refresh_time();
}else{
console.log("變更搜尋客戶端調度頻率: 預設...");
refresh_check_path_timer.close = 5000;
set.refresh_time();
}
}
}
// (args[0] == "accept_checkbox"){
// console.log(args[]);
// console.log("收到前端 自動接受對戰按鈕的值囉:" + );
// }else if(args == "Clean_log"){
// fs.readdir(path.join(log_dir), (err, files) => {
// if (err) {
// console.error("[ERROR - Clean log] "+err);
// }
// // DEBUG
// //console.log(files);
// //console.log(path.join(log_dir));
// for (const file of files) {
// fs.unlink(path.join(log_dir, file), err => {
// if (err) {
// console.error("[ERROR - Ready Clean log] "+err);
// }
// });
// }
// });
// }else if(args == "kill_lolrender"){
// exec('taskkill /f /im LeagueClientUxRender.exe',function (error, stdout, stderr) {
// //console.log(stdout);
// console.log("[INFO] 發送結束客戶端渲染畫面程序完畢!");
// if(error)
// console.error("[ERROR] " + error);
// });
// }
});
});
// 額外設定
// 程式所有視窗確定關閉後關閉現程
// app.on('window-all-closed', () => {
// if (process.platform !== 'darwin') {
// $.closeApp();
// }
// });
// 單一處理程序鎖定,有兩個以上的處理程序時,強制關閉最後開啟的那個
if(!instanceLock)
$.closeApp();
else{
app.on('second-instance', (event, commandLine, workingDirectory) => {
// 如果啟動第二個處理程序,則將原先啟動的那個彈出來並聚焦
if (ml_main) {
if (ml_main.isMinimized()) ml_main.restore();
ml_main.focus();
}
});
}
// log
if(!fs.existsSync(path.join(log_dir)))
fs.mkdirSync(path.join(log_dir),{ recursive: true });
// autoUpdater
autoUpdater.logger = electronLogger;
autoUpdater.logger.transports.file.level = 'info';
autoUpdater.on('checking-for-update', () => {
console.log('[INFO] Checking for update...');
ml_splash.webContents.send('update_status','Checking for updates...');
})
autoUpdater.on('update-available', (info) => {
console.log('[INFO] Update available.');
ml_splash.webContents.send('update_status','Preparing download...');
})
autoUpdater.on('update-not-available', (info) => {
console.log('[INFO] Update is latest.');
let starterTimer = setInterval(()=>{
if(mainWindowReady){
clearInterval(starterTimer);
require('./src/core/index');
ml_main.show();
ml_splash.close();
}
},100);
})
autoUpdater.on('error', (err) => {
console.warn('[WARN] Error in auto-updater. ' + err);
ml_splash.webContents.send('update_status','Update error');
//Error in auto-updater. HttpError: 500
let reg = RegExp(/HttpError: 500/);
if(reg.exec(err)){
$.showMessage(lh_splash,"Updater Error 500 - Github have some issue\n Tips: This time Update is skip.","error", software_name + " - Updater error");
}
//$.closeApp();
})
autoUpdater.on('download-progress', (progressObj) => {
let percent = progressObj.percent.toFixed(0);
console.log('[INFO] Download progress:' + percent + '%');
ml_splash.webContents.send('update_status','Downloading... '+ percent + '%');
ml_splash.webContents.send('update_percent',percent);
})
autoUpdater.on('update-downloaded', (info) => {
console.log('[INFO] Restarting Mjolnir League then Installing update.');
ml_splash.webContents.send('update_status','Restarting Mjolnir League Installing update...');
});
// 更新檔下載完畢後 過 x 秒 關閉軟體更新後重啟軟體
autoUpdater.on('update-downloaded', (ev, info) => {
autoUpdater.quitAndInstall(false, true); // isSilent: false, isForceRunAfter: true
})