diff --git a/src/main/java/net/hypixel/resourcepack/PackConverter.java b/src/main/java/net/hypixel/resourcepack/PackConverter.java index 8f1e4a77..9825d7dc 100644 --- a/src/main/java/net/hypixel/resourcepack/PackConverter.java +++ b/src/main/java/net/hypixel/resourcepack/PackConverter.java @@ -63,7 +63,9 @@ public void run() throws IOException { System.out.println(" Running Converters"); for (Converter converter : converters.values()) { - if (PackConverter.DEBUG) System.out.println(" Running " + converter.getClass().getSimpleName()); + if (PackConverter.DEBUG) { + System.out.println(" Running " + converter.getClass().getSimpleName()); + } converter.convert(pack); } diff --git a/src/main/java/net/hypixel/resourcepack/Util.java b/src/main/java/net/hypixel/resourcepack/Util.java index d24678bc..7e1ba8df 100644 --- a/src/main/java/net/hypixel/resourcepack/Util.java +++ b/src/main/java/net/hypixel/resourcepack/Util.java @@ -6,7 +6,9 @@ import javax.imageio.ImageIO; import java.awt.image.BufferedImage; -import java.io.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; @@ -28,18 +30,22 @@ public static void copyDir(Path src, Path dest) throws IOException { } public static void deleteDirectoryAndContents(Path dirPath) throws IOException { - if (!dirPath.toFile().exists()) return; - - //noinspection ResultOfMethodCallIgnored - Files.walk(dirPath) - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); + if (Files.exists(dirPath)) { + Files.walk(dirPath).sorted(Comparator.reverseOrder()) + .forEach(path -> { + try { + Files.delete(path); + } catch (IOException ignored) { + } + }); + } } public static boolean fileExistsCorrectCasing(Path path) throws IOException { - if (!path.toFile().exists()) return false; - return path.toString().equals(path.toFile().getCanonicalPath()); + if (Files.exists(path)) { + return path.toAbsolutePath().equals(path.toRealPath()); + } + return false; } public static JsonObject readJsonResource(Gson gson, String path) { @@ -68,15 +74,7 @@ public static JsonObject readJson(Gson gson, Path path) throws IOException { public static T readJson(Gson gson, Path path, Class clazz) throws IOException { // TODO Improvement: this will fail if there is a BOM in the file - return gson.fromJson(new JsonReader(new FileReader(path.toFile())), clazz); - } - - /** - * @return null if file doesn't exist, {@code true} if successfully renamed, {@code false} if failed - */ - public static Boolean renameFile(Path file, String newName) { - if (!file.toFile().exists()) return null; - return file.toFile().renameTo(new File(file.getParent() + "/" + newName)); + return gson.fromJson(new JsonReader(Files.newBufferedReader(path)), clazz); } public static RuntimeException propagate(Throwable t) { diff --git a/src/main/java/net/hypixel/resourcepack/impl/AnimationConverter.java b/src/main/java/net/hypixel/resourcepack/impl/AnimationConverter.java index fb5fc8fc..f087921c 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/AnimationConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/AnimationConverter.java @@ -7,12 +7,10 @@ import net.hypixel.resourcepack.Util; import net.hypixel.resourcepack.pack.Pack; -import java.io.File; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collections; public class AnimationConverter extends Converter { @@ -22,13 +20,15 @@ public AnimationConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { - fixAnimations(pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures" + File.separator + "block")); - fixAnimations(pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures" + File.separator + "item")); + Path texturesPath = pack.getMinecraftPath().resolve("textures"); + fixAnimations(texturesPath.resolve("block")); + fixAnimations(texturesPath.resolve("item")); } protected void fixAnimations(Path animations) throws IOException { - if (!animations.toFile().exists()) return; - + if (!Files.exists(animations)) { + return; + } Files.list(animations) .filter(file -> file.toString().endsWith(".png.mcmeta")) .forEach(file -> { @@ -48,9 +48,10 @@ protected void fixAnimations(Path animations) throws IOException { } if (anyChanges) { - Files.write(file, Collections.singleton(packConverter.getGson().toJson(json)), Charset.forName("UTF-8")); - - if (PackConverter.DEBUG) System.out.println(" Converted " + file.getFileName()); + Files.write(file, packConverter.getGson().toJson(json).getBytes(StandardCharsets.UTF_8)); + if (PackConverter.DEBUG) { + System.out.println(" Converted " + file.getFileName()); + } } } catch (IOException e) { Util.propagate(e); diff --git a/src/main/java/net/hypixel/resourcepack/impl/BlockStateConverter.java b/src/main/java/net/hypixel/resourcepack/impl/BlockStateConverter.java index 9f1ee50b..6fb0a1d3 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/BlockStateConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/BlockStateConverter.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; @@ -24,9 +25,11 @@ public BlockStateConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { - Path states = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "blockstates"); - if (!states.toFile().exists()) return; - + Path states = pack.getMinecraftPath() + .resolve("blockstates"); + if (!Files.exists(states)) { + return; + } Files.list(states) .filter(file -> file.toString().endsWith(".json")) .forEach(file -> { @@ -68,9 +71,10 @@ public void convert(Pack pack) throws IOException { } if (anyChanges) { - Files.write(file, Collections.singleton(packConverter.getGson().toJson(json)), Charset.forName("UTF-8")); - - if (PackConverter.DEBUG) System.out.println(" Converted " + file.getFileName()); + Files.write(file, packConverter.getGson().toJson(json).getBytes(StandardCharsets.UTF_8)); + if (PackConverter.DEBUG) { + System.out.println(" Converted " + file.getFileName()); + } } } catch (IOException e) { Util.propagate(e); diff --git a/src/main/java/net/hypixel/resourcepack/impl/MapIconConverter.java b/src/main/java/net/hypixel/resourcepack/impl/MapIconConverter.java index 2665a03f..350b2080 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/MapIconConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/MapIconConverter.java @@ -8,8 +8,8 @@ import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; -import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; @@ -20,7 +20,6 @@ public class MapIconConverter extends Converter { public MapIconConverter(PackConverter packConverter) { super(packConverter); - mapping.put(pack(0, 0), pack(0, 0)); mapping.put(pack(8, 0), pack(8, 0)); mapping.put(pack(16, 0), pack(16, 0)); @@ -35,30 +34,31 @@ public MapIconConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { - Path imagePath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures" + File.separator + "map" + File.separator + "map_icons.png"); - if (!imagePath.toFile().exists()) return; - - BufferedImage newImage = Util.readImageResource("/map_icons.png"); - if (newImage == null) throw new NullPointerException(); - Graphics2D g2d = (Graphics2D) newImage.getGraphics(); - - BufferedImage image = ImageIO.read(imagePath.toFile()); - int scale = image.getWidth() / 32; - - for (int x = 0; x <= 32 - 8; x += 8) { - for (int y = 0; y <= 32 - 8; y += 8) { - Long mapped = mapping.get(pack(x, y)); - if (mapped == null) continue; - - int newX = (int) (mapped >> 32); - int newY = (int) (long) mapped; - System.out.println(" Mapping " + x + "," + y + " to " + newX + "," + newY); - - g2d.drawImage(image.getSubimage(x * scale, y * scale, 8 * scale, 8 * scale), newX * scale, newY * scale, null); + Path imagePath = pack.getMinecraftPath() + .resolve("textures") + .resolve("map") + .resolve("map_icons.png"); + if (Files.exists(imagePath)) { + BufferedImage newImage = Util.readImageResource("/map_icons.png"); + if (newImage == null) { + throw new NullPointerException(); } + Graphics2D g2d = (Graphics2D) newImage.getGraphics(); + BufferedImage image = ImageIO.read(Files.newInputStream(imagePath)); + int scale = image.getWidth() / 32; + for (int x = 0; x <= 32 - 8; x += 8) { + for (int y = 0; y <= 32 - 8; y += 8) { + Long mapped = mapping.get(pack(x, y)); + if (mapped != null) { + int newX = (int) (mapped >> 32); + int newY = (int) (long) mapped; + System.out.println(" Mapping " + x + "," + y + " to " + newX + "," + newY); + g2d.drawImage(image.getSubimage(x * scale, y * scale, 8 * scale, 8 * scale), newX * scale, newY * scale, null); + } + } + } + ImageIO.write(newImage, "png", imagePath.toFile()); } - - ImageIO.write(newImage, "png", imagePath.toFile()); } protected long pack(int x, int y) { diff --git a/src/main/java/net/hypixel/resourcepack/impl/ModelConverter.java b/src/main/java/net/hypixel/resourcepack/impl/ModelConverter.java index 9b6efaed..71bff7b8 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/ModelConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/ModelConverter.java @@ -7,12 +7,10 @@ import net.hypixel.resourcepack.Util; import net.hypixel.resourcepack.pack.Pack; -import java.io.File; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collections; import java.util.Map; public class ModelConverter extends Converter { @@ -23,29 +21,29 @@ public ModelConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { - Path models = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "models"); - + Path models = pack.getMinecraftPath().resolve("models"); remapModelJson(models.resolve("block")); remapModelJson(models.resolve("item")); } protected void remapModelJson(Path path) throws IOException { - if (!path.toFile().exists()) return; - + if (!Files.exists(path)) { + return; + } Files.list(path) .filter(path1 -> path1.toString().endsWith(".json")) .forEach(model -> { try { JsonObject jsonObject = Util.readJson(packConverter.getGson(), model); - // minify the json so we can replace spaces in paths easily + // minify the JSON, so we can replace spaces in paths easily // TODO Improvement: handle this in a cleaner way? String content = jsonObject.toString(); content = content.replaceAll("items/", "item/"); content = content.replaceAll("blocks/", "block/"); content = content.replaceAll(" ", "_"); - Files.write(model, Collections.singleton(content), Charset.forName("UTF-8")); + Files.write(model, content.getBytes(StandardCharsets.UTF_8)); // handle the remapping of textures, for models that use default texture names jsonObject = Util.readJson(packConverter.getGson(), model); @@ -62,8 +60,10 @@ protected void remapModelJson(Path path) throws IOException { } } } - - Files.write(model, Collections.singleton(packConverter.getGson().toJson(jsonObject)), Charset.forName("UTF-8")); + Files.write(model, packConverter.getGson() + .toJson(jsonObject) + .getBytes(StandardCharsets.UTF_8) + ); } catch (IOException e) { throw Util.propagate(e); } diff --git a/src/main/java/net/hypixel/resourcepack/impl/NameConverter.java b/src/main/java/net/hypixel/resourcepack/impl/NameConverter.java index c9888481..5711960f 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/NameConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/NameConverter.java @@ -7,7 +7,6 @@ import net.hypixel.resourcepack.Util; import net.hypixel.resourcepack.pack.Pack; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -25,38 +24,62 @@ public NameConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { - Path models = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "models"); - if (models.resolve("blocks").toFile().exists()) Files.move(models.resolve("blocks"), models.resolve("block")); - renameAll(blockMapping, ".json", models.resolve("block")); - if (models.resolve("items").toFile().exists()) Files.move(models.resolve("items"), models.resolve("item")); - renameAll(itemMapping, ".json", models.resolve("item")); - - Path blockStates = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "blockstates"); - renameAll(itemMapping, ".json", blockStates); - - Path textures = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures"); - if (textures.resolve("blocks").toFile().exists()) Files.move(textures.resolve("blocks"), textures.resolve("block")); - renameAll(blockMapping, ".png", textures.resolve("block")); - renameAll(blockMapping, ".png.mcmeta", textures.resolve("block")); - if (textures.resolve("items").toFile().exists()) Files.move(textures.resolve("items"), textures.resolve("item")); - renameAll(itemMapping, ".png", textures.resolve("item")); - renameAll(itemMapping, ".png.mcmeta", textures.resolve("item")); + { // Giving them their own scopes to reuse variable names + Path modelsPath = pack.getMinecraftPath().resolve("models"); + Path blocksPath = modelsPath.resolve("blocks"); + Path blockPath = modelsPath.resolve("block"); + if (Files.exists(blocksPath)) { + Files.move(blocksPath, blockPath); + } + renameAll(blockMapping, ".json", blockPath); + Path itemsPath = modelsPath.resolve("items"); + Path itemPath = modelsPath.resolve("item"); + if (Files.exists(itemsPath)) { + Files.move(itemsPath, itemPath); + } + renameAll(itemMapping, ".json", itemPath); + + Path blockStates = pack.getMinecraftPath().resolve("blockstates"); + renameAll(itemMapping, ".json", blockStates); + } + { + Path texturesPath = pack.getMinecraftPath().resolve("textures"); + Path blocksPath = texturesPath.resolve("blocks"); + Path blockPath = texturesPath.resolve("block"); + if (Files.exists(blocksPath)) { + Files.move(blocksPath, blockPath); + } + renameAll(blockMapping, ".png", blockPath); + renameAll(blockMapping, ".png.mcmeta", blockPath); + Path itemsPath = texturesPath.resolve("items"); + Path itemPath = texturesPath.resolve("item"); + if (Files.exists(itemsPath)) { + Files.move(itemsPath, itemPath); + } + renameAll(itemMapping, ".png", itemPath); + renameAll(itemMapping, ".png.mcmeta", itemPath); + } } protected void renameAll(Mapping mapping, String extension, Path path) throws IOException { - if (path.toFile().exists()) { - Files.list(path).forEach(path1 -> { - if (!path1.toString().endsWith(extension)) return; - - String baseName = path1.getFileName().toString().substring(0, path1.getFileName().toString().length() - extension.length()); + if (Files.exists(path)) { + Files.list(path).forEach(file -> { + if (!file.toString().endsWith(extension)) { + return; + } + String fileName = file.getFileName().toString(); + String baseName = fileName.substring(0, fileName.length() - extension.length()); String newName = mapping.remap(baseName); if (newName != null && !newName.equals(baseName)) { - Boolean ret = Util.renameFile(path1, newName + extension); - if (ret == null) return; - if (ret && PackConverter.DEBUG) { - System.out.println(" Renamed: " + path1.getFileName().toString() + "->" + newName + extension); - } else if (!ret) { - System.err.println(" Failed to rename: " + path1.getFileName().toString() + "->" + newName + extension); + try { + if (Files.exists(file)) { + Files.move(file, file.getParent().resolve(newName + extension)); + if(PackConverter.DEBUG) { + System.out.println(" Renamed: " + fileName + "->" + newName + extension); + } + } + } catch (IOException e) { + System.err.println(" Failed to rename: " + fileName + "->" + newName + extension); } } }); diff --git a/src/main/java/net/hypixel/resourcepack/impl/PackMetaConverter.java b/src/main/java/net/hypixel/resourcepack/impl/PackMetaConverter.java index 4ebaee78..a59d0636 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/PackMetaConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/PackMetaConverter.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; @@ -21,22 +22,28 @@ public PackMetaConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { Path file = pack.getWorkingPath().resolve("pack.mcmeta"); - if (!file.toFile().exists()) return; - - JsonObject json = Util.readJson(packConverter.getGson(), file); - { - JsonObject meta = json.getAsJsonObject("meta"); - if (meta == null) meta = new JsonObject(); - meta.addProperty("game_version", "1.13"); - json.add("meta", meta); - } - { - JsonObject packObject = json.getAsJsonObject("pack"); - if (packObject == null) packObject = new JsonObject(); - packObject.addProperty("pack_format", 4); - json.add("pack", packObject); + if (Files.exists(file)) { + JsonObject json = Util.readJson(packConverter.getGson(), file); + { + JsonObject meta = json.getAsJsonObject("meta"); + if (meta == null) { + meta = new JsonObject(); + } + meta.addProperty("game_version", "1.13"); + json.add("meta", meta); + } + { + JsonObject packObject = json.getAsJsonObject("pack"); + if (packObject == null) { + packObject = new JsonObject(); + } + packObject.addProperty("pack_format", 4); + json.add("pack", packObject); + } + Files.write(file, packConverter.getGson() + .toJson(json) + .getBytes(StandardCharsets.UTF_8) + ); } - - Files.write(file, Collections.singleton(packConverter.getGson().toJson(json)), Charset.forName("UTF-8")); } } \ No newline at end of file diff --git a/src/main/java/net/hypixel/resourcepack/impl/ParticleConverter.java b/src/main/java/net/hypixel/resourcepack/impl/ParticleConverter.java index 326aae76..ea729ce7 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/ParticleConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/ParticleConverter.java @@ -7,8 +7,8 @@ import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; -import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; public class ParticleConverter extends Converter { @@ -19,20 +19,21 @@ public ParticleConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { - Path imagePath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures" + File.separator + "particle" + File.separator + "particles.png"); - if (!imagePath.toFile().exists()) return; - - BufferedImage image = ImageIO.read(imagePath.toFile()); - - // TODO check how higher resolution will handle this. - if (image.getWidth() == 128 && image.getHeight() == 128) { - // make a new bigger image and just paste the existing on in the top left corner - BufferedImage newImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); - Graphics2D g2d = (Graphics2D) newImage.getGraphics(); - g2d.drawImage(image, 0, 0, null); - - // save the new one - ImageIO.write(newImage, "png", imagePath.toFile()); + Path imagePath = pack.getMinecraftPath() + .resolve("textures") + .resolve("particle") + .resolve("particles.png"); + if (Files.exists(imagePath)) { + BufferedImage image = ImageIO.read(Files.newInputStream(imagePath)); + // TODO check how higher resolution will handle this. + if (image.getWidth() == 128 && image.getHeight() == 128) { + // make a new bigger image and just paste the existing on in the top left corner + BufferedImage newImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = (Graphics2D) newImage.getGraphics(); + g2d.drawImage(image, 0, 0, null); + // save the new one + ImageIO.write(newImage, "png", Files.newOutputStream(imagePath)); + } } } diff --git a/src/main/java/net/hypixel/resourcepack/impl/SoundsConverter.java b/src/main/java/net/hypixel/resourcepack/impl/SoundsConverter.java index 61a449bd..6877f463 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/SoundsConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/SoundsConverter.java @@ -12,6 +12,7 @@ import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; @@ -25,61 +26,67 @@ public SoundsConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { - Path soundsJsonPath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "sounds.json"); - if (!soundsJsonPath.toFile().exists()) return; - - JsonObject sounds = Util.readJson(packConverter.getGson(), soundsJsonPath); - JsonObject newSoundsObject = new JsonObject(); - - for (Map.Entry entry : sounds.entrySet()) { - if (entry.getValue().isJsonObject()) { - JsonObject soundObject = entry.getValue().getAsJsonObject(); - if (soundObject.has("sounds") && soundObject.get("sounds").isJsonArray()) { - JsonArray soundsArray = soundObject.getAsJsonArray("sounds"); - - JsonArray newSoundsArray = new JsonArray(); - for (JsonElement jsonElement : soundsArray) { - String sound; - - if (jsonElement instanceof JsonObject) { - sound = ((JsonObject) jsonElement).get("name").getAsString(); - } else if (jsonElement instanceof JsonPrimitive) { - sound = jsonElement.getAsString(); - } else { - throw new IllegalArgumentException("Unknown element type: " + jsonElement.getClass().getSimpleName()); + Path soundsJsonPath = pack.getMinecraftPath().resolve("sounds.json"); + if (Files.exists(soundsJsonPath)) { + + + JsonObject sounds = Util.readJson(packConverter.getGson(), soundsJsonPath); + JsonObject newSoundsObject = new JsonObject(); + + for (Map.Entry entry : sounds.entrySet()) { + JsonElement value = entry.getValue(); + if (value.isJsonObject()) { + JsonObject soundObject = value.getAsJsonObject(); + if (soundObject.has("sounds") && soundObject.get("sounds").isJsonArray()) { + JsonArray soundsArray = soundObject.getAsJsonArray("sounds"); + + JsonArray newSoundsArray = new JsonArray(); + for (JsonElement jsonElement : soundsArray) { + String sound; + + if (jsonElement instanceof JsonObject) { + sound = ((JsonObject) jsonElement).get("name").getAsString(); + } else if (jsonElement instanceof JsonPrimitive) { + sound = jsonElement.getAsString(); + } else { + throw new IllegalArgumentException("Unknown element type: " + jsonElement.getClass().getSimpleName()); + } + + Path baseSoundsPath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "sounds"); + Path path = baseSoundsPath.resolve(sound + ".ogg"); + if (!Util.fileExistsCorrectCasing(path)) { + String rewrite = path.toFile().getCanonicalPath().substring(baseSoundsPath.toString().length() + 1, path.toFile().getCanonicalPath().length() - 4); + if (PackConverter.DEBUG) + System.out.println(" Rewriting Sound: '" + jsonElement.getAsString() + "' -> '" + rewrite + "'"); + sound = rewrite; + } else { + sound = jsonElement.getAsString(); + } + + // windows fix + sound = sound.replaceAll("\\\\", "/"); + + JsonElement newSound = null; + if (jsonElement instanceof JsonObject) { + ((JsonObject) jsonElement).addProperty("name", sound); + newSound = jsonElement; + } else if (jsonElement instanceof JsonPrimitive) { + newSound = new JsonPrimitive(jsonElement.getAsString()); + } + + newSoundsArray.add(newSound); } - - Path baseSoundsPath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "sounds"); - Path path = baseSoundsPath.resolve(sound + ".ogg"); - if (!Util.fileExistsCorrectCasing(path)) { - String rewrite = path.toFile().getCanonicalPath().substring(baseSoundsPath.toString().length() + 1, path.toFile().getCanonicalPath().length() - 4); - if (PackConverter.DEBUG) System.out.println(" Rewriting Sound: '" + jsonElement.getAsString() + "' -> '" + rewrite + "'"); - sound = rewrite; - } else { - sound = jsonElement.getAsString(); - } - - // windows fix - sound = sound.replaceAll("\\\\", "/"); - - JsonElement newSound = null; - if (jsonElement instanceof JsonObject) { - ((JsonObject) jsonElement).addProperty("name", sound); - newSound = jsonElement; - } else if (jsonElement instanceof JsonPrimitive) { - newSound = new JsonPrimitive(jsonElement.getAsString()); - } - - newSoundsArray.add(newSound); + soundObject.add("sounds", newSoundsArray); } - soundObject.add("sounds", newSoundsArray); - } - newSoundsObject.add(entry.getKey().toLowerCase(), soundObject); + newSoundsObject.add(entry.getKey().toLowerCase(), soundObject); + } } + Files.write(soundsJsonPath, packConverter.getGson() + .toJson(newSoundsObject) + .getBytes(StandardCharsets.UTF_8) + ); } - - Files.write(soundsJsonPath, Collections.singleton(packConverter.getGson().toJson(newSoundsObject)), Charset.forName("UTF-8")); } } diff --git a/src/main/java/net/hypixel/resourcepack/impl/SpacesConverter.java b/src/main/java/net/hypixel/resourcepack/impl/SpacesConverter.java index d66fcaaa..b8d83fed 100644 --- a/src/main/java/net/hypixel/resourcepack/impl/SpacesConverter.java +++ b/src/main/java/net/hypixel/resourcepack/impl/SpacesConverter.java @@ -2,7 +2,6 @@ import net.hypixel.resourcepack.Converter; import net.hypixel.resourcepack.PackConverter; -import net.hypixel.resourcepack.Util; import net.hypixel.resourcepack.pack.Pack; import java.io.IOException; @@ -20,16 +19,20 @@ public void convert(Pack pack) throws IOException { Path assets = pack.getWorkingPath().resolve("assets"); if (!assets.toFile().exists()) return; - Files.walk(assets).forEach(path -> { - if (!path.getFileName().toString().contains(" ")) return; - - String noSpaces = path.getFileName().toString().replaceAll(" ", "_"); - Boolean ret = Util.renameFile(path, noSpaces); - if (ret == null) return; - if (ret && PackConverter.DEBUG) { - System.out.println(" Renamed: " + path.getFileName().toString() + "->" + noSpaces); - } else if (!ret) { - System.err.println(" Failed to rename: " + path.getFileName().toString() + "->" + noSpaces); + Files.walk(assets).forEach(file -> { + String fileName = file.getFileName().toString(); + if (fileName.contains(" ")) { + String noSpaces = fileName.replaceAll(" ", "_"); + try { + if (Files.exists(file)) { + Files.move(file, file.getParent().resolve(noSpaces)); + if (PackConverter.DEBUG) { + System.out.println(" Renamed: " + file.getFileName().toString() + "->" + noSpaces); + } + } + } catch (IOException e) { + System.err.println(" Failed to rename: " + file.getFileName().toString() + "->" + noSpaces); + } } }); } diff --git a/src/main/java/net/hypixel/resourcepack/pack/Pack.java b/src/main/java/net/hypixel/resourcepack/pack/Pack.java index 02357ad9..8c83d67e 100644 --- a/src/main/java/net/hypixel/resourcepack/pack/Pack.java +++ b/src/main/java/net/hypixel/resourcepack/pack/Pack.java @@ -3,17 +3,22 @@ import net.hypixel.resourcepack.Util; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; public class Pack { - protected static final String CONVERTED_SUFFIX = "_converted_1_13"; + private static final String CONVERTED_SUFFIX = "_converted_1_13"; protected Path path; + private final Path workingPath; + private final Path minecraftPath; protected Handler handler; protected Pack(Path path) { this.path = path; + this.workingPath = path.getParent().resolve(getFileName() + CONVERTED_SUFFIX); + this.minecraftPath = workingPath.resolve("assets").resolve("minecraft"); this.handler = createHandler(); } @@ -42,7 +47,11 @@ public Handler getHandler() { } public Path getWorkingPath() { - return path.getParent().resolve(getFileName() + CONVERTED_SUFFIX); + return workingPath; + } + + public Path getMinecraftPath() { + return minecraftPath; } public String getFileName() { @@ -65,13 +74,13 @@ public Handler(Pack pack) { } public void setup() throws IOException { - if (pack.getWorkingPath().toFile().exists()) { + Path workingPath = pack.getWorkingPath(); + if (Files.exists(workingPath)) { System.out.println(" Deleting existing conversion"); - Util.deleteDirectoryAndContents(pack.getWorkingPath()); + Util.deleteDirectoryAndContents(workingPath); } - System.out.println(" Copying existing pack"); - Util.copyDir(pack.getOriginalPath(), pack.getWorkingPath()); + Util.copyDir(pack.getOriginalPath(), workingPath); } public void finish() throws IOException { diff --git a/src/main/java/net/hypixel/resourcepack/pack/ZipPack.java b/src/main/java/net/hypixel/resourcepack/pack/ZipPack.java index d1d35633..28b3930b 100644 --- a/src/main/java/net/hypixel/resourcepack/pack/ZipPack.java +++ b/src/main/java/net/hypixel/resourcepack/pack/ZipPack.java @@ -6,6 +6,7 @@ import net.lingala.zip4j.model.ZipParameters; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; public class ZipPack extends Pack { @@ -36,22 +37,29 @@ public Path getConvertedZipPath() { @Override public void setup() throws IOException { - if (pack.getWorkingPath().toFile().exists()) { + Path workingPath = pack.getWorkingPath(); + if (Files.exists(workingPath)) { System.out.println(" Deleting existing conversion"); - Util.deleteDirectoryAndContents(pack.getWorkingPath()); + Util.deleteDirectoryAndContents(workingPath); } Path convertedZipPath = getConvertedZipPath(); - if (convertedZipPath.toFile().exists()) { + if (Files.exists(convertedZipPath)) { System.out.println(" Deleting existing conversion zip"); - convertedZipPath.toFile().delete(); + try { + Files.delete(convertedZipPath); + } catch (IOException e) { + System.err.println(" Failed to delete existing conversion zip: " + e.getMessage()); + } + } + try { + Files.createDirectory(workingPath); + }catch (IOException e) { + System.err.println(" Failed to create working path: " + e.getMessage()); } - - pack.getWorkingPath().toFile().mkdir(); - try { ZipFile zipFile = new ZipFile(pack.getOriginalPath().toFile()); - zipFile.extractAll(pack.getWorkingPath().toString()); + zipFile.extractAll(workingPath.toString()); } catch (ZipException e) { Util.propagate(e); }