Skip to content
This repository was archived by the owner on Sep 9, 2023. It is now read-only.

Commit bbd12d1

Browse files
Allow direct installation
1 parent 0c434df commit bbd12d1

6 files changed

Lines changed: 76 additions & 43 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Written in Java. Designed for all platform and portable use.
2929
- `mod` Commands for mods.
3030
- `search <keywords>` Search for mods with keywords. Will also export results to file for easier adding to profile.
3131

32+
### Other Features
33+
If you put the JAR file together with a zip of the exported profile, CurseForge CLI will automatically install that profile to the current directory.
34+
This has 1 limitation, which is you should not put multiple zip files in the same directory.
35+
3236
### Note
3337
When you run the CLI, you should see a new file called `cf.json`. It is essentially the save file of the program. If you don't know what you are doing, do not touch it.
3438

src/ml/northwestwind/Config.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,32 @@ public static void printHelp(String prefix) {
3737

3838
private static void createDefaultConfig() {
3939
directory = new File("./curseforge-cli");
40-
if (!directory.exists()) directory.mkdir();
4140
save();
4241
}
4342

4443
public static void load() {
4544
File config = new File("./cf.json");
46-
if (!config.exists()) {
47-
createDefaultConfig();
48-
return;
49-
}
45+
if (!config.exists()) createDefaultConfig();
5046
JSONParser parser = new JSONParser();
5147
try {
5248
JSONObject json = (JSONObject) parser.parse(new FileReader(config));
5349
directory = new File((String) json.getOrDefault("directory", "./curseforge-cli"));
54-
if (!directory.exists() || !directory.isDirectory()) directory.mkdir();
5550
modpackDir = new File(directory.getPath() + File.separator + "modpack");
56-
if (!modpackDir.exists() || !modpackDir.isDirectory()) modpackDir.mkdir();
5751
profileDir = new File(directory.getPath() + File.separator + "profile");
58-
if (!profileDir.exists() || !profileDir.isDirectory()) profileDir.mkdir();
5952
exportDir = new File(directory.getPath() + File.separator + "exported");
60-
if (!exportDir.exists() || !exportDir.isDirectory()) exportDir.mkdir();
6153
tempDir = new File(directory.getPath() + File.separator + "tmp");
6254
} catch(Exception e) {
6355
e.printStackTrace();
6456
}
6557
}
6658

59+
public static void createDirs() {
60+
if (!directory.exists() || !directory.isDirectory()) directory.mkdir();
61+
if (!modpackDir.exists() || !modpackDir.isDirectory()) modpackDir.mkdir();
62+
if (!profileDir.exists() || !profileDir.isDirectory()) profileDir.mkdir();
63+
if (!exportDir.exists() || !exportDir.isDirectory()) exportDir.mkdir();
64+
}
65+
6766
public static Map<Integer, String> loadModpacks() {
6867
File[] files = modpackDir.listFiles();
6968
Map<Integer, String> map = new HashMap<>();

src/ml/northwestwind/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public class Constants {
44
public static final String CURSEFORGE_API = "https://addons-ecs.forgesvc.net/api/v2/addon/";
5-
public static final String VERSION = "1.0.9";
5+
public static final String VERSION = "1.1.0";
66
}

src/ml/northwestwind/Main.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ public static void main(String[] args) {
1515
Config.load();
1616
int index = Arrays.asList(args).indexOf("--args");
1717
if (index < 0) {
18+
if (Profile.findAndImport()) return;
1819
printHelp();
1920
return;
2021
}
2122
args = Arrays.stream(args).skip(index + 1).toArray(String[]::new);
23+
if (args.length < 1 && Profile.findAndImport()) return;
24+
Config.createDirs();
2225
if (args.length < 1 || args[0].equalsIgnoreCase("help")) printHelp();
2326
else if (args[0].equalsIgnoreCase("mod")) Mod.run(args);
2427
else if (args[0].equalsIgnoreCase("modpack")) Modpack.run(args);
2528
else if (args[0].equalsIgnoreCase("profile")) Profile.run(args);
2629
else if (args[0].equalsIgnoreCase("config")) Config.run(args);
27-
System.out.println(ansi().reset().a("Job done!"));
30+
System.out.println(ansi().reset());
2831
AnsiConsole.systemUninstall();
2932
}
3033

@@ -46,5 +49,7 @@ private static void printHelp() {
4649
Modpack.printHelp("\t");
4750
Profile.printHelp("\t");
4851
Mod.printHelp("\t");
52+
System.out.println();
53+
System.out.println("You can also put this in the same directory with a CurseForge export to install it.");
4954
}
5055
}

src/ml/northwestwind/Modpack.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void downloadMods(String folder) {
7676
File manifest = new File(folder + File.separator + "manifest.json");
7777
JSONObject json = (JSONObject) parser.parse(new FileReader(manifest));
7878
JSONArray array = (JSONArray) json.get("files");
79-
int i = 0, suc = 0, fai = 0, lastlength = 0;
79+
int i = 0, suc = 0, fai = 0;
8080
for (Object o : array) {
8181
String name = "";
8282
try {
@@ -95,12 +95,7 @@ public static void downloadMods(String folder) {
9595
fai++;
9696
e.printStackTrace();
9797
} finally {
98-
StringBuilder formatted = new StringBuilder(String.format("[%d/%d] [S: %d | F: %d] Downloaded %s", ++i, array.size(), suc, fai, name));
99-
int ll = formatted.length() - lastlength;
100-
if (ll > 0) for (int ii = 0; ii < ll; ii++) formatted.append(" ");
101-
System.out.print(Ansi.ansi().fg(Ansi.Color.GREEN).a(formatted.toString()).reset().a("\r"));
102-
System.out.flush();
103-
lastlength = formatted.length();
98+
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a(String.format("[%d/%d] [S: %d | F: %d] Downloaded %s", ++i, array.size(), suc, fai, name)));
10499
}
105100
}
106101
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a(String.format("Iterated through %d projects. %d success, %d failed", i, suc, fai)));

src/ml/northwestwind/Profile.java

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,18 @@ private static void update(String[] args) {
493493
}
494494

495495
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-
}
496+
importProf(paths, false, false);
497+
}
498+
499+
private static boolean importProf(String[] paths, boolean silent, boolean toCD) {
500+
if (toCD && paths.length > 1) return false;
501+
boolean imported = false;
501502
for (String path : paths) {
503+
if (!Config.tempDir.exists() || !Config.tempDir.isDirectory()) Config.tempDir.mkdir();
504+
else {
505+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Another instance is importing a modpack. Please wait until it is finished."));
506+
return imported;
507+
}
502508
File zipped = new File(path);
503509
try {
504510
if (!zipped.exists()) throw new Exception("The file " + path + " does not exist");
@@ -512,31 +518,55 @@ private static void importProf(String[] paths) {
512518
Modpack.downloadMods(Config.tempDir.getPath());
513519
JSONObject json = (JSONObject) parser.parse(new FileReader(manifest));
514520
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());
521+
if (toCD) {
522+
File current = new File(".");
523+
for (File file : Config.tempDir.listFiles()) {
524+
if (file.getName().equals(zipped.getName())) continue;
525+
if (file.isFile()) FileUtils.moveFileToDirectory(file, current, true);
526+
else FileUtils.moveDirectoryToDirectory(file, current, true);
527+
}
528+
FileUtils.deleteDirectory(Config.tempDir);
529+
} else {
530+
FileUtils.moveDirectoryToDirectory(Config.tempDir, Config.profileDir, true);
531+
if (!Config.profileDir.exists() || !Config.profileDir.isDirectory()) Config.profileDir.mkdir();
532+
File packFolder = new File(Config.profileDir.getPath() + File.separator + "tmp");
533+
File newFolder = new File(Config.profileDir.getPath() + File.separator + slug);
534+
if (!packFolder.renameTo(newFolder)) {
535+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Failed to rename modpack, but we are continuing anyway."));
536+
newFolder = packFolder;
537+
}
538+
JSONObject profile = new JSONObject();
539+
JSONObject minecraft = (JSONObject) json.get("minecraft");
540+
String[] launcher = ((String) ((JSONObject) ((JSONArray) minecraft.get("modLoaders")).get(0)).get("id")).split("-", 2);
541+
profile.put("name", json.get("name"));
542+
profile.put("mcVer", minecraft.get("version"));
543+
profile.put("launcher", launcher[0]);
544+
profile.put("modVer", launcher[1]);
545+
PrintWriter pw = new PrintWriter(newFolder.getPath() + File.separator + "profile.json");
546+
pw.write(profile.toJSONString());
531547

532-
pw.flush();
533-
pw.close();
534-
manifest.delete();
548+
pw.flush();
549+
pw.close();
550+
manifest.delete();
551+
}
535552
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Converted modpack " + slug + " into profile."));
553+
imported = true;
536554
} catch (Exception e) {
537-
e.printStackTrace();
555+
if (!silent) e.printStackTrace();
538556
}
557+
try {
558+
if (Config.tempDir.exists() && Config.tempDir.isDirectory()) FileUtils.deleteDirectory(Config.tempDir);
559+
} catch (Exception ignored) { }
539560
}
561+
return imported;
562+
}
563+
564+
public static boolean findAndImport() {
565+
try {
566+
File currentDir = new File(".");
567+
return importProf(currentDir.list((dir, name) -> name.endsWith(".zip")), true, true);
568+
} catch (Exception ignored) { }
569+
return false;
540570
}
541571

542572
public static void printHelp(String prefix) {

0 commit comments

Comments
 (0)