Feat[agent]: modpack version ID fixer

This commit is contained in:
artdeell
2023-08-20 14:16:49 +03:00
committed by ArtDev
parent 0e5797981b
commit d5fc862f0e
6 changed files with 71 additions and 11 deletions

View File

@@ -1 +1 @@
1688133008591
1692525087345

View File

@@ -59,4 +59,10 @@ public class ForgeUtils {
" -jar "+modInstallerJar.getAbsolutePath());
intent.putExtra("skipDetectMod", true);
}
public static void addAutoInstallArgs(Intent intent, File modInstallerJar, String modpackFixupId) {
intent.putExtra("javaArgs", "-javaagent:"+ Tools.DIR_DATA+"/forge_installer/forge_installer.jar"
+ "=\"" + modpackFixupId +"\"" +
" -jar "+modInstallerJar.getAbsolutePath());
intent.putExtra("skipDetectMod", true);
}
}

View File

@@ -73,7 +73,7 @@ public class ModLoader {
Intent baseIntent = new Intent(context, JavaGUILauncherActivity.class);
switch (modLoaderType) {
case MOD_LOADER_FORGE:
ForgeUtils.addAutoInstallArgs(baseIntent, modInstallerJar, false);
ForgeUtils.addAutoInstallArgs(baseIntent, modInstallerJar, getVersionId());
return baseIntent;
case MOD_LOADER_FABRIC:
FabricUtils.addAutoInstallArgs(baseIntent, modInstallerJar, minecraftVersion, modLoaderVersion, false, false);

View File

@@ -21,11 +21,13 @@ public class Agent implements AWTEventListener {
private boolean forgeWindowHandled = false;
private final boolean suppressProfileCreation;
private final boolean optiFineInstallation;
private final String modpackFixupId;
private final Timer componentTimer = new Timer();
public Agent(boolean nps, boolean of) {
public Agent(boolean nps, boolean of, String mf) {
this.suppressProfileCreation = !nps;
this.optiFineInstallation = of;
this.modpackFixupId = mf;
}
@Override
@@ -104,7 +106,7 @@ public class Agent implements AWTEventListener {
JOptionPane optionPane = (JOptionPane) components.get(0);
if(optionPane.getMessageType() == JOptionPane.INFORMATION_MESSAGE) { // forge doesn't emit information messages for other reasons yet
System.out.println("The install was successful!");
ProfileFixer.reinsertProfile(optiFineInstallation ? "OptiFine" : "forge", suppressProfileCreation);
ProfileFixer.reinsertProfile(optiFineInstallation ? "OptiFine" : "forge", modpackFixupId, suppressProfileCreation);
System.exit(0); // again, forge doesn't call exit for some reason, so we do that ourselves here
}
}
@@ -124,13 +126,30 @@ public class Agent implements AWTEventListener {
public static void premain(String args, Instrumentation inst) {
boolean noProfileSuppression = false;
boolean optifine = false;
String modpackFixupId = null;
if(args != null ) {
noProfileSuppression = args.contains("NPS"); // No Profile Suppression
optifine = args.contains("OF"); // OptiFine
modpackFixupId = findQuotedString(args);
if(modpackFixupId != null) {
noProfileSuppression = args.contains("NPS") && !modpackFixupId.contains("NPS");
// No Profile Suppression
optifine = args.contains("OF") && !modpackFixupId.contains("OF");
// OptiFine
}else {
noProfileSuppression = args.contains("NPS"); // No Profile Suppression
optifine = args.contains("OF"); // OptiFine
}
}
Agent agent = new Agent(noProfileSuppression, optifine);
Agent agent = new Agent(noProfileSuppression, optifine, modpackFixupId);
Toolkit.getDefaultToolkit()
.addAWTEventListener(agent,
AWTEvent.WINDOW_EVENT_MASK);
}
private static String findQuotedString(String args) {
int quoteIndex = args.indexOf('"');
if(quoteIndex == -1) return null;
int nextQuoteIndex = args.indexOf('"', quoteIndex+1);
if(nextQuoteIndex == -1) return null;
return args.substring(quoteIndex+1, nextQuoteIndex);
}
}

View File

@@ -10,6 +10,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Random;
import java.util.Set;
public class ProfileFixer {
private static final Random random = new Random();
@@ -22,7 +23,8 @@ public class ProfileFixer {
StandardCharsets.UTF_8)
);
JSONObject profilesArray = minecraftProfiles.getJSONObject("profiles");
oldProfile = profilesArray.optJSONObject(profileName, null);
profileName = findProfileName(profileName, profilesArray);
oldProfile = profileName != null ? minecraftProfiles.getJSONObject(profileName) : null;
}catch (IOException | JSONException e) {
System.out.println("Failed to store Forge profile: "+e);
}
@@ -31,22 +33,24 @@ public class ProfileFixer {
private static String pickProfileName(String profileName) {
return profileName+random.nextInt();
}
public static void reinsertProfile(String profileName, boolean suppressProfileCreation) {
public static void reinsertProfile(String profileName, String modpackFixupId, boolean suppressProfileCreation) {
try {
JSONObject minecraftProfiles = new JSONObject(
new String(Files.readAllBytes(profilesPath),
StandardCharsets.UTF_8)
);
JSONObject profilesArray = minecraftProfiles.getJSONObject("profiles");
profileName = findProfileName(profileName, profilesArray);
if(modpackFixupId != null) fixupModpackProfile(profileName, modpackFixupId, profilesArray);
if(oldProfile != null) {
if(suppressProfileCreation) profilesArray.put("forge", oldProfile); // restore the old profile
if(suppressProfileCreation) profilesArray.put(profileName, oldProfile); // restore the old profile
else {
String name = pickProfileName(profileName);
while(profilesArray.has(name)) name = pickProfileName(profileName);
profilesArray.put(name, oldProfile); // restore the old profile under a new name
}
}else{
if(suppressProfileCreation) profilesArray.remove("forge"); // remove the new profile
if(suppressProfileCreation) profilesArray.remove(profileName); // remove the new profile
// otherwise it wont be removed
}
minecraftProfiles.put("profiles", profilesArray);
@@ -56,4 +60,35 @@ public class ProfileFixer {
System.out.println("Failed to restore old Forge profile: "+e);
}
}
private static void fixupModpackProfile(String profileId, String expectedVersionId, JSONObject profilesArray) {
System.out.println("Fixing up modpack profile version ID...");
JSONObject modloaderProfile = profilesArray.optJSONObject(profileId);
if(modloaderProfile == null) {
System.out.println("Failed to find the modloader profile, keys:" + profilesArray.keySet().toString());
return;
}
String modloaderVersionId = modloaderProfile.optString("lastVersionId");
if(modloaderVersionId == null) {
System.out.println("Failed to find the modloader profile version, keys:" + modloaderProfile.keySet().toString());
return;
}
System.out.println("Expected version ID: "+expectedVersionId+" Modloader version ID: "+modloaderVersionId);
if(expectedVersionId.equals(modloaderVersionId)) return;
for(String profileKey : profilesArray.keySet()) {
if(profileKey.equals(profileId)) continue;
JSONObject profile = profilesArray.getJSONObject(profileKey);
if(!expectedVersionId.equals(profile.optString("lastVersionId"))) continue;
profile.put("lastVersionId", modloaderVersionId);
System.out.println("Replacing version ID in profile "+profileKey);
}
}
private static String findProfileName(String profileId, JSONObject profilesArray) {
Set<String> profiles = profilesArray.keySet();
for(String profile : profiles) {
if(profile.equalsIgnoreCase(profileId)) return profile;
}
return null;
}
}