Skip to content

Commit

Permalink
Update to MRAPI 1.21.x switch to api v1.
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Apr 27, 2024
1 parent dcf4aa4 commit 2d0466a
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 180 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ dependencies {
include(group: "local", name: "guava", version: "12.0.1")
include(group: "local", name: "bcprov-jdk15on", version: "147")

// modRuntimeOnly(files("./forge-${project.minecraft_version}-${project.forge_version}-universal-intermediary.jar"))
modRuntimeOnly(files("./forge-${project.minecraft_version}-${project.forge_version}-universal-intermediary.jar"))
}

subprojects {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx2G
loader_version = 0.15.10

# Legacy Fabric API
mod_remapping_api = 1.20.0
mod_remapping_api = 1.21.1
forge_version = 6.6.2.534
mixin_extras_version=0.2.1
spasm_version=0.2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package fr.catcore.fabricatedforge.compat.asm;

import fr.catcore.modremapperapi.api.IClassTransformer;
import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
Expand All @@ -25,71 +23,8 @@ public class ASMRemapperTransformer implements IClassTransformer, Opcodes {
"org.lwjgl."
};

private final static Map<String, String> INSTANCE_REPLACER = arrayToMap(new String[][]{
{"org/objectweb/asm/tree/FieldInsnNode", "fr/catcore/fabricatedforge/compat/asm/BetterFieldInsnNode"},
{"org/objectweb/asm/tree/MethodInsnNode", "fr/catcore/fabricatedforge/compat/asm/BetterMethodInsnNode"},
{"org/objectweb/asm/ClassWriter", "fr/catcore/fabricatedforge/compat/asm/BetterClassWriter"}
});

private final static Map<String, Map<String, Map<String, ClassAwareMember>>> CALL_REPLACER = new HashMap<>();

private final static List<String> TRANSFORMED = new ArrayList<>();

private static void registerCallReplacer(String className, Object... args) {
Map<String, Map<String, ClassAwareMember>> classMap = CALL_REPLACER.compute(className, (s, stringMapMap) -> new HashMap<>());

for (int i = 0; i < args.length; i++) {
String name = (String) args[i];
String desc = (String) args[i + 1];
ClassAwareMember member = (ClassAwareMember) args[i + 2];

Map<String, ClassAwareMember> methodMap = classMap.compute(name, (s, stringClassAwareMemberMap) -> new HashMap<>());
methodMap.put(desc, member);

i += 2;
}
}

static {
registerCallReplacer("java/lang/Class",
"getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", new ClassAwareMember(
"fr/catcore/fabricatedforge/compat/asm/RemapAwareClass",
"getDeclaredMethod",
"(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"
),

"getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", new ClassAwareMember(
"fr/catcore/fabricatedforge/compat/asm/RemapAwareClass",
"getDeclaredField",
"(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;"
),

"getMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", new ClassAwareMember(
"fr/catcore/fabricatedforge/compat/asm/RemapAwareClass",
"getMethod",
"(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"
),

"getField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", new ClassAwareMember(
"fr/catcore/fabricatedforge/compat/asm/RemapAwareClass",
"getField",
"(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;"
),

"forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", new ClassAwareMember(
"fr/catcore/fabricatedforge/compat/asm/RemapAwareClass",
"forName",
"(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"
),

"forName", "(Ljava/lang/String;)Ljava/lang/Class;", new ClassAwareMember(
"fr/catcore/fabricatedforge/compat/asm/RemapAwareClass",
"forName",
"(Ljava/lang/String;)Ljava/lang/Class;"
)
);
}

@Override
public boolean handlesClass(String s, String s1) {
for (String exclude : EXCLUDED) {
Expand All @@ -110,50 +45,7 @@ public byte[] transformClass(String s, String s1, byte[] bytes) {

boolean touched = false;

for (MethodNode methodNode : classNode.methods) {
AbstractInsnNode insnNode = methodNode.instructions.getFirst();

if (insnNode != null) {
while (insnNode != null) {
if (insnNode.getOpcode() == NEW && insnNode instanceof TypeInsnNode) {
TypeInsnNode typeInsnNode = (TypeInsnNode) insnNode;

if (INSTANCE_REPLACER.containsKey(typeInsnNode.desc)) {
typeInsnNode.desc = INSTANCE_REPLACER.get(typeInsnNode.desc);
touched = true;
}
} else if (insnNode instanceof MethodInsnNode) {
MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;

if (methodInsnNode.getOpcode() == INVOKESPECIAL) {
if (methodInsnNode.name.equals("<init>") && INSTANCE_REPLACER.containsKey(methodInsnNode.owner)) {
methodInsnNode.owner = INSTANCE_REPLACER.get(methodInsnNode.owner);
touched = true;
}
} else if (methodInsnNode.getOpcode() == INVOKEVIRTUAL || methodInsnNode.getOpcode() == INVOKESTATIC) {
if (CALL_REPLACER.containsKey(methodInsnNode.owner)) {
Map<String, Map<String, ClassAwareMember>> classMethods = CALL_REPLACER.get(methodInsnNode.owner);

if (classMethods.containsKey(methodInsnNode.name)) {
Map<String, ClassAwareMember> classMethod = classMethods.get(methodInsnNode.name);

if (classMethod.containsKey(methodInsnNode.desc)) {
ClassAwareMember classAwareMember = classMethod.get(methodInsnNode.desc);
methodInsnNode.setOpcode(INVOKESTATIC);
methodInsnNode.owner = classAwareMember.owner;
methodInsnNode.name = classAwareMember.name;
methodInsnNode.desc = classAwareMember.desc;
touched = true;
}
}
}
}
}

insnNode = insnNode.getNext();
}
}
}
// Transform class here

if (touched) {
ClassWriter classWriter = new BetterClassWriter(3);
Expand All @@ -175,12 +67,4 @@ private static Map<String, String> arrayToMap(String[][] arrays) {

return map;
}

public static class ClassAwareMember extends MappingUtils.ClassMember {
public final String owner;
public ClassAwareMember(String owner, String name, @Nullable String desc) {
super(name, desc);
this.owner = owner;
}
}
}
Loading

0 comments on commit 2d0466a

Please sign in to comment.