From 7b5ae4dff6508c2a38de663cc5838cf90d263d7f Mon Sep 17 00:00:00 2001 From: The Judge Date: Thu, 11 Apr 2024 20:22:22 -0700 Subject: [PATCH] Add deletion, fix mod dir and default mods --- src/main/java/pojlib/api/API_V1.java | 10 ++++--- .../java/pojlib/instance/InstanceHandler.java | 7 +++-- .../pojlib/instance/MinecraftInstances.java | 26 ++++++++++++------- src/main/java/pojlib/util/JREUtils.java | 4 +-- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/main/java/pojlib/api/API_V1.java b/src/main/java/pojlib/api/API_V1.java index a63289ad..819e5927 100644 --- a/src/main/java/pojlib/api/API_V1.java +++ b/src/main/java/pojlib/api/API_V1.java @@ -88,6 +88,10 @@ public static MinecraftInstances.Instance load(MinecraftInstances instances, Str return instances.load(name); } + public static boolean deleteInstance(MinecraftInstances instances, MinecraftInstances.Instance instance, String gameDir) { + return InstanceHandler.delete(instances, instance, gameDir); + } + /** * Creates a new game instance with a selected mod loader. The latest version of the mod loader will be installed * @@ -99,15 +103,15 @@ public static MinecraftInstances.Instance load(MinecraftInstances instances, Str * @return A minecraft instance object * @throws IOException Throws if download of library or asset fails */ - public static MinecraftInstances.Instance createNewInstance(Activity activity, String instanceName, String home, + public static MinecraftInstances.Instance createNewInstance(Activity activity, String instanceName, String home, boolean useDefaultMods, MinecraftMeta.MinecraftVersion minecraftVersion, InstanceHandler.ModLoader modLoader, String modsFolderName) throws IOException { if(ignoreInstanceName) { - return InstanceHandler.create(activity, instanceName, home, minecraftVersion, modLoader, modsFolderName); + return InstanceHandler.create(activity, instanceName, home, useDefaultMods, minecraftVersion, modLoader, modsFolderName); } else if (instanceName.contains("/") || instanceName.contains("!")) { throw new IOException("You cannot use special characters (!, /, ., etc) when creating instances."); } else { - return InstanceHandler.create(activity, instanceName, home, minecraftVersion, modLoader, modsFolderName); + return InstanceHandler.create(activity, instanceName, home, useDefaultMods, minecraftVersion, modLoader, modsFolderName); } } diff --git a/src/main/java/pojlib/instance/InstanceHandler.java b/src/main/java/pojlib/instance/InstanceHandler.java index c4da6249..68c6911e 100644 --- a/src/main/java/pojlib/instance/InstanceHandler.java +++ b/src/main/java/pojlib/instance/InstanceHandler.java @@ -44,13 +44,15 @@ public enum ModLoader { } //creates a new instance of a minecraft version, install game + mod loader, stores non login related launch info to json - public static MinecraftInstances.Instance create(Activity activity, String instanceName, String gameDir, MinecraftMeta.MinecraftVersion minecraftVersion, ModLoader modLoader, String modsFolderName) { + public static MinecraftInstances.Instance create(Activity activity, String instanceName, String gameDir, boolean useDefaultMods, + MinecraftMeta.MinecraftVersion minecraftVersion, ModLoader modLoader, String modsFolderName) { Logger.getInstance().appendToLog("Creating new instance: " + instanceName); MinecraftInstances.Instance instance = new MinecraftInstances.Instance(); instance.instanceName = instanceName; instance.versionName = minecraftVersion.id; instance.gameDir = new File(gameDir).getAbsolutePath(); + instance.defaultMods = useDefaultMods; if(modsFolderName != null) { instance.modsDirName = modsFolderName; } else { @@ -173,8 +175,9 @@ public static void launchInstance(Activity activity, MinecraftAccount account, M try { JREUtils.redirectAndPrintJRELog(); VLoader.setAndroidInitInfo(activity); + instance.updateMods(Constants.MC_DIR); while(!API_V1.finishedDownloading); - JREUtils.launchJavaVM(activity, instance.generateLaunchArgs(account), instance.versionName); + JREUtils.launchJavaVM(activity, instance.generateLaunchArgs(account), instance.modsDirName); } catch (Throwable e) { e.printStackTrace(); } diff --git a/src/main/java/pojlib/instance/MinecraftInstances.java b/src/main/java/pojlib/instance/MinecraftInstances.java index 191de124..95572396 100644 --- a/src/main/java/pojlib/instance/MinecraftInstances.java +++ b/src/main/java/pojlib/instance/MinecraftInstances.java @@ -37,6 +37,7 @@ public static class Instance { public String assetsDir; public String mainClass; public ArrayList mods; + public boolean defaultMods; public List generateLaunchArgs(MinecraftAccount account) { String[] mcArgs = {"--username", account.username, "--version", versionName, "--gameDir", gameDir, @@ -61,6 +62,7 @@ private ModsJson parseModsJson(String gameDir) throws Exception { } public void updateMods(String gameDir) { + API_V1.finishedDownloading = false; try { ModsJson modsJson = parseModsJson(gameDir); @@ -88,21 +90,25 @@ public void updateMods(String gameDir) { } } - for(ModInfo info : version.defaultMods) { - for(ModInfo currInfo : mods) { - if(!currInfo.name.equals(info.name)) { - continue; + if(defaultMods) { + for (ModInfo info : version.defaultMods) { + for (ModInfo currInfo : mods) { + if (!currInfo.name.equals(info.name)) { + continue; + } + if (currInfo.version.equals(info.version)) { + continue; + } + + File mod = new File(gameDir + "/mods/" + modsDirName + "/" + info.name); + DownloadUtils.downloadFile(info.url, mod); } - if(currInfo.version.equals(info.version)) { - continue; - } - - File mod = new File(gameDir + "/mods/" + modsDirName + "/" + info.name); - DownloadUtils.downloadFile(info.url, mod); } } + API_V1.finishedDownloading = true; } catch (Exception e) { Logger.getInstance().appendToLog("Mods failed to download! Are you offline?\n" + e.getMessage()); + API_V1.finishedDownloading = true; } } } diff --git a/src/main/java/pojlib/util/JREUtils.java b/src/main/java/pojlib/util/JREUtils.java index ee620464..ca40a348 100644 --- a/src/main/java/pojlib/util/JREUtils.java +++ b/src/main/java/pojlib/util/JREUtils.java @@ -171,7 +171,7 @@ public static void setJavaEnvironment(Activity activity) throws Throwable { setLdLibraryPath(jvmLibraryPath+":"+LD_LIBRARY_PATH); } - public static int launchJavaVM(final Activity activity, final List JVMArgs, String versionName) throws Throwable { + public static int launchJavaVM(final Activity activity, final List JVMArgs, String modsDirName) throws Throwable { JREUtils.relocateLibPath(activity); setJavaEnvironment(activity); @@ -196,7 +196,7 @@ public static int launchJavaVM(final Activity activity, final List JVMAr userArgs.add("-Dorg.lwjgl.opengl.libname=" + graphicsLib); userArgs.add("-Dorg.lwjgl.opengles.libname=" + "/system/lib64/libGLESv3.so"); userArgs.add("-Dorg.lwjgl.egl.libname=" + "/system/lib64/libEGL_dri.so"); - userArgs.add("-Dfabric.addMods=" + Constants.MC_DIR + "/mods/" + versionName); + userArgs.add("-Dfabric.addMods=" + Constants.MC_DIR + "/mods/" + modsDirName); userArgs.addAll(JVMArgs); System.out.println(JVMArgs);