Skip to content

Commit

Permalink
Add more possible visitors during remapping and start moving more api…
Browse files Browse the repository at this point in the history
… under our new group
  • Loading branch information
thecatcore committed Apr 25, 2024
1 parent 1ea4a4d commit 07039b8
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
package fr.catcore.modremapperapi.remapping;

import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl;
import org.objectweb.asm.*;

import java.util.Map;

public class MRAClassVisitor extends ClassVisitor {
private final VisitorInfos infos;
private final VisitorInfosImpl infos;
private final String className;
protected MRAClassVisitor(ClassVisitor classVisitor, VisitorInfos infos, String className) {

protected MRAClassVisitor(ClassVisitor classVisitor, VisitorInfosImpl infos, String className) {
super(Opcodes.ASM9, classVisitor);
this.infos = infos;
this.className = className;
}

@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
VisitorInfos.Type superType = new VisitorInfos.Type(superName);
String superType = superName;

for (Map.Entry<VisitorInfos.Type, VisitorInfos.Type> entry : infos.SUPERS.entrySet()) {
if (entry.getKey().type.equals(superName)) {
superType = entry.getValue();
break;
}
if (infos.SUPERS.containsKey(superName)) {
superType = infos.SUPERS.get(superName);
}

super.visit(version, access, name, signature, superType.type, interfaces);
super.visit(version, access, name, signature, superType, interfaces);
}

@Override
public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String descriptor, boolean visible) {
VisitorInfos.Type superType = new VisitorInfos.Type(descriptor);
String annotationType = descriptor;

for (Map.Entry<VisitorInfos.Type, VisitorInfos.Type> entry : infos.ANNOTATION.entrySet()) {
if (entry.getKey().type.equals(descriptor)) {
superType = entry.getValue();
break;
}
if (infos.ANNOTATION.containsKey(descriptor)) {
annotationType = infos.ANNOTATION.get(descriptor);
}

return super.visitTypeAnnotation(typeRef, typePath, superType.type, visible);
return super.visitTypeAnnotation(typeRef, typePath, annotationType, visible);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,144 @@
package fr.catcore.modremapperapi.remapping;

import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.Map;

public class MRAMethodVisitor extends MethodVisitor {
private final VisitorInfos infos;
public class MRAMethodVisitor extends MethodVisitor implements Opcodes {
private final VisitorInfosImpl infos;
private final String className;
protected MRAMethodVisitor(MethodVisitor methodVisitor, VisitorInfos visitorInfos, String className) {
protected MRAMethodVisitor(MethodVisitor methodVisitor, VisitorInfosImpl visitorInfos, String className) {
super(Opcodes.ASM9, methodVisitor);
this.infos = visitorInfos;
this.className = className;
}

@Override
public void visitTypeInsn(int opcode, String type) {
VisitorInfos.Type superType = new VisitorInfos.Type(type);
String currentType = type;

for (Map.Entry<VisitorInfos.Type, VisitorInfos.Type> entry : infos.METHOD_TYPE.entrySet()) {
if (entry.getKey().type.equals(type)) {
superType = entry.getValue();
break;
}
boolean skip = false;

if (opcode == NEW && infos.INSTANTIATION.containsKey(type)) {
currentType = infos.INSTANTIATION.get(type);
skip = true;
}

if (!skip && infos.METHOD_TYPE.containsKey(type)) {
currentType = infos.METHOD_TYPE.get(type);
}

super.visitTypeInsn(opcode, superType.type);
super.visitTypeInsn(opcode, currentType);
}

@Override
public void visitFieldInsn(int opcode, String owner, String name, String descriptor) {
VisitorInfos.MethodNamed superType = new VisitorInfos.MethodNamed(owner, name);
String currentOwner = owner;
String currentName = name;
String currentDescriptor = descriptor;

if (infos.FIELD_REF.containsKey(owner)) {
Map<String, Map<String, io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos.FullClassMember>> fields = infos.FIELD_REF.get(owner);

Map<String, io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos.FullClassMember> args = fields.get(name);

if (args == null) {
args = fields.get("");
}

for (Map.Entry<VisitorInfos.MethodNamed, VisitorInfos.MethodNamed> entry : infos.METHOD_FIELD.entrySet()) {
if (entry.getKey().owner.equals(owner)) {
if (entry.getKey().name.isEmpty() || entry.getKey().name.equals(name)) {
superType = entry.getValue();
if (args != null) {
io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos.FullClassMember classMember = args.get(descriptor);

if (classMember == null) {
classMember = args.get("");
}

if (classMember != null) {
currentOwner = classMember.owner;
currentName = classMember.name;
currentDescriptor = classMember.desc;
}
}
}

super.visitFieldInsn(opcode, superType.owner, superType.name.isEmpty() ? name : superType.name, descriptor);
if (currentName.isEmpty()) {
currentName = name;
}

if (currentDescriptor == null || currentDescriptor.isEmpty()) {
currentDescriptor = descriptor;
}

super.visitFieldInsn(opcode, currentOwner, currentName, currentDescriptor);
}

@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
VisitorInfos.MethodNamed superType = new VisitorInfos.MethodNamed(owner, name);
int currentOpcode = opcode;
String currentOwner = owner;
String currentName = name;
String currentDescriptor = descriptor;

for (Map.Entry<VisitorInfos.MethodNamed, VisitorInfos.MethodNamed> entry : infos.METHOD_METHOD.entrySet()) {
if (entry.getKey().owner.equals(owner)) {
if (entry.getKey().name.isEmpty() || entry.getKey().name.equals(name)) {
superType = entry.getValue();
boolean skip = false;

if (opcode == INVOKESPECIAL && infos.INSTANTIATION.containsKey(owner) && name.equals("<init>")) {
currentOwner = infos.INSTANTIATION.get(owner);
skip = true;
}

if (!skip && (opcode == INVOKEVIRTUAL || opcode == INVOKESTATIC)) {
if (infos.METHOD_INVOCATION.containsKey(owner)) {
Map<String, Map<String, io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos.FullClassMember>> methods = infos.METHOD_INVOCATION.get(owner);

Map<String, io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos.FullClassMember> args = methods.get(currentName);

if (args == null) {
args = methods.get("");
}

if (args != null) {
io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos.FullClassMember fullClassMember = args.get(currentDescriptor);

if (fullClassMember == null) {
fullClassMember = args.get("");
}

if (fullClassMember != null) {
currentOwner = fullClassMember.owner;
currentName = fullClassMember.name;
currentDescriptor = fullClassMember.desc;

if (fullClassMember.isStatic != null) currentOpcode = fullClassMember.isStatic ? INVOKESTATIC : INVOKEVIRTUAL;
}
}
}
}

super.visitMethodInsn(opcode, superType.owner, superType.name.isEmpty() ? name : superType.name, descriptor, isInterface);
if (currentName.isEmpty()) {
currentName = name;
}

if (currentDescriptor == null || currentDescriptor.isEmpty()) {
currentDescriptor = descriptor;
}

super.visitMethodInsn(currentOpcode, currentOwner, currentName, currentDescriptor, isInterface);
}

@Override
public void visitLdcInsn(Object value) {
VisitorInfos.MethodValue val = new VisitorInfos.MethodValue(this.className, value);
Object currentValue = value;

if (infos.LDC.containsKey(this.className)) {
Map<Object, Object> map = infos.LDC.get(this.className);

for (Map.Entry<VisitorInfos.MethodValue, VisitorInfos.MethodValue> entry : infos.METHOD_LDC.entrySet()) {
if (entry.getKey().value.equals(value)) {
val = entry.getValue();
break;
if (map.containsKey(value)) {
currentValue = map.get(value);
}
}

super.visitLdcInsn(val.value);
super.visitLdcInsn(currentValue);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package fr.catcore.modremapperapi.remapping;

import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.api.TrClass;
import org.objectweb.asm.ClassVisitor;

public class MRAPostApplyVisitor implements TinyRemapper.ApplyVisitorProvider {
private VisitorInfos infos;
private VisitorInfosImpl infos;
@Override
public ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next) {
final String className = cls.getName();
return new MRAClassVisitor(next, infos, className);
}

public void setInfos(VisitorInfos infos) {
public void setInfos(VisitorInfosImpl infos) {
this.infos = infos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.catcore.modremapperapi.utils.MappingsUtils;
import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.mappingio.MappingVisitor;
Expand Down Expand Up @@ -517,7 +518,7 @@ private static TinyRemapper makeRemapper(MappingTree... trees) {
MRAPostApplyVisitor applyVisitor = new MRAPostApplyVisitor();
MixinPostApplyVisitor mixinPostApplyVisitor = new MixinPostApplyVisitor(trees);

VisitorInfos infos = new VisitorInfos();
VisitorInfosImpl infos = new VisitorInfosImpl();

for (ModRemapper modRemapper : ModRemappingAPI.MOD_REMAPPERS) {
modRemapper.registerVisitors(infos);
Expand Down
44 changes: 23 additions & 21 deletions src/main/java/fr/catcore/modremapperapi/remapping/VisitorInfos.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package fr.catcore.modremapperapi.remapping;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Deprecated
public class VisitorInfos {
protected final Map<Type, Type> SUPERS = new HashMap<>();
protected final Map<Type, Type> ANNOTATION = new HashMap<>();
protected final Map<Type, Type> METHOD_TYPE = new HashMap<>();
protected final Map<MethodNamed, MethodNamed> METHOD_FIELD = new HashMap<>();
protected final Map<MethodNamed, MethodNamed> METHOD_METHOD = new HashMap<>();
protected final Map<MethodValue, MethodValue> METHOD_LDC = new HashMap<>();

public void registerSuperType(Type methodType, Type methodType2) {
SUPERS.put(methodType, methodType2);

@Deprecated
public void registerSuperType(Type target, Type replacement) {
throw new RuntimeException("This is not supposed to happen!");
}

public void registerTypeAnnotation(Type methodType, Type methodType2) {
ANNOTATION.put(methodType, methodType2);
@Deprecated
public void registerTypeAnnotation(Type target, Type replacement) {
throw new RuntimeException("This is not supposed to happen!");
}

public void registerMethodTypeIns(Type methodType, Type methodType2) {
METHOD_TYPE.put(methodType, methodType2);
@Deprecated
public void registerMethodTypeIns(Type target, Type replacement) {
throw new RuntimeException("This is not supposed to happen!");
}

public void registerMethodFieldIns(MethodNamed methodNamed, MethodNamed methodNamed2) {
METHOD_FIELD.put(methodNamed, methodNamed2);
@Deprecated
public void registerMethodFieldIns(MethodNamed target, MethodNamed replacementObject) {
throw new RuntimeException("This is not supposed to happen!");
}

public void registerMethodMethodIns(MethodNamed methodNamed, MethodNamed methodNamed2) {
METHOD_METHOD.put(methodNamed, methodNamed2);
@Deprecated
public void registerMethodMethodIns(MethodNamed target, MethodNamed replacementObject) {
throw new RuntimeException("This is not supposed to happen!");
}

public void registerMethodLdcIns(MethodValue methodValue, MethodValue methodValue2) {
METHOD_LDC.put(methodValue, methodValue2);
@Deprecated
public void registerMethodLdcIns(MethodValue target, MethodValue replacement) {
throw new RuntimeException("This is not supposed to happen!");
}

@Deprecated
public static class Type {
public final String type;

Expand All @@ -52,6 +52,7 @@ public boolean equals(Object o) {
}
}

@Deprecated
public static class MethodValue {
public final String owner;
public final Object value;
Expand All @@ -70,6 +71,7 @@ public boolean equals(Object o) {
}
}

@Deprecated
public static class MethodNamed {
public final String owner, name;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.fabriccompatibiltylayers.modremappingapi.api;

import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface MappingUtils {
Expand Down Expand Up @@ -37,10 +38,12 @@ static MappingUtils.ClassMember mapMethod(Class<?> owner, String methodName, Cla
}

class ClassMember {
public final String name;
public final @NotNull String name;
public final @Nullable String desc;

public ClassMember(String name, @Nullable String desc) {
public ClassMember(@NotNull String name, @Nullable String desc) {
assert name != null;

this.name = name;
this.desc = desc;
}
Expand Down
Loading

0 comments on commit 07039b8

Please sign in to comment.