From d00a2ad8fe35079468d7101f3194c8e3b5b0efe4 Mon Sep 17 00:00:00 2001 From: Tom Martin Date: Sat, 20 Jan 2024 13:58:08 +0000 Subject: [PATCH] Support class/string targets in dasm annotations --- build.gradle | 2 +- .../cubicchunks/mixin/ASMConfigPlugin.java | 24 ++++--- .../cubicchunks/mixin/CopyFrom.java | 7 ++ .../cubicchunks/mixin/TransformFrom.java | 2 +- .../cubicchunks/mixin/TransformFromClass.java | 2 +- .../mixin/core/common/TestMixin.java | 22 ------ .../world/level/cube/CubeAccess.java | 69 ++++++++++--------- .../resources/cubicchunks.mixins.core.json | 7 +- 8 files changed, 61 insertions(+), 74 deletions(-) create mode 100644 src/main/java/io/github/opencubicchunks/cubicchunks/mixin/CopyFrom.java delete mode 100644 src/main/java/io/github/opencubicchunks/cubicchunks/mixin/core/common/TestMixin.java diff --git a/build.gradle b/build.gradle index 964f1614..8d5f7b7f 100644 --- a/build.gradle +++ b/build.gradle @@ -213,7 +213,7 @@ dependencies { targetConfiguration = "testArchivesOutput" } - libraries("com.github.OpenCubicChunks:dasm:9d5aa6b061") { + libraries("com.github.OpenCubicChunks:dasm:da8944f034") { transitive = false } libraries("io.github.opencubicchunks:regionlib:0.63.0-SNAPSHOT") diff --git a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/ASMConfigPlugin.java b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/ASMConfigPlugin.java index 2ff49fd9..a3c54636 100644 --- a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/ASMConfigPlugin.java +++ b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/ASMConfigPlugin.java @@ -10,7 +10,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -234,10 +233,7 @@ private static void buildClassTarget(ClassNode targetClass, RedirectsParser.Clas String name = (String) values.get(i); Object value = values.get(i + 1); if (name.equals("value")) { - Type val = (Type) value; - if (!Objects.equals(val, Type.getObjectType(Object.class.getName()))) { // Special case the default - srcClass = val; - } + srcClass = parseCopyFromAnnotation((AnnotationNode) value); } else if (name.equals("stage")) { var parts = ((String[]) value); requestedStage = TransformFrom.ApplicationStage.valueOf(parts[1]); @@ -283,12 +279,7 @@ private static void buildClassTarget(ClassNode targetClass, RedirectsParser.Clas var parts = ((String[]) value); requestedStage = TransformFrom.ApplicationStage.valueOf(parts[1]); } - case "copyFrom" -> { - var val = (Type) value; - if (!Objects.equals(val, Type.getObjectType(Object.class.getName()))) { // Special case the default - srcOwner = val; - } - } + case "copyFrom" -> srcOwner = parseCopyFromAnnotation((AnnotationNode) value); } } if (stage != requestedStage) { @@ -324,6 +315,17 @@ private static void buildClassTarget(ClassNode targetClass, RedirectsParser.Clas } } + private static Type parseCopyFromAnnotation(AnnotationNode copyFromAnnotation) { + assert copyFromAnnotation.values.size() == 2 : "CopyFrom annotation has multiple targeting fields"; + + if ((copyFromAnnotation.values.get(0)).equals("clazz")) { + return (Type) copyFromAnnotation.values.get(1); + } else if ((copyFromAnnotation.values.get(0)).equals("string")) { + return Type.getObjectType((String) copyFromAnnotation.values.get(1)); + } + return Type.getType(Object.class); + } + private static String parseMethodDescriptor(AnnotationNode ann) { if (ann == null) { return null; diff --git a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/CopyFrom.java b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/CopyFrom.java new file mode 100644 index 00000000..c38b7952 --- /dev/null +++ b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/CopyFrom.java @@ -0,0 +1,7 @@ +package io.github.opencubicchunks.cubicchunks.mixin; + +public @interface CopyFrom { + Class clazz() default Object.class; + + String string() default ""; +} diff --git a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/TransformFrom.java b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/TransformFrom.java index 06cacdd3..02f38d68 100644 --- a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/TransformFrom.java +++ b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/TransformFrom.java @@ -11,7 +11,7 @@ String value(); - Class copyFrom() default Object.class; + CopyFrom copyFrom() default @CopyFrom(); Signature signature() default @Signature(fromString = true); diff --git a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/TransformFromClass.java b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/TransformFromClass.java index 0de952eb..2d849285 100644 --- a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/TransformFromClass.java +++ b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/TransformFromClass.java @@ -1,7 +1,7 @@ package io.github.opencubicchunks.cubicchunks.mixin; public @interface TransformFromClass { - Class value(); + CopyFrom value(); TransformFrom.ApplicationStage stage() default TransformFrom.ApplicationStage.PRE_APPLY; } diff --git a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/core/common/TestMixin.java b/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/core/common/TestMixin.java deleted file mode 100644 index 6fba1ba2..00000000 --- a/src/main/java/io/github/opencubicchunks/cubicchunks/mixin/core/common/TestMixin.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.github.opencubicchunks.cubicchunks.mixin.core.common; - -import io.github.opencubicchunks.cc_core.api.CubePos; -import io.github.opencubicchunks.cubicchunks.mixin.DasmRedirect; -import io.github.opencubicchunks.cubicchunks.mixin.TransformFrom; -import io.github.opencubicchunks.cubicchunks.mixin.TransformFrom.Signature; -import net.minecraft.server.level.ChunkMap; -import net.minecraft.world.level.ChunkPos; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(ChunkMap.class) -@DasmRedirect -public abstract class TestMixin { - @TransformFrom(value = "getChunkDebugData", signature = @Signature( - args = { ChunkPos.class }, - ret = String.class - )) - public abstract String getCubeDebugData(CubePos pos); - - @TransformFrom(value = "markPositionReplaceable(Lnet/minecraft/world/level/ChunkPos;)V") - public abstract void markCubePositionReplaceable(CubePos pos); -} diff --git a/src/main/java/io/github/opencubicchunks/cubicchunks/world/level/cube/CubeAccess.java b/src/main/java/io/github/opencubicchunks/cubicchunks/world/level/cube/CubeAccess.java index 3ec90bb4..2a20d850 100644 --- a/src/main/java/io/github/opencubicchunks/cubicchunks/world/level/cube/CubeAccess.java +++ b/src/main/java/io/github/opencubicchunks/cubicchunks/world/level/cube/CubeAccess.java @@ -13,6 +13,7 @@ import io.github.opencubicchunks.cc_core.api.CubicConstants; import io.github.opencubicchunks.cc_core.utils.Coords; import io.github.opencubicchunks.cubicchunks.CubicChunks; +import io.github.opencubicchunks.cubicchunks.mixin.CopyFrom; import io.github.opencubicchunks.cubicchunks.mixin.DasmRedirect; import io.github.opencubicchunks.cubicchunks.mixin.TransformFrom; import io.github.opencubicchunks.cubicchunks.world.level.chunklike.CloAccess; @@ -127,13 +128,13 @@ private static void replaceMissingSections(Registry p_281389_, LevelChunk return this.getMinBuildHeight(); } - @TransformFrom(copyFrom = ChunkAccess.class, value = "getBlockEntitiesPos()Ljava/util/Set;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getBlockEntitiesPos()Ljava/util/Set;") @Override public native Set getBlockEntitiesPos(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getSections()[Lnet/minecraft/world/level/chunk/LevelChunkSection;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getSections()[Lnet/minecraft/world/level/chunk/LevelChunkSection;") @Override public native LevelChunkSection[] getSections(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getSection(I)Lnet/minecraft/world/level/chunk/LevelChunkSection;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getSection(I)Lnet/minecraft/world/level/chunk/LevelChunkSection;") @Override public native LevelChunkSection getSection(int p_187657_); // TODO (P2) heightmap methods on cubes @@ -161,31 +162,31 @@ private static void replaceMissingSections(Registry p_281389_, LevelChunk return cloPos; } - @TransformFrom(copyFrom = ChunkAccess.class, value = "getStartForStructure(Lnet/minecraft/world/level/levelgen/structure/Structure;)" + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getStartForStructure(Lnet/minecraft/world/level/levelgen/structure/Structure;)" + "Lnet/minecraft/world/level/levelgen/structure/StructureStart;") @Override @Nullable public native StructureStart getStartForStructure(Structure p_223005_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "setStartForStructure(Lnet/minecraft/world/level/levelgen/structure/Structure;" + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "setStartForStructure(Lnet/minecraft/world/level/levelgen/structure/Structure;" + "Lnet/minecraft/world/level/levelgen/structure/StructureStart;)V") @Override public native void setStartForStructure(Structure p_223010_, StructureStart p_223011_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getAllStarts()Ljava/util/Map;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getAllStarts()Ljava/util/Map;") @Override public native Map getAllStarts(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "setAllStarts(Ljava/util/Map;)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "setAllStarts(Ljava/util/Map;)V") @Override public native void setAllStarts(Map p_62090_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getReferencesForStructure(Lnet/minecraft/world/level/levelgen/structure/Structure;)" + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getReferencesForStructure(Lnet/minecraft/world/level/levelgen/structure/Structure;)" + "Lit/unimi/dsi/fastutil/longs/LongSet;") @Override public native LongSet getReferencesForStructure(Structure p_223017_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "addReferenceForStructure(Lnet/minecraft/world/level/levelgen/structure/Structure;J)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "addReferenceForStructure(Lnet/minecraft/world/level/levelgen/structure/Structure;J)V") @Override public native void addReferenceForStructure(Structure p_223007_, long p_223008_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getAllReferences()Ljava/util/Map;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getAllReferences()Ljava/util/Map;") @Override public native Map getAllReferences(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "setAllReferences(Ljava/util/Map;)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "setAllReferences(Ljava/util/Map;)V") @Override public native void setAllReferences(Map p_187663_); @Override public boolean isYSpaceEmpty(int p_62075_, int p_62076_) { @@ -193,10 +194,10 @@ private static void replaceMissingSections(Registry p_281389_, LevelChunk return false; } - @TransformFrom(copyFrom = ChunkAccess.class, value = "setUnsaved(Z)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "setUnsaved(Z)V") @Override public native void setUnsaved(boolean p_62094_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "isUnsaved()Z") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "isUnsaved()Z") @Override public native boolean isUnsaved(); @Override public abstract ChunkStatus getStatus(); @@ -208,28 +209,28 @@ private static void replaceMissingSections(Registry p_281389_, LevelChunk @Override public abstract void removeBlockEntity(BlockPos p_62101_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "markPosForPostprocessing(Lnet/minecraft/core/BlockPos;)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "markPosForPostprocessing(Lnet/minecraft/core/BlockPos;)V") @Override public native void markPosForPostprocessing(BlockPos p_62102_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getPostProcessing()[Lit/unimi/dsi/fastutil/shorts/ShortList;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getPostProcessing()[Lit/unimi/dsi/fastutil/shorts/ShortList;") @Override public native ShortList[] getPostProcessing(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "addPackedPostProcess(SI)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "addPackedPostProcess(SI)V") @Override public native void addPackedPostProcess(short p_62092_, int p_62093_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "setBlockEntityNbt(Lnet/minecraft/nbt/CompoundTag;)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "setBlockEntityNbt(Lnet/minecraft/nbt/CompoundTag;)V") @Override public native void setBlockEntityNbt(CompoundTag p_62091_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getBlockEntityNbt(Lnet/minecraft/core/BlockPos;)Lnet/minecraft/nbt/CompoundTag;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getBlockEntityNbt(Lnet/minecraft/core/BlockPos;)Lnet/minecraft/nbt/CompoundTag;") @Override @Nullable public native CompoundTag getBlockEntityNbt(BlockPos p_62103_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getBlockEntityNbtForSaving(Lnet/minecraft/core/BlockPos;)Lnet/minecraft/nbt/CompoundTag;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getBlockEntityNbtForSaving(Lnet/minecraft/core/BlockPos;)Lnet/minecraft/nbt/CompoundTag;") @Override @Nullable public native CompoundTag getBlockEntityNbtForSaving(BlockPos p_62104_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "findBlockLightSources(Ljava/util/function/BiConsumer;)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "findBlockLightSources(Ljava/util/function/BiConsumer;)V") @Override public native void findBlockLightSources(BiConsumer p_285269_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "findBlocks(Ljava/util/function/Predicate;Ljava/util/function/BiConsumer;)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "findBlocks(Ljava/util/function/Predicate;Ljava/util/function/BiConsumer;)V") @Override public native void findBlocks(Predicate p_285343_, BiConsumer p_285030_); @Override public void findBlocks(BiPredicate p_285343_, BiConsumer p_285030_) { @@ -265,37 +266,37 @@ private static void replaceMissingSections(Registry p_281389_, LevelChunk @Override public abstract ChunkAccess.TicksToSave getTicksForSerialization(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getUpgradeData()Lnet/minecraft/world/level/chunk/UpgradeData;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getUpgradeData()Lnet/minecraft/world/level/chunk/UpgradeData;") @Override public native UpgradeData getUpgradeData(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "isOldNoiseGeneration()Z") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "isOldNoiseGeneration()Z") @Override public native boolean isOldNoiseGeneration(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getBlendingData()Lnet/minecraft/world/level/levelgen/blending/BlendingData;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getBlendingData()Lnet/minecraft/world/level/levelgen/blending/BlendingData;") @Override @Nullable public native BlendingData getBlendingData(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "setBlendingData(Lnet/minecraft/world/level/levelgen/blending/BlendingData;)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "setBlendingData(Lnet/minecraft/world/level/levelgen/blending/BlendingData;)V") @Override public native void setBlendingData(BlendingData p_187646_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getInhabitedTime()J") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getInhabitedTime()J") @Override public native long getInhabitedTime(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "incrementInhabitedTime(J)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "incrementInhabitedTime(J)V") @Override public native void incrementInhabitedTime(long p_187633_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "setInhabitedTime(J)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "setInhabitedTime(J)V") @Override public native void setInhabitedTime(long p_62099_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "isLightCorrect()Z") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "isLightCorrect()Z") @Override public native boolean isLightCorrect(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "setLightCorrect(Z)V") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "setLightCorrect(Z)V") @Override public native void setLightCorrect(boolean p_62100_); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getMinBuildHeight()I") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getMinBuildHeight()I") @Override public native int getMinBuildHeight(); - @TransformFrom(copyFrom = ChunkAccess.class, value = "getHeight()I") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getHeight()I") @Override public native int getHeight(); @Override public NoiseChunk getOrCreateNoiseChunk(Function p_223013_) { @@ -314,7 +315,7 @@ private static void replaceMissingSections(Registry p_281389_, LevelChunk throw new UnsupportedOperationException(); // TODO P3 } - @TransformFrom(copyFrom = ChunkAccess.class, value = "hasAnyStructureReferences()Z") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "hasAnyStructureReferences()Z") @Override public native boolean hasAnyStructureReferences(); @Override @Nullable public BelowZeroRetrogen getBelowZeroRetrogen() { @@ -325,7 +326,7 @@ private static void replaceMissingSections(Registry p_281389_, LevelChunk return false; // Used for below-zero retrogen; not applicable to cubes } - @TransformFrom(copyFrom = ChunkAccess.class, value = "getHeightAccessorForGeneration()Lnet/minecraft/world/level/LevelHeightAccessor;") + @TransformFrom(copyFrom = @CopyFrom(clazz = ChunkAccess.class), value = "getHeightAccessorForGeneration()Lnet/minecraft/world/level/LevelHeightAccessor;") @Override public native LevelHeightAccessor getHeightAccessorForGeneration(); @Override public void initializeLightSources() { diff --git a/src/main/resources/cubicchunks.mixins.core.json b/src/main/resources/cubicchunks.mixins.core.json index 8c910615..b6fa8e2b 100644 --- a/src/main/resources/cubicchunks.mixins.core.json +++ b/src/main/resources/cubicchunks.mixins.core.json @@ -13,6 +13,7 @@ }, "mixins": [ "common.client.multiplayer.MixinClientLevel", + "common.server.MixinMinecraftServer", "common.server.level.MixinChunkTaskPriorityQueue", "common.server.level.MixinChunkTaskPriorityQueueSorter", "common.server.level.MixinChunkTicketTracker", @@ -24,11 +25,9 @@ "common.server.level.MixinServerLevel", "common.server.level.MixinServerPlayer", "common.server.level.MixinTickingTracker", - "common.server.MixinMinecraftServer", - "common.TestMixin", + "common.world.level.MixinLevel", "common.world.level.chunk.MixinChunkAccess", - "common.world.level.cube.MixinCubeAccess", - "common.world.level.MixinLevel" + "common.world.level.cube.MixinCubeAccess" ], "client": [], "server": []