From 443b03120b09e916d21b2d2f3819b9a877dc9280 Mon Sep 17 00:00:00 2001 From: Cory Borek Date: Sat, 9 Nov 2024 19:54:44 -0500 Subject: [PATCH 1/2] Rework Debug Logs, Check for some invalid json objects, fix squid.png Debug Logs now do not check for the global variable, only their own internal debug session. Tabs are not manually added anymore. Check for null and invalid json objects. Squid.png only moved if it didn't exist. Now fixed. --- .../com/agentdid127/resourcepack/Options.java | 2 +- .../com/agentdid127/resourcepack/GUI.java | 8 +-- .../backwards/BackwardsPackConverter.java | 15 ++-- .../backwards/impl/BlockStateConverter.java | 3 +- .../backwards/impl/LangConverter.java | 4 +- .../backwards/impl/MCPatcherConverter.java | 3 +- .../backwards/impl/ModelConverter.java | 15 ++-- .../backwards/impl/NameConverter.java | 34 ++++----- .../impl/textures/MapIconConverter.java | 2 +- .../forwards/ForwardsPackConverter.java | 15 ++-- .../forwards/impl/AnimationConverter.java | 30 ++++---- .../forwards/impl/BlockStateConverter.java | 70 ++++++++++--------- .../forwards/impl/LangConverter.java | 4 +- .../forwards/impl/MCPatcherConverter.java | 3 +- .../forwards/impl/ModelConverter.java | 34 +++++---- .../forwards/impl/NameConverter.java | 34 ++++----- .../forwards/impl/SoundsConverter.java | 3 +- .../forwards/impl/SpacesConverter.java | 6 +- .../impl/textures/MapIconConverter.java | 2 +- .../textures/ParticleTextureConverter.java | 2 +- .../impl/textures/slicing/Slicer.java | 4 +- .../resourcepack/library/PackConverter.java | 1 - .../resourcepack/library/pack/Pack.java | 6 +- .../resourcepack/library/pack/ZipPack.java | 8 +-- .../library/utilities/FileUtil.java | 6 +- .../library/utilities/ImageConverter.java | 4 +- .../library/utilities/Logger.java | 42 ++++++++++- 27 files changed, 193 insertions(+), 167 deletions(-) diff --git a/Applications/Console/src/main/java/com/agentdid127/resourcepack/Options.java b/Applications/Console/src/main/java/com/agentdid127/resourcepack/Options.java index 3362bfff..15f81b4f 100644 --- a/Applications/Console/src/main/java/com/agentdid127/resourcepack/Options.java +++ b/Applications/Console/src/main/java/com/agentdid127/resourcepack/Options.java @@ -16,7 +16,7 @@ public class Options { public static final OptionSpec INPUT_DIR = PARSER.acceptsAll(Arrays.asList("i", "input", "input-dir"), "Input directory for the packs").withRequiredArg().withValuesConvertedBy(new PathConverter()).defaultsTo(Paths.get("./")); - public static final OptionSpec DEBUG = PARSER.accepts("debug", "Displays other output").withRequiredArg().ofType(Boolean.class).defaultsTo(true); + public static final OptionSpec DEBUG = PARSER.accepts("debug", "Displays other output").withRequiredArg().ofType(Boolean.class).defaultsTo(false); public static final OptionSpec TO = PARSER.accepts("to", "Updates to version").withRequiredArg().ofType(String.class).defaultsTo("1.13"); diff --git a/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.java b/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.java index b8cf1a77..684fd9d7 100644 --- a/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.java +++ b/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.java @@ -50,11 +50,11 @@ public GUI() { convertButton.setVisible(false); try { if (Util.getVersionProtocol(gson, from) > Util.getVersionProtocol(gson, to)) - new BackwardsPackConverter(from, to, light, minify, Paths.get("./"), true, out).runDir(); + new BackwardsPackConverter(from, to, light, minify, Paths.get("./"), false, out).runDir(); else - new ForwardsPackConverter(from, to, light, minify, Paths.get("./"), true, out).runDir(); - } catch (IOException ioException) { - out.println(Arrays.toString(ioException.getStackTrace())); + new ForwardsPackConverter(from, to, light, minify, Paths.get("./"), false, out).runDir(); + } catch (Exception exception) { + out.println(Arrays.toString(exception.getStackTrace())); } convertButton.setVisible(true); return; diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/BackwardsPackConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/BackwardsPackConverter.java index 565909fe..67abc86e 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/BackwardsPackConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/BackwardsPackConverter.java @@ -24,10 +24,9 @@ public BackwardsPackConverter(String from, String to, String light, boolean mini if (!minify) gsonBuilder.setPrettyPrinting(); gson = gsonBuilder.create(); - DEBUG = debug; + Logger.setDebug(debug); Logger.setStream(out); - Logger.log(from); - Logger.log(to); + Logger.log("Converting packs from: " + from + " to " + to); this.INPUT_DIR = input; converterRunner(from, to, light); } @@ -88,15 +87,19 @@ private void converterRunner(String from, String to, String light) { public void runPack(Pack pack) { try { Logger.log("Converting " + pack); + Logger.addTab(); pack.getHandler().setup(); - Logger.log(" Running Converters"); + Logger.log("Running Converters"); for (Converter converter : converters.values()) { - if (DEBUG) - Logger.log(" Running " + converter.getClass().getSimpleName()); + Logger.addTab(); + Logger.log("Running " + converter.getClass().getSimpleName()); converter.convert(pack); + Logger.subTab(); } pack.getHandler().finish(); + Logger.subTab(); } catch (Throwable t) { + Logger.resetTab(); Logger.log("Failed to convert!"); Util.propagate(t); } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/BlockStateConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/BlockStateConverter.java index 02b5f407..e39d45f3 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/BlockStateConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/BlockStateConverter.java @@ -89,8 +89,7 @@ public void convert(Pack pack) throws IOException { } if (anyChanges) { JsonUtil.writeJson(packConverter.getGson(), file, json); - if (PackConverter.DEBUG) - Logger.log(" Converted " + file.getFileName()); + Logger.debug("Converted " + file.getFileName()); } } catch (IOException e) { Util.propagate(e); diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/LangConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/LangConverter.java index 88427e5a..f86fb5ee 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/LangConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/LangConverter.java @@ -86,7 +86,7 @@ public void convert(Pack pack) throws IOException { try { int modelNoJson = model.getFileName().toString().indexOf(".json"); String file2 = model.getFileName().toString().substring(0, modelNoJson); - Logger.log("Saving: " + file2 + ".lang"); + Logger.debug("Saving: " + file2 + ".lang"); out.store( new FileOutputStream( pack.getWorkingPath() @@ -103,7 +103,7 @@ public void convert(Pack pack) throws IOException { for (int i = 0; i < models.size(); i++) { Path langFilePath = pack.getWorkingPath() .resolve(("assets/minecraft/lang/" + models.get(i)).replace("/", File.separator)); - Logger.log("Deleting: " + langFilePath); + Logger.debug("Deleting: " + langFilePath); Files.delete(langFilePath); } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/MCPatcherConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/MCPatcherConverter.java index a9af3ca2..53aecfcd 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/MCPatcherConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/MCPatcherConverter.java @@ -64,8 +64,7 @@ protected void remapProperties(Path path) throws IOException { .filter(path1 -> path1.toString().endsWith(".properties")) .forEach(model -> { try (InputStream input = new FileInputStream(model.toString())) { - if (PackConverter.DEBUG) - Logger.log("Updating:" + model.getFileName()); + Logger.debug("Updating:" + model.getFileName()); PropertiesEx prop = new PropertiesEx(); prop.load(input); diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ModelConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ModelConverter.java index 9465902b..732f1d3e 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ModelConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ModelConverter.java @@ -80,13 +80,13 @@ protected void remapModelJson(Path models) throws IOException { && JsonUtil.readJson(packConverter.getGson(), model).isJsonObject()) jsonObject = JsonUtil.readJson(packConverter.getGson(), model); else { - if (PackConverter.DEBUG) { - Logger.log("Could not convert model: " + model.getFileName()); + Logger.debug("Could not convert model: " + model.getFileName()); + Logger.addTab(); if (JsonUtil.readJson(packConverter.getGson(), model) == null) - Logger.log("Check for Syntax Errors in file."); + Logger.debug("Check for Syntax Errors in file."); else - Logger.log("File is not JSON Object."); - } + Logger.debug("File is not JSON Object."); + Logger.subTab(); return; } @@ -223,8 +223,7 @@ else if (value.startsWith("item/")) } if (!JsonUtil.readJson(packConverter.getGson(), model).equals(jsonObject)) { - if (PackConverter.DEBUG) - Logger.log("Updating Model: " + model.getFileName()); + Logger.debug("Updating Model: " + model.getFileName()); JsonUtil.writeJson(packConverter.getGson(), model, jsonObject); } } catch (IOException e) { @@ -354,7 +353,7 @@ protected String setParent(String prefix, String path, String parent, String ite String parent2 = parent.replace(prefix, ""); JsonObject file = JsonUtil.readJsonResource(packConverter.getGson(), path).getAsJsonObject(item); if (file == null) { - Logger.log("Prefix Failed on: " + parent); + Logger.debug("Prefix Failed on: " + parent); return ""; } return file.has(parent2) ? prefix + file.get(parent2).getAsString() : parent; diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java index 46525417..553c808e 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java @@ -68,11 +68,9 @@ public void convert(Pack pack) throws IOException { && from > Util.getVersionProtocol(packConverter.getGson(), "1.13")) { // OptiFine conversion if (minecraftPath.resolve("mcpatcher").toFile().exists()) { - if (PackConverter.DEBUG) - Logger.log("MCPatcher exists, switching to optifine"); + Logger.debug("MCPatcher exists, switching to optifine"); if (minecraftPath.resolve("optifine").toFile().exists()) { - if (PackConverter.DEBUG) - Logger.log("OptiFine exists, merging directories"); + Logger.debug("OptiFine exists, merging directories"); FileUtil.mergeDirectories(minecraftPath.resolve("optifine").toFile(), minecraftPath.resolve("mcpatcher").toFile()); } else @@ -182,8 +180,7 @@ public void convert(Pack pack) throws IOException { // Less than 1.12 if (from > Util.getVersionProtocol(packConverter.getGson(), "1.13") && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - if (PackConverter.DEBUG) - Logger.log("Finding files that are greater than 1.12"); + Logger.debug("Finding files that are greater than 1.12"); findFiles(texturesPath); } } @@ -220,14 +217,12 @@ protected void findFiles(Path path) throws IOException { for (File file : directory.listFiles()) { if (file.isDirectory()) { if (file.getName().equals("item")) { - if (PackConverter.DEBUG) - Logger.log("Found Items folder, renaming"); + Logger.debug("Found Items folder, renaming"); FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("item", "items")); } if (file.getName().equals("block")) { - if (PackConverter.DEBUG) - Logger.log("Found blocks folder, renaming"); + Logger.debug("Found blocks folder, renaming"); FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("block", "blocks")); } @@ -238,8 +233,7 @@ protected void findFiles(Path path) throws IOException { FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("[()]", "")); if (!file.getName().equals(file.getName().toLowerCase())) - if (PackConverter.DEBUG) - Logger.log("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); + Logger.debug("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); FileUtil.renameFile(path.resolve(file.getName()), file.getName().toLowerCase()); } @@ -266,8 +260,8 @@ protected void renameAll(Mapping mapping, String extension, Path path) throws IO newName + extension); if (ret == null) return; - if (ret && PackConverter.DEBUG) - Logger.log(" Renamed: " + name + extension + "->" + newName + extension); + if (ret) + Logger.debug("Renamed: " + name + extension + "->" + newName + extension); else if (!ret) System.err.println(" Failed to rename: " + name + extension + "->" + newName + extension); }); @@ -282,11 +276,10 @@ else if (!ret) newName + extension); if (ret == null) return; - if (ret && PackConverter.DEBUG) - Logger.log(" Renamed: " + name + extension + "->" + newName + extension); + if (ret) + Logger.debug("Renamed: " + name + extension + "->" + newName + extension); else if (!ret) - System.err.println( - " Failed to rename: " + name + extension + "->" + newName + extension); + Logger.log("Failed to rename: " + name + extension + "->" + newName + extension); } }); } @@ -307,9 +300,8 @@ else if (!ret) Boolean ret = FileUtil.renameFile(path1, newName + extension); if (ret == null) return; - if (ret && PackConverter.DEBUG) - Logger.log( - " Renamed: " + path1.getFileName().toString() + "->" + newName + extension); + if (ret) + Logger.debug("Renamed: " + path1.getFileName().toString() + "->" + newName + extension); else if (!ret) System.err.println(" Failed to rename: " + path1.getFileName().toString() + "->" + newName + extension); diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/MapIconConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/MapIconConverter.java index 3cc23ba6..dc107284 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/MapIconConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/MapIconConverter.java @@ -61,7 +61,7 @@ public void convert(Pack pack) throws IOException { int newX = (int) (mapped >> 32); int newY = (int) (long) mapped; - Logger.log(" Mapping " + x + "," + y + " to " + newX + "," + newY); + Logger.log("Mapping " + x + "," + y + " to " + newX + "," + newY); g2d.drawImage(image.getSubimage(x * scale, y * scale, 8 * scale, 8 * scale), newX * scale, newY * scale, null); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/ForwardsPackConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/ForwardsPackConverter.java index 6d225bb0..595a9f5b 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/ForwardsPackConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/ForwardsPackConverter.java @@ -24,10 +24,9 @@ public ForwardsPackConverter(String from, String to, String light, boolean minif if (!minify) gsonBuilder.setPrettyPrinting(); gson = gsonBuilder.create(); - DEBUG = debug; + Logger.setDebug(debug); Logger.setStream(out); - Logger.log(from); - Logger.log(to); + Logger.log("Converting packs from: " + from + " to " + to); this.INPUT_DIR = input; converterRunner(from, to, light); } @@ -122,14 +121,18 @@ public void runPack(Pack pack) { try { Logger.log("Converting " + pack); pack.getHandler().setup(); - Logger.log(" Running Converters"); + Logger.addTab(); + Logger.log("Running Converters"); for (Converter converter : converters.values()) { - if (DEBUG) - Logger.log(" Running " + converter.getClass().getSimpleName()); + Logger.addTab(); + Logger.log("Running " + converter.getClass().getSimpleName()); converter.convert(pack); + Logger.subTab(); } + Logger.subTab(); pack.getHandler().finish(); } catch (Throwable t) { + Logger.resetTab(); Logger.log("Failed to convert!"); Util.propagate(t); } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.java index eecc5e59..1df3f78f 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.java @@ -42,24 +42,28 @@ protected void fixAnimations(Path animations) throws IOException { try { JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); - boolean anyChanges = false; - JsonElement animationElement = json.get("animation"); - if (animationElement instanceof JsonObject) { - JsonObject animationObject = (JsonObject) animationElement; + if (json != null) { + boolean anyChanges = false; + JsonElement animationElement = json.get("animation"); + if (animationElement instanceof JsonObject) { + JsonObject animationObject = (JsonObject) animationElement; - // TODO: Confirm this doesn't break any packs - animationObject.remove("width"); - animationObject.remove("height"); + // TODO: Confirm this doesn't break any packs + animationObject.remove("width"); + animationObject.remove("height"); - anyChanges = true; - } + anyChanges = true; + } - if (anyChanges) { - JsonUtil.writeJson(packConverter.getGson(), file, json); - if (PackConverter.DEBUG) - Logger.log(" Converted " + file.getFileName()); + if (anyChanges) { + JsonUtil.writeJson(packConverter.getGson(), file, json); + Logger.debug("Converted " + file.getFileName()); + } + } else { + Logger.log("File: " + file.getFileName() + " is not a valid JSON file."); } } catch (IOException e) { + Logger.log("Failed to convert file: " + file.getFileName()); Util.propagate(e); } }); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.java index 8fe1965c..624f74de 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.java @@ -43,47 +43,49 @@ public void convert(Pack pack) throws IOException { .forEach(file -> { try { JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); - - // process multipart - if (json.has("multipart")) { - JsonElement multipart = json.get("multipart"); - if (multipart.isJsonArray()) { - JsonArray multipartArray = multipart.getAsJsonArray(); - for (JsonElement element : multipartArray) { - JsonObject multipartObject = element.getAsJsonObject(); - for (Map.Entry entry : multipartObject.entrySet()) - updateModelPath(entry); + if (json != null) { + // process multipart + if (json.has("multipart")) { + JsonElement multipart = json.get("multipart"); + if (multipart.isJsonArray()) { + JsonArray multipartArray = multipart.getAsJsonArray(); + for (JsonElement element : multipartArray) { + JsonObject multipartObject = element.getAsJsonObject(); + for (Map.Entry entry : multipartObject.entrySet()) + updateModelPath(entry); + } } } - } - // process variants - if (json.has("variants")) { - JsonElement variants = json.get("variants"); - if (variants.isJsonObject()) { - JsonObject variantsObject = variants.getAsJsonObject(); - // change "normal" key to "" - if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") - && to >= Util.getVersionProtocol(packConverter.getGson(), "1.13") - && variantsObject.has("normal")) { - JsonElement normal = variantsObject.get("normal"); - if (normal instanceof JsonObject || normal instanceof JsonArray) { - variantsObject.add("", normal); - variantsObject.remove("normal"); - anyChanges = true; + // process variants + if (json.has("variants")) { + JsonElement variants = json.get("variants"); + if (variants.isJsonObject()) { + JsonObject variantsObject = variants.getAsJsonObject(); + // change "normal" key to "" + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") + && to >= Util.getVersionProtocol(packConverter.getGson(), "1.13") + && variantsObject.has("normal")) { + JsonElement normal = variantsObject.get("normal"); + if (normal instanceof JsonObject || normal instanceof JsonArray) { + variantsObject.add("", normal); + variantsObject.remove("normal"); + anyChanges = true; + } } - } - // update model paths to prepend block - for (Map.Entry entry : variantsObject.entrySet()) - updateModelPath(entry); + // update model paths to prepend block + for (Map.Entry entry : variantsObject.entrySet()) + updateModelPath(entry); + } } - } - if (anyChanges) { - JsonUtil.writeJson(packConverter.getGson(), file, json); - if (PackConverter.DEBUG) - Logger.log(" Converted " + file.getFileName()); + if (anyChanges) { + JsonUtil.writeJson(packConverter.getGson(), file, json); + Logger.debug("Converted " + file.getFileName()); + } + } else { + Logger.log("Failed to convert: " + file.getFileName() + " as JSON is invalid."); } } catch (IOException e) { Util.propagate(e); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/LangConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/LangConverter.java index 76fd7de7..5e2cb3dd 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/LangConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/LangConverter.java @@ -97,7 +97,7 @@ public void convert(Pack pack) throws IOException { try { int modelNoLang = model.getFileName().toString().indexOf(".lang"); String file2 = model.getFileName().toString().substring(0, modelNoLang); - Logger.log("Saving: " + file2 + ".json"); + Logger.debug("Saving: " + file2 + ".json"); Path outLangPath = pack.getWorkingPath() .resolve(("assets/minecraft/lang/" + file2 + ".json").replace("/", File.separator)); JsonUtil.writeJson(packConverter.getGson(), outLangPath, out); @@ -112,7 +112,7 @@ public void convert(Pack pack) throws IOException { for (int i = 0; i < models.size(); i++) { Path langFilePath = pack.getWorkingPath() .resolve(("assets/minecraft/lang/" + models.get(i)).replace("/", File.separator)); - Logger.log("Deleting: " + langFilePath); + Logger.debug("Deleting: " + langFilePath); Files.delete(langFilePath); } } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.java index e91a1e88..a99c0bec 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.java @@ -63,8 +63,7 @@ protected void remapProperties(Path path) throws IOException { Files.list(path).filter(path1 -> path1.toString().endsWith(".properties")).forEach(model -> { try (InputStream input = new FileInputStream(model.toString())) { - if (PackConverter.DEBUG) - Logger.log("Updating:" + model.getFileName()); + Logger.debug("Updating:" + model.getFileName()); PropertiesEx prop = new PropertiesEx(); prop.load(input); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ModelConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ModelConverter.java index 85df5b7e..7fb468ad 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ModelConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ModelConverter.java @@ -78,13 +78,12 @@ protected void remapModelJson(Path path) throws IOException { && JsonUtil.readJson(packConverter.getGson(), model).isJsonObject()) jsonObject = JsonUtil.readJson(packConverter.getGson(), model); else { - if (PackConverter.DEBUG) { - Logger.log("Could not convert model: " + model.getFileName()); - if (JsonUtil.readJson(packConverter.getGson(), model) == null) - Logger.log("Check for Syntax Errors in file."); - else - Logger.log("File is not JSON Object."); - } + Logger.debug("Could not convert model: " + model.getFileName()); + Logger.addTab(); + if (JsonUtil.readJson(packConverter.getGson(), model) == null) + Logger.debug("Check for Syntax Errors in file."); + else Logger.debug("File is not JSON Object."); + Logger.subTab(); return; } @@ -102,7 +101,7 @@ protected void remapModelJson(Path path) throws IOException { // handle the remapping of textures, for models that use default texture names jsonObject = packConverter.getGson().fromJson(content, JsonObject.class); if (jsonObject.keySet().isEmpty() || jsonObject.entrySet().isEmpty()) { - Logger.log("Model '" + model.getFileName() + "' was empty, skipping..."); + Logger.debug("Model '" + model.getFileName() + "' was empty, skipping..."); return; } @@ -129,13 +128,13 @@ protected void remapModelJson(Path path) throws IOException { value = "block/" + nameConverter.getBlockMapping() .remap(value.substring("block/".length())).toLowerCase() .replaceAll("[()]", ""); - if (PackConverter.DEBUG) { - Logger.log(value.substring("block/".length()).toLowerCase() - .replaceAll("[()]", "")); - Logger.log(nameConverter.getBlockMapping() - .remap(value.substring("block/".length())).toLowerCase() - .replaceAll("[()]", "")); - } + + Logger.debug(value.substring("block/".length()).toLowerCase() + .replaceAll("[()]", "")); + Logger.debug(nameConverter.getBlockMapping() + .remap(value.substring("block/".length())).toLowerCase() + .replaceAll("[()]", "")); + } else if (value.startsWith("item/")) { value = "item/" + nameConverter.getItemMapping() .remap(value.substring("item/".length())).toLowerCase() @@ -278,8 +277,7 @@ else if (parent.startsWith("item/")) } if (!JsonUtil.readJson(packConverter.getGson(), model).equals(jsonObject)) { - if (PackConverter.DEBUG) - Logger.log("Updating Model: " + model.getFileName()); + Logger.debug("Updating Model: " + model.getFileName()); JsonUtil.writeJson(packConverter.getGson(), model, jsonObject); } } catch (IOException e) { @@ -300,7 +298,7 @@ protected String setParent(String prefix, String path, String parent, String ite String parent2 = parent.replace(prefix, ""); JsonObject file = JsonUtil.readJsonResource(packConverter.getGson(), path).getAsJsonObject(item); if (file == null) { - Logger.log("Prefix Failed on: " + parent); + Logger.debug("Prefix Failed on: " + parent); return ""; } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java index ea75a0f7..5cbd76f3 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java @@ -65,8 +65,7 @@ public void convert(Pack pack) throws IOException { // Less than 1.12 if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") && to > Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - if (PackConverter.DEBUG) - Logger.log("Finding files that are less than 1.12"); + Logger.debug("Finding files that are less than 1.12"); findFiles(minecraftPath); } @@ -74,11 +73,9 @@ public void convert(Pack pack) throws IOException { if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) { // OptiFine conversion if (minecraftPath.resolve("mcpatcher").toFile().exists()) { - if (PackConverter.DEBUG) - Logger.log("MCPatcher exists, switching to optifine"); + Logger.debug("MCPatcher exists, switching to optifine"); if (minecraftPath.resolve("optifine").toFile().exists()) { - if (PackConverter.DEBUG) - Logger.log("OptiFine exists, merging directories"); + Logger.debug("OptiFine exists, merging directories"); FileUtil.mergeDirectories(minecraftPath.resolve("optifine").toFile(), minecraftPath.resolve("mcpatcher").toFile()); } else @@ -212,7 +209,7 @@ public void convert(Pack pack) throws IOException { renameAll(itemMapping17, ".png", modelsPath.resolve("item")); if (!entityPath.resolve("squid").toFile().exists()) entityPath.resolve("squid").toFile().mkdir(); - if (entityPath.resolve("squid.png").toFile().exists()) + if (!entityPath.resolve("squid.png").toFile().exists()) Files.move(entityPath.resolve("squid.png"), entityPath.resolve("squid/squid.png")); } @@ -261,14 +258,12 @@ protected void findFiles(Path path) throws IOException { for (File file : directory.listFiles()) { if (file.isDirectory()) { if (file.getName().equals("items")) { - if (PackConverter.DEBUG) - Logger.log("Found Items folder, renaming"); + Logger.debug("Found Items folder, renaming"); FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("items", "item")); } if (file.getName().equals("blocks")) { - if (PackConverter.DEBUG) - Logger.log("Found blocks folder, renaming"); + Logger.debug("Found blocks folder, renaming"); FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("blocks", "block")); } @@ -279,8 +274,7 @@ protected void findFiles(Path path) throws IOException { FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("[()]", "")); if (!file.getName().equals(file.getName().toLowerCase())) - if (PackConverter.DEBUG) - Logger.log("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); + Logger.debug("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); FileUtil.renameFile(path.resolve(file.getName()), file.getName().toLowerCase()); } @@ -308,11 +302,10 @@ protected void renameAll(Mapping mapping, String extension, Path path) throws IO newName + extension); if (ret == null) return; - if (ret && PackConverter.DEBUG) { - Logger.log(" Renamed: " + name + extension + "->" + newName + extension); + if (ret) { + Logger.debug("Renamed: " + name + extension + "->" + newName + extension); } else if (!ret) { - System.err.println( - " Failed to rename: " + name + extension + "->" + newName + extension); + Logger.log("Failed to rename: " + name + extension + "->" + newName + extension); } }); } @@ -342,11 +335,10 @@ protected void renameAll(Mapping mapping, String extension, Path path) throws IO Boolean ret = FileUtil.renameFile(path1, newName + extension); if (ret == null) return; - if (ret && PackConverter.DEBUG) { - Logger.log( - " Renamed: " + path1.getFileName().toString() + "->" + newName + extension); + if (ret) { + Logger.debug("Renamed: " + path1.getFileName().toString() + "->" + newName + extension); } else if (!ret) { - System.err.println(" Failed to rename: " + path1.getFileName().toString() + "->" + newName + Logger.log("Failed to rename: " + path1.getFileName().toString() + "->" + newName + extension); } } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.java index 855d9d14..caea50ee 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.java @@ -62,8 +62,7 @@ else if (jsonElement instanceof JsonPrimitive) String rewrite = path.toFile().getCanonicalPath().substring( baseSoundsPath.toString().length() + 1, path.toFile().getCanonicalPath().length() - 4); - if (PackConverter.DEBUG) - Logger.log(" Rewriting Sound: '" + sound + "' -> '" + rewrite + "'"); + Logger.debug("Rewriting Sound: '" + sound + "' -> '" + rewrite + "'"); sound = rewrite; } else { sound = jsonElement.getAsString(); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.java index 1a36301a..59ab3ccb 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.java @@ -64,11 +64,11 @@ protected String fixSpaces(Path path) throws IOException { if (ret == null) return "null"; - if (ret && PackConverter.DEBUG) { - Logger.log(" Renamed: " + path.getFileName().toString() + "->" + noSpaces); + if (ret) { + Logger.debug("Renamed: " + path.getFileName().toString() + "->" + noSpaces); return path.getParent() + File.separator + noSpaces; } else if (!ret) { - System.err.println(" Failed to rename: " + path.getFileName().toString() + "->" + noSpaces); + Logger.log("Failed to rename: " + path.getFileName().toString() + "->" + noSpaces); return path.getParent() + File.separator + noSpaces; } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.java index e35aba8f..7382f3cc 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.java @@ -62,7 +62,7 @@ public void convert(Pack pack) throws IOException { int newX = (int) (mapped >> 32); int newY = (int) (long) mapped; - Logger.log(" Mapping " + x + "," + y + " to " + newX + "," + newY); + Logger.debug("Mapping " + x + "," + y + " to " + newX + "," + newY); g2d.drawImage(image.getSubimage(x * scale, y * scale, 8 * scale, 8 * scale), newX * scale, newY * scale, null); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.java index 8f109837..d0044972 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.java @@ -49,7 +49,7 @@ public void convert(Pack pack) throws IOException { if (from <= Util.getVersionProtocol(gson, "1.12.2")) { Path particlesPath = particlePath.resolve(slice.getPath()); if (particlesPath.toFile().exists()) { - Logger.log("Detected 'particles.png' from versions before 1.13, converting to newer size.."); + Logger.debug("Detected 'particles.png' from versions before 1.13, converting to newer size.."); ImageConverter converter = new ImageConverter(slice.getWidth(), slice.getHeight(), particlesPath); converter.newImage(256, 256); converter.subImage(0, 0, 128, 128, 0, 0); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slicer.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slicer.java index a9505de0..c3a1b11e 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slicer.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slicer.java @@ -21,7 +21,7 @@ public static void runSlicer(Gson gson, Slice slice, Path root, Class pre Path path = root.resolve(slice.getPath()); if (!path.toFile().exists()) { - Logger.log("Texture '" + slice.getPath() + "' doesn't exist, ignoring..."); + Logger.debug("Texture '" + slice.getPath() + "' doesn't exist, ignoring..."); return; } @@ -79,7 +79,7 @@ public static void runSlicer(Gson gson, Slice slice, Path root, Class pre } if (!path.toFile().delete()) - Logger.log("Failed to remove '" + path.getFileName() + "' whilst slicing."); + Logger.debug("Failed to remove '" + path.getFileName() + "' whilst slicing."); if (!slice.shouldDelete()) converter.store(imagePath); diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/PackConverter.java b/library/src/main/java/com/agentdid127/resourcepack/library/PackConverter.java index 7bcebc1e..b392d2e6 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/PackConverter.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/PackConverter.java @@ -8,7 +8,6 @@ public abstract class PackConverter { protected final Map, Converter> converters = new LinkedHashMap<>(); protected Gson gson; - public static boolean DEBUG = true; /** * Registers Converter. diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/pack/Pack.java b/library/src/main/java/com/agentdid127/resourcepack/library/pack/Pack.java index 8dde31fa..f3aa2a3e 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/pack/Pack.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/pack/Pack.java @@ -74,11 +74,11 @@ public Handler(Pack pack) { */ public void setup() throws IOException { if (pack.getWorkingPath().toFile().exists()) { - Logger.log(" Deleting existing conversion"); + Logger.log("Deleting existing conversion"); FileUtil.deleteDirectoryAndContents(pack.getWorkingPath()); } - Logger.log(" Copying existing pack"); + Logger.log("Copying existing pack"); FileUtil.copyDirectory(pack.getOriginalPath(), pack.getWorkingPath()); bomRemover(pack.getWorkingPath()); @@ -90,7 +90,7 @@ static void bomRemover(Path workingPath) throws IOException { ".txt", ".json", ".mcmeta", ".properties", ".lang"); int count = bom.findBOMs().size(); if (count > 0) - Logger.log("Removing BOMs from " + count + " files."); + Logger.debug("Removing BOMs from " + count + " files."); bom.removeBOMs(); } diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/pack/ZipPack.java b/library/src/main/java/com/agentdid127/resourcepack/library/pack/ZipPack.java index d07036d7..673ad0cc 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/pack/ZipPack.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/pack/ZipPack.java @@ -44,13 +44,13 @@ public Path getConvertedZipPath() { @Override public void setup() throws IOException { if (pack.getWorkingPath().toFile().exists()) { - Logger.log(" Deleting existing conversion"); + Logger.log("Deleting existing conversion"); FileUtil.deleteDirectoryAndContents(pack.getWorkingPath()); } Path convertedZipPath = getConvertedZipPath(); if (convertedZipPath.toFile().exists()) { - Logger.log(" Deleting existing conversion zip"); + Logger.log("Deleting existing conversion zip"); convertedZipPath.toFile().delete(); } @@ -75,7 +75,7 @@ public void setup() throws IOException { @Override public void finish() throws IOException { try { - Logger.log(" Zipping working directory"); + Logger.log("Zipping working directory"); ZipFile zipFile = new ZipFile(getConvertedZipPath().toFile()); ZipParameters parameters = new ZipParameters(); parameters.setIncludeRootFolder(false); @@ -85,7 +85,7 @@ public void finish() throws IOException { Util.propagate(e); } - Logger.log(" Deleting working directory"); + Logger.log("Deleting working directory"); FileUtil.deleteDirectoryAndContents(pack.getWorkingPath()); } diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/FileUtil.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/FileUtil.java index 3853ced1..6a431f46 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/FileUtil.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/FileUtil.java @@ -102,13 +102,13 @@ public static Boolean mergeDirectories(File dir1, File dir2) throws IOException File[] files = dir2.listFiles(); for (File file : files) { if (file.isDirectory()) { - Logger.log(targetDirPath + File.separator + file.getName()); + Logger.debug(targetDirPath + File.separator + file.getName()); File file3 = new File(targetDirPath + File.separator + file.getName()); file3.mkdirs(); - Logger.log("Created" + file3.getName()); + Logger.debug("Created" + file3.getName()); mergeDirectories(file3, file); } else { - Logger.log(targetDirPath + File.separator + file.getName()); + Logger.debug(targetDirPath + File.separator + file.getName()); file.renameTo(new File(targetDirPath + File.separator + file.getName())); } } diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/ImageConverter.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/ImageConverter.java index 1c4d6041..777c7daa 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/ImageConverter.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/ImageConverter.java @@ -39,7 +39,7 @@ public ImageConverter(Integer defaultWIn, Integer defaultHIn, Path locationIn) t } if (!fileIsPowerOfTwo()) { - Logger.log("Image (" + image.getWidth() + "x" + image.getHeight() + ") '" + locationIn.getFileName() + Logger.debug("Image (" + image.getWidth() + "x" + image.getHeight() + ") '" + locationIn.getFileName() + "' resolution size is not a power of 2. Converting to be so."); int fixed_width = (int) Math.ceil(Math.log(image.getWidth()) / Math.log(2)); @@ -105,7 +105,7 @@ public void fillEmpty(int x, int y, int width, int height) { */ public void setImage(int defaultWIn, int defaultHIn) throws IOException { if (!imageIsPowerOfTwo(newImage)) { - Logger.log("Image '" + location.getFileName() + "' is not a power of 2"); + Logger.debug("Image '" + location.getFileName() + "' is not a power of 2"); return; } image = newImage; diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java index d6b28220..2334f9f8 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java @@ -4,16 +4,54 @@ public class Logger { private static PrintStream stream; + private static boolean debug; + + private static int tabs = 0; + + public static void addTab() { + tabs++; + } + + public static void subTab() { + tabs--; + if (tabs < 0) tabs = 0; + } + + public static void resetTab() { + tabs = 0; + } + + private static String getTabs() { + String out = ""; + + for (int i = 0; i < tabs; i++) { + out += " "; + } + return out; + } public static void setStream(PrintStream stream) { Logger.stream = stream; } + public static void setDebug(boolean debug) { + Logger.debug = debug; + } + + public static void debug(String message) { + if (debug) log(message); + } + public static void log(String message) { - stream.println(message); + stream.println(getTabs() + message); + } + + private static void debug(Object thing) { + if (debug) log(thing); } public static void log(Object thing) { - stream.println(String.valueOf(thing)); + String tabs = getTabs(); + stream.println(tabs + String.valueOf(thing).replaceAll("\n", "\n" + tabs)); } } From 29a0eb383a6e1ad4233d58a55bcebb15eb87a426 Mon Sep 17 00:00:00 2001 From: Cory Borek Date: Sat, 9 Nov 2024 19:55:28 -0500 Subject: [PATCH 2/2] Don't throw an error with invalid json objects We are checking for a valid json object here. It is suppsed to throw an error if it isn't here. --- .../com/agentdid127/resourcepack/library/utilities/JsonUtil.java | 1 - 1 file changed, 1 deletion(-) diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/JsonUtil.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/JsonUtil.java index fa38bb0d..14e0fb0b 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/JsonUtil.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/JsonUtil.java @@ -71,7 +71,6 @@ public static boolean isJson(Gson gson, String Json) { gson.fromJson(Json, Object.class); return true; } catch (com.google.gson.JsonSyntaxException ex) { - ex.printStackTrace(); return false; } }