Skip to content

Commit f366477

Browse files
committed
Override system.yaml path to fix problems introduced by the new LCU patcher
1 parent 6c3f744 commit f366477

File tree

5 files changed

+408
-85
lines changed

5 files changed

+408
-85
lines changed

app.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
const electron = require('electron');
22
const LCUConnector = require('lcu-connector');
3-
const fs = require('fs-extra');
4-
const path = require('path');
5-
const yaml = require('yaml');
6-
const { getLCUPathFromProcess } = require('./util');
3+
const {
4+
duplicateSystemYaml,
5+
restartLCUWithOverride,
6+
} = require('./util');
77

88
const connector = new LCUConnector();
9-
const { app } = electron;
9+
const { app, dialog } = electron;
1010
const { BrowserWindow } = electron;
1111
const root = __dirname + '/app';
12+
let LCURestarted = false;
1213

1314
app.commandLine.appendSwitch('--ignore-certificate-errors');
1415

1516
app.on('ready', () => {
1617
let mainWindow = null;
1718
let windowLoaded = false;
1819
let LCUData = null;
19-
let swaggerDisabled = true;
2020

2121
mainWindow = new BrowserWindow({
2222
center: true,
@@ -27,7 +27,7 @@ app.on('ready', () => {
2727
});
2828

2929
// Check if dev env FIXME
30-
// mainWindow.openDevTools();
30+
mainWindow.openDevTools();
3131

3232
// Remove default menu
3333
mainWindow.setMenu(null);
@@ -43,7 +43,6 @@ app.on('ready', () => {
4343
return;
4444
}
4545

46-
mainWindow.webContents.send(swaggerDisabled ? 'swagger-disabled' : 'swagger-enabled');
4746
mainWindow.webContents.send('lcu-load', LCUData);
4847
});
4948

@@ -58,34 +57,31 @@ app.on('ready', () => {
5857
LCUData = data;
5958

6059
try {
61-
const LCUPath = await getLCUPathFromProcess();
62-
const systemFile = path.join(LCUPath, 'system.yaml');
63-
64-
// File doesn't exist, do nothing
65-
if (!(await fs.pathExists(systemFile))) {
66-
throw new Error('system.yaml not found');
60+
if (LCURestarted) {
61+
mainWindow.webContents.send('lcu-load', LCUData);
62+
return;
6763
}
6864

69-
const file = await fs.readFile(systemFile, 'utf8');
70-
const fileParsed = yaml.parse(file);
71-
72-
swaggerDisabled = !fileParsed.enable_swagger;
73-
mainWindow.webContents.send(swaggerDisabled ? 'swagger-disabled' : 'swagger-enabled');
74-
75-
if (!fileParsed.enable_swagger) {
76-
fileParsed.enable_swagger = true;
77-
const stringifiedFile = yaml.stringify(fileParsed);
78-
79-
// Rito's file is prefixed with --- newline
80-
await fs.outputFile(systemFile, `---\n${stringifiedFile}`);
65+
await duplicateSystemYaml();
66+
const response = dialog.showMessageBox({
67+
type: 'info',
68+
buttons: [ 'Cancel', 'Ok' ],
69+
title: 'Rift Explorer',
70+
message: 'Rift Explorer needs to restart your League of Legends client to work properly',
71+
cancelId: 0,
72+
});
73+
74+
if (!response) {
75+
mainWindow.close();
76+
return;
8177
}
8278

79+
await restartLCUWithOverride();
80+
LCURestarted = true;
8381
} catch (error) {
8482
console.error(error);
8583
// No error handling for now
8684
}
87-
88-
mainWindow.webContents.send('lcu-load', LCUData);
8985
}, 5000);
9086
});
9187

app/index.html

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
let LCUData = null;
4343
let swaggerDisabled = true;
44-
let loadingSwagger = false;
4544

4645
// We all love global variables
4746
window.RE = {
@@ -57,16 +56,12 @@
5756
const header = `Basic ${btoa(`${username}:${password}`)}`;
5857
const auth = new SwaggerClient.ApiKeyAuthorization("Authorization", header, "header");
5958

60-
loadingSwagger = true;
61-
6259
window.swaggerUi = new SwaggerUi({
6360
validatorUrl: null,
6461
url: `https://${username}:${password}@${address}:${port}/swagger/v2/swagger.json`,
6562
authorizations: { riot: auth },
6663
dom_id: "swagger-ui-container",
6764
onComplete() {
68-
loadingSwagger = false;
69-
7065
document.querySelectorAll('pre code').forEach((block) => {
7166
hljs.highlightBlock(block);
7267
});
@@ -86,10 +81,7 @@
8681

8782
IPC.on('lcu-load', (event, data) => {
8883
LCUData = data;
89-
90-
if (!swaggerDisabled && !loadingSwagger) {
91-
loadSwagger();
92-
}
84+
loadSwagger();
9385
});
9486

9587
IPC.on('lcu-unload', () => {
@@ -98,34 +90,6 @@
9890
messageBar.innerHTML = 'Waiting for League to start...';
9991
});
10092

101-
IPC.on('swagger-disabled', () => {
102-
swaggerDisabled = true;
103-
messageBar.innerHTML = 'API has been auto-enabled. Please restart the League Client';
104-
105-
// Allow the DOM to render the new HTML
106-
setTimeout(() => {
107-
const allowedRestart = confirm('Rift Explorer needs to restart the League Client to enable the API documentation');
108-
109-
if (allowedRestart) {
110-
const { username, password, address, port } = LCUData;
111-
112-
fetch(`https://${address}:${port}/process-control/v1/process/restart?delaySeconds=0`, {
113-
method: 'POST',
114-
headers: new Headers({ Authorization: `Basic ${btoa(`${username}:${password}`)}` })
115-
});
116-
}
117-
}, 100);
118-
});
119-
120-
IPC.on('swagger-enabled', () => {
121-
swaggerDisabled = false;
122-
messageBar.innerHTML = 'Waiting for League to start...';
123-
124-
if (LCUData && !loadingSwagger) {
125-
loadSwagger();
126-
}
127-
});
128-
12993
function _handleGithubLink(event) {
13094
shell.openExternal('https://github.com/pupix/rift-explorer');
13195
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rift-explorer",
3-
"version": "5.0.0",
3+
"version": "6.0.0",
44
"description": "Always up to date documentation for the League Client API",
55
"main": "app.js",
66
"scripts": {
@@ -10,6 +10,8 @@
1010
"author": "Robert Manolea <[email protected]>",
1111
"license": "MIT",
1212
"dependencies": {
13+
"execa": "^1.0.0",
14+
"fkill": "^6.1.0",
1315
"fs-extra": "^7.0.1",
1416
"lcu-connector": "^2.0.0",
1517
"mixin-deep": "^2.0.0",

util.js

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,74 @@
11
const cp = require('child_process');
22
const path = require('path');
3+
const fs = require('fs-extra');
4+
const yaml = require('yaml');
5+
const fkill = require('fkill');
6+
const execa = require('execa');
7+
38
const IS_WIN = process.platform === 'win32';
9+
const LEAGUE_PROCESS = IS_WIN ? 'LeagueClient.exe' : 'LeagueClient';
410

5-
module.exports.getLCUPathFromProcess = () => {
11+
function getLCUExecutableFromProcess() {
612
return new Promise(resolve => {
713
const command = IS_WIN ?
8-
`WMIC PROCESS WHERE name='LeagueClientUx.exe' GET ExecutablePath` :
9-
`ps x -o comm= | grep 'LeagueClientUx$'`;
14+
`WMIC PROCESS WHERE name='${LEAGUE_PROCESS}' GET ExecutablePath` :
15+
`ps x -o comm= | grep '${LEAGUE_PROCESS}$'`;
1016

11-
cp.exec(command, (err, stdout, stderr) => {
12-
if (err || !stdout || stderr) {
13-
resolve();
17+
cp.exec(command, (error, stdout, stderr) => {
18+
if (error || !stdout || stderr) {
19+
reject(error || stderr);
1420
return;
1521
}
1622

17-
let normalizedPath = path.normalize(stdout);
23+
const normalizedPath = path.normalize(stdout);
24+
resolve(IS_WIN ? normalizedPath.split(/\n|\n\r/)[1] : normalizedPath);
25+
});
26+
});
27+
};
28+
1829

19-
// On MAC we need to go up a few levels to reach `deploy`
20-
// This script also assumes that only Riot Games managed regions are provided
21-
// Hasn't been tested on Garena
22-
normalizedPath = IS_WIN ? normalizedPath.split(/\n|\n\r/)[1] : path.join(normalizedPath, '../../../');
23-
normalizedPath = path.dirname(normalizedPath);
30+
async function duplicateSystemYaml() {
31+
const LCUExePath = await getLCUExecutableFromProcess();
32+
const LCUDir = path.dirname(LCUExePath);
2433

25-
resolve(normalizedPath);
26-
});
34+
const originalSystemFile = path.join(LCUDir, 'system.yaml');
35+
const overrideSystemFile = path.join(LCUDir, 'Config', 'rift-explorer', 'system.yaml');
36+
37+
// File doesn't exist, do nothing
38+
if (!(await fs.exists(originalSystemFile))) {
39+
throw new Error('system.yaml not found');
40+
}
41+
42+
const file = await fs.readFile(originalSystemFile, 'utf8');
43+
const fileParsed = yaml.parse(file);
44+
45+
fileParsed.enable_swagger = true;
46+
47+
const stringifiedFile = yaml.stringify(fileParsed);
48+
// Rito's file is prefixed with --- newline
49+
await fs.outputFile(overrideSystemFile, `---\n${stringifiedFile}`);
50+
}
51+
52+
function restartLCUWithOverride() {
53+
return new Promise(async (resolve, reject) => {
54+
const LCUExePath = await getLCUExecutableFromProcess();
55+
const LCUDir = path.dirname(LCUExePath);
56+
const overrideSystemFile = path.join(LCUDir, 'Config', 'rift-explorer', 'system.yaml');
57+
58+
// Windows is unable to kill the child processes for some reason so we have to force kill it
59+
await fkill(LEAGUE_PROCESS, { force: true });
60+
61+
// By force killing it the LeagueClient doesn't cleanup the lockfile so we gotta do it manually
62+
await fs.remove(path.join(LCUDir, 'lockfile'));
63+
64+
// Give it some time to do cleanup
65+
execa(LCUExePath, [`--system-yaml-override=${overrideSystemFile}`], { detached: true });
66+
resolve();
2767
});
28-
};
68+
}
69+
70+
module.exports = {
71+
getLCUExecutableFromProcess,
72+
duplicateSystemYaml,
73+
restartLCUWithOverride,
74+
};

0 commit comments

Comments
 (0)