-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenderer.js
More file actions
354 lines (325 loc) · 9.23 KB
/
renderer.js
File metadata and controls
354 lines (325 loc) · 9.23 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
342
343
344
345
346
347
348
349
350
351
352
353
354
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
// -- { Imports } --
const fs = require("fs");
const path = require("path");
const unzipper = require("unzipper");
const { DownloaderHelper } = require("node-downloader-helper");
const copydir = require("copy-dir");
const childprocess = require("child_process");
// -- { Util functions } --
// Write messages under text input
function writeOnScreen(message) {
document.getElementById("error").innerHTML = message;
document.getElementById("error").style.display = "block";
}
// Convert bytes to megabytes
const bytesToMegaBytes = (bytes) => bytes / (1024 * 1024);
// Store temp files in appdata
function correctPath(pathToFile) {
if (process.platform == "darwin") {
applicationSupportPath =
"/Users/" +
process.env.USER +
"/Library/Application Support/modpackInstaller";
pathToFile = pathToFile.substring(1);
pathToFile = applicationSupportPath + pathToFile;
}
if (process.platform == "win32") {
appdataPath = path.join(process.env.APPDATA, "/modpackInstaller");
pathToFile = pathToFile.substring(1);
pathToFile = path.join(appdataPath, pathToFile);
}
return pathToFile;
}
// Determine if a downloaded file is a modpack
function isModpack() {
if (!fs.existsSync(correctPath("./modpack"))) {
return false;
}
if (!fs.existsSync(correctPath("./modpack/installer"))) {
return false;
}
if (!fs.existsSync(correctPath("./modpack/installer/profile.json"))) {
return false;
}
if (!fs.existsSync(correctPath("./modpack/installer/installer.jar"))) {
return false;
}
return true;
}
// Add correct path to game JSON
function prepareJson(profileData) {
tempProfile = profileData["profiles"]["profile"];
if (process.platform == "darwin") {
tempProfile["gameDir"] = path.join(
"/Users/",
process.env.USER,
"/Library/Application Support/minecraft/",
tempProfile["gameDir"]
);
}
if (process.platform == "win32") {
tempProfile["gameDir"] = path.join(
process.env.APPDATA.replace("\\", "/"),
"/",
tempProfile["gameDir"]
);
}
return tempProfile;
}
// Edit Launcher Profiles
function editLauncherProfiles(tempProfile, callback) {
if (process.platform == "darwin") {
launcherProfilePath = path.join(
"/Users/",
process.env.USER,
"/Library/Application Support/minecraft/launcher_profiles.json"
);
}
if (process.platform == "win32") {
launcherProfilePath = path.join(
process.env.APPDATA,
"/.minecraft/launcher_profiles.json"
);
}
fs.readFile(launcherProfilePath, "utf8", (err, data) => {
profileName = tempProfile["name"].toLowerCase().replace(" ", "");
launcherProfileData = JSON.parse(data);
launcherProfileData["profiles"][profileName] = tempProfile;
fs.writeFile(
launcherProfilePath,
JSON.stringify(launcherProfileData),
"utf8",
() => {
callback();
}
);
});
}
// Get bundled Java from Minecraft launcher
function getJavaRuntime() {
if (process.platform == "darwin") {
return path.join(
"/Users/",
process.env.USER,
"/Library/Application Support/minecraft/runtime/jre-x64/jre.bundle/Contents/Home/bin/java"
);
}
if (process.platform == "win32") {
// TODO
return "java";
}
}
// Clean up files
function cleanUp() {
writeOnScreen("Cleaning up");
if (fs.existsSync(correctPath("./modpack.zip"))) {
fs.unlinkSync(correctPath("./modpack.zip"));
}
if (fs.existsSync(correctPath("./installer.jar.log"))) {
fs.unlinkSync(correctPath("./installer.jar.log"));
}
if (fs.existsSync(correctPath("./modpack"))) {
fs.rmSync(correctPath("./modpack"), { recursive: true });
}
if (process.platform == "darwin") {
applicationSupportPath =
"/Users/" +
process.env.USER +
"/Library/Application Support/modpackInstaller";
if (fs.existsSync(applicationSupportPath)) {
fs.rmSync(applicationSupportPath, { recursive: true });
}
}
if (process.platform == "win32") {
appdataPath = path.join(process.env.APPDATA, "/modpackInstaller");
if (fs.existsSync(appdataPath)) {
fs.rmSync(appdataPath, { recursive: true });
}
}
}
// Clean up in case of errors
function cleanError() {
try {
profilePath = correctPath("./modpack/installer/profile.json");
fs.readFile(profilePath, "utf8", (err, data) => {
profileData = JSON.parse(data);
tempProfile = prepareJson(profileData);
if (fs.existsSync(tempProfile["gameDir"])) {
fs.rmSync(tempProfile["gameDir"], { recursive: true });
}
});
} catch (error) {}
cleanUp();
}
// Download Files
function download(url, dest, name, callback) {
const dl = new DownloaderHelper(url, dest, { fileName: name });
dl.on("end", callback);
dl.on("progress.throttled", (stats) => {
writeOnScreen(
"Downloading: " +
stats.progress.toFixed(0) +
"% " +
bytesToMegaBytes(stats.speed).toFixed(2) +
"MB/s"
);
});
dl.on("error", () => {
cleanUp();
writeOnScreen("Invalid Download Link!");
document.getElementById("loader").style.display = "none";
});
dl.start();
}
// Start and stop loader
function startLoader() {
document.getElementById("loader").style.display = "block";
}
function stopLoader() {
document.getElementById("loader").style.display = "none";
}
// TODO - Increase list of downloadable websites
function isModpackURL(downloadLink) {
if (downloadLink.substr(downloadLink.length - 4) == ".zip") {
return true;
} else if (
downloadLink.includes("dropbox") &&
downloadLink.charAt(downloadLink.length - 1) == "1"
) {
return true;
} else {
return false;
}
}
// -- { Installer stages } --
// Make temporary dirs
function makeTempDirs() {
console.log("Installing on " + process.platform);
if (process.platform == "darwin") {
if (
!fs.existsSync(
"/Users/" +
process.env.USER +
"/Library/Application Support/modpackInstaller"
)
) {
fs.mkdirSync(
"/Users/" +
process.env.USER +
"/Library/Application Support/modpackInstaller"
);
}
}
if (process.platform == "win32") {
if (!fs.existsSync(path.join(process.env.APPDATA, "/modpackInstaller"))) {
fs.mkdirSync(path.join(process.env.APPDATA, "/modpackInstaller"));
}
}
}
// Download modpack
function downloadModpack(callback) {
startLoader();
console.log("Using modpack at " + document.getElementById("link").value);
downloadLink = document.getElementById("link").value;
if (isModpackURL(downloadLink)) {
download(
document.getElementById("link").value,
correctPath("./"),
"modpack.zip",
callback
);
} else {
cleanUp();
writeOnScreen("Link is not a valid download link!");
stopLoader();
}
}
// extract modpack zip
function extractModpack(callback) {
writeOnScreen("Extracting");
fs.createReadStream(correctPath("./modpack.zip"))
.pipe(unzipper.Extract({ path: correctPath("./") }))
.on("close", callback);
}
// verify that the modpack is a modpack
function verifyModpack(callback) {
writeOnScreen("Verifying");
if (!isModpack()) {
cleanUp();
writeOnScreen("Link is not a valid modpack!");
stopLoader();
throw error;
}
callback();
}
// move the modpack to the target directory
function moveModpack(callback) {
writeOnScreen("Moving Game Directory");
profilePath = correctPath("./modpack/installer/profile.json");
fs.readFile(profilePath, "utf8", (err, data) => {
let profileData = JSON.parse(data);
tempProfile = prepareJson(profileData);
if (fs.existsSync(tempProfile["gameDir"])) {
copydir.sync(
correctPath("./modpack/mods"),
path.join(tempProfile["gameDir"], "/mods")
);
} else {
copydir.sync(correctPath("./modpack"), tempProfile["gameDir"]);
}
callback(tempProfile);
});
}
// install the modloader (forge, fabric, etc)
function installModloader(callback) {
writeOnScreen("Installing Modloader");
childprocess.exec(
`"${getJavaRuntime()}" -jar "` +
correctPath("./modpack/installer/installer.jar") +
`"`,
{ encoding: "utf-8", cwd: correctPath("./") },
callback
);
}
// install the launcher profile
function installProfile(tempProfile, callback) {
writeOnScreen("Inserting Profile");
editLauncherProfiles(tempProfile, callback);
}
// cleanup
function cleanInstall() {
cleanUp();
console.log("done!");
stopLoader();
writeOnScreen("Modpack Successfully Installed");
}
// -- { Main install function } --
function install() {
startLoader();
try {
makeTempDirs();
downloadModpack(() =>
extractModpack(() =>
verifyModpack(() =>
moveModpack((tempProfile) =>
installProfile(tempProfile, () =>
installModloader(() => cleanInstall())
)
)
)
)
);
} catch (error) {
cleanError();
writeOnScreen("Error Installing Modpack");
stopLoader();
}
}
// Add install function to button
document.getElementById("install").onclick = install;