Skip to content

Cleanup & Proper usage of NIO #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/net/hypixel/resourcepack/PackConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
36 changes: 17 additions & 19 deletions src/main/java/net/hypixel/resourcepack/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -68,15 +74,7 @@ public static JsonObject readJson(Gson gson, Path path) throws IOException {

public static <T> T readJson(Gson gson, Path path, Class<T> 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) {
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/net/hypixel/resourcepack/impl/AnimationConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 -> {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 -> {
Expand Down Expand Up @@ -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);
Expand Down
48 changes: 24 additions & 24 deletions src/main/java/net/hypixel/resourcepack/impl/MapIconConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/net/hypixel/resourcepack/impl/ModelConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
79 changes: 51 additions & 28 deletions src/main/java/net/hypixel/resourcepack/impl/NameConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
});
Expand Down
Loading