Skip to content

Commit

Permalink
Add a hacky fix for Windows portable builds
Browse files Browse the repository at this point in the history
Signed-off-by: mmvanheusden <[email protected]>
  • Loading branch information
mmvanheusden committed Jul 9, 2022
1 parent 5c0fe6f commit b8eb915
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
19 changes: 14 additions & 5 deletions downloader.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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())
Expand Down
38 changes: 28 additions & 10 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Checks if dotnet is installed in the system path
* @returns {Promise<unknown>} A promise that resolves to true if dotnet is installed, false otherwise
*/


function checkDotnet() {
return new Promise((resolve) => {
if (process.platform.toString().includes("win")) {
Expand Down Expand Up @@ -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<unknown>} 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 () {
Expand All @@ -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<unknown>} 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)
}
Expand All @@ -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<unknown>} 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)
}
Expand All @@ -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<unknown>} 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)
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -173,4 +184,11 @@ function runCommand(command) {
})
}

module.exports = {checkDotnet, download, createCommand, runCommand, removeDir, removeFile, unzip}
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}

0 comments on commit b8eb915

Please sign in to comment.