-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.js
More file actions
341 lines (299 loc) · 8.69 KB
/
setup.js
File metadata and controls
341 lines (299 loc) · 8.69 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
const cp = require("child_process");
const fs = require("fs");
const fetch = require("node-fetch");
const inquirer = require("inquirer");
global.version = "1.4.2";
global.delay = (ms) => new Promise((res) => setTimeout(res, ms));
global.cacheObj = {};
global.clearLastLine = () => {
process.stdout.moveCursor(0, -1);
process.stdout.clearLine(1);
};
global.getCompiled = () => {
let path = require.main.path;
let splitPath = path.split("\\");
splitPath.pop();
let name = splitPath.pop();
if (name === "snapshot") return true;
return false;
};
global.writeCache = async () => {
let path = getConfigPath();
path += "cache.json";
await fs.writeFileSync(path, JSON.stringify(cacheObj, null, "\t"));
};
global.configObj = {};
let defaultConfig = {
updateDetected: false,
unfinishedDownloads: [],
};
module.exports = async () => {
console.log("\x1b[34m[Setup]\x1b[0m", "Checking python version...");
let py_ver;
try {
result = await cp.execSync("py --version", { stdio: "pipe" }).toString();
py_ver = result.slice(7, result.length - 2);
} catch (e) {
if (e.toString() === "Error: Command failed: py --version") {
console.log(
"\x1b[31m[Error]\x1b[0m",
"Python not installed! First install it in order to use this tool! (Remember to add it to the path)"
);
await delay(5000);
process.exit(1);
}
}
let installedVersion = py_ver.split(".");
let compatible;
if (3 <= parseInt(installedVersion[0])) {
if (parseInt(installedVersion[0]) === 3) {
if (9 <= parseInt(installedVersion[1])) compatible = true;
else compatible = false;
} else compatible = true;
} else compatible = false;
if (compatible)
console.log(
"\x1b[34m[Setup]\x1b[0m",
`Compatible Python detected (${py_ver})! Proceding...`
);
else {
console.log(
"\x1b[31m[Error]\x1b[0m",
`Incompatible Python detected (${py_ver}), you need Python 3.9+ to run this program! Exiting in 5 seconds...`
);
await delay(5000);
process.exit(1);
}
let configPath = getConfigPath();
if (!(await fs.existsSync(configPath + "config.json"))) {
console.log(
"\x1b[34m[Setup]\x1b[0m",
"No config file found! Creating one..."
);
createConfig();
await updateLegendary();
} else {
console.log(
"\x1b[34m[Setup]\x1b[0m",
"Config file found! Proceding..."
);
await checkConfig();
checkForUpdateLegendary();
}
console.log("\x1b[34m[Setup]\x1b[0m", "Checking if a valid session exists!");
await loginOntoLegendary();
console.log("\x1b[34m[Setup]\x1b[0m", `Loading cache...`);
await loadCache();
console.log("\x1b[34m[Setup]\x1b[0m", `Finished loading cache...`);
console.log("\x1b[36m[Info]\x1b[0m", `Checking for updates...`);
const isThereAnUpdate = await isUpdateAvailable();
if (isThereAnUpdate)
console.log(
"\x1b[36m[Info]\x1b[0m",
`A new update has been detected! (v${isThereAnUpdate})`
);
else
console.log("\x1b[36m[Info]\x1b[0m", `You are running the latest version!`);
console.log(
"\x1b[34m[Setup]\x1b[0m",
"Setup completed! Starting app and clearing console..."
);
console.log("\n");
await delay(isThereAnUpdate ? 5000 : 1000);
console.clear();
};
async function updateLegendary() {
let legendary_log;
console.log(
"\x1b[34m[Setup]\x1b[0m",
"Checking if legendary is installed and updated..."
);
try {
legendary_log = cp.execSync("py -m pip install --upgrade legendary-gl", {
stdio: "pipe",
});
} catch (e) {
throw new Error(e);
}
if (legendary_log.toString().includes("Collecting legendary-gl"))
console.log(
"\x1b[34m[Setup]\x1b[0m",
"legendary got installed/updated! Proceding..."
);
else
console.log(
"\x1b[34m[Setup]\x1b[0m",
"legendary was already updated! Proceding..."
);
let legendary_ver = cp.execSync("legendary --version").toString().split('"');
console.log(
"\x1b[36m[Info]\x1b[0m",
"Detected legendary version: " + legendary_ver[1] + " | " + legendary_ver[3]
);
}
/**
* @param {array} prevConfigs
*/
async function createConfig(prevConfigs) {
let path = getConfigPath();
let newConfigs = defaultConfig;
if (prevConfigs)
prevConfigs.forEach((config) => {
newConfigs[config.name] = config.value;
});
await fs.writeFileSync(
path + "config.json",
JSON.stringify(newConfigs, null, "\t")
);
}
configObj.setConfig = async (entry, value) => {
let configPath = getConfigPath();
let config = await configObj.getConfig();
config[entry] = value;
await fs.writeFileSync(
configPath + "config.json",
JSON.stringify(config, null, "\t")
);
};
configObj.getConfig = async (returnConfigPath) => {
let configPath = getConfigPath();
if (returnConfigPath) return configPath;
let config = await fs.readFileSync(configPath + "config.json").toString();
config = await JSON.parse(config);
return config;
};
function getConfigPath() {
let configPath;
if (getCompiled())
configPath = process.argv0.split("\\");
else configPath = process.argv[1].split("\\");
configPath.pop();
configPath = configPath.join("\\") + "\\";
return configPath;
}
async function checkConfig() {
let config = await configObj.getConfig();
if (config.updateDetected) {
await updateLegendary();
configObj.setConfig("updateDetected", false);
}
if (Object.entries(config).length !== Object.entries(defaultConfig).length) {
let prevConfigs = [];
Object.entries(config).forEach((entry) => {
prevConfigs.push({
name: entry,
value: config[entry],
});
});
createConfig(prevConfigs);
}
}
async function checkForUpdateLegendary() {
let legendary_ver = await cp
.execSync("legendary --version")
.toString()
.split('"');
const release = await fetch(
"https://api.github.com/repos/derrod/legendary/releases/latest"
).then((res) => {
return res.json();
});
if (legendary_ver[1] !== release.tag_name) {
configObj.setConfig("updateDetected", true);
}
}
global.isUpdateAvailable = async () => {
const release = await fetch(
"https://api.github.com/repos/AngelCMHxD/easy-legendary/releases/latest"
).then((res) => {
return res.json();
});
if (version !== release.tag_name) return release.tag_name;
return false;
};
async function loadCache() {
let cache;
let cachePath = getConfigPath() + "cache.json";
let cacheExists = fs.existsSync(cachePath);
if (cacheExists) {
cache = await fs.readFileSync(cachePath).toString();
cacheObj = JSON.parse(cache);
} else await startCaching();
removeFinishedDownloadsFromConfig();
}
global.startCaching = async () => {
console.log(
"\x1b[36m[Cache]\x1b[0m",
"(1/6) Caching installed games list..."
);
await require("./utils/searchGames.js")("installed");
clearLastLine();
console.log("\x1b[36m[Cache]\x1b[0m", "(2/6) Caching owned games list...");
await require("./utils/searchGames.js")("owned");
clearLastLine();
console.log(
"\x1b[36m[Cache]\x1b[0m",
"(3/6) Caching installed games logs..."
);
await require("./utils/getInstalledGames")();
clearLastLine();
console.log("\x1b[36m[Cache]\x1b[0m", "(4/6) Caching owned games logs...");
await require("./utils/getOwnedGames")();
clearLastLine();
console.log("\x1b[36m[Cache]\x1b[0m", "(5/6) Caching legendary-gl path...");
await require("./utils/getLegendaryPath")();
clearLastLine();
console.log("\x1b[36m[Cache]\x1b[0m", "(6/6) Saving cache...");
writeCache();
clearLastLine();
};
global.removeFinishedDownloadsFromConfig = async () => {
const config = await configObj.getConfig();
let unfinishedDownloads = [];
await config.unfinishedDownloads.forEach(async (download) => {
const isInstalled = await require("./utils/isInstalled")(download.name);
console.log(isInstalled);
if (!isInstalled) unfinishedDownloads.push(download);
});
configObj.setConfig("unfinishedDownloads", unfinishedDownloads);
};
async function loginOntoLegendary() {
let alreadyLoggedIn = true;
try {
await cp.execSync("legendary auth", { stdio: "pipe" });
} catch (e) {
alreadyLoggedIn = false;
console.clear();
console.log(
"Follow these steps in order to login with your Epic Games Account:"
);
console.log("1. Login into Epic Games (A pop-up should have appeared).");
const authorizationCode = await inquirer
.prompt([
{
name: "authJson",
type: "input",
message: "2. Copy and paste the result text here: ",
},
])
.then(async (a) => {
try {
let auth = JSON.parse(a.authJson).authorizationCode;
if (auth) return auth;
else throw new Error("Invalid JSON");
} catch (e) {
if (e.toString().startsWith("SyntaxError")) return a.authJson;
if (e.toString().startsWith("Error: Invalid JSON")) {
console.log(
"\x1b[31m[Error]\x1b[0m JSON does not contains authorizationCode."
);
await delay(3000);
process.exit(1);
}
}
});
await cp.execSync(`legendary auth --code "${authorizationCode}"`, { stdio: "pipe" });
console.clear();
}
return alreadyLoggedIn;
}