import java.nio.file.*; import java.util.*; import java.util.zip.*; import java.io.*; import java.nio.charset.StandardCharsets; import com.google.gson.*; import org.lwjgl.stb.STBImage; import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryUtil; /** Checks merged resources, including ItemsAdder's texture aliases and lossless PNG optimization. */ class VerifySharedPack { public static void main(String[] args) throws Exception { int checked = 0; try (var pack = new ZipFile(args[0])) { Map aliases = new HashMap<>(); for (var entries = pack.entries(); entries.hasMoreElements();) { var entry = entries.nextElement(); if (!entry.getName().contains("/atlases/") || !entry.getName().endsWith(".json")) continue; try (var in = pack.getInputStream(entry)) { var json = JsonParser.parseString(new String(in.readAllBytes(), StandardCharsets.UTF_8)).getAsJsonObject(); for (var source : json.getAsJsonArray("sources")) { var object = source.getAsJsonObject(); if (object.has("sprite") && object.has("resource")) aliases.put(object.get("sprite").getAsString(), object.get("resource").getAsString()); } } } for (int i = 1; i < args.length; i++) { try (var jar = new ZipFile(args[i])) { int count = 0; for (var entries = jar.entries(); entries.hasMoreElements();) { var entry = entries.nextElement(); if (!entry.getName().startsWith("astra-itemsadder/")) continue; int offset = entry.getName().indexOf("/resourcepack/assets/"); if (offset < 0 || entry.isDirectory()) continue; String name = entry.getName().substring(offset + "/resourcepack/".length()); var actual = pack.getEntry(name); if (actual == null) throw new AssertionError("Missing asset: " + name); try (var expectedBytes = jar.getInputStream(entry); var actualBytes = pack.getInputStream(actual)) { byte[] expected = expectedBytes.readAllBytes(), received = actualBytes.readAllBytes(); if (!Arrays.equals(expected, received)) { if (name.endsWith(".json")) { var a = JsonParser.parseString(new String(expected, StandardCharsets.UTF_8)); var b = normalize(JsonParser.parseString(new String(received, StandardCharsets.UTF_8)), aliases); // ItemsAdder's configured fix_dark_in_inventory adds front lighting // to parentless models; it does not change their geometry or tints. if (name.contains("/models/") && a.isJsonObject() && b.isJsonObject() && !a.getAsJsonObject().has("gui_light") && new JsonPrimitive("front").equals(b.getAsJsonObject().get("gui_light"))) b.getAsJsonObject().remove("gui_light"); if (!a.equals(b)) throw new AssertionError("Changed JSON semantics: " + name + " " + diff(a, b, "")); } else if (name.endsWith(".png")) { var a = pixels(expected); var b = pixels(received); if (a.width != b.width || a.height != b.height || !Arrays.equals(a.rgba, b.rgba)) throw new AssertionError("Changed texture pixels: " + name); } else throw new AssertionError("Changed asset: " + name); } } count++; } System.out.println("PASS " + Path.of(args[i]).getFileName() + ": " + count + " equivalent client assets"); checked += count; } } if (checked == 0) throw new AssertionError("No assets checked"); if (pack.getEntry("pack.mcmeta") == null || pack.getEntry("assets/astrapacks/lang/en_us.json") == null) throw new AssertionError("Missing shared pack metadata"); } System.out.println("PASS shared pack: " + checked + " assets; geometry, tints, fonts and texture pixels preserved"); } static JsonElement normalize(JsonElement value, Map aliases) { if (value.isJsonObject()) { var result = new JsonObject(); value.getAsJsonObject().entrySet().forEach(e -> result.add(e.getKey(), normalize(e.getValue(), aliases))); return result; } if (value.isJsonArray()) { var result = new JsonArray(); value.getAsJsonArray().forEach(e -> result.add(normalize(e, aliases))); return result; } if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString() && aliases.containsKey(value.getAsString())) return new JsonPrimitive(aliases.get(value.getAsString())); return value; } static String diff(JsonElement a, JsonElement b, String path) { if (a.isJsonObject() && b.isJsonObject()) { var keys = new TreeSet<>(a.getAsJsonObject().keySet()); keys.addAll(b.getAsJsonObject().keySet()); for (String key : keys) { var x = a.getAsJsonObject().get(key); var y = b.getAsJsonObject().get(key); if (x == null || y == null) return path + "/" + key + " key added/removed"; if (!x.equals(y)) return diff(x, y, path + "/" + key); } } String text = path + ": " + a + " -> " + b; return text.substring(0, Math.min(200, text.length())); } record Pixels(int width, int height, byte[] rgba) {} static Pixels pixels(byte[] bytes) { // Minecraft uses stb_image, which supports the PNG checksum protection used by ItemsAdder. var input = MemoryUtil.memAlloc(bytes.length); input.put(bytes).flip(); try (var stack = MemoryStack.stackPush()) { var width = stack.mallocInt(1); var height = stack.mallocInt(1); var channels = stack.mallocInt(1); var image = STBImage.stbi_load_from_memory(input, width, height, channels, 4); if (image == null) throw new AssertionError("Minecraft PNG decoder: " + STBImage.stbi_failure_reason()); try { byte[] rgba = new byte[width.get(0) * height.get(0) * 4]; image.get(rgba); return new Pixels(width.get(0), height.get(0), rgba); } finally { image.rewind(); STBImage.stbi_image_free(image); } } finally { MemoryUtil.memFree(input); } } }