Advertisement
Thatguy5532

Untitled

May 16th, 2023
121
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 { execFile } = require('child_process');
  5. const { promisify } = require('util');
  6. const promisifiedExecFile = promisify(execFile);
  7.  
  8. async function download({ published_file_id, name, updated, image, link }) {
  9.  
  10.     let { steam_user, steam_pass, app_id, mod_dir, SteamCMD } = require('./config.json');
  11.  
  12.  
  13.     let args = [
  14.         // `+force_install_dir ${mod_dir}`, wee thing doesnt just throw the downloaded folders into the directory, it creates the whole steam tree
  15.         `+login ${steam_user} ${steam_pass}`,
  16.         `+workshop_download_item ${app_id} ${published_file_id}`,
  17.         `+quit`
  18.     ];
  19.  
  20.     console.log("Checking Directory Existence...")
  21.     if (fs.existsSync(mod_dir + `\\${published_file_id}`)) {
  22.         console.log("Directory Exists Already");
  23.         return -4; // Mod Already Exists
  24.     }
  25.  
  26.     try {
  27.         const { stdout, stderr } = await promisifiedExecFile(SteamCMD, args, { cwd: 'C:\\Users\\Thatguy553\\Downloads\\steamcmd\\' });
  28.         // Process the stdout and stderr as needed
  29.         console.log(stdout);
  30.         console.log(stderr);
  31.         console.log('Process completed successfully.');
  32.  
  33.         let path = stdout.substring(stdout.indexOf("\"") + 1);
  34.         path = path.substring(0, path.indexOf("\""));
  35.  
  36.         let new_mod_dir = mod_dir + `\\${published_file_id}`;
  37.  
  38.         console.log("Moving...");
  39.         fs_extra.move(path, new_mod_dir, (err) => {
  40.             if (err) return -3; // Move Destination Error
  41.            
  42.             console.log(`Your mod has been moved to the following directory:\n${new_mod_dir}`)
  43.         })
  44.        
  45.         console.log("Creating Workshop Object...");
  46.        
  47.         // Creating the game ID and Mod ID properties
  48.         workshop_object[app_id] = {};
  49.         workshop_object[app_id][published_file_id] = {};
  50.  
  51.         // Assigning the mod data to the previously created Mod ID property
  52.         workshop_object[app_id][published_file_id] = { name: name, image: image, link: link, updated: updated, mod_path: new_mod_dir};
  53.         console.log(workshop_object);
  54.  
  55.         const jsonString = JSON.stringify(workshop_object);
  56.  
  57.         console.log("Updating Workshop.json...")
  58.         fs.writeFile('./workshop.json', jsonString, (err) => {
  59.             if (err) {
  60.                 console.log('Error writing file, reverting changes', err);
  61.                 fs_extra.remove(new_mod_dir);
  62.                 return -1;
  63.             } else {
  64.                 console.log('Successfully wrote file');
  65.             };
  66.         });
  67.  
  68.         return 1;
  69.     } catch (error) {
  70.         console.log('Error:', error);
  71.         // Handle the error accordingly
  72.         return 0;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement