From 4022b7ebb9a7795b6dfce68ec60ad34258c1297a Mon Sep 17 00:00:00 2001 From: ishland Date: Tue, 29 Oct 2024 10:47:55 +0800 Subject: [PATCH] refactor: remove obsolete ASM transformations --- .../c2me/base/common/util/ASMUtils.java | 41 ---------- .../asm/ASMTransformerMakeVolatile.java | 5 -- .../ishland/c2me/opts/allocs/MixinPlugin.java | 2 - ...ransformerNbtOpsMapBuilderFastUtilMap.java | 74 ------------------- .../opts/allocs/mixin/asm/ASMTargets.java | 10 --- .../resources/c2me-opts-allocs.mixins.json | 1 - .../c2me/opts/chunk_access/MixinPlugin.java | 2 - ...ansformerLithiumChunkAccessWorkaround.java | 59 --------------- .../chunk_access/mixin/asm/ASMTargets.java | 10 --- .../c2me-opts-chunk-access.mixins.json | 1 - 10 files changed, 205 deletions(-) delete mode 100644 c2me-base/src/main/java/com/ishland/c2me/base/common/util/ASMUtils.java delete mode 100644 c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/asm/ASMTransformerNbtOpsMapBuilderFastUtilMap.java delete mode 100644 c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/mixin/asm/ASMTargets.java delete mode 100644 c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/asm/ASMTransformerLithiumChunkAccessWorkaround.java delete mode 100644 c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/mixin/asm/ASMTargets.java diff --git a/c2me-base/src/main/java/com/ishland/c2me/base/common/util/ASMUtils.java b/c2me-base/src/main/java/com/ishland/c2me/base/common/util/ASMUtils.java deleted file mode 100644 index 3fc42fbb6..000000000 --- a/c2me-base/src/main/java/com/ishland/c2me/base/common/util/ASMUtils.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.ishland.c2me.base.common.util; - -import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.MappingResolver; -import org.objectweb.asm.Type; - -import java.util.Arrays; - -public class ASMUtils { - public static final String INTERMEDIARY = "intermediary"; - public static final MappingResolver mappingResolver = FabricLoader.getInstance().getMappingResolver(); - - public static String remapMethodDescriptor(String desc) { - final Type returnType = Type.getReturnType(desc); - final Type[] argumentTypes = Type.getArgumentTypes(desc); - return Type.getMethodDescriptor( - Type.getType(remapFieldDescriptor(returnType.getDescriptor())), - Arrays.stream(argumentTypes) - .map(type -> Type.getType(remapFieldDescriptor(type.getDescriptor()))) - .toArray(Type[]::new) - ); - } - - public static String remapFieldDescriptor(String desc) { - final Type type = Type.getType(desc); - if (type.getSort() == Type.ARRAY) { // remap arrays - return "[".repeat(type.getDimensions()) + remapFieldDescriptor(type.getElementType().getDescriptor()); - } - if (type.getSort() != Type.OBJECT) { // no need to remap primitives - return desc; - } - final String unmappedClassDesc = type.getClassName(); - final String unmappedClass; - if (unmappedClassDesc.endsWith(";") && unmappedClassDesc.startsWith("L")) { - unmappedClass = unmappedClassDesc.substring(1, unmappedClassDesc.length() - 1); // trim starting "L" and ending ";" - } else { - unmappedClass = unmappedClassDesc; - } - return 'L' + mappingResolver.mapClassName(INTERMEDIARY, unmappedClass.replace('/', '.')).replace('.', '/') + ";"; - } -} diff --git a/c2me-fixes-worldgen-threading-issues/src/main/java/com/ishland/c2me/fixes/worldgen/threading_issues/asm/ASMTransformerMakeVolatile.java b/c2me-fixes-worldgen-threading-issues/src/main/java/com/ishland/c2me/fixes/worldgen/threading_issues/asm/ASMTransformerMakeVolatile.java index 539e1efb4..5306a4ca7 100644 --- a/c2me-fixes-worldgen-threading-issues/src/main/java/com/ishland/c2me/fixes/worldgen/threading_issues/asm/ASMTransformerMakeVolatile.java +++ b/c2me-fixes-worldgen-threading-issues/src/main/java/com/ishland/c2me/fixes/worldgen/threading_issues/asm/ASMTransformerMakeVolatile.java @@ -1,6 +1,5 @@ package com.ishland.c2me.fixes.worldgen.threading_issues.asm; -import com.ishland.c2me.base.common.util.ASMUtils; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; import org.objectweb.asm.tree.ClassNode; @@ -8,10 +7,6 @@ import org.slf4j.LoggerFactory; import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; import java.util.stream.Stream; public class ASMTransformerMakeVolatile { diff --git a/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/MixinPlugin.java b/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/MixinPlugin.java index 1d330c31e..7bc073ec6 100644 --- a/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/MixinPlugin.java +++ b/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/MixinPlugin.java @@ -1,7 +1,6 @@ package com.ishland.c2me.opts.allocs; import com.ishland.c2me.base.common.ModuleMixinPlugin; -import com.ishland.c2me.opts.allocs.asm.ASMTransformerNbtOpsMapBuilderFastUtilMap; import net.fabricmc.loader.api.FabricLoader; import org.objectweb.asm.tree.ClassNode; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; @@ -27,7 +26,6 @@ public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { @Override public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { super.preApply(targetClassName, targetClass, mixinClassName, mixinInfo); - ASMTransformerNbtOpsMapBuilderFastUtilMap.transform(targetClass); } } diff --git a/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/asm/ASMTransformerNbtOpsMapBuilderFastUtilMap.java b/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/asm/ASMTransformerNbtOpsMapBuilderFastUtilMap.java deleted file mode 100644 index c9d122a06..000000000 --- a/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/asm/ASMTransformerNbtOpsMapBuilderFastUtilMap.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.ishland.c2me.opts.allocs.asm; - -import com.ishland.c2me.base.common.util.ASMUtils; -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; -import org.objectweb.asm.tree.AbstractInsnNode; -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodInsnNode; -import org.objectweb.asm.tree.MethodNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ListIterator; -import java.util.Map; - -public class ASMTransformerNbtOpsMapBuilderFastUtilMap { - - static final Logger LOGGER = LoggerFactory.getLogger("C2ME (c2me-opts-allocs) ASM Transformer"); - - private static final String NbtOps$MapBuilder = "net/minecraft/class_2509$class_5320"; - private static final String NbtOps$MapBuilderMapped = ASMUtils.mappingResolver.mapClassName(ASMUtils.INTERMEDIARY, NbtOps$MapBuilder.replace('/', '.')).replace('.', '/'); - private static final String buildDesc = "(Lnet/minecraft/class_2487;Lnet/minecraft/class_2520;)Lcom/mojang/serialization/DataResult;"; - private static final String buildDescMapped = ASMUtils.remapMethodDescriptor(buildDesc); - private static final String build = ASMUtils.mappingResolver.mapMethodName(ASMUtils.INTERMEDIARY, NbtOps$MapBuilder.replace('/', '.'), "method_29170", buildDesc); - - // NEW net/minecraft/nbt/NbtCompound - // DUP - // ALOAD 2 - // CHECKCAST net/minecraft/nbt/NbtCompound - // INVOKEVIRTUAL net/minecraft/nbt/NbtCompound.toMap ()Ljava/util/Map; - // INVOKESTATIC com/google/common/collect/Maps.newHashMap (Ljava/util/Map;)Ljava/util/HashMap; <--- - // INVOKESPECIAL net/minecraft/nbt/NbtCompound. (Ljava/util/Map;)V - // ASTORE 3 - - public static void transform(ClassNode classNode) { - try { - if (classNode.name.equals(NbtOps$MapBuilderMapped)) { - for (MethodNode method : classNode.methods) { - if (method.name.equals(build) && method.desc.equals(buildDescMapped)) { - LOGGER.debug("Replacing NbtOps$MapBuilder build method newHashMap to fastutil map"); - final ListIterator iterator = method.instructions.iterator(); - boolean patched = false; - while (iterator.hasNext()) { - final AbstractInsnNode next = iterator.next(); - if (next instanceof MethodInsnNode methodInsnNode) { - if (methodInsnNode.getOpcode() == Opcodes.INVOKESTATIC && - methodInsnNode.owner.equals("com/google/common/collect/Maps") && - methodInsnNode.name.equals("newHashMap") && - methodInsnNode.desc.equals("(Ljava/util/Map;)Ljava/util/HashMap;")) { - iterator.set(new MethodInsnNode( - Opcodes.INVOKESTATIC, - Type.getInternalName(ASMTransformerNbtOpsMapBuilderFastUtilMap.class), - "newFastUtilMap", - Type.getMethodDescriptor(ASMTransformerNbtOpsMapBuilderFastUtilMap.class.getMethod("newFastUtilMap", Map.class)) - )); - patched = true; - } - } - } - if (!patched) LOGGER.warn("Unable to find target opcode in NbtOps$MapBuilder"); - } - } - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - public static Map newFastUtilMap(Map map) { - return new Object2ObjectOpenHashMap<>(map); - } - -} diff --git a/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/mixin/asm/ASMTargets.java b/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/mixin/asm/ASMTargets.java deleted file mode 100644 index 95d20e2ab..000000000 --- a/c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/mixin/asm/ASMTargets.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.ishland.c2me.opts.allocs.mixin.asm; - -import net.minecraft.server.world.ServerChunkManager; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(value = { - ServerChunkManager.class -}) -public class ASMTargets { -} diff --git a/c2me-opts-allocs/src/main/resources/c2me-opts-allocs.mixins.json b/c2me-opts-allocs/src/main/resources/c2me-opts-allocs.mixins.json index 3faba5e63..922e12176 100644 --- a/c2me-opts-allocs/src/main/resources/c2me-opts-allocs.mixins.json +++ b/c2me-opts-allocs/src/main/resources/c2me-opts-allocs.mixins.json @@ -9,7 +9,6 @@ "MixinNbtCompound1", "MixinNbtList", "MixinUtil", - "asm.ASMTargets", "object_pooling_caching.MixinConfiguredFeature", "object_pooling_caching.MixinOreFeature", "surfacebuilder.MixinMaterialRuleContext", diff --git a/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/MixinPlugin.java b/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/MixinPlugin.java index 8f8785b80..877718780 100644 --- a/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/MixinPlugin.java +++ b/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/MixinPlugin.java @@ -1,7 +1,6 @@ package com.ishland.c2me.opts.chunk_access; import com.ishland.c2me.base.common.ModuleMixinPlugin; -import com.ishland.c2me.opts.chunk_access.asm.ASMTransformerLithiumChunkAccessWorkaround; import org.objectweb.asm.tree.ClassNode; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; @@ -10,7 +9,6 @@ public class MixinPlugin extends ModuleMixinPlugin { @Override public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { super.postApply(targetClassName, targetClass, mixinClassName, mixinInfo); - ASMTransformerLithiumChunkAccessWorkaround.transform(targetClass); } } diff --git a/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/asm/ASMTransformerLithiumChunkAccessWorkaround.java b/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/asm/ASMTransformerLithiumChunkAccessWorkaround.java deleted file mode 100644 index ed025ea00..000000000 --- a/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/asm/ASMTransformerLithiumChunkAccessWorkaround.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.ishland.c2me.opts.chunk_access.asm; - -import com.ishland.c2me.base.common.util.ASMUtils; -import com.ishland.c2me.opts.chunk_access.ModuleEntryPoint; -import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.MappingResolver; -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Optional; - -public class ASMTransformerLithiumChunkAccessWorkaround { - - static final Logger LOGGER = LoggerFactory.getLogger("C2ME (c2me-opts-chunk-access) ASM Transformer"); - - private static final String INTERMEDIARY = "intermediary"; - private static final MappingResolver mappingResolver = FabricLoader.getInstance().getMappingResolver(); - private static final String ServerChunkManager = mappingResolver.mapClassName(INTERMEDIARY, "net/minecraft/class_3215".replace('/', '.')).replace('.', '/'); - - private ASMTransformerLithiumChunkAccessWorkaround() { - } - - // private getChunkOffThread(IILnet/minecraft/world/chunk/ChunkStatus;Z)Lnet/minecraft/world/chunk/Chunk; - // INVOKEVIRTUAL java/util/concurrent/CompletableFuture.join ()Ljava/lang/Object; - public static void transform(ClassNode classNode) { - if (!ModuleEntryPoint.enabled) return; - try { - if (classNode.name.equals(ServerChunkManager) && FabricLoader.getInstance().isModLoaded("lithium")) { - for (MethodNode method : classNode.methods) { - if (method.name.equals("c2me$getChunkOffThread") && method.desc.equals(ASMUtils.remapMethodDescriptor("(IILnet/minecraft/class_2806;Z)Lnet/minecraft/class_2791;"))) { - LOGGER.debug("Replacing lithium chunk_access method getChunkOffThread to apply non-blocking async chunk request"); - final Optional getChunkOffThread = classNode.methods.stream().filter(methodNode -> methodNode.name.equals("getChunkOffThread")).findAny(); - getChunkOffThread.ifPresentOrElse(oldMethodNode -> { - final MethodNode newMethodNode = new MethodNode(); - method.accept(newMethodNode); - newMethodNode.name = oldMethodNode.name; - newMethodNode.access = method.access; - newMethodNode.desc = method.desc; - newMethodNode.signature = method.signature; - newMethodNode.exceptions = new ArrayList<>(method.exceptions); - if (method.attrs != null) newMethodNode.attrs = new ArrayList<>(method.attrs); - newMethodNode.tryCatchBlocks = new ArrayList<>(method.tryCatchBlocks); - classNode.methods.remove(oldMethodNode); - classNode.methods.add(newMethodNode); - }, () -> LOGGER.warn("lithium getChunkOffThread not found")); - break; - } - } - - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - -} diff --git a/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/mixin/asm/ASMTargets.java b/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/mixin/asm/ASMTargets.java deleted file mode 100644 index 861fa2506..000000000 --- a/c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/mixin/asm/ASMTargets.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.ishland.c2me.opts.chunk_access.mixin.asm; - -import net.minecraft.server.world.ServerChunkManager; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(value = { - ServerChunkManager.class -}) -public class ASMTargets { -} diff --git a/c2me-opts-chunk-access/src/main/resources/c2me-opts-chunk-access.mixins.json b/c2me-opts-chunk-access/src/main/resources/c2me-opts-chunk-access.mixins.json index 86cc1c343..042ec52bf 100644 --- a/c2me-opts-chunk-access/src/main/resources/c2me-opts-chunk-access.mixins.json +++ b/c2me-opts-chunk-access/src/main/resources/c2me-opts-chunk-access.mixins.json @@ -4,7 +4,6 @@ "package": "com.ishland.c2me.opts.chunk_access.mixin", "plugin": "com.ishland.c2me.opts.chunk_access.MixinPlugin", "mixins": [ - "asm.ASMTargets", "region_capture.MixinChunkGenerationStep" ] }