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

Commit 236e7a1

Browse files
Manifest Finding & Better Prof Gen
1 parent 6afcf27 commit 236e7a1

3 files changed

Lines changed: 53 additions & 10 deletions

File tree

src/ml/northwestwind/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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.2.1";
5+
public static final String VERSION = "1.2.2";
66
private static final String OS = System.getProperty("os.name").toLowerCase();
77
public static final boolean IS_WINDOWS = (OS.contains("win"));
88
public static final boolean IS_MAC = (OS.contains("mac"));

src/ml/northwestwind/Modpack.java

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.time.ZoneId;
1717
import java.util.*;
1818
import java.util.concurrent.atomic.AtomicInteger;
19+
import java.util.stream.Collectors;
1920

2021
public class Modpack {
2122
private static final JSONParser parser = new JSONParser();
@@ -49,17 +50,19 @@ private static void install(String[] ids) {
4950
File packFolder = new File(Config.modpackDir.getPath() + File.separator +slug+"_"+id);
5051
if (packFolder.exists()) throw new Exception("Found old folder of "+name+". Installation of "+name+" cancelled.");
5152
packFolder.mkdir();
52-
String downloadUrl = ((String) ((JSONObject) Utils.getLast((JSONArray) json.get("latestFiles"))).get("downloadUrl")).replaceFirst("edge", "media");
53+
JSONObject latest = (JSONObject) Utils.getLast((JSONArray) json.get("latestFiles"));
54+
String downloadUrl = ((String) latest.get("downloadUrl")).replaceFirst("edge", "media");
5355
String loc = Utils.downloadFile(downloadUrl, packFolder.getPath());
5456
if (loc == null) throw new Exception("Failed to download modpack " + name);
5557
boolean success = Utils.unzip(loc);
5658
if (!success) throw new Exception("Failed to extract modpack content of "+name);
57-
File manifest = new File(packFolder + File.separator + "manifest.json");
59+
File manifest = new File(packFolder + File.separator + getManifestFile(latest));
5860
if (!manifest.exists()) throw new Exception("Cannot find modpack manifest of "+name);
59-
copyFromOverride(packFolder.getPath(), (String) ((JSONObject) parser.parse(new FileReader(manifest))).get("overrides"));
61+
JSONObject manifestJson = (JSONObject) parser.parse(new FileReader(manifest));
62+
copyFromOverride(packFolder.getPath(), (String) manifestJson.get("overrides"));
6063
downloadMods(packFolder.getPath());
6164
String thumb = downloadThumb(json);
62-
genProfile(name, packFolder.getAbsolutePath(), thumb);
65+
genProfile(name, packFolder.getAbsolutePath(), thumb, getModVersion(manifestJson));
6366
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Finished download of " + name).reset());
6467
} catch (Exception e) {
6568
e.printStackTrace();
@@ -110,6 +113,39 @@ public static void downloadMods(String folder) {
110113
}
111114
}
112115

116+
// Pass in latestFiles' last element
117+
private static String getManifestFile(JSONObject latest) {
118+
if (latest.containsKey("modules")) {
119+
JSONArray modules = (JSONArray) latest.get("modules");
120+
Optional optional = modules.stream().filter(obj -> ((int) ((JSONObject) obj).getOrDefault("type", 0)) == 3).findFirst();
121+
if (optional.isPresent()) return (String) ((JSONObject) optional.get()).get("foldername");
122+
}
123+
return "manifest.json";
124+
}
125+
126+
// Get mod loader version from manifest
127+
private static String getModVersion(JSONObject manifest) {
128+
if (manifest.containsKey("minecraft")) {
129+
JSONObject minecraft = (JSONObject) manifest.get("minecraft");
130+
if (minecraft.containsKey("version") && minecraft.containsKey("modLoaders")) {
131+
JSONArray modLoaders = (JSONArray) minecraft.get("modLoaders");
132+
Optional optional = modLoaders.stream().filter(obj -> (boolean) ((JSONObject) obj).getOrDefault("primary", false)).findFirst();
133+
if (optional.isPresent()) {
134+
String version = (String) minecraft.get("version");
135+
String id = (String) ((JSONObject) optional.get()).get("id");
136+
String[] splitted = id.split("-");
137+
String loader = splitted[0];
138+
String modVer = Arrays.stream(splitted).skip(1).collect(Collectors.joining("-"));
139+
if (loader.equalsIgnoreCase("forge")) id = version + "-forge-" + modVer;
140+
else if (loader.equalsIgnoreCase("fabric")) id = "fabric-loader-" + modVer + "-" + version;
141+
else id = null;
142+
return id;
143+
}
144+
}
145+
}
146+
return null;
147+
}
148+
113149
private static String downloadThumb(JSONObject json) {
114150
if (!(json.get("attachments") instanceof JSONArray) || !(((JSONArray) json.get("attachments")).get(0) instanceof JSONObject)) return null;
115151
JSONObject attachment = (JSONObject) ((JSONArray) json.get("attachments")).get(0);
@@ -121,7 +157,7 @@ private static String downloadThumb(JSONObject json) {
121157
return null;
122158
}
123159

124-
private static void genProfile(String name, String path, String icon) {
160+
private static void genProfile(String name, String path, String icon, String loader) {
125161
String base64 = null;
126162
if (icon != null) {
127163
try {
@@ -143,7 +179,7 @@ private static void genProfile(String name, String path, String icon) {
143179
int memory = (int) Math.ceil(Runtime.getRuntime().maxMemory() / 1024.0 / 1024.0 / 1024.0);
144180
profile.put("javaArgs", String.format("-Xmx%dG -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M", memory));
145181
profile.put("lastUsed", LocalDateTime.of(LocalDate.MIN, LocalTime.MIN).atZone(ZoneId.of("UTC")).toString());
146-
profile.put("lastVersionId", "latest-release");
182+
profile.put("lastVersionId", loader == null ? "latest-release" : loader);
147183
profile.put("name", name);
148184
profile.put("type", "custom");
149185
profiles.put(UUID.randomUUID(), profile);
@@ -154,9 +190,12 @@ private static void genProfile(String name, String path, String icon) {
154190

155191
pw.flush();
156192
pw.close();
157-
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a("Created profile for " + name + ". However, the mod loader is not configured correctly. Please open/restart your Minecraft Launcher to edit it. Installation of mod loader might be needed, and can be downloaded in the following:").reset());
158-
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Forge: ").fg(Ansi.Color.CYAN).a("https://files.minecraftforge.net/").reset());
159-
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Fabric: ").fg(Ansi.Color.CYAN).a("https://fabricmc.net/use/installer/").reset());
193+
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a("Created profile for " + name + ".").reset());
194+
if (loader == null) {
195+
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a(" However, the mod loader is not configured correctly. Please open/restart your Minecraft Launcher to edit it. Installation of mod loader might be needed, and can be downloaded in the following:").reset());
196+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Forge: ").fg(Ansi.Color.CYAN).a("https://files.minecraftforge.net/").reset());
197+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Fabric: ").fg(Ansi.Color.CYAN).a("https://fabricmc.net/use/installer/").reset());
198+
}
160199
} catch (Exception ignored) { }
161200
}
162201

update.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"1.2.2": {
3+
"title": "Manifest Finding & Better Profile Generation",
4+
"description": "Profiles will now be generated with the responsible mod loader (but that does not mean it is installed). Some modpack does not hold their information in manifest.json, so we use the API to find the required file."
5+
},
26
"1.2.1": {
37
"title": "Added acceptParent option",
48
"description": "acceptParent allows user to install mods supporting only the parent version of a profile. Refer to the README in the repository for more details."

0 commit comments

Comments
 (0)