Skip to content

Commit

Permalink
Fix lambda expansion analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Jul 19, 2024
1 parent 4c3e67f commit dbae5f0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface MethodTransform {
default Codec<? extends MethodTransform> codec() {
throw new UnsupportedOperationException("This transform is not serializable");
throw new UnsupportedOperationException("Transform %s is not serializable".formatted(getClass()));
}

default Collection<String> getAcceptedAnnotations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.sinytra.adapter.patch.PatchInstance.MIXINPATCH;
import static org.sinytra.adapter.patch.transformer.operation.param.ParamTransformationUtil.findWrapOperationOriginalCall;

// TODO Just add @Coerce if the types are inherited
public record ReplaceParametersTransformer(int index, Type type, boolean upgradeUsage) implements ParameterTransformer {
static final Codec<ReplaceParametersTransformer> CODEC = RecordCodecBuilder.create(in -> in.group(
Codec.intRange(0, 255).fieldOf("index").forGetter(ReplaceParametersTransformer::index),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ private MethodTransformationPipeline(MethodTransform transform, List<Supplier<Me
this.filters = filters;
}

@Override
public Codec<? extends MethodTransform> codec() {
return CODEC;
}

@Override
public Patch.Result apply(ClassNode classNode, MethodNode methodNode, MethodContext methodContext, PatchContext context) {
for (MethodTransformFilter filter : filters) {
Expand Down
25 changes: 13 additions & 12 deletions plugin/src/main/java/org/sinytra/adapter/gradle/ClassAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,27 +382,28 @@ private void findExpandedLambdas(List<? super Patch> patches, Map<? super String
for (int cleanIdx = 0, dirtyIdx = 0; cleanIdx < cleanLambdas.size() && dirtyIdx < dirtyLambdas.size(); ) {
String cleanLambda = cleanLambdas.get(cleanIdx);
String dirtyLambda = dirtyLambdas.get(cleanIdx);
if (cleanLambda.equals(dirtyLambda)) {
MethodNode cleanLambdaMethod = findUniqueMethod(this.cleanMethods, cleanLambda);
MethodNode dirtyLambdaMethod = findUniqueMethod(this.dirtyMethods, dirtyLambda);
if (cleanLambdaMethod.name.equals(dirtyLambdaMethod.name) && cleanLambdaMethod.desc.equals(dirtyLambdaMethod.desc)) {
cleanIdx++;
dirtyIdx++;
} else {
boolean noDirty;
boolean removed = !dirtyLambdas.contains(cleanLambda);
// Lambda removed in Forge, ignore
if (noDirty = !dirtyLambdas.contains(cleanLambda)) {
if (removed) {
cleanIdx++;
}
// Lambda added by Forge, ignore
if (!cleanLambdas.contains(dirtyLambda)) {
if (cleanLambdas.contains(dirtyLambda)) {
cleanIdx++;
dirtyIdx++;

// Lambda (likely) modified by Forge, proceed
if (noDirty) {
MethodNode cleanLambdaMethod = findUniqueMethod(this.cleanMethods, cleanLambda);
MethodNode dirtyLambdaMethod = findUniqueMethod(this.dirtyMethods, dirtyLambda);
if (!removed) {
tryFindExpandedMethod(patches, replacementCalls, cleanLambdaMethod, dirtyLambdaMethod);
}
} else {
cleanIdx++;
}
// Lambda added by Forge, ignore
else {
dirtyIdx++;
}
}
Expand Down Expand Up @@ -453,7 +454,7 @@ private void tryFindExpandedMethod(List<? super PatchInstance> patches, Map<? su
Type[] parameterTypes = Type.getArgumentTypes(clean.desc);
LayeredParamsDiffSnapshot diff = EnhancedParamsDiff.compareMethodParameters(clean, dirty);
if (!diff.isEmpty()) {
LayeredParamsDiffSnapshot valid = valiadateSnapshot(diff, parameterTypes, clean, dirty);
LayeredParamsDiffSnapshot valid = validateSnapshot(diff, parameterTypes, clean, dirty);
if (valid == null) {
return;
}
Expand All @@ -480,7 +481,7 @@ private void tryFindExpandedMethod(List<? super PatchInstance> patches, Map<? su
}
}

private LayeredParamsDiffSnapshot valiadateSnapshot(LayeredParamsDiffSnapshot snapshot, Type[] parameterTypes, MethodNode clean, MethodNode dirty) {
private LayeredParamsDiffSnapshot validateSnapshot(LayeredParamsDiffSnapshot snapshot, Type[] parameterTypes, MethodNode clean, MethodNode dirty) {
if (!snapshot.replacements().isEmpty()) {
List<Pair<Integer, Type>> newReplacements = new ArrayList<>(snapshot.replacements());
List<Pair<Integer, Integer>> swaps = new ArrayList<>();
Expand Down

0 comments on commit dbae5f0

Please sign in to comment.