-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
566 lines (475 loc) · 20.1 KB
/
main.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
const fs = require('fs');
const { writeFile } = require('fs').promises;
const { execSync, spawn, exec } = require('child_process');
const readline = require('readline');
const figlet = require('figlet');
const path = require('path');
const chalk = require('chalk');
const configFolderPath = 'C:/ConfigManager';
const infoFilePath = 'C:/ConfigManager/info.json';
const package = require('./package.json');
const log = require('./utils/logger');
const _locales = require('./utils/messages')
const fetch = require('node-fetch');
let outputMessage = '';
let _lang = Intl.DateTimeFormat().resolvedOptions().locale;
if(_lang = "pt-BR") locale = _locales.ptBR; else locale = _locales.ptBR // Eng loc not implemented
process.on("unhandledRejection", e => {
console.clear()
log.writeError('unhandledRejection',`INTERNAL`, e, Error)
})
process.on("uncaughtException", e => {
console.clear()
log.writeError('uncaughtException',`INTERNAL`, e, Error)
})
process.on("uncaughtExceptionMonitor", e => {
console.clear()
log.writeError('uncaughtExceptionMonitor',`INTERNAL`, e, Error)
})
exec('node -v', (error, stdout, stderr) => {
if (error || stderr) {
return console.log(locale.nodeNotExist);
}
});
async function checkLatestVersion() {
try {
const response = await fetch(`https://api.github.com/repos/YuriXbr/LoLConfigManager/releases/latest`);
if (response.ok) {
const data = await response.json();
let latestVersion = data.tag_name;
const currentVersion = package.version;
if (latestVersion !== currentVersion) {
isUpdated = false;
return isUpdated;
} else {
isUpdated = true;
return isUpdated;
}
} else {
isUpdated = undefined;
return isUpdated;
}
} catch (error) {
outputMessage = 'Ocorreu um erro ao verificar a versão, verifique os logs';
isUpdated = false;
log.writeError('checkLatestVersion',`INTERNAL`, e, Error)
return isUpdated, outputMessage;
}
}
// Verifica se a pasta ConfigManager existe, senão cria
if (!fs.existsSync(configFolderPath)) {
fs.mkdirSync(configFolderPath);
}
// Verifica se o arquivo info.json existe e está vazio, senão cria ou reconstrói a estrutura inicial
if (!fs.existsSync(infoFilePath) || fs.readFileSync(infoFilePath, 'utf8').trim() === '') {
const initialInfo = {
LeaguePath: 'C:\\Riot Games\\League of Legends\\Config',
ConfigLocked: false,
Mastery: 0,
};
fs.writeFileSync(infoFilePath, JSON.stringify(initialInfo));
}
// Carrega as informações do arquivo info.json
let info = JSON.parse(fs.readFileSync(infoFilePath, 'utf8'));
log.writeSilent(locale.loadedInfo + JSON.stringify(info))
// Cria a interface de leitura
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function findPersistedSettings() {
const configPath = info.LeaguePath
const configFiles = await fs.promises.readdir(configPath);
const persistedSettingsFile = configFiles.find(file => file.toLowerCase() === 'persistedsettings.json');
if (persistedSettingsFile) {
return path.join(configPath, persistedSettingsFile);
} else {
throw new Error(locale.configFileNotFound);
}
}
// Função para fazer uma pergunta ao usuário
function askQuestion(question, options = []) {
return new Promise((resolve) => {
if (options.length > 0) {
options.forEach((option, index) => {
console.log(`[${index + 1}] ${option}`);
});
}
rl.question(question, (answer) => {
resolve(answer);
});
});
}
// Função para exibir o menu e processar a opção selecionada
async function showMenu() {
process.stdout.write('\x1Bc');
await checkLatestVersion();
console.log(chalk.green.bold(figlet.textSync(locale.appName, { horizontalLayout: 'full' })));
if (!info.LeaguePath) {
const leaguePath = await askQuestion(locale.missingLeaguePathConfig);
if (leaguePath) {
info.LeaguePath = leaguePath.trim();
updateInfoFile();
} else {
outputMessage = (chalk.redBright(locale.invalidLeaguePathDir));
showMenu();
return;
}
}
console.log(locale.notAffiliated);
if (isUpdated != undefined) console.log(chalk.yellow.bold(` App Version: `+ `${isUpdated ? chalk.greenBright.bold(locale.updatedVersion + ` (${package.version})`) : chalk.redBright.bold(`(${package.version}) `+ locale.newVersion)}`));
if (isUpdated == undefined) console.log(chalk.yellow.bold(` App Version: `+ chalk.redBright.bold(locale.gitHubRateLimit)))
console.log(locale.functions);
console.log(chalk.greenBright.bold(locale.menuOp0 + ` ${info.LeaguePath ? chalk.yellowBright(info.LeaguePath) : chalk.redBright.bold(locale.menuNotConfigured)})`));
console.log(chalk.greenBright.bold(locale.menuOp1 + `${info.ConfigLocked ? chalk.redBright(locale.menuLocked) : chalk.blueBright(locale.menuUnlocked)})`));
console.log(chalk.greenBright.bold(locale.menuOp2));
console.log(chalk.greenBright.bold(locale.menuOp3));
console.log(chalk.greenBright.bold(locale.menuOp4));
console.log(chalk.greenBright.bold(locale.menuOp5));
console.log(chalk.yellow(locale.menuOpX));
console.log(chalk.yellow(locale.menuOpC))
console.log(chalk.cyanBright(locale.menuOutput))
await console.log(outputMessage || locale.menuNoCommands);
const option = await askQuestion('\n ________________________________________________________________\n'+locale.askQuestionSelectAOption);
if (option.toUpperCase() === 'X') {
await reconfigureOptions();
} else {
processMenuOption(option);
}
}
async function reconfigureOptions() {
console.clear();
const fieldOptions = [
{ field: 'LeaguePath', description: locale.reconfigureOptionsConfigPath},
{ field: 'ConfigLocked', description: locale.reconfigureOptionsConfigLocked},
];
info.Mastery = 0;
const persistedSettingsPath = await findPersistedSettings();
const persistedSettingsData = await fs.promises.readFile(persistedSettingsPath, 'utf-8');
const persistedSettings = JSON.parse(persistedSettingsData);
const gameEventsSection = persistedSettings.files.find(file => file.name === 'Input.ini').sections.find(section => section.name === 'GameEvents');
console.log(chalk.yellow.bold(locale.reconfigureOptionsLabel));
const filteredMasteryEmote = gameEventsSection.settings.filter(setting => {
return setting.name.includes("evtRadialEmotePlaySlot") && setting.value.includes("[r]");
});
filteredMasteryEmote.forEach(setting => {
info.Mastery = 2;
});
const filteredMasteryUlt = gameEventsSection.settings.filter(setting => {
return setting.name.includes("evtChampMasteryDisplay") && setting.value.includes("[r]");
});
filteredMasteryUlt.forEach(setting => {
info.Mastery = 1;
});
if (info.Mastery == 0) _m = locale.infoMasteryNone;
if (info.Mastery == 1) _m = locale.infoMasteryUltimate
if (info.Mastery == 2) _m = locale.infoMasteryEmote
data = [
{LoLPath: `${info.LeaguePath}`},
{Locked: info.ConfigLocked},
{Mastery: `${_m}`}
]
console.table(data)
fieldOptions.forEach((option, index) => {
console.log(`[${index + 1}] ${option.description}`);
});
console.log(`[${fieldOptions.length + 1}] `+ locale.cancel);
console.log(chalk.redBright.bold(locale.redWarning))
console.log(chalk.blueBright.bold(locale.blueWarning));
const option = await askQuestion(locale.reconfigureOptionsQuestion);
const selectedIndex = parseInt(option) - 1;
if (selectedIndex >= 0 && selectedIndex < fieldOptions.length) {
const selectedOption = fieldOptions[selectedIndex];
let newValue;
if (selectedOption.field === 'ConfigLocked') {
const lockOption = await askQuestion(locale.reconfigureOptionsNewValueConfigLocked);
newValue = lockOption.trim().toLowerCase() === 'true';
toggleConfigLock(newValue); // Chama a função toggleConfigLock() passando o novo valor como argumento
} else {
newValue = await askQuestion(`${locale.reconfigureOptionsNewValueTo} "${selectedOption.description}": `);
}
info[selectedOption.field] = newValue;
outputMessage = (`${locale.reconfigureOptionsOption} "${selectedOption.description}" ${locale.reconfigureOptionsSuccess}`);
updateInfoFile();
} else if (selectedIndex === fieldOptions.length) {
outputMessage = (locale.reconfigureOptionsCancel);
} else {
outputMessage = (locale.invalidOption);
}
showMenu();
}
async function resetExplorer() {
await execSync(`taskkill /F /IM explorer.exe`);
exec(`start explorer.exe`)
outputMessage = (locale.restartedExplorer);
showMenu();
}
// Função para processar a opção selecionada no menu
async function processMenuOption(option) {
switch (option) {
case '0':
openLeaguePath();
break;
case '1':
toggleConfigLock();
break;
case '2':
resetUX();
break;
case '3':
configureMastery();
break;
case '4':
lowerPriority();
break;
case '5':
resetExplorer();
break;
default:
outputMessage = (locale.invalidOption);
showMenu();
break;
}
}
// Função para abrir a pasta do LeaguePath no explorador de arquivos do usuário
async function lowerPriority() {
console.log(chalk.yellow.bold(locale.lowerPriorityLabel));
console.log(locale.lowerPriorityRealTime);
console.log(locale.lowerPriorityHigh);
console.log(locale.lowerPriorityAboveNormal);
console.log(locale.lowerPriorityNormal);
console.log(locale.lowerPriorityBelowNormal);
console.log(locale.lowerPriorityLower);
console.log(locale.lowerPriorityExit);
const priority = await askQuestion(locale.lowerPriorityQuestion);
if (priority.toLowerCase() == 'x') return showMenu();
const selectedPriority = parseInt(priority) - 1;
if(selectedPriority == 6) return showMenu();
if(selectedPriority == 0) {
outputMessage = (locale.lowerPriorityORealTime);
execSync(`wmic process where "name='LeagueClientUxRender.exe'" CALL setpriority 256`);
showMenu();
return;
} else if(selectedPriority == 1) {
outputMessage = (locale.lowerPriorityOHigh);
execSync(`wmic process where "name='LeagueClientUxRender.exe'" CALL setpriority 128`);
showMenu();
return;
} else if(selectedPriority == 2) {
outputMessage = (locale.lowerPriorityOAboveNormal);
execSync(`wmic process where "name='LeagueClientUxRender.exe'" CALL setpriority 32768`);
showMenu();
return;
} else if(selectedPriority == 3) {
outputMessage = (locale.lowerPriorityONormal);
execSync(`wmic process where "name='LeagueClientUxRender.exe'" CALL setpriority 32`);
showMenu();
return;
} else if(selectedPriority == 4) {
outputMessage = (locale.lowerPriorityOBelowNormal);
execSync(`wmic process where "name='LeagueClientUxRender.exe'" CALL setpriority 16384`);
showMenu();
return;
} else if(selectedPriority == 5) {
outputMessage = (locale.lowerPriorityOLower);
execSync(`wmic process where "name='LeagueClientUxRender.exe'" CALL setpriority 64`);
showMenu();
return;
} else {
outputMessage = (locale.invalidOption);
showMenu();
}
}
function openLeaguePath() {
if (info.LeaguePath) {
const leaguePath = path.resolve(info.LeaguePath);
let command;
if (process.platform === 'win32') {
command = `explorer "${leaguePath}"`;
} else if (process.platform === 'darwin') {
command = `open "${leaguePath}"`;
} else {
command = `xdg-open "${leaguePath}"`;
}
try {
//const { exec } = require('child_process');
exec(command);
outputMessage = (locale.openLeaguePathSuccess);
} catch (error) {
outputMessage = (chalk.redBright(locale.openLeaguePathError +`${error.message}`));
}
showMenu();
} else {
outputMessage = (chalk.yellowBright.bold(locale.openLeaguePathNotConfigured));
showMenu();
}
}
// Função para alternar a trava de configurações
function toggleConfigLock() {
if (info.ConfigLocked) {
unlockConfig();
} else {
lockConfig();
}
}
// Função para travar as configurações
function lockConfig() {
if (info.ConfigLocked) {
outputMessage = ('As configurações já estão travadas!');
showMenu();
} else {
const persistedSettingsPath = path.join(info.LeaguePath, 'PersistedSettings.json');
try {
fs.chmodSync(persistedSettingsPath, '0444');
info.ConfigLocked = true;
outputMessage = ('Trava de configurações ativa. As configurações foram destravadas com sucesso!');
updateInfoFile();
} catch (error) {
outputMessage = (chalk.redBright(`[ERROR] Ocorreu um erro ao travar as configurações: ${error}`));
} finally {
showMenu();
}
}
}
// Função para destravar as configurações
function unlockConfig() {
if (!info.ConfigLocked) {
outputMessage = ('As configurações já estão destravadas!');
showMenu();
} else {
const persistedSettingsPath = path.join(info.LeaguePath, 'PersistedSettings.json');
try {
fs.chmodSync(persistedSettingsPath, '0666');
info.ConfigLocked = false;
outputMessage = ('Trava de configurações desativada. As configurações foram destravadas com sucesso!');
updateInfoFile();
} catch (error) {
outputMessage = (chalk.redBright(`[ERROR] Ocorreu um erro ao destravar as configurações: ${error}`));
} finally {
showMenu();
}
}
}
// Função para reiniciar os processos relacionados ao League of Legends
function resetUX() {
outputMessage = ('Reiniciando interface do League of Legends...');
// Lista de processos a serem reiniciados
const processes = [
{ name: 'LeagueClientUxRender', args: ['--render'] },
];
// Reiniciar cada processo
processes.forEach((process) => {
try {
execSync(`taskkill /f /im ${process.name}.exe`);
outputMessage += `\nProcesso ` + chalk.yellowBright(process.name)+ ` reiniciado.`;
} catch (error) {
outputMessage += (chalk.redBright(`\n[ERROR] Ocorreu um erro ao reiniciar o processo ${process.name}: \nCódigo de erro: ${error}`));
}
});
}
async function configureMastery() {
try {
const persistedSettingsPath = await findPersistedSettings();
const persistedSettingsData = await fs.promises.readFile(persistedSettingsPath, 'utf-8');
const persistedSettings = JSON.parse(persistedSettingsData);
// console.warn([[persistedSettingsData]])
const gameEventsSection = persistedSettings.files.find(file => file.name === 'Input.ini').sections.find(section => section.name === 'GameEvents');
// console.log(gameEventsSection); // Exibe o objeto diretamente no console
const masteryEventIndex = gameEventsSection.settings.findIndex(setting => setting.name === 'evtChampMasteryDisplay');
const masteryEvent = gameEventsSection.settings[masteryEventIndex];
const masteryValue = masteryEvent.value;
const isReadOnly = await isFileReadOnly(persistedSettingsPath);
if (isReadOnly) {
outputMessage = ('O arquivo PersistedSettings.json está em modo somente leitura. Desbloqueie as configurações antes de usar esta função.');
showMenu();
return;
}
console.log(chalk.yellow.bold('\nOpções de Bind de Maestria:'));
console.log(' [1] Nada ao Ultar');
console.log(' [2] Maestria ao Ultar');
console.log(' [3] Emoji ao Ultar');
console.log(` [4] Cancelar`);
const masteryOption = await askQuestion('\n » Por favor, digite o número da opção de Bind de Maestria que deseja configurar: ');
const selectedMasteryIndex = parseInt(masteryOption) - 1;
if (selectedMasteryIndex >= 0 && selectedMasteryIndex <= 2) {
if (selectedMasteryIndex === 0) { // maestria no control 6
masteryEvent.value = '[Ctrl][6]';
info.Mastery = 0;
const filteredSettings = gameEventsSection.settings.filter(setting => {
return setting.name.includes("evtRadialEmotePlaySlot") && setting.value.includes("[r]");
});
filteredSettings.forEach(setting => {
setting.value = setting.value.replace("[r]", "").trim();
});
} else if (selectedMasteryIndex === 1) { // maestria no R
masteryEvent.value = '[Ctrl][6], [r]';
info.Mastery == 1;
const filteredSettings = gameEventsSection.settings.filter(setting => {
return setting.name.includes("evtRadialEmotePlaySlot") && setting.value.includes("[r]");
});
filteredSettings.forEach(setting => {
setting.value = setting.value.replace("[r]", "").trim();
});
} else if (selectedMasteryIndex === 2) { // maestria no emoji
masteryEvent.value = '[Ctrl][6]';
console.log(chalk.yellow.bold('\nOpções de Mostrar no Emoji:'));
console.log(' [1] Mostrar no Emoji Norte');
console.log(' [2] Mostrar no Emoji Nordeste');
console.log(' [3] Mostrar no Emoji Leste');
console.log(' [4] Mostrar no Emoji Sudeste');
console.log(' [5] Mostrar no Emoji Sul');
console.log(' [6] Mostrar no Emoji Suldoeste');
console.log(' [7] Mostrar no Emoji Oeste');
console.log(' [8] Mostrar no Emoji Noroeste');
console.log(' [9] Mostrar no Emoji Centro');
console.log(`[10] Cancelar`);
const emojiOption = await askQuestion('\n » Por favor, digite o número da opção de Mostrar no Emoji que deseja configurar: ');
const selectedEmojiIndex = parseInt(emojiOption) - 1;
if (selectedEmojiIndex >= 0 && selectedEmojiIndex <= 8) {
const radialEmoteEventIndex = gameEventsSection.settings.findIndex(setting => setting.name === `evtRadialEmotePlaySlot${selectedEmojiIndex}`);
const radialEmoteEvent = gameEventsSection.settings[radialEmoteEventIndex];
const filteredSettings = gameEventsSection.settings.filter(setting => {
return setting.name.includes("evtRadialEmotePlaySlot") && setting.value.includes("[r]");
});
filteredSettings.forEach(setting => {
setting.value = setting.value.replace("[r]", "").trim();
});
radialEmoteEvent.value += ', [r]';
masteryEvent.value = '[Ctrl][6]';
info.Mastery = 2;
} else if (selectedEmojiIndex === 9) {
outputMessage = ('Reconfiguração cancelada.');
showMenu();
return;
} else {
outputMessage = ('Opção inválida! Por favor, selecione uma opção válida.');
showMenu();
return;
}
}
await fs.promises.writeFile(persistedSettingsPath, JSON.stringify(persistedSettings, null, 2), 'utf-8');
outputMessage = (`Opção "Bind de Maestria" reconfigurada com sucesso.`);
} else if (selectedMasteryIndex === 3) {
outputMessage = ('Reconfiguração cancelada.');
} else {
outputMessage = ('Opção inválida! Por favor, selecione uma opção válida.');
}
} catch (error) {
outputMessage = ('Erro ao processar o arquivo PersistedSettings.json. Certifique-se de que o LeaguePath esteja configurado corretamente. \n [ERRO]: ' + error);
}
showMenu();
}
async function isFileReadOnly(filePath) {
try {
const stats = await fs.promises.stat(filePath);
return !(stats.mode & fs.constants.W_OK);
} catch (error) {
return true; // Em caso de erro, assume que o arquivo é somente leitura
}
}
// Função para atualizar o arquivo info.json com as informações atualizadas
function updateInfoFile() {
fs.writeFileSync(infoFilePath, JSON.stringify(info));
}
// Inicializa o programa exibindo o menu
showMenu();