-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
414 lines (363 loc) · 10.8 KB
/
index.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
// PAQUETES IMPORTADOS
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const prompt = require('electron-prompt');
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const { Launcher, Downloader } = require('adlauncher-core');
const launcher = new Launcher();
const downloader = new Downloader();
const os = require('os');
const { spawn, execSync } = require('child_process');
const fetch = require('electron-fetch').default;
// VARIABLES
const User = os.userInfo().username;
let mainRoot = `C:/Users/${User}/AppData/Roaming/.minecraft`;
const appRoot = `${mainRoot}/.adlauncher`;
let javaRoot = 'C:/Program Files/Java/jdk-17/bin/java';
let java8Root = 'C:/Program Files/Java/jre-1.8/bin/java';
let configFile, maxMem, minMem, user, users, versions;
let isRunning = false;
// CREA EL ARCHIVO DE CONFIGS
if (!fs.existsSync(path.resolve(appRoot, 'configs', 'settings.json'))) {
fs.mkdirSync(path.resolve(appRoot, 'configs'), { recursive: true });
fs.writeFileSync(
path.resolve(appRoot, 'configs', 'settings.json'),
JSON.stringify({
javaPath: javaRoot,
java8Path: java8Root,
gameDirectory: mainRoot,
memory: { max: '4G', min: '2G' },
users: ['Steve'],
})
);
}
// LEE EL ARCHIVO DE CONFIGS
function reloadConfigs() {
configFile = JSON.parse(fs.readFileSync(path.resolve(appRoot, 'configs', 'settings.json')));
// OBTIENE LA RUTA DEL JUEGO
mainRoot = configFile.gameDirectory;
// OBTIENE LAS MEMORIAS DECLARADAS
maxMem = configFile.memory.max;
minMem = configFile.memory.min;
// OBTIENE EL ULTIMO USER CONECTADO
try {
user = configFile.users[0];
} catch (error) {
user = 'Steve';
}
// OBTIENE LOS USUARIOS REGISTRADOS
users = configFile.users;
mainRoot = configFile.gameDirectory;
// OBTIENE LAS VERSIONES DESCARGADAS
try {
versions = fs.readdirSync(`${mainRoot}/versions`);
} catch (error) {
versions = [];
}
// OBTIENE LOS DIRECTORIOS DE JAVA
javaRoot = configFile.javaPath;
java8Root = configFile.java8Path;
}
// CHECKJAVA
function checkJava(javaVersions, win) {
javaVersions.forEach((javaSpawn) => {
const java = spawn(javaSpawn, ['-version']);
java.on('error', () => {
// CREATE DOWNLOAD JAVA WINDOW
const wi = new BrowserWindow({
parent: win,
height: 200,
width: 300,
webPreferences: {
contextIsolation: true,
preload: path.join(__dirname, 'src', 'preload.js'),
devTools: false,
},
autoHideMenuBar: true,
icon: path.join(__dirname, 'src', 'assets', 'icon.png'),
resizable: false,
});
wi.loadFile(path.join(__dirname, 'src', 'pages', 'checkJava.html'));
if (javaSpawn.includes('1.8')) {
wi.webContents.send('java', '8');
} else {
wi.webContents.send('java', '17');
}
});
});
}
// DOWNLOAD JAVA
async function downloadJava(javaUri, name) {
try {
const filePath = path.join(appRoot, name);
const response = await fetch(javaUri);
const writeStream = fs.createWriteStream(filePath);
response.body.pipe(writeStream);
await new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
execSync(filePath);
fs.unlinkSync(filePath);
console.log(`Java ${name} instalado correctamente.`);
} catch (error) {
console.log(error);
}
}
// ELECTRON
const createWindow = () => {
// CREA LA VENTANA
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
contextIsolation: true,
preload: path.join(__dirname, 'src', 'preload.js'),
devTools: false,
},
autoHideMenuBar: true,
icon: path.join(__dirname, 'src', 'assets', 'icon.png'),
resizable: false,
});
win.menuBarVisible = false;
reloadConfigs();
checkJava([javaRoot, java8Root], win);
ipcMain.on('accept', (event, javaV) => {
switch (javaV) {
case '8':
downloadJava(
'https://javadl.oracle.com/webapps/download/AutoDL?BundleId=249553_4d245f941845490c91360409ecffb3b4',
'java8.exe'
);
break;
case '17':
downloadJava(
'https://download.oracle.com/java/17/archive/jdk-17.0.10_windows-x64_bin.exe',
'java17.exe'
);
break;
default:
break;
}
});
ipcMain.on('cancel', () => {
win.close();
});
// EVENTOS DEL PROGRAMA
ipcMain.on('redirect', (event, uri) => {
const wi = new BrowserWindow({
parent: win,
webPreferences: {
contextIsolation: true,
devTools: false,
},
autoHideMenuBar: true,
resizable: false,
});
switch (uri) {
case 'discord':
wi.loadURL('https://discord.gg/a93w5NpBR9');
break;
case 'yt':
wi.loadURL('https://www.youtube.com/@dani_adbg');
break;
case 'github':
wi.loadURL('https://github.com/dani-adbg');
break;
default:
break;
}
});
ipcMain.on('getImg', (event, version) => {
reloadConfigs();
win.webContents.send('sendImg', version);
});
ipcMain.on('getVersions', () => {
reloadConfigs();
win.webContents.send('sendVersions', versions);
});
ipcMain.on('getUser', () => {
reloadConfigs();
win.webContents.send('sendUser', user);
});
ipcMain.on('getUsers', (event, settings) => {
reloadConfigs();
win.webContents.send('sendUsers', users, settings);
});
ipcMain.on('play', async (event, user, version) => {
reloadConfigs();
await launcher.launch({
username: user,
version: version,
gameDirectory: mainRoot,
memory: {
max: maxMem,
min: minMem,
},
java: javaRoot,
java8: java8Root,
});
if (isRunning === false) {
isRunning = true;
launcher.on('debug', (data) => {
win.webContents.send('debug', data);
});
}
});
ipcMain.on('getSettings', () => {
reloadConfigs();
win.webContents.send('sendSettings', mainRoot, minMem, maxMem, java8Root, javaRoot);
});
ipcMain.on('changeRoot', async (event, option) => {
reloadConfigs();
const dir = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [
{ name: 'Archivo', extensions: ['exe'] },
{ name: 'Carpeta', extensions: [''] },
],
});
if (dir.filePaths.length !== 0) {
switch (option) {
case 'main':
configFile.gameDirectory = dir.filePaths[0].replace(/\\/g, '/');
win.webContents.send('changeRoot', option, configFile.gameDirectory);
break;
case 'java8':
configFile.java8Path = dir.filePaths[0].replace(/\\/g, '/');
win.webContents.send('changeRoot', option, configFile.java8Path);
break;
case 'java':
configFile.javaPath = dir.filePaths[0].replace(/\\/g, '/');
win.webContents.send('changeRoot', option, configFile.javaPath);
break;
}
}
});
ipcMain.on('input', async (event, opt) => {
reloadConfigs();
switch (opt) {
case 'min':
const min = await prompt({
title: 'Ingrese un número entero',
label: 'Valor:',
inputAttrs: {
type: 'number',
},
type: 'input',
});
configFile.memory.min = min + 'G';
win.webContents.send('changeMin', min + 'G');
break;
case 'max':
const max = await prompt({
title: 'Ingrese un número entero',
label: 'Valor:',
inputAttrs: {
type: 'number',
},
type: 'input',
});
configFile.memory.max = max + 'G';
win.webContents.send('changeMax', max + 'G');
break;
case 'user':
const newUser = await prompt({
title: 'Ingrese el nuevo usuario',
label: 'Nombre de Usuario',
inputAttrs: {
type: 'text',
},
type: 'input',
});
let repeated = true;
if (!configFile.users.includes(newUser) || newUser.length === 0 || newUser === null) {
configFile.users.push(newUser);
fs.writeFileSync(
path.resolve(appRoot, 'configs', 'settings.json'),
JSON.stringify(configFile)
);
repeated = false;
}
win.webContents.send('newUser', newUser, repeated);
reloadConfigs();
break;
default:
break;
}
});
ipcMain.on('saveSettings', (event, newRoot, newMin, newMax) => {
configFile.gameDirectory = newRoot;
configFile.memory.min = newMin;
configFile.memory.max = newMax;
const availableMemory = Math.floor((os.totalmem() - os.freemem()) / 1073741824);
if (newMin >= newMax || newMax >= availableMemory) {
win.webContents.send('memoriesError');
} else {
win.webContents.send('succesSaveSettings');
fs.writeFileSync(
path.resolve(appRoot, 'configs', 'settings.json'),
JSON.stringify(configFile)
);
reloadConfigs();
}
});
ipcMain.on('delete', (event, element, type) => {
reloadConfigs();
if (type === 'version') {
fse.removeSync(path.resolve(mainRoot, 'versions', element));
fse.removeSync(path.resolve(mainRoot, 'natives', element));
} else {
configFile.users = configFile.users.filter((x) => x !== element);
fs.writeFileSync(
path.resolve(appRoot, 'configs', 'settings.json'),
JSON.stringify(configFile)
);
}
});
// VERSIONS PAGES
ipcMain.on('getVersionsPages', async () => {
downloader.getVersions('vanilla').then((versions) => {
versions = versions.filter((x) => x.id.split('.')[1] >= 8);
win.webContents.send('sendVersionsPages', versions);
});
});
ipcMain.on('downloadVersion', (event, version) => {
downloader.download(version, mainRoot);
downloader.on('percentDownloaded', (data) => {
if (data.includes('100%')) {
data = 'Instalando...';
win.webContents.send('percentDownloaded', data);
} else {
win.webContents.send('percentDownloaded', data);
}
});
downloader.on('downloadFiles', (data) => {
if (data.includes('All files are downloaded')) {
win.webContents.send('versionDownloaded');
} else if (data.includes('Minecraft')) {
win.webContents.send('versionDownloading');
}
});
});
// CARGA EL ARCHIVO PRINCIPAL EN LA VENTANA
win.loadFile(path.join(__dirname, 'src', 'index.html'));
};
// CONFIGURACIONES GENERALES
app.on('ready', () => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
});