From 1e5643efaef209f8ebbc9fe8e693104466668acd Mon Sep 17 00:00:00 2001 From: ACGaming <4818419+ACGaming@users.noreply.github.com> Date: Wed, 12 Jul 2023 12:03:16 +0200 Subject: [PATCH] Implement recursive folder deletion #5 --- build.gradle | 2 +- mod-director-core/build.gradle | 1 + .../ConfigurationController.java | 47 +++++++++++-------- .../configuration/type/CurseRemoteMod.java | 3 +- .../core/configuration/type/DisableMod.java | 2 +- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/build.gradle b/build.gradle index dc715cb..37ab49f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,2 +1,2 @@ group 'net.jan' -version '1.7.0-SNAPSHOT' +version '1.8.0-SNAPSHOT' diff --git a/mod-director-core/build.gradle b/mod-director-core/build.gradle index 9201b42..7657cb0 100644 --- a/mod-director-core/build.gradle +++ b/mod-director-core/build.gradle @@ -16,4 +16,5 @@ dependencies { implementation group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.13.5" implementation group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.13.5" implementation 'com.google.code.gson:gson:2.10.1' + implementation 'commons-io:commons-io:2.13.0' } diff --git a/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/ConfigurationController.java b/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/ConfigurationController.java index 4a975d0..e2b046d 100644 --- a/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/ConfigurationController.java +++ b/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/ConfigurationController.java @@ -28,9 +28,11 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.apache.commons.io.FileUtils; public class ConfigurationController { public static final ObjectMapper OBJECT_MAPPER = createObjectMapper(); + private static final String LOG_DOMAIN = "ModDirector/ConfigurationController"; private static ObjectMapper createObjectMapper() { ObjectMapper instance = new ObjectMapper(); @@ -53,10 +55,8 @@ public ConfigurationController(ModDirector director, Path configurationDirectory public void load() { Path modpackConfigPath = configurationDirectory.resolve("modpack.json"); - if(Files.exists(modpackConfigPath)) { - if(!loadModpackConfiguration(modpackConfigPath)) { + if(Files.exists(modpackConfigPath) && !loadModpackConfiguration(modpackConfigPath)) { return; - } } try(Stream paths = Files.walk(configurationDirectory)) { @@ -67,7 +67,7 @@ public void load() { .sorted() .forEach(this::addConfig); } catch(IOException e) { - director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, "ModDirector/ConfigurationController", + director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, LOG_DOMAIN, "CORE", e, "Failed to iterate configuration directory!"); director.addError(new ModDirectorError(ModDirectorSeverityLevel.ERROR, "Failed to iterate configuration directory", e)); @@ -79,7 +79,7 @@ private boolean loadModpackConfiguration(Path configurationPath) { modpackConfiguration = OBJECT_MAPPER.readValue(stream, ModpackConfiguration.class); return true; } catch(IOException e) { - director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, "ModDirector/ConfigurationController", + director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, LOG_DOMAIN, "CORE", e, "Failed to read modpack configuration!"); director.addError(new ModDirectorError(ModDirectorSeverityLevel.ERROR, "Failed to read modpack configuration!")); @@ -90,7 +90,7 @@ private boolean loadModpackConfiguration(Path configurationPath) { private void addConfig(Path configurationPath) { String configString = configurationPath.toString(); - director.getLogger().log(ModDirectorSeverityLevel.INFO, "ModDirector/ConfigurationController", + director.getLogger().log(ModDirectorSeverityLevel.INFO, LOG_DOMAIN, "CORE", "Loading config %s", configString); if(configString.endsWith(".remote.json")) { @@ -125,7 +125,7 @@ private void handleRemoteConfig(Path configurationPath) { private void handleBundleConfig(Path configurationPath) { try(InputStream stream = Files.newInputStream(configurationPath); BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { - JsonObject jsonObject = new JsonParser().parse(reader).getAsJsonObject(); + JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject(); JsonArray jsonArray = jsonObject.getAsJsonArray("curse"); if(jsonArray != null) { @@ -177,16 +177,25 @@ private void handleDisableConfig(Path configurationPath) { private void handleDisableConfig(DisableMod disableMod) { try { Path installationRoot = director.getPlatform().installationRoot().toAbsolutePath().normalize(); - Path disableModPath = installationRoot.resolve(disableMod.getFolder()).resolve(disableMod.getFileName()); - if(Files.isRegularFile(disableModPath)) { - if(disableMod.shouldDelete()) { - director.getLogger().log(ModDirectorSeverityLevel.INFO, "ModDirector/ConfigurationController", - "CORE", "Deleting %s", disableModPath); - Files.delete(disableModPath); - } else { - director.getLogger().log(ModDirectorSeverityLevel.INFO, "ModDirector/ConfigurationController", - "CORE", "Disabling %s", disableModPath); - Files.move(disableModPath, disableModPath.resolveSibling(disableModPath.getFileName() + ".disabled-by-mod-director")); + Path disableModFolderPath = installationRoot.resolve(disableMod.getFolder()); + if(disableMod.getFileName() == null) { + if(Files.isDirectory(disableModFolderPath) && disableMod.shouldDelete()) { + director.getLogger().log(ModDirectorSeverityLevel.INFO, LOG_DOMAIN, + "CORE", "Deleting folder %s", disableModFolderPath); + FileUtils.deleteDirectory(disableModFolderPath.toFile()); + } + } else { + Path disableModFilePath = disableModFolderPath.resolve(disableMod.getFileName()); + if(Files.isRegularFile(disableModFilePath)) { + if(disableMod.shouldDelete()) { + director.getLogger().log(ModDirectorSeverityLevel.INFO, LOG_DOMAIN, + "CORE", "Deleting file %s", disableModFilePath); + Files.delete(disableModFilePath); + } else { + director.getLogger().log(ModDirectorSeverityLevel.INFO, LOG_DOMAIN, + "CORE", "Disabling file %s", disableModFilePath); + Files.move(disableModFilePath, disableModFilePath.resolveSibling(disableModFilePath.getFileName() + ".disabled-by-mod-director")); + } } } } catch(IOException e) { @@ -195,7 +204,7 @@ private void handleDisableConfig(DisableMod disableMod) { } private void handleConfigException(IOException e) { - director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, "ModDirector/ConfigurationController", + director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, LOG_DOMAIN, "CORE", e, "Failed to " + (e instanceof JsonParseException ? "parse" : "open") + " a configuration for reading!"); director.addError(new ModDirectorError(ModDirectorSeverityLevel.ERROR, "Failed to " + (e instanceof JsonParseException ? "parse" : "open") + " a configuration for reading", e)); @@ -208,7 +217,7 @@ private Class getTypeForFile(Path file) { } else if(name.endsWith(".url.json")) { return UrlRemoteMod.class; } else { - director.getLogger().log(ModDirectorSeverityLevel.WARN, "ModDirector/ConfigurationController", + director.getLogger().log(ModDirectorSeverityLevel.WARN, LOG_DOMAIN, "CORE", "Ignoring unknown json file %s", name); return null; } diff --git a/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/CurseRemoteMod.java b/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/CurseRemoteMod.java index aaf7813..1aefb66 100644 --- a/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/CurseRemoteMod.java +++ b/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/CurseRemoteMod.java @@ -3,7 +3,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -80,7 +79,7 @@ public RemoteModInformation queryInformation() throws ModDirectorException { WebGetResponse response = WebClient.get(apiUrl); JsonObject jsonObject; try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getInputStream(), StandardCharsets.UTF_8))) { - jsonObject = new JsonParser().parse(reader).getAsJsonObject().getAsJsonObject("data"); + jsonObject = JsonParser.parseReader(reader).getAsJsonObject().getAsJsonObject("data"); } information = ConfigurationController.OBJECT_MAPPER.readValue(jsonObject.toString(), CurseAddonFileInformation.class); } catch(MalformedURLException e) { diff --git a/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/DisableMod.java b/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/DisableMod.java index 5e0311a..fb0b09e 100644 --- a/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/DisableMod.java +++ b/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/DisableMod.java @@ -10,7 +10,7 @@ public class DisableMod { @JsonCreator public DisableMod( - @JsonProperty(value = "fileName", required = true) String fileName, + @JsonProperty(value = "fileName") String fileName, @JsonProperty(value = "folder", required = true) String folder, @JsonProperty(value = "delete") boolean delete ) {