This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
240 lines (178 loc) · 6.7 KB
/
index.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
import { app, BrowserWindow, ipcMain, dialog, shell, App } from "electron"
import { exec } from 'child_process'
import { existsSync, writeFileSync, readFileSync } from 'fs'
import https from "https"
async function fetch(url: string): Promise<any> {
return new Promise((resolve, reject) => {
https.get(url, {
headers: {
"User-Agent": "Mozilla/ 5.0(Windows NT 6.1; Win64; x64; rv: 47.0) Gecko / 20100101 Firefox / 47.0"
}
} , (res) => {
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
resolve(JSON.parse(rawData))
} catch (e) {
reject(e)
}
});
}).on('error', reject)
})
}
const currentVer = "v1.0.1"
let authInf: auth = null
type auth = {
port: number,
token: string
}
async function isProcessRunning(processName: string): Promise<boolean> {
return new Promise((resolve, reject) => {
switch (process.platform) {
case 'win32':
exec('tasklist', async (err: Error, stdout: string, stderr: string) => {
if (err) reject(err)
resolve(stdout.split(processName).length !== 1 && !!((await getAuth()).token))
})
break;
case 'darwin':
exec('ps -ax | grep LeagueClientUx', async (err: Error, stdout: string, stderr: string) => {
if (err) reject(err)
resolve(stdout.split('riotclient-app').length !== 1 && !!((await getAuth()).token))
})
}
})
};
ipcMain.on("first", async event => {
event.returnValue = await getAuth()
})
async function getAuth(): Promise<auth> {
return new Promise((res, rej) => {
exec((process.platform === "win32") ? "wmic PROCESS WHERE name='LeagueClientUx.exe' GET commandline" : "ps -A | grep LeagueClientUx", (err, out) => {
res({
port: parseInt(out.match(/--app-port=([0-9]*)/)[1]),
token: out.match(/--remoting-auth-token=([\w-_]*)/)[1]
})
})
})
}
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
event.preventDefault();
callback(true);
});
ipcMain.on("cacheAdd", (event, data) => {
const file = JSON.parse(readFileSync("./runes.json").toString())
file.main.push(data)
writeFileSync("./runes.json", JSON.stringify(file, null, 2))
event.returnValue = file
})
ipcMain.on("cacheRemove", (event, nonce) => {
const file = JSON.parse(readFileSync("./runes.json").toString())
file.main.splice(file.main.findIndex((x: any) => x.nonce === nonce), 1)
writeFileSync("./runes.json", JSON.stringify(file, null, 2))
event.returnValue = file
})
ipcMain.on("cacheRequest", (event) => {
event.returnValue = JSON.parse(readFileSync("./runes.json").toString())
})
function handleSquirrelEvent(application: App) {
if (process.argv.length === 1) {
return false;
}
const ChildProcess = require('child_process');
const path = require('path');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
const spawn = function (command: any, args: any) {
let spawnedProcess, error;
try {
spawnedProcess = ChildProcess.spawn(command, args, {
detached: true
});
} catch (error) { }
return spawnedProcess;
};
const spawnUpdate = function (args: any) {
return spawn(updateDotExe, args);
};
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
spawnUpdate(['--createShortcut', exeName]);
setTimeout(application.quit, 1000);
return true;
case '--squirrel-uninstall':
spawnUpdate(['--removeShortcut', exeName]);
setTimeout(application.quit, 1000);
return true;
case '--squirrel-obsolete':
application.quit();
return true;
}
};
app.allowRendererProcessReuse = true
app.on("ready", async () => {
const win = new BrowserWindow({
width: 400,
height: 600,
fullscreenable: false,
frame: /*(process.platform !== "win32")*/ true,
icon: "logo/Icon.ico",
resizable: false,
title: "Runes++",
movable: true,
webPreferences: {
nodeIntegration: true
}
})
if (!existsSync("./runes.json")) writeFileSync("./runes.json", `{"main":[{"name": "Example", "nonce": "0", "perks":[8128, 8126, 8138, 8135, 8226, 8232, 5008, 5008, 5001]}], "ignoreUpdate": false, "currentVer": "${currentVer}"}`)
const file = JSON.parse(readFileSync("./runes.json").toString())
if (file.currentVer !== currentVer) writeFileSync("./runes.json", JSON.stringify(Object.assign(file, { ignoreUpdate: false, currentVer }), null, 2))
fetch("https://api.github.com/repos/Sardonyx78/runes-plus-plus/releases/latest").then(r => {
if (file.ignoreUpdate) return
if (r.tag_name !== currentVer) dialog.showMessageBox(win, {
type: 'warning',
buttons: ['Yes', 'No'],
defaultId: 2,
title: 'Question',
message: 'There\'s an update',
detail: 'Would you like to update?',
checkboxLabel: 'Remember my answer',
checkboxChecked: false,
}).then(resp => {
if (resp.response === 1) {
if (resp.checkboxChecked) {
file.ignoreUpdate = true
writeFileSync("./runes.json", JSON.stringify(file, null, 2))
}
} else if (resp.response === 0) shell.openExternal('https://github.com/Sardonyx78/runes-plus-plus/releases')
});
})
if (process.env.NODE_ENV === "DEVELOPMENT") win.webContents.openDevTools()
let loaded = false
async function unload() {
win.loadFile('views/loading/index.html')
while (!loaded) {
loaded = await isProcessRunning("LeagueClientUx")
if (loaded) {
authInf = await getAuth()
load()
}
}
}
unload()
async function load() {
win.loadFile('views/main/index.html')
while (loaded) {
loaded = await isProcessRunning("LeagueClientUx")
if (!loaded) {
unload()
}
}
}
})