forked from Pupix/rift-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
89 lines (73 loc) · 2.91 KB
/
util.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
const cp = require('child_process');
const path = require('path');
const fs = require('fs-extra');
const yaml = require('yaml');
const { spawn } = require('child_process');
const requestPromise = require('request-promise');
const IS_WIN = process.platform === 'win32';
const LEAGUE_PROCESS = IS_WIN ? 'LeagueClient.exe' : 'LeagueClient';
function getLCUExecutableFromProcess() {
return new Promise((resolve, reject) => {
const command = IS_WIN ?
`WMIC PROCESS WHERE name='${LEAGUE_PROCESS}' GET ExecutablePath` :
`ps x -o comm= | grep '${LEAGUE_PROCESS}$'`;
cp.exec(command, (error, stdout, stderr) => {
if (error || !stdout || stderr) {
reject(error || stderr);
return;
}
const normalizedPath = path.normalize(stdout);
resolve(IS_WIN ? normalizedPath.split(/\n|\n\r/)[1] : normalizedPath);
});
});
}
async function duplicateSystemYaml() {
const LCUExePath = await getLCUExecutableFromProcess();
const LCUDir = IS_WIN ? path.dirname(LCUExePath) : `${path.dirname(LCUExePath)}/../../..`;
const originalSystemFile = path.join(LCUDir, 'system.yaml');
const overrideSystemFile = path.join(LCUDir, 'Config', 'rift-explorer', 'system.yaml');
// File doesn't exist, do nothing
if (!(await fs.exists(originalSystemFile))) {
throw new Error('system.yaml not found');
}
const file = await fs.readFile(originalSystemFile, 'utf8');
const fileParsed = yaml.parseDocument(file);
fileParsed.delete('riotclient');
fileParsed.set('enable_swagger', true);
const stringifiedFile = yaml.stringify(fileParsed);
// Rito's file is prefixed with --- newline
await fs.outputFile(overrideSystemFile, `---\n${stringifiedFile}`);
}
function restartLCUWithOverride(LCUData) {
return new Promise(async (resolve) => {
const LCUExePath = await getLCUExecutableFromProcess();
const LCUDir = IS_WIN ? path.dirname(LCUExePath) : `${path.dirname(LCUExePath)}/../../..`;
const overrideSystemFile = path.join(LCUDir, 'Config', 'rift-explorer', 'system.yaml');
const {
username,
password,
address,
port,
} = LCUData;
await requestPromise({
strictSSL: false,
method: 'POST',
uri: `https://${username}:${password}@${address}:${port}/process-control/v1/process/quit`,
});
// Give it some time to do cleanup
setTimeout(() => {
const leagueProcess = spawn(LCUExePath.trim(), [`--system-yaml-override=${overrideSystemFile}`], {
cwd: LCUDir,
detached: true,
stdio: 'ignore',
});
leagueProcess.unref();
resolve();
}, 5000);
});
}
module.exports = {
getLCUExecutableFromProcess,
duplicateSystemYaml,
restartLCUWithOverride,
};