diff --git a/index.js b/index.js index c0a1680..acacae0 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ 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 crypto = require("node:crypto"); const manifestURL = "https://cdn.plutonium.pw/updater/prod/info.json"; @@ -66,7 +66,24 @@ function httpsDownload(url, filePath) { }); } -function checkFiles(manifest) { +function getFileSHA1(filePath) { + return new Promise(function (resolve, reject) { + var fileStream = fs.createReadStream(filePath); + + var hash = crypto.createHash("sha1"); + hash.setEncoding("hex"); + + fileStream.on("end", function () { + hash.end(); + + resolve(hash.read()); + }); + + fileStream.pipe(hash); + }); +} + +async function checkFiles(manifest) { var filesToDownload = []; for (var f = 0; f < manifest.files.length; f++) { @@ -79,9 +96,7 @@ function checkFiles(manifest) { continue; } - var sum = checksum(filePath, { - "algorithm": "sha1" - }); + var sum = await getFileSHA1(filePath); if (sum !== file.hash) { console.log("File " + file.name + " needs to be downloaded again. "); @@ -99,7 +114,7 @@ async function downloadFiles(baseURL, files) { var file = files[f]; var filePath = path.join(basePath, ...(file.name.split("/"))); - console.log("Downloading file " + file.name + "..."); + console.log("Downloading file " + file.name + "... "); await httpsDownload(baseURL + file.hash, filePath); } @@ -125,14 +140,28 @@ 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); - } + checkFiles(data).then(function (wrongFiles) { + if (wrongFiles.length > 0) { + console.log("We have " + wrongFiles.length + " files to download. "); + downloadFiles(data.baseUrl, wrongFiles).then(function () { + launchLauncher().then(function () { - await launchLauncher(); + }).catch(function (error) { + console.error("An error has occurred while opening the launcher. \n" + error.stack); + }); + + }).catch(function (error) { + console.error("An error has occurred while downloading a file. \n" + error.stack); + }); + } else { + console.log("All files seem good ! "); + } + }).catch(function (error) { + console.error("An error has occurred while checking the files. \n" + error.stack); + }); } catch (error) { - console.error(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); }); diff --git a/package.json b/package.json index 4a8f454..d7fac4e 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,7 @@ { - "dependencies": { - "checksum": "^1.0.0" - }, "name": "open-plutonium-updater", - "version": "0.0.1", + "version": "0.2.0", "main": "index.js", "author": "X3F200C", - "license": "MIT", - "private": true + "license": "GPL-3.0" }