Skip to content

Commit

Permalink
Override system.yaml path to fix problems introduced by the new LCU p…
Browse files Browse the repository at this point in the history
…atcher
  • Loading branch information
Pupix committed May 19, 2019
1 parent 6c3f744 commit f366477
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 85 deletions.
52 changes: 24 additions & 28 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const electron = require('electron');
const LCUConnector = require('lcu-connector');
const fs = require('fs-extra');
const path = require('path');
const yaml = require('yaml');
const { getLCUPathFromProcess } = require('./util');
const {
duplicateSystemYaml,
restartLCUWithOverride,
} = require('./util');

const connector = new LCUConnector();
const { app } = electron;
const { app, dialog } = electron;
const { BrowserWindow } = electron;
const root = __dirname + '/app';
let LCURestarted = false;

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

app.on('ready', () => {
let mainWindow = null;
let windowLoaded = false;
let LCUData = null;
let swaggerDisabled = true;

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

// Check if dev env FIXME
// mainWindow.openDevTools();
mainWindow.openDevTools();

// Remove default menu
mainWindow.setMenu(null);
Expand All @@ -43,7 +43,6 @@ app.on('ready', () => {
return;
}

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

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

try {
const LCUPath = await getLCUPathFromProcess();
const systemFile = path.join(LCUPath, 'system.yaml');

// File doesn't exist, do nothing
if (!(await fs.pathExists(systemFile))) {
throw new Error('system.yaml not found');
if (LCURestarted) {
mainWindow.webContents.send('lcu-load', LCUData);
return;
}

const file = await fs.readFile(systemFile, 'utf8');
const fileParsed = yaml.parse(file);

swaggerDisabled = !fileParsed.enable_swagger;
mainWindow.webContents.send(swaggerDisabled ? 'swagger-disabled' : 'swagger-enabled');

if (!fileParsed.enable_swagger) {
fileParsed.enable_swagger = true;
const stringifiedFile = yaml.stringify(fileParsed);

// Rito's file is prefixed with --- newline
await fs.outputFile(systemFile, `---\n${stringifiedFile}`);
await duplicateSystemYaml();
const response = dialog.showMessageBox({
type: 'info',
buttons: [ 'Cancel', 'Ok' ],
title: 'Rift Explorer',
message: 'Rift Explorer needs to restart your League of Legends client to work properly',
cancelId: 0,
});

if (!response) {
mainWindow.close();
return;
}

await restartLCUWithOverride();
LCURestarted = true;
} catch (error) {
console.error(error);
// No error handling for now
}

mainWindow.webContents.send('lcu-load', LCUData);
}, 5000);
});

Expand Down
38 changes: 1 addition & 37 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

let LCUData = null;
let swaggerDisabled = true;
let loadingSwagger = false;

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

loadingSwagger = true;

window.swaggerUi = new SwaggerUi({
validatorUrl: null,
url: `https://${username}:${password}@${address}:${port}/swagger/v2/swagger.json`,
authorizations: { riot: auth },
dom_id: "swagger-ui-container",
onComplete() {
loadingSwagger = false;

document.querySelectorAll('pre code').forEach((block) => {
hljs.highlightBlock(block);
});
Expand All @@ -86,10 +81,7 @@

IPC.on('lcu-load', (event, data) => {
LCUData = data;

if (!swaggerDisabled && !loadingSwagger) {
loadSwagger();
}
loadSwagger();
});

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

IPC.on('swagger-disabled', () => {
swaggerDisabled = true;
messageBar.innerHTML = 'API has been auto-enabled. Please restart the League Client';

// Allow the DOM to render the new HTML
setTimeout(() => {
const allowedRestart = confirm('Rift Explorer needs to restart the League Client to enable the API documentation');

if (allowedRestart) {
const { username, password, address, port } = LCUData;

fetch(`https://${address}:${port}/process-control/v1/process/restart?delaySeconds=0`, {
method: 'POST',
headers: new Headers({ Authorization: `Basic ${btoa(`${username}:${password}`)}` })
});
}
}, 100);
});

IPC.on('swagger-enabled', () => {
swaggerDisabled = false;
messageBar.innerHTML = 'Waiting for League to start...';

if (LCUData && !loadingSwagger) {
loadSwagger();
}
});

function _handleGithubLink(event) {
shell.openExternal('https://github.com/pupix/rift-explorer');
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rift-explorer",
"version": "5.0.0",
"version": "6.0.0",
"description": "Always up to date documentation for the League Client API",
"main": "app.js",
"scripts": {
Expand All @@ -10,6 +10,8 @@
"author": "Robert Manolea <[email protected]>",
"license": "MIT",
"dependencies": {
"execa": "^1.0.0",
"fkill": "^6.1.0",
"fs-extra": "^7.0.1",
"lcu-connector": "^2.0.0",
"mixin-deep": "^2.0.0",
Expand Down
76 changes: 61 additions & 15 deletions util.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,74 @@
const cp = require('child_process');
const path = require('path');
const fs = require('fs-extra');
const yaml = require('yaml');
const fkill = require('fkill');
const execa = require('execa');

const IS_WIN = process.platform === 'win32';
const LEAGUE_PROCESS = IS_WIN ? 'LeagueClient.exe' : 'LeagueClient';

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

cp.exec(command, (err, stdout, stderr) => {
if (err || !stdout || stderr) {
resolve();
cp.exec(command, (error, stdout, stderr) => {
if (error || !stdout || stderr) {
reject(error || stderr);
return;
}

let normalizedPath = path.normalize(stdout);
const normalizedPath = path.normalize(stdout);
resolve(IS_WIN ? normalizedPath.split(/\n|\n\r/)[1] : normalizedPath);
});
});
};


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

resolve(normalizedPath);
});
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.parse(file);

fileParsed.enable_swagger = true;

const stringifiedFile = yaml.stringify(fileParsed);
// Rito's file is prefixed with --- newline
await fs.outputFile(overrideSystemFile, `---\n${stringifiedFile}`);
}

function restartLCUWithOverride() {
return new Promise(async (resolve, reject) => {
const LCUExePath = await getLCUExecutableFromProcess();
const LCUDir = path.dirname(LCUExePath);
const overrideSystemFile = path.join(LCUDir, 'Config', 'rift-explorer', 'system.yaml');

// Windows is unable to kill the child processes for some reason so we have to force kill it
await fkill(LEAGUE_PROCESS, { force: true });

// By force killing it the LeagueClient doesn't cleanup the lockfile so we gotta do it manually
await fs.remove(path.join(LCUDir, 'lockfile'));

// Give it some time to do cleanup
execa(LCUExePath, [`--system-yaml-override=${overrideSystemFile}`], { detached: true });
resolve();
});
};
}

module.exports = {
getLCUExecutableFromProcess,
duplicateSystemYaml,
restartLCUWithOverride,
};
Loading

0 comments on commit f366477

Please sign in to comment.