Advertisement
PlowmanPlow

AddSolderMod

Jun 14th, 2019 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.54 KB | None | 0 0
  1. <?php
  2.  
  3. // Is the "mods" folder local or remote?
  4. // Valid values here are: true or false
  5. define('LOCALMODS', false);
  6.  
  7. // Database connection info
  8. define('DBHOST', 'localhost');
  9. define('DBPORT', '33060');
  10. define('DBUSER', 'solder');
  11. define('DBPASS', 'solder');
  12. define('DBNAME', 'solder');
  13. define('DBPREFIX', '');
  14.  
  15. // Make sure these both have a trailing slash!!!
  16. // MODFOLDER is only used if LOCALMODS is true
  17. define('MODFOLDER', 'C:/Apache24/solder/public/mods/');
  18. define('TEMPFOLDER', 'D:/temp/');
  19.  
  20. // Remote target configuration. Can be ignored if LOCALMODS=true
  21. define('SCPCOMMAND', 'pscp -load RemoteHost');
  22. define('REMOTETARGET', 'solder@solderhost.com:/var/www/htdocs.solder/public/mods/');
  23.  
  24. //
  25. // No need to edit below here.
  26. // (but hey, you do you :) )
  27. //
  28.  
  29. //
  30. // Try to connect to the database. Abort on failure.
  31. //
  32. try {
  33.    $globaldbh = new PDO("mysql:host=" . DBHOST . ";port=" . DBPORT . ";dbname=" . DBNAME, DBUSER, DBPASS);
  34. } catch (PDOException $e) {
  35.    echo "Error connecting to DB!: ", $e->getMessage() . "\n";
  36.    die();
  37. }
  38.  
  39. //
  40. // Ensure there are at least two command line parameters.
  41. // Display usage on failure.
  42. //
  43. if ( $argc < 3 ) {
  44.    echo "Error: Invalid command line parameters\n\n";
  45.    echo "Usage: addsoldermod <slug> <modfilename> [-forge]\n";
  46.    echo "Info: <param> is required, [param] is optional, omit < > and [ ]\n\n";
  47.    exit();
  48. }
  49.  
  50. $forge = false;
  51. $fabric = false;
  52. $config = false;
  53. //
  54. // If there are 3 parameters, and the 3rd is '-forge' or '-fabric'
  55. // set the appropriate boolean to true so we can package
  56. // up the mod differently
  57. //
  58. if ( ($argc == 4) && ($argv[3] == "-forge") ) {
  59.    $forge = true;
  60.    echo "Forge mode specified! Creating /bin/modpack.jar instead of /mods/<modfile>\n";
  61. } elseif ( ($argc == 4) && ($argv[3] == "-fabric") ) {
  62.    $fabric = true;
  63.    echo "Fabric mode specified! Creating /bin/modpack.jar from version.json instead of /mods/<modfile>\n";
  64. } elseif ( ($argc == 4) && ($argv[3] == "-config") ) {
  65.    $config = true;
  66.    echo "Config mode specified! Creating Solder zip from config folder instead of /mods/<modfile>\n";
  67. }
  68.  
  69. //
  70. // Function to validate the format of a slug
  71. //
  72. function isValidSlug($slug = "") {
  73.    $validchars = "abcdefghijklmnopqrstuvwxyz1234567890-";
  74.    $splitslug = str_split($slug);
  75.    foreach ( $splitslug as $char ) {
  76.       if ( strpos($validchars, $char) === false ) return false;
  77.    }
  78.    return true;
  79. }
  80.  
  81. //
  82. // Grab the command line arguments. Abort on failure.
  83. $slug = trim($argv[1]);
  84. $modfile = trim($argv[2]);
  85. echo "Slug: {$slug}\n";
  86. if ( !isValidSlug($slug) ) {
  87.    echo "Error: Invalid slug! Slugs can only contain lower case characters or hyphens!\n";
  88.    exit();
  89. }
  90. echo "Mod File: {$modfile}\n";
  91. if ( !file_exists($modfile) ) {
  92.    echo "Error: Mod file \"{$modfile}\" does not exist!\n";
  93.    exit();
  94. }
  95. //
  96. // Auto-detect forge or fabric
  97. //
  98. if ( !$forge && ($slug == "minecraft-forge") ) {
  99.    echo "*** Forge SLUG detected. Switching to Forge mode now\n";
  100.    $forge = true;
  101. } elseif ( !$fabric && ($slug == "fabric") ) {
  102.    echo "*** Fabric SLUG detected. Switching to Fabric mode now\n";
  103.    $fabric = true;
  104. }
  105.  
  106. //
  107. // Query the database for mod metadata based on provided slug
  108. //
  109. $query = "SELECT id, name, description, author, link, pretty_name FROM " . DBPREFIX . "mods WHERE name=:name";
  110. $sth = $globaldbh->prepare($query);
  111. $fields = array();
  112. $fields[':name'] = $slug;
  113. $sth->execute($fields);
  114. if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
  115.    //
  116.    // If the slug exists, grab its metadata
  117.    //
  118.    $mod_id = $row['id'];
  119.    $mod_name = $slug;
  120.    $mod_pretty_name = $row['pretty_name'];
  121.    $mod_description = $row['description'];
  122.    $mod_author = $row['author'];
  123.    $mod_link = $row['link'];
  124.    $newmod = false;
  125. } else {
  126.    //
  127.    // If the slug does not exist, try to create a new Solder mod
  128.    //
  129.    $newmod = true;
  130.    $mod_id = null;
  131.    $mod_name = $slug;
  132.    $mod_pretty_name = "";
  133.    $mod_description = null;
  134.    $mod_author = null;
  135.    $mod_link = null;
  136.    echo "The mod slug \"{$slug}\" does not seem to exist yet.\n";
  137.    echo "Let's create a new Solder mod in the system now...\n";
  138.    echo "(<CTRL-C> now if you think you made a typo)\n\n";
  139.    //
  140.    // Get the mod name. This is the human readable name.
  141.    //
  142.    while ( $mod_pretty_name == "" ) {
  143.       $mod_pretty_name = readline("Mod Name (human readable display name): ");
  144.       if ( $mod_pretty_name == "" ) {
  145.          echo "Error: Mod name cannot be blank!\n";
  146.          continue;
  147.       }
  148.       if ( strtolower(trim($mod_pretty_name)) == "stop" ) {
  149.          echo "User interrupt: Stopping script...\n";
  150.          exit();
  151.       }
  152.       // Check to see if that name matches anything in the database
  153.       $query = "SELECT id FROM " . DBPREFIX . "mods WHERE pretty_name like :pretty_name";
  154.       $fields = array();
  155.       $fields[':pretty_name'] = $mod_pretty_name;
  156.       $sth = $globaldbh->prepare($query);
  157.       $sth->execute($fields);
  158.       if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
  159.          echo "Error: A mod with that name already exists!\n";
  160.          $mod_pretty_name = "";
  161.          continue;
  162.       }
  163.    }
  164.    while ( $mod_description === null ) {
  165.       $mod_description = readline("Mod Description (can be blank): ");
  166.    }
  167.    while ( $mod_author === null ) {
  168.       $mod_author = readline("Mod Author (can be blank): ");
  169.    }
  170.    while ( $mod_link === null ) {
  171.       $mod_link = readline("Mod Homepage URL (can be blank): ");
  172.       if ( ($mod_link != "") && !filter_var($mod_link, FILTER_VALIDATE_URL) ) {
  173.          echo "Error: That is not a valid URL!\n";
  174.          $mod_link = null;
  175.       }
  176.    }
  177.    //
  178.    // Allow the user to abort if the information is incorrect.
  179.    //
  180.    echo "\nImportant! Please review the following information!\n\n";
  181.    echo "About to add this mod to solder:\n";
  182.    echo "   Mod Name: {$mod_pretty_name}\n";
  183.    echo "   Mod Slug: {$mod_name}\n";
  184.    echo "   Mod Author: {$mod_author}\n";
  185.    echo "   Mod Description: {$mod_description}\n";
  186.    echo "   Mod Homepage: {$mod_link}\n";
  187.    echo "\n";
  188.    // Query the user and abort if they don't hit "y" or "yes"
  189.    $response = strtolower(readline("Create this Solder mod now? (y/n): "));
  190.    if ( ($response !== "y") && ($response !== "yes") ) {
  191.       echo "Not creating mod! Aborting now!\n\n";
  192.       exit();
  193.    }
  194.    //
  195.    // Add the mod to the Solder database
  196.    //
  197.    $query = "INSERT INTO " . DBPREFIX . "mods (id, name, description, author, link, created_at, updated_at, pretty_name) ";
  198.    $query .= "VALUES(null, :name, :description, :author, :link, NOW(), NOW(), :pretty_name)";
  199.    $fields = array();
  200.    $fields[':name'] = $mod_name;
  201.    $fields[':description'] = $mod_description;
  202.    $fields[':author'] = $mod_author;
  203.    $fields[':link'] = $mod_link;
  204.    $fields[':pretty_name'] = $mod_pretty_name;
  205.    $sth = $globaldbh->prepare($query);
  206.    $sth->execute($fields);
  207.    if ( !$sth ) {
  208.       echo "Error: Could not add mod to Solder!\n";
  209.       echo "PDO::errorInfo():\n";
  210.       print_r($sth->errorInfo());
  211.       exit();
  212.    }
  213.    //
  214.    // Grab the ID of the newly created mod. Abort if not available.
  215.    //
  216.    $query = "SELECT id FROM " . DBPREFIX . "mods WHERE name=:name";
  217.    $fields = array();
  218.    $fields[':name'] = $mod_name;
  219.    $sth = $globaldbh->prepare($query);
  220.    $sth->execute($fields);
  221.    if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
  222.       $mod_id = $row['id'];
  223.    } else {
  224.       echo "Could not get ID of new mod! Aborting!\n\n";
  225.       exit();
  226.    }
  227.    echo "New mod successfully created!\n\n";
  228. }
  229. //
  230. // Get the mod version number
  231. //
  232. $modfile_basename = basename($modfile);
  233. echo "Please provide the mod version number for Solder.\n";
  234. echo "This is typically in the format of <MCVersion>-<ModVersion>\n";
  235. echo "(i.e. 1.12.2-3.4.8b1).\n";
  236. echo "Mod File Name: {$modfile_basename}\n";
  237. $mod_version = "";
  238. while ( $mod_version == "" ) {
  239.    $mod_version = readline("Mod Version: ");
  240. }
  241.  
  242. //
  243. // Create the properly formated Solder mod ZIP archive
  244. //
  245.  
  246. // Check to see if a mod ZIP with that version number already exists
  247. $solderzipname = $mod_name . "-" . $mod_version . ".zip";
  248. if ( LOCALMODS && file_exists(MODFOLDER . $mod_name . "/" . $solderzipname) ) {
  249.    echo "Error: A mod file named \"{$mod_name}-{$mod_version}.zip\" already exists!\n";
  250.    echo "Aborting!\n\n";
  251.    exit();
  252. }
  253. // Delete the existing temp file if it exists
  254. if ( LOCALMODS && file_exists(TEMPFOLDER . $solderzipname) ) unlink(TEMPFOLDER . $solderzipname);
  255. mkdir(TEMPFOLDER . $mod_name);
  256. $zip = new ZipArchive();
  257. // Abort here if we can't write to the temp folder
  258. if ( $zip->open(TEMPFOLDER . $mod_name . "/" . $solderzipname, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE ) {
  259.    echo "Error: The solder mod ZIP file could not be created!\n";
  260.    echo "Tried: {$solderzipname}\n";
  261.    echo "Aborting!\n\n";
  262. }
  263. // If this is Forge create a bin/modpack.jar. Create mods/modjarname.jar
  264. if ( $forge ) {
  265.    $zip->addEmptyDir('bin');
  266.    $zip->addFile($modfile, "bin/modpack.jar");
  267. } elseif ( $fabric ) {
  268.    $fabriczip = new ZipArchive();
  269.    $fabriczip->open(TEMPFOLDER . $mod_name . "/fabric-modpack-{$mod_version}.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
  270.    $fabriczip->addFile($modfile, "version.json");
  271.    $fabriczip->close();
  272.    $zip->addEmptyDir('bin');
  273.    $zip->addFile(TEMPFOLDER . $mod_name . "/fabric-modpack-{$mod_version}.zip", "bin/modpack.jar");
  274. } elseif ( $config ) {
  275.    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($modfile_basename), RecursiveIteratorIterator::LEAVES_ONLY);
  276.    foreach ( $files as $name => $file) {
  277.       $filePath = $file->getRealPath();
  278.       $relativePath = str_replace("\\", "/", substr($filePath, strlen(getcwd()) + 1));
  279.       if ( !$file->isDir() ) {
  280.          $zip->addFile($filePath, $relativePath);
  281.       } else {
  282.          if ( $relativePath !== false ) $zip->addEmptyDir($relativePath);
  283.       }
  284.    }
  285. } else {
  286.    $zip->addEmptyDir('mods');
  287.    $zip->addFile($modfile, "mods/{$modfile_basename}");
  288. }
  289. $zip->close();
  290. if ( $fabric ) unlink(TEMPFOLDER . $mod_name . "/fabric-modpack-{$mod_version}.zip");
  291. // Get MD5 sum and file size of the new Solder ZIP archive
  292. $solderzipmd5 = md5_file(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  293. $solderzipsize = filesize(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  294. //
  295. // Display the mod metadata and allow the user to abort
  296. //
  297. echo "\nPlease review the mod information carefully!\n\n";
  298. echo "Ready to add the new mod version:\n";
  299. echo "   Mod Name: {$mod_pretty_name}\n";
  300. echo "   Mod JAR: {$modfile_basename}\n";
  301. echo "   Mod ZIP: {$solderzipname}\n";
  302. echo "   Mod Version: {$mod_version}\n";
  303. echo "\n";
  304. $response = strtolower(readline("Add this new mod version? (y/n): "));
  305. if ( ($response !== "y") && ($response !== "yes") ) {
  306.    echo "Not adding new version! Aborting now!\n\n";
  307.    unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  308.    rmdir(TEMPFOLDER . $mod_name);
  309.    exit();
  310. }
  311. //
  312. // If mods are local we just make folders and move files around
  313. //
  314. if ( LOCALMODS ) {
  315.    // Create the mod's folder under MODFOLDER if not present
  316.    if ( !is_dir(MODFOLDER . $mod_name) ) {
  317.       mkdir(MODFOLDER . $mod_name);
  318.    }
  319.    // Abort if we could not make the folder
  320.    if ( !is_dir(MODFOLDER . $mod_name) ) {
  321.       echo "Error! Could not create mod folder in Solder. Aborting!\n\n";
  322.       unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  323.       rmdir(TEMPFOLDER . $mod_name);
  324.       exit();
  325.    }
  326.    // Move the Solder ZIP archive to the mod's folder under Solder. Abort on failure.
  327.    if ( rename(TEMPFOLDER . $mod_name . "/" . $solderzipname, MODFOLDER . $mod_name . "/" . $solderzipname) === false ) {
  328.       echo "Error! Could not place mod into Solder's mods folder. Aborting!\n\n";
  329.       unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  330.       rmdir(TEMPFOLDER . $mod_name);
  331.       exit();
  332.    } else {
  333.       rmdir(TEMPFOLDER . $mod_name);
  334.    }
  335. } else {
  336.    $junk = system(SCPCOMMAND . " -r \"" . TEMPFOLDER . $mod_name . "\" " . REMOTETARGET);
  337.    unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  338.    rmdir(TEMPFOLDER . $mod_name);
  339. }
  340. //
  341. // Insert the new mod version into the Solder database
  342. //
  343. $query = "INSERT INTO " . DBPREFIX . "modversions (id, mod_id, version, md5, created_at, updated_at, filesize) ";
  344. $query .= "VALUES(null, :mod_id, :version, :md5, NOW(), NOW(), :filesize)";
  345. $fields = array();
  346. $fields[':mod_id'] = $mod_id;
  347. $fields[':version'] = $mod_version;
  348. $fields[':md5'] = $solderzipmd5;
  349. $fields[':filesize'] = $solderzipsize;
  350. $sth = $globaldbh->prepare($query);
  351. $sth->execute($fields);
  352.  
  353. // All done!
  354. echo "New mod created. Have a nice day!\n";
  355. exit();
  356.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement