From 17e4ead5620eb38323e4aa6084029f0e8f803a30 Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Tue, 24 Sep 2024 00:59:24 -0500 Subject: [PATCH] Allow mergeTools to produce a merged jar with fabric-loader `@Environment` annotation type (#4) Use of fabric annotations can be enabled by passing `-ann FABRIC` --- .../mergetool/AnnotationVersion.java | 28 ++++++++++++++----- .../neoforged/mergetool/ConsoleMerger.java | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/neoforged/mergetool/AnnotationVersion.java b/src/main/java/net/neoforged/mergetool/AnnotationVersion.java index 73fd9b9..0e61db8 100644 --- a/src/main/java/net/neoforged/mergetool/AnnotationVersion.java +++ b/src/main/java/net/neoforged/mergetool/AnnotationVersion.java @@ -34,7 +34,8 @@ public enum AnnotationVersion { CPW(cpw.mods.fml.relauncher.SideOnly.class, cpw.mods.fml.relauncher.Side.class, "CLIENT", "SERVER"), NMF(net.neoforged.fml.relauncher.SideOnly.class, net.neoforged.fml.relauncher.Side.class, "CLIENT", "SERVER"), - API(OnlyIn.class, Dist.class, OnlyIns.class, "_interface", "CLIENT", "DEDICATED_SERVER"); + API(OnlyIn.class, Dist.class, OnlyIns.class, "_interface", "CLIENT", "DEDICATED_SERVER"), + FABRIC("Lnet/fabricmc/api/Environment;", "Lnet/fabricmc/api/EnvType;", "CLIENT", "SERVER", false); private final String holder; private final String value; @@ -42,6 +43,7 @@ public enum AnnotationVersion private final String interface_key; private final String client; private final String server; + private final boolean runtimeRetention; private static final MinecraftVersion MC_8 = MinecraftVersion.from("14w02a"); private static final MinecraftVersion MC_13 = MinecraftVersion.from("17w43a"); @@ -50,6 +52,17 @@ private AnnotationVersion(Class holder, Class value, String client, String { this(holder, value, null, null, client, server); } + + private AnnotationVersion(String holder, String value, String client, String server, boolean runtimeRetention) + { + this.holder = holder; + this.value = value; + this.repeatable = null; + this.interface_key = null; + this.client = client; + this.server = server; + this.runtimeRetention = runtimeRetention; + } private AnnotationVersion(Class holder, Class value, Class repeatable, String interface_key, String client, String server) { @@ -59,6 +72,7 @@ private AnnotationVersion(Class holder, Class value, Class repeatable, this.interface_key = interface_key; this.client = client; this.server = server; + this.runtimeRetention = true; } public static AnnotationVersion fromVersion(String v) @@ -98,28 +112,28 @@ public void add(ClassVisitor cls, List clientOnly, List serverOn if (clientOnly.size() + serverOnly.size() == 1) { if (clientOnly.size() == 1) - add(cls.visitAnnotation(this.holder, true), true).visit(interface_key, Type.getObjectType(clientOnly.get(0))); + add(cls.visitAnnotation(this.holder, this.runtimeRetention), true).visit(interface_key, Type.getObjectType(clientOnly.get(0))); else - add(cls.visitAnnotation(this.holder, true), false).visit(interface_key, Type.getObjectType(serverOnly.get(0))); + add(cls.visitAnnotation(this.holder, this.runtimeRetention), false).visit(interface_key, Type.getObjectType(serverOnly.get(0))); } else { - AnnotationVisitor rep = cls.visitAnnotation(this.holder, true).visitArray("value"); + AnnotationVisitor rep = cls.visitAnnotation(this.holder, this.runtimeRetention).visitArray("value"); clientOnly.forEach(intf -> add(rep.visitAnnotation(null, this.repeatable), true).visit(interface_key, Type.getObjectType(intf))); serverOnly.forEach(intf -> add(rep.visitAnnotation(null, this.repeatable), false).visit(interface_key, Type.getObjectType(intf))); } } public void add(ClassVisitor cls, boolean isClientOnly) { - add(cls.visitAnnotation(this.holder, true), isClientOnly); + add(cls.visitAnnotation(this.holder, this.runtimeRetention), isClientOnly); } public void add(FieldVisitor fld, boolean isClientOnly) { - add(fld.visitAnnotation(this.holder, true), isClientOnly); + add(fld.visitAnnotation(this.holder, this.runtimeRetention), isClientOnly); } public void add(MethodVisitor mtd, boolean isClientOnly) { - add(mtd.visitAnnotation(this.holder, true), isClientOnly); + add(mtd.visitAnnotation(this.holder, this.runtimeRetention), isClientOnly); } private AnnotationVisitor add(AnnotationVisitor ann, boolean isClientOnly) { diff --git a/src/main/java/net/neoforged/mergetool/ConsoleMerger.java b/src/main/java/net/neoforged/mergetool/ConsoleMerger.java index 905b00a..99a8823 100644 --- a/src/main/java/net/neoforged/mergetool/ConsoleMerger.java +++ b/src/main/java/net/neoforged/mergetool/ConsoleMerger.java @@ -133,7 +133,7 @@ private static void merge(String[] args) } catch (OptionException e) { - System.out.println("Usage: ConsoleMerger --merge --client --server --output [--ann CPW|NMF|API] [--keep-data] [--keep-meta]"); + System.out.println("Usage: ConsoleMerger --merge --client --server --output [--ann CPW|NMF|API|FABRIC] [--keep-data] [--keep-meta]"); e.printStackTrace(); } }