Skip to content

Commit

Permalink
Fix: Soft-remap inferred accessor and invoker names.
Browse files Browse the repository at this point in the history
  • Loading branch information
LlamaLad7 authored and modmuss50 committed Jan 20, 2025
1 parent 0bbcbe4 commit 64e7855
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import net.fabricmc.tinyremapper.extension.mixin.common.data.CommonData;
import net.fabricmc.tinyremapper.extension.mixin.common.data.Constant;
import net.fabricmc.tinyremapper.extension.mixin.common.data.MxMember;
import net.fabricmc.tinyremapper.extension.mixin.hard.annotation.AccessorAnnotationVisitor;
import net.fabricmc.tinyremapper.extension.mixin.hard.annotation.InvokerAnnotationVisitor;
import net.fabricmc.tinyremapper.extension.mixin.hard.annotation.OverwriteAnnotationVisitor;
import net.fabricmc.tinyremapper.extension.mixin.hard.annotation.ShadowAnnotationVisitor;

Expand All @@ -57,10 +55,6 @@ public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
av = new ShadowAnnotationVisitor(data, av, method, targets);
} else if (Annotation.OVERWRITE.equals(descriptor)) {
av = new OverwriteAnnotationVisitor(data, av, method, targets);
} else if (Annotation.ACCESSOR.equals(descriptor)) {
av = new AccessorAnnotationVisitor(data, av, method, targets);
} else if (Annotation.INVOKER.equals(descriptor)) {
av = new InvokerAnnotationVisitor(data, av, method, targets);
}

return av;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.objectweb.asm.AnnotationVisitor;

import net.fabricmc.tinyremapper.extension.mixin.common.StringUtility;
import net.fabricmc.tinyremapper.extension.mixin.common.data.AnnotationElement;
import net.fabricmc.tinyremapper.extension.mixin.common.data.CommonData;
import net.fabricmc.tinyremapper.extension.mixin.common.data.Constant;
Expand All @@ -38,9 +39,12 @@
public class AccessorAnnotationVisitor extends AnnotationVisitor {
private final CommonData data;

private final MxMember method;
private final List<String> targets;
private final String fieldDesc;

private boolean isSoftTarget;

private static final Pattern GETTER_PATTERN = Pattern.compile("(?<=\\(\\)).*");
private static final Pattern SETTER_PATTERN = Pattern.compile("(?<=\\().*(?=\\)V)");

Expand All @@ -50,6 +54,7 @@ public AccessorAnnotationVisitor(CommonData data, AnnotationVisitor delegate, Mx
this.data = Objects.requireNonNull(data);
Objects.requireNonNull(method);

this.method = method;
this.targets = Objects.requireNonNull(targets);

Matcher getterMatcher = GETTER_PATTERN.matcher(method.getDesc());
Expand All @@ -67,11 +72,41 @@ public AccessorAnnotationVisitor(CommonData data, AnnotationVisitor delegate, Mx
@Override
public void visit(String name, Object value) {
if (name.equals(AnnotationElement.VALUE)) {
isSoftTarget = true;
String fieldName = Objects.requireNonNull((String) value);

value = new NamedMappable(data, fieldName, fieldDesc, targets).result();
setAnnotationValue(fieldName);
return;
}

super.visit(name, value);
}

@Override
public void visitEnd() {
if (!isSoftTarget) {
setAnnotationValue(inferFieldName());
}

super.visitEnd();
}

private void setAnnotationValue(String fieldName) {
super.visit(AnnotationElement.VALUE, new NamedMappable(data, fieldName, fieldDesc, targets).result());
}

private String inferFieldName() {
String prefix;

if (method.getName().startsWith("get")) {
prefix = "get";
} else if (method.getName().startsWith("set")) {
prefix = "set";
} else if (method.getName().startsWith("is")) {
prefix = "is";
} else {
throw new RuntimeException(String.format("%s does not start with get, set or is.", method.getName()));
}

return StringUtility.removeCamelPrefix(prefix, method.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.objectweb.asm.AnnotationVisitor;

import net.fabricmc.tinyremapper.extension.mixin.common.StringUtility;
import net.fabricmc.tinyremapper.extension.mixin.common.data.AnnotationElement;
import net.fabricmc.tinyremapper.extension.mixin.common.data.CommonData;
import net.fabricmc.tinyremapper.extension.mixin.common.data.Constant;
Expand All @@ -39,6 +40,8 @@ public class InvokerAnnotationVisitor extends AnnotationVisitor {

private final List<String> targets;

private boolean isSoftTarget;

public InvokerAnnotationVisitor(CommonData data, AnnotationVisitor delegate, MxMember method, List<String> targets) {
super(Constant.ASM_VERSION, Objects.requireNonNull(delegate));

Expand All @@ -51,12 +54,49 @@ public InvokerAnnotationVisitor(CommonData data, AnnotationVisitor delegate, MxM
@Override
public void visit(String name, Object value) {
if (name.equals(AnnotationElement.VALUE)) {
isSoftTarget = true;
String methodName = Objects.requireNonNull((String) value);
String methodDesc = method.getDesc();

value = new NamedMappable(data, methodName, methodDesc, targets).result();
setAnnotationValue(methodName);
return;
}

super.visit(name, value);
}

@Override
public void visitEnd() {
if (!isSoftTarget) {
String inferredName = inferMethodName();

if (inferredName != null) {
setAnnotationValue(inferredName);
}
}

super.visitEnd();
}

private void setAnnotationValue(String methodName) {
super.visit(AnnotationElement.VALUE, new NamedMappable(data, methodName, method.getDesc(), targets).result());
}

private String inferMethodName() {
if (method.getName().startsWith("new") || method.getName().startsWith("create")) {
// The rest of the name isn't important, leave it as-is
return null;
}

String prefix;

if (method.getName().startsWith("call")) {
prefix = "call";
} else if (method.getName().startsWith("invoke")) {
prefix = "invoke";
} else {
throw new RuntimeException(String.format("%s does not start with call or invoke.", method.getName()));
}

return StringUtility.removeCamelPrefix(prefix, method.getName());
}
}

0 comments on commit 64e7855

Please sign in to comment.