Advertisement
Thatguy5532

Untitled

May 16th, 2023
72
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 }) {
  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.         console.log(`${path}\n${new_mod_dir}`);
  40.         fs_extra.moveSync(path, new_mod_dir, err => {
  41.             if (err) return -3; // Move Destination Error
  42.            
  43.             console.log(`Your mod has been moved to the following directory:\n${new_mod_dir}`)
  44.         })
  45.        
  46.         console.log("Creating Workshop Object...");
  47.        
  48.         // Creating the game ID and Mod ID properties
  49.         workshop_object[app_id] = {};
  50.         workshop_object[app_id][published_file_id] = {};
  51.  
  52.         // Assigning the mod data to the previously created Mod ID property
  53.         workshop_object[app_id][published_file_id] = { name: name, updated: updated};
  54.         console.log(workshop_object);
  55.  
  56.         const jsonString = JSON.stringify(workshop_object);
  57.  
  58.         console.log("Updating Workshop.json...")
  59.         fs.writeFileSync('./workshop.json', jsonString, err => {
  60.             if (err) {
  61.                 console.log('Error writing file, reverting changes', err);
  62.                 fs_extra.remove(new_mod_dir);
  63.                 return -1;
  64.             } else {
  65.                 console.log('Successfully wrote file');
  66.             };
  67.         });
  68.  
  69.         return 1;
  70.     } catch (error) {
  71.         console.log('Error:', error);
  72.         // Handle the error accordingly
  73.         return 0;
  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