Include shared profile protection for Lobby Builder

This commit is contained in:
unimatrixgit 2026-12-05 14:33:18 +01:00
commit f4f8312f71
2 changed files with 294 additions and 0 deletions

View file

@ -0,0 +1,75 @@
import java.nio.file.Files;
import java.nio.file.Path;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.FrameNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.VarInsnNode;
class BuilderSharedProfilesOverlay {
private static final String OWNER = "com/example/dunelobby/BuilderTransfer";
private static final String GUARD = "com/example/dunelobby/BuilderSharedProfiles";
private static final String PLAYER = "Lorg/bukkit/entity/Player;";
private static final String PATH = "Ljava/nio/file/Path;";
public static void main(String[] args) throws Exception {
if (args.length != 2) throw new IllegalArgumentException("Expected baseline and output classes");
var reader = new ClassReader(Files.readAllBytes(Path.of(args[0])));
var retained = new ClassNode();
reader.accept(retained, 0);
if (!OWNER.equals(retained.name)) throw new IllegalArgumentException("Unexpected legacy Builder implementation");
var restore = retained.methods.stream().filter(m -> m.name.equals("restore") && m.desc.equals("(" + PLAYER + "Z)V"))
.findFirst().orElseThrow();
var transfer = retained.methods.stream().filter(m -> m.name.equals("transfer") && m.desc.equals("(" + PLAYER + ")V"))
.findFirst().orElseThrow();
var recover = snapshot();
recover.add(new VarInsnNode(Opcodes.ILOAD, 2));
recover.add(new VarInsnNode(Opcodes.ALOAD, 0));
recover.add(new VarInsnNode(Opcodes.ALOAD, 1));
recover.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "org/bukkit/entity/Player", "getWorld",
"()Lorg/bukkit/World;", true));
recover.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, OWNER, "inBuilder", "(Lorg/bukkit/World;)Z", false));
recover.add(new VarInsnNode(Opcodes.ALOAD, 0));
recover.add(new FieldInsnNode(Opcodes.GETFIELD, OWNER, "returning", "Ljava/util/Set;"));
recover.add(new MethodInsnNode(Opcodes.INVOKESTATIC, GUARD, "restore",
"(" + PLAYER + PATH + "ZZLjava/util/Set;)Z", false));
finish(recover, Opcodes.IFEQ);
restore.instructions.insert(recover);
restore.maxStack = Math.max(restore.maxStack, 5);
var enter = snapshot();
enter.add(new MethodInsnNode(Opcodes.INVOKESTATIC, GUARD, "allowTransfer", "(" + PLAYER + PATH + ")Z", false));
finish(enter, Opcodes.IFNE);
transfer.instructions.insert(enter);
transfer.maxStack = Math.max(transfer.maxStack, 3);
var writer = new ClassWriter(reader, 0);
retained.accept(writer);
Files.write(Path.of(args[1]), writer.toByteArray());
}
private static InsnList snapshot() {
var code = new InsnList();
code.add(new VarInsnNode(Opcodes.ALOAD, 1));
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
code.add(new VarInsnNode(Opcodes.ALOAD, 1));
code.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, OWNER, "snapshot", "(" + PLAYER + ")" + PATH, false));
return code;
}
private static void finish(InsnList code, int jump) {
var old = new LabelNode();
code.add(new JumpInsnNode(jump, old));
code.add(new InsnNode(Opcodes.RETURN));
code.add(old);
code.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
}
}

View file

@ -0,0 +1,46 @@
"""Graft the shared inventory guard onto the retained Lobby Builder JAR."""
import argparse
import os
from pathlib import Path
import subprocess
from tempfile import TemporaryDirectory
from plugin_wild_update import digest, read_archive, write_archive
PREFIX = "com/example/dunelobby/"
BUILDER = PREFIX + "BuilderTransfer.class"
GUARD = PREFIX + "BuilderSharedProfiles.class"
BASELINE = "18f8878b18f249ff97728542a63effedb1d19f473168b9ab7ec8ef8f11c25538"
def assemble(baseline, donor, donor_sha, output, classpath):
existing = baseline.read_bytes()
compiled = donor.read_bytes()
if digest(existing) != BASELINE or digest(compiled) != donor_sha:
raise ValueError("Unexpected Builder baseline or donor checksum")
archive = read_archive(existing)
payload = read_archive(compiled)
if GUARD in archive:
raise ValueError("Builder already contains the shared profile guard")
java_home = os.environ.get("JAVA_HOME")
executable = "java.exe" if os.name == "nt" else "java"
java = str(Path(java_home) / "bin" / executable) if java_home else executable
helper = Path(__file__).with_name("BuilderSharedProfilesOverlay.java")
with TemporaryDirectory() as temp:
old, new = (Path(temp) / name for name in ("retained.class", "guarded.class"))
old.write_bytes(archive[BUILDER])
subprocess.run([java, "--class-path", classpath, str(helper), str(old), str(new)], check=True)
archive[BUILDER] = new.read_bytes()
archive[GUARD] = payload[GUARD]
with output.open("xb") as candidate:
candidate.write(write_archive(archive))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("baseline", type=Path)
parser.add_argument("donor", type=Path)
parser.add_argument("donor_sha")
parser.add_argument("output", type=Path)
parser.add_argument("classpath", help="ASM core and tree JARs")
assemble(**vars(parser.parse_args()))