Add support for multiple manifests, command line options and rework some functions

This commit is contained in:
X3F200C 2023-08-07 19:34:58 -04:00
parent b3e33c7d75
commit 01915233c0

View File

@ -4,10 +4,41 @@ const path = require("node:path");
const child_process = require("node:child_process");
const crypto = require("node:crypto");
const manifestURL = "https://cdn.plutonium.pw/updater/prod/info.json";
const manifestURLs = [
"https://cdn.plutonium.pw/updater/prod/info.json",
"https://cdn.kroniker.fr/updater/prod/info.json"
];
var basePath = path.join(process.env["LOCALAPPDATA"], "Plutonium");
var mustSkipCustom = false;
for (var a = 0; a < process.argv.length; a++) {
if (a > 0) {
switch (process.argv[a - 1]) {
case "--install-dir":
basePath = process.argv[a];
break;
default:
break;
}
}
switch (process.argv[a]) {
case "--skip-custom":
mustSkipCustom = true;
break;
default:
break;
}
}
var manifest = {
"files": []
};
function httpsGet(url) {
return new Promise(function (resolve, reject) {
var request = https.request(url, function (response) {
@ -66,6 +97,37 @@ function httpsDownload(url, filePath) {
});
}
function readAndMergeManifest(manifestURL) {
return new Promise(function (resolve, reject) {
httpsGet(manifestURL).then(function (response) {
try {
var data = JSON.parse(response.body);
if ("version" in data) manifest.version = data.version;
for (var f = 0; f < data.files.length; f++) {
var file = data.files[f];
file.baseUrl = data.baseUrl;
var fileMatch = manifest.files.find(entry => entry.name == file.name);
if (typeof fileMatch == "object") {
var matchIndex = manifest.files.indexOf(fileMatch);
manifest.files.splice(matchIndex, 1);
}
manifest.files.push(file);
}
resolve();
} catch (error) {
reject(error);
}
}).catch(function (error) {
reject(error);
});
});
}
function getFileSHA1(filePath) {
return new Promise(function (resolve, reject) {
var fileStream = fs.createReadStream(filePath);
@ -83,7 +145,7 @@ function getFileSHA1(filePath) {
});
}
async function checkFiles(manifest) {
async function checkFiles() {
var filesToDownload = [];
for (var f = 0; f < manifest.files.length; f++) {
@ -109,14 +171,14 @@ async function checkFiles(manifest) {
return filesToDownload;
}
async function downloadFiles(baseURL, files) {
async function downloadFiles(files) {
for (var f = 0; f < files.length; f++) {
var file = files[f];
var filePath = path.join(basePath, ...(file.name.split("/")));
console.log("Downloading file " + file.name + "... ");
await httpsDownload(baseURL + file.hash, filePath);
await httpsDownload(file.baseUrl + file.hash, filePath);
}
}
@ -135,15 +197,21 @@ function launchLauncher() {
});
}
console.log("Getting manifest...");
httpsGet(manifestURL).then(async function (response) {
try {
var data = JSON.parse(response.body);
(async function () {
var manifestCount = mustSkipCustom ? 1 : manifestURLs.length;
checkFiles(data).then(function (wrongFiles) {
for (var m = 0; m < manifestCount; m++) {
console.log("Getting manifest " + (m + 1) + "...");
try {
await readAndMergeManifest(manifestURLs[m]);
} catch (error) {
console.error("An error has occurred while obtaining manifest " + (m + 1) + " : " + error.message);
}
checkFiles().then(function (wrongFiles) {
if (wrongFiles.length > 0) {
console.log("We have " + wrongFiles.length + " files to download. ");
downloadFiles(data.baseUrl, wrongFiles).then(function () {
downloadFiles(wrongFiles).then(function () {
launchLauncher().then(function () {
}).catch(function (error) {
@ -159,9 +227,5 @@ httpsGet(manifestURL).then(async function (response) {
}).catch(function (error) {
console.error("An error has occurred while checking the files. \n" + error.stack);
});
} catch (error) {
console.error("An error has occurred while decoding the main manifest. \n" + error.stack);
}
}).catch(function (error) {
console.error("An error has occurred while getting the main manifest. \n" + error.stack);
});
})();