Skip to content

Commit

Permalink
Delegate mod file removal to the mod installer
Browse files Browse the repository at this point in the history
  • Loading branch information
zpiddock committed Aug 20, 2020
1 parent 37c977f commit 8d3b457
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 33 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ publishing {
artifact javadocJar
groupId 'me.shadowchild.candor'
artifactId 'candor-full'
version '0.1.2'
version '0.1.3'
}
}
}
Expand All @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions genericmodule/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -92,7 +92,7 @@ publishing {
artifact javadocJar
groupId 'me.shadowchild.candor'
artifactId 'candor-genericmodule'
version '0.1.2'
version '0.1.3'
}
}
}
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions src/main/java/me/shadowchild/candor/CandorLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Expand Down
53 changes: 27 additions & 26 deletions src/main/java/me/shadowchild/candor/window/ModScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8d3b457

Please sign in to comment.