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

Commit 2d308e9

Browse files
Better logging
1 parent 0de374c commit 2d308e9

7 files changed

Lines changed: 44 additions & 26 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
out
2-
.idea
2+
.idea
3+
lib

CurseForge-CLI.iml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10-
<orderEntry type="library" exported="" name="jansi-2.1.0" level="application" />
11-
<orderEntry type="library" exported="" name="json-simple-1.1.1" level="application" />
12-
<orderEntry type="library" exported="" name="commons-io-2.11.0" level="application" />
10+
<orderEntry type="library" name="commons-io-2.11.0-javadoc" level="application" />
11+
<orderEntry type="library" name="jansi-2.0.1" level="application" />
12+
<orderEntry type="library" name="json-simple-1.1.1" level="application" />
1313
</component>
1414
</module>

src/ml/northwestwind/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static Map<Integer, Map.Entry<Integer, String>> loadMods(String profile)
9898
String[] splitted1 = Arrays.stream(Arrays.copyOf(splitted, splitted.length - 1)).toArray(String[]::new);
9999
String id = Utils.getLast(Arrays.asList(splitted1));
100100
if (!Utils.isInteger(id)) continue;
101-
String name = Arrays.stream(Arrays.copyOf(splitted, splitted.length - 1)).collect(Collectors.joining());
101+
String name = String.join("_", Arrays.copyOf(splitted, splitted.length - 1));
102102
map.put(Integer.parseInt(id), new Map.Entry<Integer, String>() {
103103
@Override
104104
public Integer getKey() {

src/ml/northwestwind/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class Main {
1111
public static void main(String[] args) {
12-
if (!AnsiConsole.isInstalled()) AnsiConsole.systemInstall();
12+
AnsiConsole.systemInstall();
1313
Config.load();
1414
int index = Arrays.asList(args).indexOf("--args");
1515
if (index < 0) {
@@ -34,7 +34,7 @@ private static void printHelp() {
3434
" \\ \\_______\\ \\__\\ \\ \\_______\\ \\_______\\ \\__\\\n" +
3535
" \\|_______|\\|__| \\|_______|\\|_______|\\|__|"
3636
).reset());
37-
System.out.println(ansi().fg(GREEN.fgBright()).a("Made by NorthWestWind").reset());
37+
System.out.println(ansi().fg(GREEN).a("Made by NorthWestWind").reset());
3838
System.out.println();
3939
System.out.println("\"curseforge\", or \"./curseforge\" if you are running terminal in this directory.");
4040
System.out.println("\thelp: Display this message.");

src/ml/northwestwind/Modpack.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private static void downloadMods(String folder) {
7272
File manifest = new File(folder + File.separator + "manifest.json");
7373
JSONObject json = (JSONObject) parser.parse(new FileReader(manifest));
7474
JSONArray array = (JSONArray) json.get("files");
75-
int i = 0, suc = 0, fai = 0;
75+
int i = 0, suc = 0, fai = 0, lastlength = 0;
7676
for (Object o : array) {
7777
String name = "";
7878
try {
@@ -81,7 +81,7 @@ private static void downloadMods(String folder) {
8181
long file = (long) obj.get("fileID");
8282
JSONArray files = (JSONArray) Utils.readJsonFromUrl(Constants.CURSEFORGE_API + project + "/files");
8383
Optional f = files.stream().filter(o1 -> ((JSONObject) o1).get("id").equals(file)).findFirst();
84-
if (!f.isPresent()) throw new Exception("Cannot find required file");
84+
if (!f.isPresent()) throw new Exception("Cannot find required file of project " + project);
8585
JSONObject j = (JSONObject) f.get();
8686
name = (String) j.get("displayName");
8787
String downloaded = Utils.downloadFile((String) j.get("downloadUrl"), folder + File.separator + "mods", ((String) j.get("fileName")).replace(".jar", "_" + project + "_" + file + ".jar"));
@@ -91,8 +91,12 @@ private static void downloadMods(String folder) {
9191
fai++;
9292
e.printStackTrace();
9393
} finally {
94-
System.out.print(Ansi.ansi().fg(Ansi.Color.GREEN).a(String.format("[%d/%d] [S: %d | F: %d] Downloaded %s", ++i, array.size(), suc, fai, name)).reset()+"\r");
94+
StringBuilder formatted = new StringBuilder(String.format("[%d/%d] [S: %d | F: %d] Downloaded %s", ++i, array.size(), suc, fai, name));
95+
int ll = formatted.length() - lastlength;
96+
if (ll > 0) for (int ii = 0; ii < ll; ii++) formatted.append(" ");
97+
System.out.print(Ansi.ansi().fg(Ansi.Color.GREEN).a(formatted.toString()).reset()+"\r");
9598
System.out.flush();
99+
lastlength = formatted.length();
96100
}
97101
}
98102
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: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ private static void add(String[] ids) {
220220
JSONObject json = (JSONObject) Utils.readJsonFromUrl(Constants.CURSEFORGE_API + id);
221221
if (json == null || ((long) ((JSONObject) json.get("categorySection")).get("gameCategoryId")) != 6) throw new Exception("The ID "+id+" does not represent a mod.");
222222
JSONArray files = (JSONArray) Utils.readJsonFromUrl(Constants.CURSEFORGE_API + id + "/files");
223-
List f = (List) files.stream().filter(o -> {
224-
JSONArray versions = (JSONArray) ((JSONObject) o).get("gameVersion");
225-
return versions.get(0).equals(config.get("mcVer")) && ((String) versions.get(1)).equalsIgnoreCase((String) config.get("launcher"));
226-
}).collect(Collectors.toList());
223+
List f = (List) files.stream().filter(o -> Utils.checkVersion((JSONArray) ((JSONObject) o).get("gameVersion"), config)).collect(Collectors.toList());
227224
f.sort((a, b) -> (int) ((long) ((JSONObject) b).get("id") - (long) ((JSONObject) a).get("id")));
228225
if (f.size() < 1) throw new Exception("No available file of " + id + " found for this profile.");
229226
JSONObject bestFile = (JSONObject) f.get(0);
@@ -440,18 +437,16 @@ private static void update(String[] args) {
440437
JSONObject json = (JSONObject) Utils.readJsonFromUrl(Constants.CURSEFORGE_API + entry.getKey());
441438
if (json == null || ((long) ((JSONObject) json.get("categorySection")).get("gameCategoryId")) != 6) throw new Exception("The ID "+entry.getKey()+" does not represent a mod.");
442439
JSONArray files = (JSONArray) Utils.readJsonFromUrl(Constants.CURSEFORGE_API + entry.getKey() + "/files");
443-
List f = (List) files.stream().filter(o -> {
444-
JSONArray versions = (JSONArray) ((JSONObject) o).get("gameVersion");
445-
return versions.get(0).equals(config.get("mcVer")) && ((String) versions.get(1)).equalsIgnoreCase((String) config.get("launcher"));
446-
}).collect(Collectors.toList());
440+
List f = (List) files.stream().filter(o -> Utils.checkVersion((JSONArray) ((JSONObject) o).get("gameVersion"), config)).collect(Collectors.toList());
447441
f.sort((a, b) -> (int) ((long) ((JSONObject) b).get("id") - (long) ((JSONObject) a).get("id")));
448-
if (f.size() < 1) throw new Exception("No available file of " + entry.getKey() + " found for this profile.");
442+
if (f.size() < 1) continue;
449443
JSONObject fjson = (JSONObject) f.get(0);
450444
if (((long) fjson.get("id")) > entry.getValue().getKey()) {
451445
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a(entry.getValue().getValue()).reset().a(" | ").fg(Ansi.Color.MAGENTA).a(entry.getKey()));
452446
updatables.put(entry.getKey(), entry.getValue().getValue());
453447
}
454448
} catch (Exception e) {
449+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Having trouble with mod " + entry.getKey()));
455450
e.printStackTrace();
456451
}
457452
}
@@ -463,7 +458,7 @@ private static void update(String[] args) {
463458
String cmd = "curseforge profile add <profile> " + mods.keySet().stream().map(String::valueOf).collect(Collectors.joining(" "));
464459
pw.println(cmd);
465460
pw.close();
466-
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Exported mods with update available to search.txt"));
461+
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Exported mods with update available to mod_updates.txt"));
467462
} catch (Exception e) {
468463
e.printStackTrace();
469464
}
@@ -476,10 +471,7 @@ private static void update(String[] args) {
476471
JSONObject json = (JSONObject) Utils.readJsonFromUrl(Constants.CURSEFORGE_API + id);
477472
if (json == null || ((long) ((JSONObject) json.get("categorySection")).get("gameCategoryId")) != 6) throw new Exception("The ID "+id+" does not represent a mod.");
478473
JSONArray files = (JSONArray) Utils.readJsonFromUrl(Constants.CURSEFORGE_API + id + "/files");
479-
List f = (List) files.stream().filter(o -> {
480-
JSONArray versions = (JSONArray) ((JSONObject) o).get("gameVersion");
481-
return versions.get(0).equals(config.get("mcVer")) && ((String) versions.get(1)).equalsIgnoreCase((String) config.get("launcher"));
482-
}).collect(Collectors.toList());
474+
List f = (List) files.stream().filter(o -> Utils.checkVersion((JSONArray) ((JSONObject) o).get("gameVersion"), config)).collect(Collectors.toList());
483475
f.sort((a, b) -> (int) ((long) ((JSONObject) b).get("id") - (long) ((JSONObject) a).get("id")));
484476
if (f.size() < 1) throw new Exception("No available file of " + id + " found for this profile.");
485477
JSONObject bestFile = (JSONObject) f.get(0);

src/ml/northwestwind/Utils.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package ml.northwestwind;
22

3+
import org.apache.commons.io.Charsets;
34
import org.apache.commons.io.FileUtils;
45
import org.fusesource.jansi.Ansi;
6+
import org.json.simple.JSONArray;
7+
import org.json.simple.JSONObject;
58
import org.json.simple.parser.JSONParser;
69

710
import java.io.*;
811
import java.net.HttpURLConnection;
912
import java.net.URL;
13+
import java.net.URLEncoder;
1014
import java.nio.charset.StandardCharsets;
1115
import java.util.Arrays;
1216
import java.util.List;
@@ -43,7 +47,7 @@ public static String downloadFile(String fileURL, String saveDir) throws IOExcep
4347
}
4448

4549
public static String downloadFile(String fileURL, String saveDir, String name) throws IOException {
46-
URL url = new URL(fileURL);
50+
URL url = new URL(fileURL.replace(" ", "%20"));
4751
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
4852
int responseCode = httpConn.getResponseCode();
4953
String saveFilePath = null;
@@ -83,7 +87,7 @@ public static String downloadFile(String fileURL, String saveDir, String name) t
8387

8488
outputStream.close();
8589
inputStream.close();
86-
} else System.err.println(Ansi.ansi().fg(Ansi.Color.RED.fgBright()).a("No file to download. Server replied HTTP code: " + responseCode));
90+
} else System.err.println(Ansi.ansi().fg(Ansi.Color.RED).a("No file to download. Server replied HTTP code: " + responseCode));
8791
httpConn.disconnect();
8892
return saveFilePath;
8993
}
@@ -215,4 +219,21 @@ public V setValue(V value) {
215219
return null;
216220
}
217221
}
222+
223+
public static String getOppositeLauncher(String launcher) {
224+
if (launcher.equalsIgnoreCase("forge")) return "fabric";
225+
if (launcher.equalsIgnoreCase("fabric")) return "forge";
226+
return null;
227+
}
228+
229+
public static boolean checkVersion(JSONArray versions, JSONObject config) {
230+
String requiredVer = (String) config.get("mcVer");
231+
String oppoLauncher = getOppositeLauncher((String) config.get("launcher"));
232+
boolean launcherMatch = false, versionMatch = false;
233+
if (oppoLauncher == null || !versions.contains(oppoLauncher)) launcherMatch = true;
234+
if (!versions.contains(requiredVer)) {
235+
if (versions.stream().filter(ver -> isMCVersionValid((String) ver)).count() <= 0) versionMatch = true;
236+
} else versionMatch = true;
237+
return versionMatch && launcherMatch;
238+
}
218239
}

0 commit comments

Comments
 (0)