From 29bffa472b99d2e03df8bf3cb0fcca3749c9f4b2 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 22 Feb 2018 19:13:47 +0100 Subject: [PATCH] Fixed modManager add and remove functions --- lib/manager/modManager.js | 87 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/lib/manager/modManager.js b/lib/manager/modManager.js index 8d104a8f..af432a8d 100644 --- a/lib/manager/modManager.js +++ b/lib/manager/modManager.js @@ -17,7 +17,7 @@ const config = require("./../../config"); const instances = require("./../getInstances.js").sync(); function ellipse(str, max){ - return str.length > (max - 3) ? str.substring(0,max-3) + '...' : str; + return str.length > (max - 3) ? str.substring(0,max-3) + '...' : str; } /** @@ -39,7 +39,7 @@ module.exports.listMods = function(instance){ token: config.token, require_ownership: false }).then(token => { - console.log("Currently installed:"); + console.log("* Currently installed:"); factorioAPI.readModZips().then(list => { console.log(asTable(list.map(mod => { return { @@ -54,7 +54,7 @@ module.exports.listMods = function(instance){ // Had issues earlier due to bug in node-factorio-api, see https://github.com/Danacus/node-factorio-api/issues/2 // bug is resolved, thanks to @Danacus and @brotherpot factorioAPI.getModsFromSaves().then(list => { - console.log("Mods used in save:"); + console.log("* Mods used in save:"); console.log(asTable(list[0].mods)) }); }).catch(reason => { @@ -68,7 +68,7 @@ module.exports.listMods = function(instance){ token: config.token, require_ownership: false }).then(() => { - console.log("Mods shared by all instances:") + console.log("* Mods shared by all instances:") factorioAPI.getInstalledMods().then(mods => { console.log(asTable(mods)); }); @@ -110,9 +110,86 @@ module.exports.findMods = function(searchTerm){ }); } +module.exports.addMod = function(term, instance){ + if(instance == "shared"){ // modDirectory has to end in / + var modDirectory = "sharedMods/"; + } else { + var modDirectory = "instances/"+instance+"/instanceMods/"; + } + if(term && term.length > 0){ + if(term.includes("mods.factorio.com/mods/")){ // https://mods.factorio.com/mods/Danielv123/ChooChoo + // treat it as a link download + factorioAPI.init(false, modDirectory, "", "0.16.15"); + factorioAPI.authenticate({ + username: config.username, + token: config.token, + require_ownership: false + }).then(() => { + factorioAPI.searchMods(term.split("/")[term.split("/").length-1]).then(body => { + console.log("Im gonna download: "+body[0].name); + factorioAPI.downloadMod(body[0]).then(()=>{ + console.log("Mod downloaded"); + }); + }); + }); + } else if(term.includes("github.com")){ // https://github.com/Danielv123/factorioClusterioMod + // treat it as a github releases download + let apiURL = "https://api.github.com/repos/" + term.split("/")[3] + "/" + term.split("/")[4] + "/releases"; // https://api.github.com/repos/Danielv123/factorioClusterioMod/releases + + let res = syncRequest('GET', apiURL, {"headers":{"User-Agent":"Fuck you for requiring user agents!"}}); + let url = JSON.parse(res.getBody())[0].assets[0].browser_download_url; + let name = JSON.parse(res.getBody())[0].assets[0].name; + if(url) { + console.log("Downloading " + url); + let file = fs.createWriteStream(modDirectory + name); + https.get(url, function(response) { + response.pipe(file); + console.log("Downloaded "+name); + }); + } + } else { + // treat it as a search and download the top result + factorioAPI.init(false, modDirectory, "", "0.16.15"); + factorioAPI.authenticate({ + username: config.username, + token: config.token, + require_ownership: false + }).then(() => { + factorioAPI.searchMods(term).then(body => { + console.log("Im gonna download: "+body[0].name); + factorioAPI.downloadMod(body[0]).then(()=>{ + console.log("Mod downloaded"); + }); + }); + }); + } + } +} +module.exports.removeMod = function removeMod(term, instance, force = false){ + if(instance == "shared"){ // modDirectory has to end in / + var modDirectory = "sharedMods/"; + } else { + var modDirectory = "instances/"+instance+"/instanceMods/"; + } + if(term && typeof term == "string" && term.length > 0){ + let mods = fs.readdirSync(modDirectory); + let modsMatchingFilter = mods.filter(mod => mod.toLowerCase().includes(term.toLowerCase())); + if(modsMatchingFilter.length < 1){ + console.log("No mods found matching search"); + } else if(modsMatchingFilter.length == 1){ + console.log("Deleting "+modsMatchingFilter[0]); + fs.unlinkSync(modDirectory+modsMatchingFilter[0]); + console.log("Deleted."); + } else if(modsMatchingFilter.length > 1){ + console.log("Found multiple mods, not doing anything"); + modsMatchingFilter.forEach(mod => console.log(mod)); + } + } +} + // 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases' /** -Download mod from github repo by looking for the first binary download in +Download mod from github repo by looking for the first binary download in the latest release, saving it to /sharedMods @param {string} url a github repo URL, for example https://api.github.com/repos/Danielv123/factorioClusterioMod/releases. Must link to /releases