Package scoped station and iron door overlays for retained variants
This commit is contained in:
parent
0e790bd154
commit
2c6aedaf8a
4 changed files with 222 additions and 6 deletions
51
.forgejo/workflows/station-origins-doors.yml
Normal file
51
.forgejo/workflows/station-origins-doors.yml
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
name: Station Origins and Iron Doors Overlay
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
branches: ['fix/station-origins-*']
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
station-and-doors:
|
||||||
|
runs-on: dune-ci
|
||||||
|
steps:
|
||||||
|
- name: Check out exact revision
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
git init .
|
||||||
|
git -c http.extraheader="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$REPO_TOKEN" | base64 -w0)" fetch --depth=1 https://www.dunerealms.com/git/unimatrix/plugins.git "$GITHUB_SHA"
|
||||||
|
git checkout --detach FETCH_HEAD
|
||||||
|
- uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
distribution: temurin
|
||||||
|
java-version: '21'
|
||||||
|
- uses: gradle/actions/setup-gradle@v4
|
||||||
|
with:
|
||||||
|
gradle-version: '9.2.1'
|
||||||
|
- name: Test both plugins and extract exact overlay donors
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
gradle --no-daemon -p JarvisConnector test jar
|
||||||
|
gradle --no-daemon -p DuneEss test jar
|
||||||
|
mkdir station-and-doors
|
||||||
|
PYTHONPATH=tools python3 - <<'PY'
|
||||||
|
from pathlib import Path
|
||||||
|
from plugin_station_and_doors import donor_archive
|
||||||
|
output = Path("station-and-doors")
|
||||||
|
for role, jar in (
|
||||||
|
("station", "JarvisConnector/build/libs/jarvistranslator.jar"),
|
||||||
|
("ess", "DuneEss/build/libs/DuneRealms-Essentials.jar"),
|
||||||
|
):
|
||||||
|
(output / (role + "-donor.zip")).write_bytes(donor_archive(role, Path(jar)))
|
||||||
|
PY
|
||||||
|
cp tools/{StationAndDoorOverlay.java,plugin_station_and_doors.py,plugin_wild_update.py} station-and-doors/
|
||||||
|
printf '%s\n' "$GITHUB_SHA" > station-and-doors/SOURCE_COMMIT
|
||||||
|
(cd station-and-doors && sha256sum -- *.zip *.py *.java SOURCE_COMMIT > SHA256SUMS)
|
||||||
|
- uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: DuneRealms-Station-Origins-Iron-Doors
|
||||||
|
path: station-and-doors
|
||||||
|
if-no-files-found: error
|
||||||
|
|
@ -51,12 +51,9 @@ final class ItemOriginRemainders implements Listener {
|
||||||
|
|
||||||
static boolean bucketPickup(Material held, Material source, Material result) {
|
static boolean bucketPickup(Material held, Material source, Material result) {
|
||||||
if (held != Material.BUCKET) return false;
|
if (held != Material.BUCKET) return false;
|
||||||
return switch (result) {
|
return result == Material.LAVA_BUCKET && (source == Material.LAVA || source == Material.LAVA_CAULDRON)
|
||||||
case LAVA_BUCKET -> source == Material.LAVA || source == Material.LAVA_CAULDRON;
|
|| result == Material.WATER_BUCKET && (source == Material.WATER || source == Material.WATER_CAULDRON)
|
||||||
case WATER_BUCKET -> source == Material.WATER || source == Material.WATER_CAULDRON;
|
|| result == Material.POWDER_SNOW_BUCKET && (source == Material.POWDER_SNOW || source == Material.POWDER_SNOW_CAULDRON);
|
||||||
case POWDER_SNOW_BUCKET -> source == Material.POWDER_SNOW || source == Material.POWDER_SNOW_CAULDRON;
|
|
||||||
default -> false;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static String pickupOrigin(String previous, String world) {
|
static String pickupOrigin(String previous, String world) {
|
||||||
|
|
|
||||||
88
tools/StationAndDoorOverlay.java
Normal file
88
tools/StationAndDoorOverlay.java
Normal file
|
|
@ -0,0 +1,88 @@
|
||||||
|
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.*;
|
||||||
|
|
||||||
|
final class StationAndDoorOverlay implements Opcodes {
|
||||||
|
private static final String JARVIS = "com/dunerealms/jarvis/";
|
||||||
|
private static final String ESS = "com/example/duneess/";
|
||||||
|
private static final String BUKKIT = "org/bukkit/Bukkit";
|
||||||
|
private static final String MANAGER = "org/bukkit/plugin/PluginManager";
|
||||||
|
private static final String REGISTER = "(Lorg/bukkit/event/Listener;Lorg/bukkit/plugin/Plugin;)V";
|
||||||
|
private static final String MATERIAL = "Lorg/bukkit/Material;";
|
||||||
|
private static final String PICKUP = "(" + MATERIAL.repeat(3) + ")Z";
|
||||||
|
private static final String FILL = "(Lorg/bukkit/event/player/PlayerBucketFillEvent;)V";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
if (args.length != 5) throw new IllegalArgumentException("Expected role, entry, retained, donor and output");
|
||||||
|
String role = args[0], entry = args[1];
|
||||||
|
var reader = new ClassReader(Files.readAllBytes(Path.of(args[2])));
|
||||||
|
var retained = read(reader);
|
||||||
|
var donor = read(new ClassReader(Files.readAllBytes(Path.of(args[3]))));
|
||||||
|
if (!retained.name.equals(donor.name) || !retained.name.equals(entry)) {
|
||||||
|
throw new IllegalArgumentException("Unexpected retained or donor class: " + entry);
|
||||||
|
}
|
||||||
|
if (role.equals("ess")) {
|
||||||
|
if (!entry.equals(ESS + "DuneEss")) throw new IllegalArgumentException("Unexpected Essentials class");
|
||||||
|
registerAfterExisting(method(retained, "onEnable", "()V"), ESS + "IronDoorInteraction");
|
||||||
|
} else if (role.equals("station")) {
|
||||||
|
switch (entry.substring(JARVIS.length())) {
|
||||||
|
case "WorkstationOrigins" -> registerAfterExisting(method(retained, "<init>",
|
||||||
|
"(Lorg/bukkit/plugin/Plugin;Ljava/util/function/BiFunction;)V"), JARVIS + "ComposterOrigins");
|
||||||
|
case "ItemOrigin" -> replace(retained, donor, "label", "label", "(Ljava/lang/String;)Ljava/lang/String;");
|
||||||
|
case "ArrakisBrewing" -> replace(retained, donor, "brewed", "brewed", "(Lorg/bukkit/event/inventory/BrewEvent;)V");
|
||||||
|
case "ItemOriginRemainders" -> {
|
||||||
|
replace(retained, donor, "fillLava", "fillBucket", FILL);
|
||||||
|
replace(retained, donor, "burn", "burn", "(Lorg/bukkit/event/inventory/FurnaceBurnEvent;)V");
|
||||||
|
// Retain the old package-private predicate for callers compiled against the baseline.
|
||||||
|
if (retained.methods.stream().anyMatch(m -> m.name.equals("bucketPickup")))
|
||||||
|
throw new IllegalArgumentException("Bucket pickup already installed");
|
||||||
|
retained.methods.add(method(donor, "bucketPickup", PICKUP));
|
||||||
|
}
|
||||||
|
default -> throw new IllegalArgumentException("Unexpected station class: " + entry);
|
||||||
|
}
|
||||||
|
} else throw new IllegalArgumentException("Unknown overlay role: " + role);
|
||||||
|
var writer = new ClassWriter(reader, 0);
|
||||||
|
retained.accept(writer);
|
||||||
|
Files.write(Path.of(args[4]), writer.toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MethodNode method(ClassNode node, String name, String descriptor) {
|
||||||
|
return node.methods.stream().filter(m -> m.name.equals(name) && m.desc.equals(descriptor)).findFirst().orElseThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ClassNode read(ClassReader reader) {
|
||||||
|
var node = new ClassNode();
|
||||||
|
reader.accept(node, 0);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void replace(ClassNode retained, ClassNode donor, String oldName, String newName, String descriptor) {
|
||||||
|
var original = method(retained, oldName, descriptor);
|
||||||
|
var next = method(donor, newName, descriptor);
|
||||||
|
retained.methods.set(retained.methods.indexOf(original), next);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void registerAfterExisting(MethodNode method, String listener) {
|
||||||
|
MethodInsnNode registration = null;
|
||||||
|
for (var instruction : method.instructions) {
|
||||||
|
if (instruction instanceof MethodInsnNode call && call.owner.equals(MANAGER)
|
||||||
|
&& call.name.equals("registerEvents") && call.desc.equals(REGISTER)) {
|
||||||
|
if (registration != null) throw new IllegalArgumentException("Ambiguous existing listener registration");
|
||||||
|
registration = call;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (registration == null) throw new IllegalArgumentException("Retained listener registration missing");
|
||||||
|
var code = new InsnList();
|
||||||
|
code.add(new MethodInsnNode(INVOKESTATIC, BUKKIT, "getPluginManager", "()L" + MANAGER + ";", false));
|
||||||
|
code.add(new TypeInsnNode(NEW, listener));
|
||||||
|
code.add(new InsnNode(DUP));
|
||||||
|
code.add(new MethodInsnNode(INVOKESPECIAL, listener, "<init>", "()V", false));
|
||||||
|
code.add(new VarInsnNode(ALOAD, method.name.equals("<init>") ? 1 : 0));
|
||||||
|
code.add(new MethodInsnNode(INVOKEINTERFACE, MANAGER, "registerEvents", REGISTER, true));
|
||||||
|
method.instructions.insert(registration, code);
|
||||||
|
method.maxStack = Math.max(method.maxStack, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
80
tools/plugin_station_and_doors.py
Normal file
80
tools/plugin_station_and_doors.py
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
"""Build exact station-origin and Essentials door overlays against retained JARs."""
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
|
from plugin_wild_update import digest, read_archive, write_archive
|
||||||
|
|
||||||
|
JARVIS = "com/dunerealms/jarvis/"
|
||||||
|
ESS = "com/example/duneess/"
|
||||||
|
SCOPES = {
|
||||||
|
"station": (
|
||||||
|
tuple(JARVIS + name for name in ("WorkstationOrigins", "ItemOrigin", "ArrakisBrewing", "ItemOriginRemainders")),
|
||||||
|
(JARVIS + "ComposterOrigins",),
|
||||||
|
{
|
||||||
|
"86af7e1b6127dbb577cf9223b9b850a613c1a56e42fcfcac53d4dbad367b2703",
|
||||||
|
"8facb13e8049a73b8072eb1af5053ec3255c2ae22d28b37419ad5cb34c24b31c",
|
||||||
|
"adc66adcfc1db4ead4de1f5b0d4863822158f23c0b612c669b86fcd1f9ac287a",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
"ess": (
|
||||||
|
(ESS + "DuneEss",),
|
||||||
|
(ESS + "IronDoorInteraction",),
|
||||||
|
{
|
||||||
|
"35d6bc1946574159defd7e296d1f037c5d0be1ee21bc767801380ed9b7d5a214",
|
||||||
|
"2e4de7e769c47a036fa9f6c5f175d966aa38c336f5e537d60b07cdd1c267eb1e",
|
||||||
|
"fd9f633b1d0a89a265a6dd91cfd863ca7c1901652030ee521941d40eb0182954",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def donor_archive(role, jar):
|
||||||
|
changed, added, _ = SCOPES[role]
|
||||||
|
source = read_archive(jar.read_bytes())
|
||||||
|
return write_archive({name + ".class": source[name + ".class"] for name in (*changed, *added)})
|
||||||
|
|
||||||
|
|
||||||
|
def assemble(role, baseline, baseline_sha, donor, donor_sha, output, classpath):
|
||||||
|
changed, added, approved = SCOPES[role]
|
||||||
|
existing = baseline.read_bytes()
|
||||||
|
compiled = donor.read_bytes()
|
||||||
|
if digest(existing) != baseline_sha or baseline_sha not in approved or digest(compiled) != donor_sha:
|
||||||
|
raise ValueError("Unexpected baseline or donor checksum")
|
||||||
|
retained = read_archive(existing)
|
||||||
|
source = read_archive(compiled)
|
||||||
|
if set(source) != {name + ".class" for name in (*changed, *added)}:
|
||||||
|
raise ValueError("Unexpected source class scope")
|
||||||
|
if any(name + ".class" in retained for name in added):
|
||||||
|
raise ValueError("The retained archive already contains the new feature")
|
||||||
|
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("StationAndDoorOverlay.java")
|
||||||
|
with TemporaryDirectory() as temporary:
|
||||||
|
root = Path(temporary)
|
||||||
|
for name in changed:
|
||||||
|
path = name + ".class"
|
||||||
|
old, new, merged = (root / file for file in ("retained.class", "donor.class", "merged.class"))
|
||||||
|
old.write_bytes(retained[path])
|
||||||
|
new.write_bytes(source[path])
|
||||||
|
subprocess.run([java, "--class-path", classpath, str(helper), role, name, str(old), str(new), str(merged)], check=True)
|
||||||
|
retained[path] = merged.read_bytes()
|
||||||
|
for name in added:
|
||||||
|
retained[name + ".class"] = source[name + ".class"]
|
||||||
|
with output.open("xb") as candidate:
|
||||||
|
candidate.write(write_archive(retained))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument("role", choices=SCOPES)
|
||||||
|
parser.add_argument("baseline", type=Path)
|
||||||
|
parser.add_argument("baseline_sha")
|
||||||
|
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()))
|
||||||
Loading…
Reference in a new issue

Staff