Skip to content

Commit

Permalink
Remove global remapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Jul 11, 2024
1 parent 2338679 commit adcc207
Show file tree
Hide file tree
Showing 26 changed files with 68 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public MethodQualifier getTargetMethodQualifier() {
// Resolve method reference
String reference = patchContext().remap(methodRefs.getFirst());
// Extract owner, name and desc using regex
return MethodQualifier.create(reference, false).orElse(null);
return MethodQualifier.create(reference).orElse(null);
}

@Nullable
Expand All @@ -118,7 +118,7 @@ public MethodQualifier getInjectionPointMethodQualifier() {
// Resolve method reference
String reference = patchContext().remap(target);
// Extract owner, name and desc using regex
return MethodQualifier.create(reference, false).orElse(null);
return MethodQualifier.create(reference).orElse(null);
}

@Override
Expand Down Expand Up @@ -272,7 +272,7 @@ public Pair<ClassNode, List<MethodNode>> findInjectionTargetCandidates(ClassLook
.orElseGet(() -> {
List<Type> targetTypes = targetTypes();
if (targetTypes.size() == 1) {
return targetTypes.get(0).getInternalName();
return targetTypes.getFirst().getInternalName();
}
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public int getOrdinal(LocalVariableNode node) {
}

public LocalVariableNode getLast() {
return this.sortedLocals.get(this.sortedLocals.size() - 1);
return this.sortedLocals.getLast();
}

public List<LocalVariableNode> getForType(LocalVariableNode node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*;
import org.objectweb.asm.tree.analysis.*;
import org.sinytra.adapter.patch.api.GlobalReferenceMapper;
import org.sinytra.adapter.patch.util.MethodQualifier;

import java.util.*;
Expand All @@ -32,56 +31,36 @@ public static Multimap<String, MethodInsnNode> getMethodCalls(MethodNode node, L
}

public static InstructionMatcher findSurroundingInstructions(AbstractInsnNode insn, int range) {
return findSurroundingInstructions(insn, range, false);
}

public static InstructionMatcher findSurroundingInstructions(AbstractInsnNode insn, int range, boolean remapCalls) {
LabelNode previousLabel = findFirstInsn(insn, LabelNode.class, BACKWARDS);
LabelNode nextLabel = findFirstInsn(insn, LabelNode.class, FORWARD);

List<AbstractInsnNode> previousInsns = getInsns(previousLabel, range, remapCalls, BACKWARDS);
List<AbstractInsnNode> nextInsns = getInsns(nextLabel, range, remapCalls, FORWARD);
List<AbstractInsnNode> previousInsns = getInsns(previousLabel, range, BACKWARDS);
List<AbstractInsnNode> nextInsns = getInsns(nextLabel, range, FORWARD);

return new InstructionMatcher(insn, previousInsns, nextInsns);
}

public static InstructionMatcher findBackwardsInstructions(AbstractInsnNode insn, int range, boolean remapCalls) {
// TODO Remove global remapping
public static InstructionMatcher findBackwardsInstructions(AbstractInsnNode insn, int range) {
LabelNode previousLabel = findFirstInsn(insn, LabelNode.class, BACKWARDS);
List<AbstractInsnNode> previousInsns = getInsns(previousLabel, range, remapCalls, BACKWARDS);
List<AbstractInsnNode> previousInsns = getInsns(previousLabel, range, BACKWARDS);

return new InstructionMatcher(insn, previousInsns, List.of());
}

public static InstructionMatcher findForwardInstructions(AbstractInsnNode insn, int range, boolean remapCalls) {
public static InstructionMatcher findForwardInstructions(AbstractInsnNode insn, int range) {
LabelNode nextLabel = findFirstInsn(insn, LabelNode.class, FORWARD);
List<AbstractInsnNode> nextInsns = getInsns(nextLabel, range, remapCalls, FORWARD);
List<AbstractInsnNode> nextInsns = getInsns(nextLabel, range, FORWARD);

return new InstructionMatcher(insn, List.of(), nextInsns);
}

private static List<AbstractInsnNode> getInsns(AbstractInsnNode root, int range, boolean remapCalls, UnaryOperator<AbstractInsnNode> operator) {
private static List<AbstractInsnNode> getInsns(AbstractInsnNode root, int range, UnaryOperator<AbstractInsnNode> operator) {
return Stream.iterate(root, Objects::nonNull, operator)
.filter(insn -> !(insn instanceof FrameNode) && !(insn instanceof LineNumberNode))
.limit(range)
.map(insn -> remapCalls ? remapInsn(insn) : insn)
.toList();
}

private static AbstractInsnNode remapInsn(AbstractInsnNode insn) {
if (insn instanceof MethodInsnNode minsn) {
MethodInsnNode clone = (MethodInsnNode) minsn.clone(Map.of());
clone.name = GlobalReferenceMapper.remapReference(clone.name);
return clone;
}
if (insn instanceof FieldInsnNode finsn) {
FieldInsnNode clone = (FieldInsnNode) finsn.clone(Map.of());
clone.name = GlobalReferenceMapper.remapReference(clone.name);
return clone;
}
return insn;
}

@SuppressWarnings("unchecked")
@Nullable
public static <T extends AbstractInsnNode> T findFirstInsn(AbstractInsnNode insn, Class<T> type, UnaryOperator<AbstractInsnNode> operator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ private static void buildDiffWithContext(ParamsDiffSnapshotBuilder builder, List
// Look ahead for matching types at the beginning of the list
// If the first two are equal, remove the first ones and repeat
if (predictParameterMatch(builder, cleanQueue, dirtyQueue, compareNames, sameSize)) {
cleanQueue.remove(0);
dirtyQueue.remove(0);
cleanQueue.removeFirst();
dirtyQueue.removeFirst();
continue;
}
// Handle replaced types, needs improving
Expand Down Expand Up @@ -145,8 +145,8 @@ private static boolean predictParameterMatch(ParamsDiffSnapshotBuilder builder,
// 1 World
// 2 Sheep
if (cleanQueue.size() > 2 && dirtyQueue.size() > 2 && cleanQueue.get(1).matches(dirtyQueue.get(0)) && cleanQueue.get(2).matches(dirtyQueue.get(1))) {
builder.remove(cleanQueue.get(0).pos());
cleanQueue.remove(0);
builder.remove(cleanQueue.getFirst().pos());
cleanQueue.removeFirst();
return true;
}
return false;
Expand All @@ -171,9 +171,9 @@ else if (compareNames) {
// 2 F two
if (dirtyQueue.size() > 2 && cleanQueue.get(1).matches(dirtyQueue.get(2))) {
builder.insert(dirtyQueue.get(1).pos(), dirtyQueue.get(1).type());
cleanQueue.remove(0);
dirtyQueue.remove(0);
dirtyQueue.remove(0);
cleanQueue.removeFirst();
dirtyQueue.removeFirst();
dirtyQueue.removeFirst();
return true;
}
} else {
Expand Down Expand Up @@ -314,14 +314,14 @@ private static SwapResult checkForSwaps(ParamsDiffSnapshotBuilder builder, List<
}

// Remove leading equal types
while (!rearrangeClean.isEmpty() && rearrangeClean.get(0).sameType(rearrangeDirty.get(0))) {
rearrangeClean.remove(0);
rearrangeDirty.remove(0);
while (!rearrangeClean.isEmpty() && rearrangeClean.getFirst().sameType(rearrangeDirty.getFirst())) {
rearrangeClean.removeFirst();
rearrangeDirty.removeFirst();
}
// Remove trailing equal types
while (!rearrangeClean.isEmpty() && rearrangeClean.get(rearrangeClean.size() - 1).sameType(rearrangeDirty.get(rearrangeDirty.size() - 1))) {
rearrangeClean.remove(rearrangeClean.size() - 1);
rearrangeDirty.remove(rearrangeDirty.size() - 1);
while (!rearrangeClean.isEmpty() && rearrangeClean.getLast().sameType(rearrangeDirty.getLast())) {
rearrangeClean.removeLast();
rearrangeDirty.removeLast();
}
if (!rearrangeClean.isEmpty() && !rearrangeDirty.isEmpty()) {
builder.merge(tempDiff.build());
Expand Down Expand Up @@ -396,7 +396,7 @@ private static void compare(ParamsDiffSnapshotBuilder builder, List<TypeWithCont
if (DEBUG) {
LOGGER.info("Comparison results:\n\tInserted: {}\n\tReplaced: {}\n\tSwapped: {}\n\tRemoved: {}", diff.insertions(), diff.removals(), diff.swaps(), diff.removals());
}
int indexOffset = !dirty.isEmpty() ? dirty.get(0).pos() : 0;
int indexOffset = !dirty.isEmpty() ? dirty.getFirst().pos() : 0;
builder.merge(SimpleParamsDiffSnapshot.create(diff), indexOffset);
}

Expand Down Expand Up @@ -433,7 +433,7 @@ private static boolean sameTypeCount(Map<Type, Integer> cleanGroup, Map<Type, In
private static <T> List<T> extract(List<T> list, int amount) {
List<T> res = new ArrayList<>();
for (int i = 0; i < amount && !list.isEmpty(); i++) {
res.add(list.remove(0));
res.add(list.removeFirst());
}
return res;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static InterfacePatchBuilder interfaceBuilder() {
return new InterfacePatchInstance.InterfaceClassPatchBuilderImpl();
}

Result apply(ClassNode classNode, PatchEnvironment remaper);
Result apply(ClassNode classNode, PatchEnvironment environment);

Codec<? extends Patch> codec();

Expand Down Expand Up @@ -112,7 +112,7 @@ default ClassPatchBuilder targetConstant(double doubleValue) {
.orElseGet(() -> handle.getNested("at")
.flatMap(at -> at.<String>getValue("value").map(s -> s.get().equals("CONSTANT") &&
at.<List<String>>getValue("args").map(AnnotationValueHandle::get).map(t -> t.size() == 1
&& (t.get(0).equals("doubleValue=" + doubleValue + "D") || t.get(0).equals("doubleValue=" + doubleValue)))
&& (t.getFirst().equals("doubleValue=" + doubleValue + "D") || t.getFirst().equals("doubleValue=" + doubleValue)))
.orElse(false)))
.orElse(false)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.sinytra.adapter.patch.fixes;

import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.util.Pair;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.Type;
import org.sinytra.adapter.patch.api.GlobalReferenceMapper;

import java.util.List;
import java.util.Map;
Expand All @@ -24,14 +22,7 @@ public BytecodeFixerUpper(Map<String, Map<String, Pair<Type, Type>>> newFieldTyp
}

public BytecodeFixerUpper(Map<String, Map<String, Pair<Type, Type>>> newFieldTypes, List<TypeAdapter> fieldTypeAdapters, List<TypeAdapterProvider> dynamicTypeAdapters) {
// Remap field names
ImmutableMap.Builder<String, Map<String, Pair<Type, Type>>> builder = ImmutableMap.builder();
newFieldTypes.forEach((owner, fields) -> {
ImmutableMap.Builder<String, Pair<Type, Type>> fieldsBuilder = ImmutableMap.builder();
fields.forEach((k, v) -> fieldsBuilder.put(GlobalReferenceMapper.remapReference(k), v));
builder.put(owner, fieldsBuilder.build());
});
this.newFieldTypes = builder.build();
this.newFieldTypes = newFieldTypes;
this.fieldTypeAdapters = fieldTypeAdapters;
this.dynamicTypeAdapters = dynamicTypeAdapters;
this.generator = new BytecodeFixerJarGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Patch.Result apply(ClassNode classNode, MethodNode methodNode, MethodCont
String fieldFqn = AdapterUtil.getAccessorTargetFieldName(classNode.name, methodNode, methodContext.methodAnnotation(), context.environment()).orElse(null);
if (fieldFqn != null) {
String fieldName = new FieldMatcher(fieldFqn).getName();
Type owner = methodContext.targetTypes().get(0);
Type owner = methodContext.targetTypes().getFirst();
Pair<Type, Type> updatedTypes = bfu.getFieldTypeChange(owner.getInternalName(), fieldName);
if (updatedTypes != null) {
TypeAdapter typeAdapter = bfu.getTypeAdapter(updatedTypes.getSecond(), updatedTypes.getFirst());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.objectweb.asm.tree.*;
import org.sinytra.adapter.patch.analysis.MethodCallAnalyzer;
import org.sinytra.adapter.patch.api.ClassTransform;
import org.sinytra.adapter.patch.api.GlobalReferenceMapper;
import org.sinytra.adapter.patch.api.Patch;
import org.sinytra.adapter.patch.api.PatchContext;
import org.sinytra.adapter.patch.selector.AnnotationValueHandle;
Expand All @@ -30,10 +29,10 @@ public Patch.Result apply(ClassNode classNode, @Nullable AnnotationValueHandle<?
Map<String, Pair<Type, Type>> classUpdatedTypes = new HashMap<>();
// Update class field types
if (context.targetTypes().size() == 1) {
Type targetType = context.targetTypes().get(0);
Type targetType = context.targetTypes().getFirst();
for (FieldNode field : classNode.fields) {
if (AdapterUtil.isShadowField(field)) {
Pair<Type, Type> updatedTypes = bfu.getFieldTypeChange(targetType.getInternalName(), GlobalReferenceMapper.remapReference(field.name));
Pair<Type, Type> updatedTypes = bfu.getFieldTypeChange(targetType.getInternalName(), field.name);
if (updatedTypes != null) {
field.desc = updatedTypes.getSecond().getDescriptor();
// Update shadow field usages
Expand All @@ -56,7 +55,7 @@ public Patch.Result apply(ClassNode classNode, @Nullable AnnotationValueHandle<?
}

// Update used fields of other classes
Pair<Type, Type> updatedTypes = bfu.getFieldTypeChange(finsn.owner, GlobalReferenceMapper.remapReference(finsn.name));
Pair<Type, Type> updatedTypes = bfu.getFieldTypeChange(finsn.owner, finsn.name);
if (updatedTypes != null) {
applied |= runFieldFix(bfu, updatedTypes, method, finsn);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public <U> Optional<U> maybeUnwrap() {
Object value = get();
if (value instanceof List<?> list) {
if (!list.isEmpty()) {
return Optional.of((U) list.get(0));
return Optional.of((U) list.getFirst());
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mojang.serialization.Codec;
import org.jetbrains.annotations.Nullable;
import org.sinytra.adapter.patch.api.GlobalReferenceMapper;

import java.util.Objects;

Expand All @@ -15,8 +14,7 @@ public class FieldMatcher {

public FieldMatcher(String field) {
int descIndex = field.indexOf(':');
String name = descIndex == -1 ? field : field.substring(0, descIndex);
this.name = GlobalReferenceMapper.remapReference(name);
this.name = descIndex == -1 ? field : field.substring(0, descIndex);
this.desc = descIndex == -1 ? null : field.substring(descIndex + 1);
}

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

import com.mojang.serialization.Codec;
import org.jetbrains.annotations.Nullable;
import org.sinytra.adapter.patch.api.GlobalReferenceMapper;

import java.util.Objects;

Expand All @@ -15,8 +14,7 @@ public class MethodMatcher {

public MethodMatcher(String method) {
int descIndex = method.indexOf('(');
String name = descIndex == -1 ? method : method.substring(0, descIndex);
this.name = GlobalReferenceMapper.remapReference(name);
this.name = descIndex == -1 ? method : method.substring(0, descIndex);
this.desc = descIndex == -1 ? null : method.substring(descIndex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Patch.Result apply(ClassNode classNode, MethodNode methodNode, MethodCont
if (this.replacementMethods.size() > 1) {
throw new IllegalStateException("Cannot determine replacement @Overwrite method name, multiple specified: " + this.replacementMethods);
}
String replacement = this.replacementMethods.get(0);
String replacement = this.replacementMethods.getFirst();
MethodQualifier.create(replacement)
.map(MethodQualifier::name)
.ifPresent(str -> methodNode.name = str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Patch.Result apply(ClassNode classNode, MethodNode methodNode, MethodCont
if (types.size() == 1) {
Type[] params = Type.getArgumentTypes(methodNode.desc);
List<Type> newParams = new ArrayList<>(Arrays.asList(params));
newParams.add(0, types.get(0));
newParams.addFirst(types.getFirst());

methodContext.updateDescription(newParams);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Patch.Result apply(ClassNode classNode, MethodNode methodNode, MethodCont
addNewParamsPatch.apply(classNode, methodNode, methodContext, context);

LocalVariableLookup updatedLookup = new LocalVariableLookup(methodNode);
LocalVariableNode operationVar = updatedLookup.getForType(OPERATION_TYPE).get(0);
LocalVariableNode operationVar = updatedLookup.getForType(OPERATION_TYPE).getFirst();
int operationParamOrdinal = updatedLookup.getOrdinal(operationVar);
List<LocalVariableNode> localVars = new ArrayList<>();
for (int i = 1; i < operationParamOrdinal; i++) {
Expand All @@ -101,7 +101,7 @@ public Patch.Result apply(ClassNode classNode, MethodNode methodNode, MethodCont
OpcodeUtil.castObjectType(Type.getReturnType(this.newTarget.desc()), a);
});
for (List<AbstractInsnNode> list : insns) {
methodNode.instructions.insertBefore(list.get(0), originalCallInsns);
methodNode.instructions.insertBefore(list.getFirst(), originalCallInsns);
list.forEach(methodNode.instructions::remove);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Patch.Result apply(ClassNode classNode, @Nullable AnnotationValueHandle<?
return Patch.Result.PASS;
}

String targetReference = context.remap(targets.get(0));
String targetReference = context.remap(targets.getFirst());
if (!AdapterUtil.isAnonymousClass(targetReference)) {
return Patch.Result.PASS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private boolean isFixedField(AbstractInsnNode insn, PatchContext context) {
}
if (prev instanceof FieldInsnNode finsn) {
BytecodeFixerUpper bfu = context.environment().bytecodeFixerUpper();
return bfu.getFieldTypeChange(finsn.owner, GlobalReferenceMapper.remapReference(finsn.name)) != null;
return bfu.getFieldTypeChange(finsn.owner, finsn.name) != null;
}
}
return false;
Expand Down
Loading

0 comments on commit adcc207

Please sign in to comment.