Skip to content

Commit d5fc862

Browse files
committed
Feat[agent]: modpack version ID fixer
1 parent 0e57979 commit d5fc862

File tree

6 files changed

+71
-11
lines changed

6 files changed

+71
-11
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1688133008591
1+
1692525087345

Diff for: app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/ForgeUtils.java

+6
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,10 @@ public static void addAutoInstallArgs(Intent intent, File modInstallerJar, boole
5959
" -jar "+modInstallerJar.getAbsolutePath());
6060
intent.putExtra("skipDetectMod", true);
6161
}
62+
public static void addAutoInstallArgs(Intent intent, File modInstallerJar, String modpackFixupId) {
63+
intent.putExtra("javaArgs", "-javaagent:"+ Tools.DIR_DATA+"/forge_installer/forge_installer.jar"
64+
+ "=\"" + modpackFixupId +"\"" +
65+
" -jar "+modInstallerJar.getAbsolutePath());
66+
intent.putExtra("skipDetectMod", true);
67+
}
6268
}

Diff for: app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModLoader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Intent getInstallationIntent(Context context, File modInstallerJar) {
7373
Intent baseIntent = new Intent(context, JavaGUILauncherActivity.class);
7474
switch (modLoaderType) {
7575
case MOD_LOADER_FORGE:
76-
ForgeUtils.addAutoInstallArgs(baseIntent, modInstallerJar, false);
76+
ForgeUtils.addAutoInstallArgs(baseIntent, modInstallerJar, getVersionId());
7777
return baseIntent;
7878
case MOD_LOADER_FABRIC:
7979
FabricUtils.addAutoInstallArgs(baseIntent, modInstallerJar, minecraftVersion, modLoaderVersion, false, false);

Diff for: forge_installer/src/main/java/git/artdeell/installer_agent/Agent.java

+24-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ public class Agent implements AWTEventListener {
2121
private boolean forgeWindowHandled = false;
2222
private final boolean suppressProfileCreation;
2323
private final boolean optiFineInstallation;
24+
private final String modpackFixupId;
2425
private final Timer componentTimer = new Timer();
2526

26-
public Agent(boolean nps, boolean of) {
27+
public Agent(boolean nps, boolean of, String mf) {
2728
this.suppressProfileCreation = !nps;
2829
this.optiFineInstallation = of;
30+
this.modpackFixupId = mf;
2931
}
3032

3133
@Override
@@ -104,7 +106,7 @@ public void handleDialog(Window window) {
104106
JOptionPane optionPane = (JOptionPane) components.get(0);
105107
if(optionPane.getMessageType() == JOptionPane.INFORMATION_MESSAGE) { // forge doesn't emit information messages for other reasons yet
106108
System.out.println("The install was successful!");
107-
ProfileFixer.reinsertProfile(optiFineInstallation ? "OptiFine" : "forge", suppressProfileCreation);
109+
ProfileFixer.reinsertProfile(optiFineInstallation ? "OptiFine" : "forge", modpackFixupId, suppressProfileCreation);
108110
System.exit(0); // again, forge doesn't call exit for some reason, so we do that ourselves here
109111
}
110112
}
@@ -124,13 +126,30 @@ public void insertAllComponents(List<Component> components, Container parent, Co
124126
public static void premain(String args, Instrumentation inst) {
125127
boolean noProfileSuppression = false;
126128
boolean optifine = false;
129+
String modpackFixupId = null;
127130
if(args != null ) {
128-
noProfileSuppression = args.contains("NPS"); // No Profile Suppression
129-
optifine = args.contains("OF"); // OptiFine
131+
modpackFixupId = findQuotedString(args);
132+
if(modpackFixupId != null) {
133+
noProfileSuppression = args.contains("NPS") && !modpackFixupId.contains("NPS");
134+
// No Profile Suppression
135+
optifine = args.contains("OF") && !modpackFixupId.contains("OF");
136+
// OptiFine
137+
}else {
138+
noProfileSuppression = args.contains("NPS"); // No Profile Suppression
139+
optifine = args.contains("OF"); // OptiFine
140+
}
130141
}
131-
Agent agent = new Agent(noProfileSuppression, optifine);
142+
Agent agent = new Agent(noProfileSuppression, optifine, modpackFixupId);
132143
Toolkit.getDefaultToolkit()
133144
.addAWTEventListener(agent,
134145
AWTEvent.WINDOW_EVENT_MASK);
135146
}
147+
148+
private static String findQuotedString(String args) {
149+
int quoteIndex = args.indexOf('"');
150+
if(quoteIndex == -1) return null;
151+
int nextQuoteIndex = args.indexOf('"', quoteIndex+1);
152+
if(nextQuoteIndex == -1) return null;
153+
return args.substring(quoteIndex+1, nextQuoteIndex);
154+
}
136155
}

Diff for: forge_installer/src/main/java/git/artdeell/installer_agent/ProfileFixer.java

+39-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.file.Paths;
1111
import java.nio.file.StandardOpenOption;
1212
import java.util.Random;
13+
import java.util.Set;
1314

1415
public class ProfileFixer {
1516
private static final Random random = new Random();
@@ -22,7 +23,8 @@ public static void storeProfile(String profileName) {
2223
StandardCharsets.UTF_8)
2324
);
2425
JSONObject profilesArray = minecraftProfiles.getJSONObject("profiles");
25-
oldProfile = profilesArray.optJSONObject(profileName, null);
26+
profileName = findProfileName(profileName, profilesArray);
27+
oldProfile = profileName != null ? minecraftProfiles.getJSONObject(profileName) : null;
2628
}catch (IOException | JSONException e) {
2729
System.out.println("Failed to store Forge profile: "+e);
2830
}
@@ -31,22 +33,24 @@ public static void storeProfile(String profileName) {
3133
private static String pickProfileName(String profileName) {
3234
return profileName+random.nextInt();
3335
}
34-
public static void reinsertProfile(String profileName, boolean suppressProfileCreation) {
36+
public static void reinsertProfile(String profileName, String modpackFixupId, boolean suppressProfileCreation) {
3537
try {
3638
JSONObject minecraftProfiles = new JSONObject(
3739
new String(Files.readAllBytes(profilesPath),
3840
StandardCharsets.UTF_8)
3941
);
4042
JSONObject profilesArray = minecraftProfiles.getJSONObject("profiles");
43+
profileName = findProfileName(profileName, profilesArray);
44+
if(modpackFixupId != null) fixupModpackProfile(profileName, modpackFixupId, profilesArray);
4145
if(oldProfile != null) {
42-
if(suppressProfileCreation) profilesArray.put("forge", oldProfile); // restore the old profile
46+
if(suppressProfileCreation) profilesArray.put(profileName, oldProfile); // restore the old profile
4347
else {
4448
String name = pickProfileName(profileName);
4549
while(profilesArray.has(name)) name = pickProfileName(profileName);
4650
profilesArray.put(name, oldProfile); // restore the old profile under a new name
4751
}
4852
}else{
49-
if(suppressProfileCreation) profilesArray.remove("forge"); // remove the new profile
53+
if(suppressProfileCreation) profilesArray.remove(profileName); // remove the new profile
5054
// otherwise it wont be removed
5155
}
5256
minecraftProfiles.put("profiles", profilesArray);
@@ -56,4 +60,35 @@ public static void reinsertProfile(String profileName, boolean suppressProfileCr
5660
System.out.println("Failed to restore old Forge profile: "+e);
5761
}
5862
}
63+
64+
private static void fixupModpackProfile(String profileId, String expectedVersionId, JSONObject profilesArray) {
65+
System.out.println("Fixing up modpack profile version ID...");
66+
JSONObject modloaderProfile = profilesArray.optJSONObject(profileId);
67+
if(modloaderProfile == null) {
68+
System.out.println("Failed to find the modloader profile, keys:" + profilesArray.keySet().toString());
69+
return;
70+
}
71+
String modloaderVersionId = modloaderProfile.optString("lastVersionId");
72+
if(modloaderVersionId == null) {
73+
System.out.println("Failed to find the modloader profile version, keys:" + modloaderProfile.keySet().toString());
74+
return;
75+
}
76+
System.out.println("Expected version ID: "+expectedVersionId+" Modloader version ID: "+modloaderVersionId);
77+
if(expectedVersionId.equals(modloaderVersionId)) return;
78+
for(String profileKey : profilesArray.keySet()) {
79+
if(profileKey.equals(profileId)) continue;
80+
JSONObject profile = profilesArray.getJSONObject(profileKey);
81+
if(!expectedVersionId.equals(profile.optString("lastVersionId"))) continue;
82+
profile.put("lastVersionId", modloaderVersionId);
83+
System.out.println("Replacing version ID in profile "+profileKey);
84+
}
85+
}
86+
87+
private static String findProfileName(String profileId, JSONObject profilesArray) {
88+
Set<String> profiles = profilesArray.keySet();
89+
for(String profile : profiles) {
90+
if(profile.equalsIgnoreCase(profileId)) return profile;
91+
}
92+
return null;
93+
}
5994
}

0 commit comments

Comments
 (0)