10
10
import java .nio .file .Paths ;
11
11
import java .nio .file .StandardOpenOption ;
12
12
import java .util .Random ;
13
+ import java .util .Set ;
13
14
14
15
public class ProfileFixer {
15
16
private static final Random random = new Random ();
@@ -22,7 +23,8 @@ public static void storeProfile(String profileName) {
22
23
StandardCharsets .UTF_8 )
23
24
);
24
25
JSONObject profilesArray = minecraftProfiles .getJSONObject ("profiles" );
25
- oldProfile = profilesArray .optJSONObject (profileName , null );
26
+ profileName = findProfileName (profileName , profilesArray );
27
+ oldProfile = profileName != null ? minecraftProfiles .getJSONObject (profileName ) : null ;
26
28
}catch (IOException | JSONException e ) {
27
29
System .out .println ("Failed to store Forge profile: " +e );
28
30
}
@@ -31,22 +33,24 @@ public static void storeProfile(String profileName) {
31
33
private static String pickProfileName (String profileName ) {
32
34
return profileName +random .nextInt ();
33
35
}
34
- public static void reinsertProfile (String profileName , boolean suppressProfileCreation ) {
36
+ public static void reinsertProfile (String profileName , String modpackFixupId , boolean suppressProfileCreation ) {
35
37
try {
36
38
JSONObject minecraftProfiles = new JSONObject (
37
39
new String (Files .readAllBytes (profilesPath ),
38
40
StandardCharsets .UTF_8 )
39
41
);
40
42
JSONObject profilesArray = minecraftProfiles .getJSONObject ("profiles" );
43
+ profileName = findProfileName (profileName , profilesArray );
44
+ if (modpackFixupId != null ) fixupModpackProfile (profileName , modpackFixupId , profilesArray );
41
45
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
43
47
else {
44
48
String name = pickProfileName (profileName );
45
49
while (profilesArray .has (name )) name = pickProfileName (profileName );
46
50
profilesArray .put (name , oldProfile ); // restore the old profile under a new name
47
51
}
48
52
}else {
49
- if (suppressProfileCreation ) profilesArray .remove ("forge" ); // remove the new profile
53
+ if (suppressProfileCreation ) profilesArray .remove (profileName ); // remove the new profile
50
54
// otherwise it wont be removed
51
55
}
52
56
minecraftProfiles .put ("profiles" , profilesArray );
@@ -56,4 +60,35 @@ public static void reinsertProfile(String profileName, boolean suppressProfileCr
56
60
System .out .println ("Failed to restore old Forge profile: " +e );
57
61
}
58
62
}
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
+ }
59
94
}
0 commit comments