Advertisement
Thatguy5532

Untitled

May 12th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let workshop_object = require("./workshop.json");
  2. const fs = require('fs');
  3. const fs_extra = require("fs-extra");
  4.  
  5. async function download({ published_file_id, name, updated }) {
  6.     const { execFile } = require('child_process');
  7.  
  8.     let { steam_user, steam_pass, app_id, mod_dir, SteamCMD } = require('./config.json');
  9.  
  10.  
  11.     let args = [
  12.         // `+force_install_dir ${mod_dir}`, wee thing doesnt just throw the downloaded folders into the directory, it creates the whole steam tree
  13.         `+login ${steam_user} ${steam_pass}`,
  14.         `+workshop_download_item ${app_id} ${published_file_id}`,
  15.         `+quit`
  16.     ];
  17.  
  18.     console.log("Checking Directory Existence...")
  19.     if (fs.existsSync(mod_dir + `\\${published_file_id}`)) {
  20.         console.log("Directory Exists Already");
  21.         return -4; // Mod Already Exists
  22.     }
  23.     console.log("Dir");
  24.     await execFile(SteamCMD, args, (error, stderr, stdout) => {
  25.         console.log("error");
  26.         if (error) {
  27.             console.log("Error:");
  28.             console.log(error);
  29.             return -1; // Some Other Error, likely with execFile call
  30.         };
  31.    
  32.         console.log("succ");
  33.         if (stderr.search("Success") == -1) {
  34.             console.log("STDError:");
  35.             console.log(stderr);
  36.             return -2; // Download Error
  37.         };
  38.        
  39.         if (stderr.search("Success") > -1) {
  40.             console.log("SUCCESS");
  41.             console.log(stderr);
  42.  
  43.             let path = stderr.substring(stderr.indexOf("\"") + 1);
  44.             path = path.substring(0, path.indexOf("\""));
  45.  
  46.             console.log("Moving...");
  47.             fs_extra.move(path, mod_dir + `\\${published_file_id}`, err => {
  48.                 if (err) return -3; // Move Destination Error
  49.                
  50.                 console.log("Creating Workshop Object...");
  51.                 workshop_object[app_id] = {};
  52.                 workshop_object[app_id][published_file_id] = { name: name, updated: updated};
  53.                 console.log(workshop_object);
  54.  
  55.                 const jsonString = JSON.stringify(workshop_object);
  56.  
  57.                 console.log("Updating Workshop.json...")
  58.                 fs.writeFileSync('./workshop.json', jsonString, err => {
  59.                     if (err) {
  60.                         console.log('Error writing file', err)
  61.                     } else {
  62.                         console.log('Successfully wrote file')
  63.                     }
  64.                 });
  65.                 console.log(`Your mod has been moved to the following directory:\n${mod_dir + `\\${published_file_id}`}`)
  66.                 return 1; // 1 == true
  67.             })
  68.         };
  69.         return 0; // Shouldnt get this code, just an in-case thing
  70.  
  71.         // SteamCMD doesnt use stdout for some reason?
  72.         // console.log(stdout);
  73.    
  74.     });
  75. }
  76.  
  77. async function delete_mod(app_id, published_file_id) {
  78.  
  79. }
  80. module.exports = { download };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement