diff --git a/src/main/java/net/augustus/config/ConfigFormatV2.java b/src/main/java/net/augustus/config/ConfigFormatV2.java index fbec627..30d2b14 100644 --- a/src/main/java/net/augustus/config/ConfigFormatV2.java +++ b/src/main/java/net/augustus/config/ConfigFormatV2.java @@ -20,17 +20,16 @@ public class ConfigFormatV2 implements ModuleConfigCodec { @Override public String[] readHeaderInfo(JsonObject json) { - Set entries = json.get("Config_Header").getAsJsonObject().entrySet(); - ArrayList list = new ArrayList(); - - for (Object obj : entries) { - Entry entry = (Entry)obj; - if (!((String)entry.getKey()).equals("Config_Version")) { - list.add(((JsonElement)entry.getValue()).getAsString()); - } + if (json == null || !json.has("Config_Header") || !json.get("Config_Header").isJsonObject()) { + return null; } - - return (String[])list.toArray(new String[0]); + JsonObject header = json.getAsJsonObject("Config_Header"); + String name = header.has("Config_Name") ? header.get("Config_Name").getAsString() : ""; + String date = header.has("Config_Date") ? header.get("Config_Date").getAsString() : ""; + String time = header.has("Config_Time") ? header.get("Config_Time").getAsString() : ""; + String editable = header.has("Config_Editable") ? header.get("Config_Editable").getAsString() : "true"; + String author = header.has("Config_Author") ? header.get("Config_Author").getAsString() : ""; + return new String[]{name, date, time, editable, author}; } private static Boolean shouldLoadProperty(ConfigProperty configProperty) { diff --git a/src/main/java/net/augustus/config/FeatureListJsonCodec.java b/src/main/java/net/augustus/config/FeatureListJsonCodec.java index 6b6ca55..4eb7499 100644 --- a/src/main/java/net/augustus/config/FeatureListJsonCodec.java +++ b/src/main/java/net/augustus/config/FeatureListJsonCodec.java @@ -46,6 +46,7 @@ public class FeatureListJsonCodec implements FeatureConfigSerializer { return json; } catch (Exception e) { + e.printStackTrace(); return null; } } @@ -61,6 +62,7 @@ public class FeatureListJsonCodec implements FeatureConfigSerializer { return true; } catch (Exception e) { + e.printStackTrace(); return false; } } diff --git a/src/main/java/net/augustus/config/JsonFileStorageHandler.java b/src/main/java/net/augustus/config/JsonFileStorageHandler.java index 21cfd96..e90413c 100644 --- a/src/main/java/net/augustus/config/JsonFileStorageHandler.java +++ b/src/main/java/net/augustus/config/JsonFileStorageHandler.java @@ -7,8 +7,8 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; @@ -37,9 +37,13 @@ public class JsonFileStorageHandler { if (this.decoder != null) { elem = (JsonElement)gson.fromJson((String)this.decoder.apply(bytes), clazz); } else { - elem = (JsonElement)gson.fromJson(new String(bytes), clazz); + elem = (JsonElement)gson.fromJson(new String(bytes, StandardCharsets.UTF_8), clazz); } } catch (Exception e) { + if (file.exists()) { + System.err.println("Failed to read JSON file: " + file.getAbsolutePath()); + e.printStackTrace(); + } elem = supplier != null ? (JsonElement)supplier.get() : null; } @@ -91,10 +95,10 @@ public class JsonFileStorageHandler { byte[] bytes = (byte[])this.encoder.apply(str); Files.write(file.toPath(), bytes, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } else { - BufferedWriter writer = new BufferedWriter(new FileWriter(file)); - writer.write(str); - writer.flush(); - writer.close(); + try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) { + writer.write(str); + writer.flush(); + } } } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/net/augustus/config/ModuleConfigStorage.java b/src/main/java/net/augustus/config/ModuleConfigStorage.java index 116138e..c7fdca1 100644 --- a/src/main/java/net/augustus/config/ModuleConfigStorage.java +++ b/src/main/java/net/augustus/config/ModuleConfigStorage.java @@ -59,6 +59,8 @@ public class ModuleConfigStorage extends JsonConfigFile { byte[] decrypted = cipher.doFinal(bytes, ENCRYPTED_MAGIC_HEADER.length, bytes.length - ENCRYPTED_MAGIC_HEADER.length); return new String(decrypted, StandardCharsets.UTF_8); } catch (Exception e) { + System.err.println("Failed to decrypt AES-256-ECB config payload (" + bytes.length + " bytes):"); + e.printStackTrace(); return ""; } } @@ -125,22 +127,31 @@ public class ModuleConfigStorage extends JsonConfigFile { } Integer version = this.readConfigVersion(json); if (version == null) { + System.err.println("Config missing Config_Version in Config_Header: " + configName); + if (notify) { + this.notifyConfigStatus("Invalid config header: §c" + configName, NotificationLevel.ERROR); + } return; } ModuleConfigCodec codec = this.formatCodecs.get(version); - if (codec != null) { - try { - long startTime = System.currentTimeMillis(); - codec.applyConfig(json); - if (notify) { - long elapsed = System.currentTimeMillis() - startTime; - this.notifyConfigStatus("Config loaded: §c" + configName + " (" + elapsed + "ms)", NotificationLevel.INFO); - Augustus.getInstance().getModuleManager().sortModules(); - ClientSounds.playSound(ClientSounds.AUGUSTUS_TEXTURE2); - } - } catch (Exception e) { - e.printStackTrace(); + if (codec == null) { + System.err.println("Unsupported Config_Version " + version + " in config: " + configName); + if (notify) { + this.notifyConfigStatus("Unsupported config version (" + version + "): §c" + configName, NotificationLevel.ERROR); } + return; + } + try { + long startTime = System.currentTimeMillis(); + codec.applyConfig(json); + if (notify) { + long elapsed = System.currentTimeMillis() - startTime; + this.notifyConfigStatus("Config loaded: §c" + configName + " (" + elapsed + "ms)", NotificationLevel.INFO); + Augustus.getInstance().getModuleManager().sortModules(); + ClientSounds.playSound(ClientSounds.AUGUSTUS_TEXTURE2); + } + } catch (Exception e) { + e.printStackTrace(); } } diff --git a/src/main/java/net/augustus/ui/config/ConfigSystemPanel.java b/src/main/java/net/augustus/ui/config/ConfigSystemPanel.java index c0f4040..13e00c6 100644 --- a/src/main/java/net/augustus/ui/config/ConfigSystemPanel.java +++ b/src/main/java/net/augustus/ui/config/ConfigSystemPanel.java @@ -107,13 +107,14 @@ public class ConfigSystemPanel extends ClickGuiWindow { try { String configName = fileName.substring(0, fileName.length() - 5); String[] header = Augustus.getInstance().getConfigManager().moduleConfigStorage.getConfigHeaderInfo(configName, "configs"); - String date = header == null ? "" : header[1]; - String time = header == null ? "" : header[2]; + String date = header != null && header.length > 1 ? header[1] : ""; + String time = header != null && header.length > 2 ? header[2] : ""; String editableStr = header != null && header.length == 5 ? header[3] : "true"; - String author = header != null ? (header.length == 5 ? header[4] : header[3]) : ""; + String author = header != null && header.length >= 4 ? (header.length == 5 ? header[4] : header[3]) : ""; this.userConfigs.stream().filter(item -> item.getConfigName().equals(configName)).findAny().ifPresent(this.userConfigs::remove); this.userConfigs.add(new UserConfigListItem(configName, author, date, time, Boolean.parseBoolean(editableStr))); } catch (Exception e) { + e.printStackTrace(); if (mc.theWorld != null) { notifyConfigStatus("Failed to load config: " + file.getName(), NotificationLevel.ERROR); } diff --git a/src/main/java/net/augustus/utils/file/ClientFileUtil.java b/src/main/java/net/augustus/utils/file/ClientFileUtil.java index 46e72a5..79a57e8 100644 --- a/src/main/java/net/augustus/utils/file/ClientFileUtil.java +++ b/src/main/java/net/augustus/utils/file/ClientFileUtil.java @@ -7,11 +7,10 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.awt.Desktop; +import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -70,12 +69,10 @@ public class ClientFileUtil { public static void writeJsonArray(String str, String str2, JsonArray array, Gson gson) { ensureDirectory(str); - try { + try (BufferedWriter writer = Files.newBufferedWriter(new File(str, str2).toPath(), StandardCharsets.UTF_8)) { String str3 = gson.toJson(array); - BufferedWriter writer = new BufferedWriter(new FileWriter(new File(str, str2))); writer.write(str3); writer.flush(); - writer.close(); } catch (IOException e) { e.printStackTrace(); } @@ -91,11 +88,8 @@ public class ClientFileUtil { JsonArray array = null; for (int i = 0; i < 5; i++) { - FileReader fileReader = null; - - try { - fileReader = new FileReader(file); - array = (JsonArray)GSON.fromJson(fileReader, JsonArray.class); + try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { + array = (JsonArray)GSON.fromJson(reader, JsonArray.class); } catch (Exception e) { try { if (runnable != null) { @@ -109,13 +103,6 @@ public class ClientFileUtil { } } - if (fileReader != null) { - try { - fileReader.close(); - } catch (IOException e3) { - } - } - if (array != null || runnable == null) { break; } @@ -127,12 +114,10 @@ public class ClientFileUtil { public static void writeJsonObject(String str, String str2, JsonObject json, Gson gson) { ensureDirectory(str); - try { + try (BufferedWriter writer = Files.newBufferedWriter(new File(str, str2).toPath(), StandardCharsets.UTF_8)) { String str3 = gson.toJson(json); - BufferedWriter writer = new BufferedWriter(new FileWriter(new File(str, str2))); writer.write(str3); writer.flush(); - writer.close(); } catch (IOException e) { e.printStackTrace(); } @@ -236,11 +221,8 @@ public class ClientFileUtil { JsonObject json = null; for (int i = 0; i < 5; i++) { - FileReader fileReader = null; - - try { - fileReader = new FileReader(file); - json = (JsonObject)GSON.fromJson(fileReader, JsonObject.class); + try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { + json = (JsonObject)GSON.fromJson(reader, JsonObject.class); } catch (Exception e) { try { if (runnable != null) { @@ -254,13 +236,6 @@ public class ClientFileUtil { } } - if (fileReader != null) { - try { - fileReader.close(); - } catch (IOException e3) { - } - } - if (json != null || runnable == null) { break; } diff --git a/src/main/java/net/augustus/utils/file/DirectoryWatchService.java b/src/main/java/net/augustus/utils/file/DirectoryWatchService.java index 498e634..d460755 100644 --- a/src/main/java/net/augustus/utils/file/DirectoryWatchService.java +++ b/src/main/java/net/augustus/utils/file/DirectoryWatchService.java @@ -29,12 +29,12 @@ public class DirectoryWatchService { Path path3 = path.resolve((Path)watchEvent.context()); if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) { if (Files.isRegularFile(path3)) { - ClientFileUtil.waitForFileStable(path, Duration.ofMillis(300L), Duration.ofMillis(100L), Duration.ofSeconds(4L)); + ClientFileUtil.waitForFileStable(path3, Duration.ofMillis(300L), Duration.ofMillis(100L), Duration.ofSeconds(4L)); DirectoryWatcher.dispatchCreate(directoryWatcher, path3); } } else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY)) { if (Files.isRegularFile(path3)) { - ClientFileUtil.waitForFileStable(path, Duration.ofMillis(300L), Duration.ofMillis(100L), Duration.ofSeconds(4L)); + ClientFileUtil.waitForFileStable(path3, Duration.ofMillis(300L), Duration.ofMillis(100L), Duration.ofSeconds(4L)); DirectoryWatcher.dispatchModify(directoryWatcher, path3); } } else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE)) {