Advertisement
Thatguy5532

Untitled

May 12th, 2023 (edited)
266
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. const util = require('util');
  5.  
  6. async function download({ published_file_id, name, updated }) {
  7.     const execFile = util.promisify(require('child_process').execFile);
  8.  
  9.     let { steam_user, steam_pass, app_id, mod_dir, SteamCMD } = require('./config.json');
  10.  
  11.  
  12.     let args = [
  13.         // `+force_install_dir ${mod_dir}`, wee thing doesnt just throw the downloaded folders into the directory, it creates the whole steam tree
  14.         `+login ${steam_user} ${steam_pass}`,
  15.         `+workshop_download_item ${app_id} ${published_file_id}`,
  16.         `+quit`
  17.     ];
  18.  
  19.     console.log("Checking Directory Existence...")
  20.     if (fs.existsSync(mod_dir + `\\${published_file_id}`)) {
  21.         console.log("Directory Exists Already");
  22.         return -4; // Mod Already Exists
  23.     }
  24.  
  25.     // Process currently doesnt seem to exit. Need to solve this somehow
  26.     let process = await execFile(SteamCMD, args, (error, stderr, stdout) => {
  27.         if (error) {
  28.             console.log("Error:");
  29.             console.log(error);
  30.             return -1; // Some Other Error, likely with execFile call
  31.         };
  32.    
  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.  
  44.         let path = stderr.substring(stderr.indexOf("\"") + 1);
  45.         path = path.substring(0, path.indexOf("\""));
  46.  
  47.         let new_mod_dir = mod_dir + `\\${published_file_id}`;
  48.  
  49.         console.log("Moving...");
  50.         fs_extra.move(path, new_mod_dir, err => {
  51.             if (err) return -3; // Move Destination Error
  52.            
  53.             console.log(`Your mod has been moved to the following directory:\n${new_mod_dir}`)
  54.         })
  55.        
  56.         console.log("Creating Workshop Object...");
  57.        
  58.         // Creating the game ID property
  59.         workshop_object[app_id] = workshop_object[app_id];
  60.  
  61.         // Creating the mod ID property inside the newly created (or existing) game id property
  62.         workshop_object[app_id][published_file_id] = { name: name, updated: updated};
  63.         console.log(workshop_object);
  64.  
  65.         const jsonString = JSON.stringify(workshop_object);
  66.  
  67.         console.log("Updating Workshop.json...")
  68.         fs.writeFileSync('./workshop.json', jsonString, err => {
  69.             if (err) {
  70.                 console.log('Error writing file, reverting changes', err);
  71.                 fs_extra.remove(new_mod_dir);
  72.             } else {
  73.                 console.log('Successfully wrote file');
  74.             };
  75.         });
  76.  
  77.         // SteamCMD doesnt use stdout for some reason?
  78.         // console.log(stdout);
  79.         return 0; // Shouldnt get this code, just an in-case thing
  80.     });
  81. }
  82.  
  83. async function delete_mod(app_id, published_file_id) {
  84.  
  85. }
  86. module.exports = { download };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement