From 57521c19609cd0ce7bd24e75f07e0cf10e01a3fd Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Tue, 11 Apr 2023 23:11:02 +0200 Subject: [PATCH] Feat[profiles]: Avoid named profiles to be erased by installers --- .../launcherprofiles/LauncherProfiles.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/launcherprofiles/LauncherProfiles.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/launcherprofiles/LauncherProfiles.java index 0efd39842..0b8ffa39c 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/launcherprofiles/LauncherProfiles.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/launcherprofiles/LauncherProfiles.java @@ -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 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;