Add code that was made before initialization of the Git repository

This commit is contained in:
X3F200C 2023-08-07 13:35:27 -04:00
parent b401a982aa
commit 4134f25d39
2 changed files with 149 additions and 0 deletions

138
index.js Normal file
View File

@ -0,0 +1,138 @@
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);
}
});

11
package.json Normal file
View File

@ -0,0 +1,11 @@
{
"dependencies": {
"checksum": "^1.0.0"
},
"name": "open-plutonium-updater",
"version": "0.0.1",
"main": "index.js",
"author": "X3F200C",
"license": "MIT",
"private": true
}