Chore[profiles]: make nice functions for profile insertion and use them

This commit is contained in:
artdeell 2023-08-29 19:59:19 +03:00 committed by ArtDev
parent 41ea1cf33b
commit 67213c09b8
3 changed files with 25 additions and 19 deletions

View File

@ -31,7 +31,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
public class ProfileEditorFragment extends Fragment {
public static final String TAG = "ProfileEditorFragment";
@ -163,11 +162,7 @@ public class ProfileEditorFragment extends Fragment {
mProfileKey = profile;
}else{
minecraftProfile = MinecraftProfile.createTemplate();
String uuid = UUID.randomUUID().toString();
while(LauncherProfiles.mainProfileJson.profiles.containsKey(uuid)) {
uuid = UUID.randomUUID().toString();
}
mProfileKey = uuid;
mProfileKey = LauncherProfiles.getFreeProfileKey();
}
return minecraftProfile;
}

View File

@ -14,8 +14,6 @@ import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
public class FabricDownloadTask implements Runnable, Tools.DownloaderFeedback{
private final ModloaderDownloadListener mModloaderDownloadListener;
@ -61,10 +59,7 @@ public class FabricDownloadTask implements Runnable, Tools.DownloaderFeedback{
MinecraftProfile fabricProfile = new MinecraftProfile();
fabricProfile.lastVersionId = versionId;
fabricProfile.name = "Minecraft " + mGameVersion + " with Fabric " + mLoaderVersion;
Map<String, MinecraftProfile> profiles = LauncherProfiles.mainProfileJson.profiles;
String uuid = UUID.randomUUID().toString();
while (profiles.get(uuid) != null) uuid = UUID.randomUUID().toString();
profiles.put(uuid, fabricProfile);
LauncherProfiles.insertMinecraftProfile(fabricProfile);
LauncherProfiles.write();
}
return true;

View File

@ -11,6 +11,7 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class LauncherProfiles {
@ -59,6 +60,25 @@ public class LauncherProfiles {
return profile;
}
/**
* Insert a new profile into the profile map
* @param minecraftProfile the profile to insert
*/
public static void insertMinecraftProfile(MinecraftProfile minecraftProfile) {
mainProfileJson.profiles.put(getFreeProfileKey(), minecraftProfile);
}
/**
* Pick an unused normalized key to store a new profile with
* @return an unused key
*/
public static String getFreeProfileKey() {
Map<String, MinecraftProfile> profileMap = mainProfileJson.profiles;
String freeKey = UUID.randomUUID().toString();
while(profileMap.get(freeKey) != null) freeKey = UUID.randomUUID().toString();
return freeKey;
}
/**
* For all keys to be UUIDs, effectively isolating profile created by installers
* This avoids certain profiles to be erased by the installer
@ -79,13 +99,9 @@ public class LauncherProfiles {
}
// Swap the new keys
for(String profileKey: keys){
String uuid = UUID.randomUUID().toString();
while(launcherProfiles.profiles.containsKey(uuid)) {
uuid = UUID.randomUUID().toString();
}
launcherProfiles.profiles.put(uuid, launcherProfiles.profiles.get(profileKey));
for(String profileKey : keys){
MinecraftProfile currentProfile = launcherProfiles.profiles.get(profileKey);
insertMinecraftProfile(currentProfile);
launcherProfiles.profiles.remove(profileKey);
hasNormalized = true;
}