open-plutonium-updater/index.js

139 lines
3.1 KiB
JavaScript
Raw Normal View History

const fs = require("node:fs");
const https = require("node:https");
const path = require("node:path");
const child_process = require("node:child_process");
const checksum = require("checksum");
const manifestURL = "https://cdn.plutonium.pw/updater/prod/info.json";
var basePath = path.join(process.env["LOCALAPPDATA"], "Plutonium");
function httpsGet(url) {
return new Promise(function (resolve, reject) {
var request = https.request(url, function (response) {
var body = "";
response.setEncoding("utf-8");
response.on("data", function (chunk) {
body += chunk;
});
response.on("end", function () {
resolve({
"status": response.statusCode,
"headers": response.headers,
"body": body
});
});
});
request.on("error", function (error) {
reject(error.message);
});
request.end();
});
}
function httpsDownload(url, filePath) {
return new Promise(function (resolve, reject) {
var fileDirectory = path.dirname(filePath);
if (!fs.existsSync(fileDirectory)) {
fs.mkdirSync(fileDirectory, {
"recursive": true
});
}
var fileWriter = fs.createWriteStream(filePath);
var request = https.request(url, function (response) {
var body = "";
response.pipe(fileWriter);
response.on("end", function () {
resolve();
});
});
request.on("error", function (error) {
reject(error.message);
fileWriter.close();
});
request.end();
});
}
function checkFiles(manifest) {
var filesToDownload = [];
for (var f = 0; f < manifest.files.length; f++) {
var file = manifest.files[f];
var filePath = path.join(basePath, ...(file.name.split("/")));
if (!fs.existsSync(filePath)) {
console.log("File " + file.name + " needs to be downloaded. ");
filesToDownload.push(file);
continue;
}
var sum = checksum(filePath, {
"algorithm": "sha1"
});
if (sum !== file.hash) {
console.log("File " + file.name + " needs to be downloaded again. ");
filesToDownload.push(file);
} else {
console.log("File " + file.name + " doesn't need to be downloaded again. ");
}
}
return filesToDownload;
}
async function downloadFiles(baseURL, 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);
}
}
function launchLauncher() {
console.log("Launching the launcher ! ");
var launcherPath = path.join(basePath, "bin", "plutonium-launcher-win32.exe");
var subprocess = child_process.spawn(launcherPath, [], {
"detached": true,
"stdio": "ignore"
});
subprocess.on("spawn", function () {
subprocess.unref();
});
}
console.log("Getting manifest...");
httpsGet(manifestURL).then(async function (response) {
try {
var data = JSON.parse(response.body);
var wrongFiles = checkFiles(data);
if (wrongFiles.length > 0) {
console.log("We have " + wrongFiles.length + " files to download");
await downloadFiles(data.baseUrl, wrongFiles);
}
await launchLauncher();
} catch (error) {
console.error(error);
}
});