Replace Paper pack bridge with server-only NeoForge resource-pack core
This commit is contained in:
@@ -1,111 +0,0 @@
|
||||
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<String, String> 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<String, String> 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); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
"""Vanilla protocol 776 probe against an isolated, localhost offline test server.
|
||||
|
||||
Does not render resources or claim to be a graphical Minecraft client.
|
||||
Requires network-compression-threshold=-1, timeoutSeconds=10 and retries=0.
|
||||
"""
|
||||
import hashlib
|
||||
import io
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
import uuid
|
||||
import zipfile
|
||||
|
||||
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 25585
|
||||
|
||||
|
||||
def varint(n):
|
||||
out = bytearray()
|
||||
while n > 127:
|
||||
out.append((n & 127) | 128)
|
||||
n >>= 7
|
||||
out.append(n)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def readint(read):
|
||||
n = 0
|
||||
for i in range(5):
|
||||
b = read(1)
|
||||
if not b:
|
||||
raise EOFError()
|
||||
n |= (b[0] & 127) << (7 * i)
|
||||
if b[0] < 128:
|
||||
return n
|
||||
raise ValueError("Bad VarInt")
|
||||
|
||||
|
||||
def string(s):
|
||||
b = s.encode()
|
||||
return varint(len(b)) + b
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, name):
|
||||
self.s = socket.create_connection(("127.0.0.1", PORT), timeout=20)
|
||||
self.f = self.s.makefile("rb")
|
||||
self.send(0, varint(776) + string("127.0.0.1") + struct.pack(">H", PORT) + varint(2))
|
||||
self.send(0, string(name) + uuid.uuid4().bytes)
|
||||
while True:
|
||||
kind, data = self.recv()
|
||||
if kind == 2:
|
||||
break
|
||||
if kind == 4: # Login query: vanilla client does not understand mod channels.
|
||||
query = readint(data.read)
|
||||
self.send(2, varint(query) + b"\x00")
|
||||
elif kind == 0:
|
||||
raise AssertionError("Login disconnected: " + repr(data.read()))
|
||||
elif kind == 3:
|
||||
raise AssertionError("Disable compression in the isolated test server")
|
||||
self.send(3) # login acknowledged
|
||||
|
||||
def send(self, kind, data=b""):
|
||||
payload = varint(kind) + data
|
||||
self.s.sendall(varint(len(payload)) + payload)
|
||||
|
||||
def recv(self):
|
||||
size = readint(self.f.read)
|
||||
data = io.BytesIO(self.f.read(size))
|
||||
return readint(data.read), data
|
||||
|
||||
def config(self):
|
||||
while True:
|
||||
kind, data = self.recv()
|
||||
if kind == 14:
|
||||
self.send(7, varint(0)) # request registry data; no known packs
|
||||
elif kind == 4:
|
||||
self.send(4, data.read())
|
||||
elif kind == 5:
|
||||
self.send(5, data.read())
|
||||
elif kind in (2, 3, 9):
|
||||
return kind, data
|
||||
|
||||
def pack(self):
|
||||
kind, data = self.config()
|
||||
assert kind == 9, (kind, data.read())
|
||||
self.id = data.read(16)
|
||||
self.url = data.read(readint(data.read)).decode()
|
||||
self.sha1 = data.read(readint(data.read)).decode()
|
||||
assert data.read(1) == b"\x01", "Pack must be required"
|
||||
with urllib.request.urlopen(self.url, timeout=10) as response:
|
||||
payload = response.read()
|
||||
assert hashlib.sha1(payload).hexdigest() == self.sha1
|
||||
with zipfile.ZipFile(io.BytesIO(payload)) as archive:
|
||||
assert "pack.mcmeta" in archive.namelist()
|
||||
|
||||
def respond(self, action, identity=None):
|
||||
self.send(6, (identity or self.id) + varint(action))
|
||||
|
||||
def close(self):
|
||||
self.f.close()
|
||||
self.s.close()
|
||||
|
||||
|
||||
for mode, action in [("declined", 1), ("failed_download", 2), ("failed_reload", 7), ("loaded", 0), ("accepted_timeout", 3), ("wrong_uuid", None)]:
|
||||
client = Client("Astra_" + mode[:10])
|
||||
try:
|
||||
client.pack()
|
||||
if action is not None:
|
||||
client.respond(action)
|
||||
else:
|
||||
client.respond(0, uuid.uuid4().bytes)
|
||||
start = time.monotonic()
|
||||
kind, data = client.config()
|
||||
assert kind == (3 if mode == "loaded" else 2), (mode, kind, data.read())
|
||||
if mode in ("accepted_timeout", "wrong_uuid"):
|
||||
assert time.monotonic() - start >= 8, "Unexpected early gate completion"
|
||||
print("PASS", mode, "HTTP SHA-1 verified;", "configuration finished" if kind == 3 else "disconnected before world", flush=True)
|
||||
finally:
|
||||
client.close()
|
||||
Reference in New Issue
Block a user