forked from Pupix/rift-explorer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
150 lines (121 loc) · 3.94 KB
/
app.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
const electron = require('electron');
const LCUConnector = require('lcu-connector');
const DiscordRPC = require('discord-rpc');
const {
duplicateSystemYaml,
restartLCUWithOverride,
} = require('./util');
const connector = new LCUConnector();
const { app, dialog } = electron;
const { BrowserWindow } = electron;
const root = `${__dirname}/app`;
// Checking if the running executable is called electron
// seems to be the most straightforward to do this
// https://stackoverflow.com/a/39395885/4895858
const isDev = process.execPath.search('electron') !== -1;
const clientId = '616399159322214425';
const rpc = new DiscordRPC.Client({ transport: 'ipc' });
const startTimestamp = new Date();
let LCURestarted = false;
app.commandLine.appendSwitch('--ignore-certificate-errors');
app.on('ready', () => {
let mainWindow = null;
let windowLoaded = false;
let LCUData = null;
mainWindow = new BrowserWindow({
center: true,
height: 720,
show: false,
width: 1280,
title: 'Rift Explorer',
backgroundColor: '#303030',
webPreferences: {
nodeIntegration: true,
},
});
if (isDev) {
mainWindow.openDevTools();
}
// Remove default menu
mainWindow.setMenu(null);
mainWindow.loadURL(`file://${root}/index.html`);
// Avoid white page on load.
mainWindow.webContents.on('did-finish-load', () => {
windowLoaded = true;
mainWindow.show();
if (!LCUData) {
return;
}
mainWindow.webContents.send('lcu-load', LCUData);
});
mainWindow.on('closed', () => {
mainWindow = null;
});
connector.on('connect', async (data) => {
// During multiple restarts of the client the backend server is not instantly
// ready to serve requests so we delay a bit
setTimeout(async () => {
LCUData = data;
try {
if (LCURestarted) {
mainWindow.webContents.send('lcu-load', LCUData);
LCURestarted = false;
return;
}
await duplicateSystemYaml();
const response = dialog.showMessageBoxSync({
type: 'info',
buttons: ['Cancel', 'Ok'],
title: 'Rift Explorer',
message: 'Rift Explorer needs to restart your League of Legends client to work properly',
cancelId: 0,
noLink: true,
});
if (!response) {
mainWindow.close();
return;
}
await restartLCUWithOverride(LCUData);
// https://github.com/eslint/eslint/issues/11899
// eslint-disable-next-line require-atomic-updates
LCURestarted = true;
} catch (error) {
console.error(error);
// No error handling for now
}
}, 5000);
});
connector.on('disconnect', () => {
LCUData = null;
if (windowLoaded) {
mainWindow.webContents.send('lcu-unload');
}
});
async function setActivity() {
if (!rpc || !mainWindow) {
return;
}
rpc.setActivity({
startTimestamp,
largeImageKey: 'rift',
largeImageText: 'Rift Explorer',
instance: false,
}).catch(console.error);
}
rpc.on('ready', () => {
setActivity().catch(console.error);
// activity can only be set every 15 seconds
setInterval(() => {
setActivity().catch(console.error);
}, 15e3);
});
rpc.on('error', console.error);
connector.start();
rpc.login({ clientId }).catch(console.error);
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
rpc.destroy().catch(console.error);
app.quit();
}
});