@@ -30,6 +30,7 @@ public static void run(String[] args) {
3030 else if (cmd .equalsIgnoreCase ("export" )) export (args );
3131 else if (cmd .equalsIgnoreCase ("edit" )) edit (args [0 ]);
3232 else if (cmd .equalsIgnoreCase ("delete" )) delete (args );
33+ else if (cmd .equals ("import" )) importProf (args );
3334 else Utils .invalid ();
3435 }
3536
@@ -491,6 +492,53 @@ private static void update(String[] args) {
491492 }
492493 }
493494
495+ private static void importProf (String [] paths ) {
496+ if (!Config .tempDir .exists () || !Config .tempDir .isDirectory ()) Config .tempDir .mkdir ();
497+ else {
498+ System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("Another instance is importing a modpack. Please wait until it is finished." ));
499+ return ;
500+ }
501+ for (String path : paths ) {
502+ File zipped = new File (path );
503+ try {
504+ if (!zipped .exists ()) throw new Exception ("The file " + path + " does not exist" );
505+ if (zipped .isFile ()) {
506+ FileUtils .copyFileToDirectory (zipped , Config .tempDir );
507+ if (!Utils .unzip (Config .tempDir .getPath () + File .separator + zipped .getName ())) throw new Exception ("Failed to extract modpack content of " +path );
508+ } else FileUtils .copyDirectoryToDirectory (zipped , Config .tempDir );
509+ File manifest = new File (Config .tempDir .getPath () + File .separator + "manifest.json" );
510+ if (!manifest .exists () || !manifest .isFile ()) throw new Exception ("Modpack is missing manifest. Cannot convert to profile." );
511+ Modpack .copyFromOverride (Config .tempDir .getPath (), (String ) ((JSONObject ) parser .parse (new FileReader (manifest ))).get ("overrides" ));
512+ Modpack .downloadMods (Config .tempDir .getPath ());
513+ JSONObject json = (JSONObject ) parser .parse (new FileReader (manifest ));
514+ String slug = ((String ) json .get ("name" )).toLowerCase ().replaceAll ("[^a-z0-9]" , "-" );
515+ FileUtils .moveDirectoryToDirectory (Config .tempDir , Config .profileDir , true );
516+ File packFolder = new File (Config .profileDir .getPath () + File .separator + "tmp" );
517+ File newFolder = new File (Config .profileDir .getPath () + File .separator + slug );
518+ if (!packFolder .renameTo (newFolder )) {
519+ System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("Failed to rename modpack, but we are continuing anyway." ));
520+ newFolder = packFolder ;
521+ }
522+ JSONObject profile = new JSONObject ();
523+ JSONObject minecraft = (JSONObject ) json .get ("minecraft" );
524+ String [] launcher = ((String ) ((JSONObject ) ((JSONArray ) minecraft .get ("modLoaders" )).get (0 )).get ("id" )).split ("-" , 2 );
525+ profile .put ("name" , json .get ("name" ));
526+ profile .put ("mcVer" , minecraft .get ("version" ));
527+ profile .put ("launcher" , launcher [0 ]);
528+ profile .put ("modVer" , launcher [1 ]);
529+ PrintWriter pw = new PrintWriter (newFolder .getPath () + File .separator + "profile.json" );
530+ pw .write (profile .toJSONString ());
531+
532+ pw .flush ();
533+ pw .close ();
534+ manifest .delete ();
535+ System .out .println (Ansi .ansi ().fg (Ansi .Color .GREEN ).a ("Converted modpack " + slug + " into profile." ));
536+ } catch (Exception e ) {
537+ e .printStackTrace ();
538+ }
539+ }
540+ }
541+
494542 public static void printHelp (String prefix ) {
495543 System .out .println (prefix + "profile: Commands for custom profiles." );
496544 System .out .println (prefix + "\t create: Create a profile." );
@@ -508,5 +556,7 @@ public static void printHelp(String prefix) {
508556 System .out .println (prefix + "\t update: Update mods in the profile." );
509557 System .out .println (prefix + "\t \t arg <profile>: Name of the profile to target." );
510558 System .out .println (prefix + "\t \t arg [ID]: The ID of mods. Omit to check for updates." );
559+ System .out .println (prefix + "\t import: Import a downloaded modpack." );
560+ System .out .println (prefix + "\t \t arg <path>: Path to the zip or directory." );
511561 }
512562}
0 commit comments