From 8d3b45790ebfb2a83075b2dcb5ab27c97fc7d3df Mon Sep 17 00:00:00 2001 From: ShadowChild Date: Thu, 20 Aug 2020 15:17:53 +0100 Subject: [PATCH] Delegate mod file removal to the mod installer --- build.gradle | 4 +- genericmodule/build.gradle | 6 +-- .../candor/generic/GenericModInstaller.java | 14 ++++- .../me/shadowchild/candor/CandorLauncher.java | 7 +++ .../candor/module/AbstractModInstaller.java | 2 + .../candor/module/ModuleSelector.java | 2 +- .../shadowchild/candor/window/ModScene.java | 53 ++++++++++--------- 7 files changed, 55 insertions(+), 33 deletions(-) diff --git a/build.gradle b/build.gradle index 9db6715..c2381f5 100644 --- a/build.gradle +++ b/build.gradle @@ -176,7 +176,7 @@ publishing { artifact javadocJar groupId 'me.shadowchild.candor' artifactId 'candor-full' - version '0.1.2' + version '0.1.3' } } } @@ -202,7 +202,7 @@ bintray { licenses = ['MIT'] version { - name = '0.1.2' + name = '0.1.3' desc = ' Initial Test Release For Candor' released = new Date() } diff --git a/genericmodule/build.gradle b/genericmodule/build.gradle index 3d88eab..0d01db9 100644 --- a/genericmodule/build.gradle +++ b/genericmodule/build.gradle @@ -38,7 +38,7 @@ dependencies { // implementation project(":core") - implementation 'me.shadowchild.candor:candor-full:0.1.2' + implementation 'me.shadowchild.candor:candor-full:0.1.3' implementation 'me.shadowchild.cybernize:cybernize:1.0.2' implementation 'com.electronwill.night-config:toml:3.6.3' @@ -92,7 +92,7 @@ publishing { artifact javadocJar groupId 'me.shadowchild.candor' artifactId 'candor-genericmodule' - version '0.1.2' + version '0.1.3' } } } @@ -118,7 +118,7 @@ bintray { licenses = ['MIT'] version { - name = '0.1.2' + name = '0.1.3' desc = ' Initial Test Release For Candor' released = new Date() } diff --git a/genericmodule/src/main/java/me/shadowchild/candor/generic/GenericModInstaller.java b/genericmodule/src/main/java/me/shadowchild/candor/generic/GenericModInstaller.java index ad2e4c2..c3fb1dc 100644 --- a/genericmodule/src/main/java/me/shadowchild/candor/generic/GenericModInstaller.java +++ b/genericmodule/src/main/java/me/shadowchild/candor/generic/GenericModInstaller.java @@ -1,9 +1,9 @@ package me.shadowchild.candor.generic; +import me.shadowchild.candor.Settings; import me.shadowchild.candor.mod.Mod; import me.shadowchild.candor.module.AbstractModInstaller; import me.shadowchild.candor.module.AbstractModule; -import me.shadowchild.candor.Settings; import me.shadowchild.cybernize.zip.ZipUtils; import org.apache.commons.io.FileUtils; @@ -58,4 +58,16 @@ public boolean install(Mod mod) { return false; } + + @Override + public boolean uninstall(Mod mod) { + + mod.getAssociatedFiles().forEach(element -> { + + File toDelete = new File(module.getModsFolder(), element.getAsString()); + System.out.println("Deleting: " + toDelete.getAbsolutePath()); + FileUtils.deleteQuietly(new File(module.getModsFolder(), element.getAsString())); + }); + return true; + } } diff --git a/src/main/java/me/shadowchild/candor/CandorLauncher.java b/src/main/java/me/shadowchild/candor/CandorLauncher.java index 49a5f4f..9669cad 100644 --- a/src/main/java/me/shadowchild/candor/CandorLauncher.java +++ b/src/main/java/me/shadowchild/candor/CandorLauncher.java @@ -8,6 +8,7 @@ import me.shadowchild.candor.window.GameSelectScene; import me.shadowchild.candor.window.ModScene; import me.shadowchild.cybernize.util.ClassLoadUtil; +import org.lwjgl.system.Platform; import javax.swing.*; import java.awt.*; @@ -19,6 +20,12 @@ public class CandorLauncher { public static void main(String[] args) { + if(Platform.get() == Platform.MACOSX) { + + // We want to restart the application here with the -XstartOnFirstThread JVM argument + // This is slightly more difficult for us as we have the java agent to deal with + } + SplashScreen splash = SplashScreen.getSplashScreen(); if(splash != null) { diff --git a/src/main/java/me/shadowchild/candor/module/AbstractModInstaller.java b/src/main/java/me/shadowchild/candor/module/AbstractModInstaller.java index 38a2b47..03e5f4f 100644 --- a/src/main/java/me/shadowchild/candor/module/AbstractModInstaller.java +++ b/src/main/java/me/shadowchild/candor/module/AbstractModInstaller.java @@ -14,4 +14,6 @@ public AbstractModInstaller(AbstractModule module) { public abstract boolean canInstall(Mod mod); public abstract boolean install(Mod mod); + + public abstract boolean uninstall(Mod mod); } diff --git a/src/main/java/me/shadowchild/candor/module/ModuleSelector.java b/src/main/java/me/shadowchild/candor/module/ModuleSelector.java index ea12897..8230f4d 100644 --- a/src/main/java/me/shadowchild/candor/module/ModuleSelector.java +++ b/src/main/java/me/shadowchild/candor/module/ModuleSelector.java @@ -99,7 +99,7 @@ public static AbstractModule getModuleForGame(File gameExe) { public static void checkGenericModule() throws IOException { File moduleJar = new File("./module/GenericModule.jar"); - String url = "https://dl.bintray.com/candor/candor-alpha/me/shadowchild/candor/candor-genericmodule/0.1.2/candor-genericmodule-0.1.2.jar"; + String url = "https://dl.bintray.com/candor/candor-alpha/me/shadowchild/candor/candor-genericmodule/0.1.3/candor-genericmodule-0.1.3.jar"; String fileName = Utils.getFileName(new URL(url)); if(!moduleJar.exists()) { diff --git a/src/main/java/me/shadowchild/candor/window/ModScene.java b/src/main/java/me/shadowchild/candor/window/ModScene.java index 39c5281..8a2ed88 100644 --- a/src/main/java/me/shadowchild/candor/window/ModScene.java +++ b/src/main/java/me/shadowchild/candor/window/ModScene.java @@ -236,43 +236,44 @@ private void removeModsSelected(ActionEvent e) { File modStore = new File("/config" + ModuleSelector.currentModule.getExeName() + "/mods"); Mod mod = (Mod)o; - mod.getAssociatedFiles().forEach(element -> { + // We let the module decide how to delete the files + if(ModuleSelector.currentModule.getModInstaller().uninstall(mod)) { - try { + // Once the module has deleted the files, remove the mod from the mods config + mod.getAssociatedFiles().forEach(element -> { - File toDelete = new File(modsFolder, element.getAsString()); - System.out.println("Deleting: " + toDelete.getAbsolutePath()); - FileUtils.deleteQuietly(new File(modsFolder, element.getAsString())); + try { - JsonObject contents = JsonUtil.getObjectFromUrl(installedModsConfig.toURI().toURL()); - JsonArray array = contents.get("mods").getAsJsonArray(); - JsonArray newArray = array.deepCopy(); + JsonObject contents = JsonUtil.getObjectFromUrl(installedModsConfig.toURI().toURL()); + JsonArray array = contents.get("mods").getAsJsonArray(); + JsonArray newArray = array.deepCopy(); - array.forEach(object -> { + array.forEach(object -> { - JsonObject obj = (JsonObject)object; - if(mod.getName().equals(obj.get("name").getAsString())) { + JsonObject obj = (JsonObject)object; + if(mod.getName().equals(obj.get("name").getAsString())) { - newArray.remove(obj); - } - }); + newArray.remove(obj); + } + }); - contents.remove("mods"); - contents.add("mods", newArray); + contents.remove("mods"); + contents.add("mods", newArray); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - FileWriter writer = new FileWriter(installedModsConfig); - gson.toJson(contents, writer); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + FileWriter writer = new FileWriter(installedModsConfig); + gson.toJson(contents, writer); - writer.close(); + writer.close(); - FileUtils.deleteQuietly(mod.getFile()); - removedMods.add(mod); - } catch (IOException exception) { + FileUtils.deleteQuietly(mod.getFile()); + } catch (IOException exception) { - exception.printStackTrace(); - } - }); + exception.printStackTrace(); + } + }); + removedMods.add(mod); + } }); removedMods.forEach(ModsHandler.MODS::remove);