Feat[profiles]: Avoid named profiles to be erased by installers

This commit is contained in:
Mathias-Boulay 2023-04-11 23:11:02 +02:00
parent 6c65334406
commit 57521c1960

View File

@ -1,8 +1,12 @@
package net.kdt.pojavlaunch.value.launcherprofiles;
import android.util.Log;
import com.google.gson.*;
import net.kdt.pojavlaunch.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
public class LauncherProfiles {
public static MinecraftLauncherProfiles mainProfileJson;
@ -13,6 +17,9 @@ public class LauncherProfiles {
if (launcherProfilesFile.exists()) {
mainProfileJson = Tools.GLOBAL_GSON.fromJson(Tools.read(launcherProfilesFile.getAbsolutePath()), MinecraftLauncherProfiles.class);
if(mainProfileJson.profiles == null) mainProfileJson.profiles = new HashMap<>();
else if(LauncherProfiles.normalizeProfileIds(mainProfileJson)){
LauncherProfiles.update();
}
} else {
mainProfileJson = new MinecraftLauncherProfiles();
mainProfileJson.profiles = new HashMap<>();
@ -27,6 +34,40 @@ public class LauncherProfiles {
throw new RuntimeException(th);
}
}
/**
* For all keys to be UUIDs, effectively isolating profile created by installers
* This avoids certain profiles to be erased by the installer
* @return Whether some profiles have been normalized
*/
private static boolean normalizeProfileIds(MinecraftLauncherProfiles launcherProfiles){
boolean hasNormalized = false;
ArrayList<String> keys = new ArrayList<>();
// Detect denormalized keys
for(String profileKey : launcherProfiles.profiles.keySet()){
try{
UUID.fromString(profileKey);
}catch (IllegalArgumentException exception){
keys.add(profileKey);
Log.w(LauncherProfiles.class.toString(), "Illegal profile uuid: " + profileKey);
}
}
// 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));
launcherProfiles.profiles.remove(profileKey);
hasNormalized = true;
}
return hasNormalized;
}
/*
public static String insert;