From b8eb915788f1be6e738f25833d59b3e9b5f586e8 Mon Sep 17 00:00:00 2001 From: mmvanheusden <50550545+mmvanheusden@users.noreply.github.com> Date: Sat, 9 Jul 2022 09:26:40 +0200 Subject: [PATCH] Add a hacky fix for Windows portable builds Signed-off-by: mmvanheusden <50550545+mmvanheusden@users.noreply.github.com> --- downloader.js | 19 ++++++++++++++----- utils.js | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/downloader.js b/downloader.js index 8180189e..159e115a 100644 --- a/downloader.js +++ b/downloader.js @@ -1,4 +1,13 @@ -const {checkDotnet, download, createCommand, runCommand, removeDir, removeFile, unzip} = require("./utils") +const { + checkDotnet, + download, + createCommand, + runCommand, + removeDir, + removeFile, + unzip, + platformpath +} = require("./utils") function submitForm() { checkDotnet().then(async function (result) { @@ -9,16 +18,16 @@ function submitForm() { console.info("dotnet found in PATH") // Remove the old depotdownloader directory - await removeDir("depotdownloader") + await removeDir("depotdownloader", platformpath()) // Download the DepotDownloader binary, so it doesn't have to be included in the source code - await download("https://github.com/SteamRE/DepotDownloader/releases/download/DepotDownloader_2.4.6/depotdownloader-2.4.6.zip") + await download("https://github.com/SteamRE/DepotDownloader/releases/download/DepotDownloader_2.4.6/depotdownloader-2.4.6.zip", platformpath()) // Unzip the DepotDownloader binary - await unzip("depotdownloader-2.4.6.zip", "depotdownloader") + await unzip("depotdownloader-2.4.6.zip", "depotdownloader", platformpath()) // Clean up the old files - await removeFile("depotdownloader-2.4.6.zip") + await removeFile("depotdownloader-2.4.6.zip", platformpath()) // Run the final command await runCommand(createCommand().toString()) diff --git a/utils.js b/utils.js index 3b4421c9..104ad6bb 100644 --- a/utils.js +++ b/utils.js @@ -2,6 +2,8 @@ * Checks if dotnet is installed in the system path * @returns {Promise} A promise that resolves to true if dotnet is installed, false otherwise */ + + function checkDotnet() { return new Promise((resolve) => { if (process.platform.toString().includes("win")) { @@ -32,13 +34,16 @@ function checkDotnet() { /** * Download a file from a url, saving it to the current directory (__dirname) * @param url The url to download from + * @param targetPath The path to save the file to * @returns {Promise} A promise that resolves when the download is finished */ -function download(url) { +function download(url, targetPath) { return new Promise((resolve) => { const {https} = require("follow-redirects") const fs = require("fs") - const file = fs.createWriteStream(url.split("/").pop().toString()) + const path = require("path") + console.log(path.sep) + const file = fs.createWriteStream(targetPath + path.sep + url.split("/").pop()) https.get(url, function (response) { response.pipe(file) file.on("finish", function () { @@ -52,12 +57,14 @@ function download(url) { /** * Removes a file from the current directory * @param file The filename to remove + * @param targetPath The directory the file is located in * @returns {Promise} A promise that resolves when the file is removed (or fails) */ -function removeFile(file) { +function removeFile(file, targetPath) { return new Promise((resolve) => { const fs = require("fs") - fs.unlink(file, function (err) { + const path = require("path") + fs.unlink(targetPath + path.sep + file, function (err) { if (err) { console.error(err) } @@ -69,12 +76,14 @@ function removeFile(file) { /** * Removes a directory from the current directory * @param dir The directory to remove + * @param targetPath The directory the directory is located in * @returns {Promise} A promise that resolves when the directory is removed (or fails) */ -function removeDir(dir) { +function removeDir(dir, targetPath) { return new Promise((resolve) => { const fs = require("fs") - fs.rm(dir, {recursive: true, force: true}, function (err) { + const path = require("path") + fs.rm(targetPath + path.sep + dir, {recursive: true, force: true}, function (err) { if (err) { console.error(err) } @@ -87,13 +96,15 @@ function removeDir(dir) { * Unzip a file to the current directory * @param file The file to unzip, preferably a .zip file * @param target The target directory to unzip to + * @param targetPath The directory the file AND the unzip dir is located in * @returns {Promise} A promise that resolves when the unzip is complete (or fails) */ -function unzip(file, target) { +function unzip(file, target, targetPath) { const {exec} = require("child_process") + const path = require("path") return new Promise((resolve) => { if (process.platform.toString().includes("win")) { - const command = "powershell.exe -Command Expand-Archive -Path " + __dirname + "/" + file + " -Destination " + target + const command = "powershell.exe -Command Expand-Archive -Path " + targetPath + path.sep + file + " -Destination " + targetPath + path.sep + target exec(command, function (error, stdout, stderr) { if (error) { console.error("Unzipping failed with error: " + error) @@ -138,7 +149,7 @@ const createCommand = () => { if (osdropdown.options[osdropdown.selectedIndex].text.includes("Gnome")) { return `gnome-terminal -e 'bash -c "dotnet ./depotdownloader/DepotDownloader.dll -username ${username} -password ${password} -app ${appid} -depot ${depotid} -manifest ${manifestid} -dir ./games/${appid}/ -max-servers 50 -max-downloads 16";bash'` } else if (osdropdown.options[osdropdown.selectedIndex].text.includes("Windows")) { - return `start cmd.exe /k dotnet ./depotdownloader/DepotDownloader.dll -username ${username} -password ${password} -app ${appid} -depot ${depotid} -manifest ${manifestid} -dir ./games/${appid}/ -max-servers 50 -max-downloads 16` + return `start cmd.exe /k dotnet ${platformpath()}/depotdownloader/DepotDownloader.dll -username ${username} -password ${password} -app ${appid} -depot ${depotid} -manifest ${manifestid} -dir ${platformpath()}/games/${appid}/ -max-servers 50 -max-downloads 16` } else if (osdropdown.options[osdropdown.selectedIndex].text.includes("macOS")) { return `osascript -c 'tell application "Terminal" to do script 'dotnet ./depotdownloader/DepotDownloader.dll -username ${username} -password ${password} -app ${appid} -depot ${depotid} -manifest ${manifestid} -dir ./games/${appid}/ -max-servers 50 -max-downloads 16'` } else if (osdropdown.options[osdropdown.selectedIndex].text.includes("Konsole")) { @@ -173,4 +184,11 @@ function runCommand(command) { }) } -module.exports = {checkDotnet, download, createCommand, runCommand, removeDir, removeFile, unzip} \ No newline at end of file +const platformpath = () => { + if (__dirname.includes("AppData") && process.platform.toString().includes("win")) { + return process.env.PORTABLE_EXECUTABLE_DIR + } else + return __dirname +} + +module.exports = {checkDotnet, download, createCommand, runCommand, removeDir, removeFile, unzip, platformpath} \ No newline at end of file