Skip to content

Commit

Permalink
Implement recursive folder deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
ACGaming committed Jul 12, 2023
1 parent ca6a69d commit 1e5643e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group 'net.jan'
version '1.7.0-SNAPSHOT'
version '1.8.0-SNAPSHOT'
1 change: 1 addition & 0 deletions mod-director-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Path> paths = Files.walk(configurationDirectory)) {
Expand All @@ -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));
Expand All @@ -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!"));
Expand All @@ -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")) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
Expand All @@ -208,7 +217,7 @@ private Class<? extends ModDirectorRemoteMod> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down

0 comments on commit 1e5643e

Please sign in to comment.