Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into next
Browse files Browse the repository at this point in the history
# Conflicts:
#	build.gradle
  • Loading branch information
shedaniel committed Nov 14, 2023
2 parents a70d7e5 + bbb4677 commit d16a1ac
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 50 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

version = '1.9.' + (System.getenv("GITHUB_RUN_NUMBER") ?: "9999")
version = '1.10.' + (System.getenv("GITHUB_RUN_NUMBER") ?: "9999")

def ENV = System.getenv()

Expand All @@ -21,10 +21,10 @@ repositories {
}

dependencies {
api 'org.ow2.asm:asm:9.3'
api 'org.ow2.asm:asm-commons:9.3'
implementation 'org.ow2.asm:asm-tree:9.3'
implementation 'org.ow2.asm:asm-util:9.3'
api 'org.ow2.asm:asm:9.6'
api 'org.ow2.asm:asm-commons:9.6'
implementation 'org.ow2.asm:asm-tree:9.6'
implementation 'org.ow2.asm:asm-util:9.6'

testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ public interface Extension {

public interface AnalyzeVisitorProvider {
ClassVisitor insertAnalyzeVisitor(int mrjVersion, String className, ClassVisitor next);

default ClassVisitor insertAnalyzeVisitor(int mrjVersion, String className, ClassVisitor next, /* @Nullable */ InputTag[] inputTags) {
return insertAnalyzeVisitor(mrjVersion, className, next);
}
}

public interface StateProcessor {
Expand All @@ -299,6 +303,10 @@ public interface StateProcessor {

public interface ApplyVisitorProvider {
ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next);

default ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next, /* @Nullable */ InputTag[] inputTags) {
return insertApplyVisitor(cls, next);
}
}

private TinyRemapper(Set<IMappingProvider> mappingProviders, boolean ignoreFieldDesc,
Expand Down Expand Up @@ -767,7 +775,7 @@ public FieldVisitor visitField(int access, String name, String desc, String sign
};

for (int i = analyzeVisitors.size() - 1; i >= 0; i--) {
cv = analyzeVisitors.get(i).insertAnalyzeVisitor(mrjVersion, name, cv);
cv = analyzeVisitors.get(i).insertAnalyzeVisitor(mrjVersion, name, cv, tags);
}

reader.accept(cv, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
Expand Down Expand Up @@ -1354,14 +1362,14 @@ private byte[] apply(final ClassInstance cls) {
}

for (int i = postApplyVisitors.size() - 1; i >= 0; i--) {
visitor = postApplyVisitors.get(i).insertApplyVisitor(cls, visitor);
visitor = postApplyVisitors.get(i).insertApplyVisitor(cls, visitor, cls.getInputTags());
}

visitor = new AsmClassRemapper(visitor, cls.getContext().remapper, rebuildSourceFilenames,
checkPackageAccess, skipLocalMapping, renameInvalidLocals, invalidLvNamePattern, inferNameFromSameLvIndex);

for (int i = preApplyVisitors.size() - 1; i >= 0; i--) {
visitor = preApplyVisitors.get(i).insertApplyVisitor(cls, visitor);
visitor = preApplyVisitors.get(i).insertApplyVisitor(cls, visitor, cls.getInputTags());
}

reader.accept(visitor, flags);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2021, FabricMC
* Copyright (c) 2021, 2023, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -18,16 +18,19 @@

package net.fabricmc.tinyremapper.extension.mixin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import java.util.function.Predicate;

import org.objectweb.asm.ClassVisitor;

import net.fabricmc.tinyremapper.InputTag;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.TinyRemapper.Builder;
import net.fabricmc.tinyremapper.api.TrClass;
Expand All @@ -40,11 +43,19 @@

/**
* A extension for remapping mixin annotation.
*
* <h2>Input filtering</h2>
*
* <p>The mixin extension can be applied to specific input tags by providing an input tag filter in the constructor.
* An input with nonnull input tags is only processed if it has a tag matching the filter.
*
* <p>If the filter is null, all inputs will be processed.
*/
public class MixinExtension implements TinyRemapper.Extension {
private final Logger logger;
private final Map<Integer, List<Consumer<CommonData>>> tasks;
private final Map<Integer, Collection<Consumer<CommonData>>> tasks;
private final Set<AnnotationTarget> targets;
private final /* @Nullable */ Predicate<InputTag> inputTagFilter;

public enum AnnotationTarget {
/**
Expand Down Expand Up @@ -74,35 +85,36 @@ public MixinExtension(Set<AnnotationTarget> targets) {
this(targets, Level.WARN);
}

public MixinExtension(/* @Nullable */ Predicate<InputTag> inputTagFilter) {
this(EnumSet.allOf(AnnotationTarget.class), Level.WARN, inputTagFilter);
}

public MixinExtension(Set<AnnotationTarget> targets, Logger.Level logLevel) {
this(targets, logLevel, null);
}

public MixinExtension(Set<AnnotationTarget> targets, Logger.Level logLevel, /* @Nullable */ Predicate<InputTag> inputTagFilter) {
this.logger = new Logger(logLevel);
this.tasks = new HashMap<>();
this.tasks = new ConcurrentHashMap<>();
this.targets = targets;
this.inputTagFilter = inputTagFilter;
}

@Override
public void attach(Builder builder) {
if (targets.contains(AnnotationTarget.HARD)) {
builder.extraAnalyzeVisitor(this::analyzeVisitor).extraStateProcessor(this::stateProcessor);
builder.extraAnalyzeVisitor(new AnalyzeVisitorProvider()).extraStateProcessor(this::stateProcessor);
}

if (targets.contains(AnnotationTarget.SOFT)) {
builder.extraPreApplyVisitor(this::preApplyVisitor);
builder.extraPreApplyVisitor(new PreApplyVisitorProvider());
}
}

/**
* Hard-target: Shadow, Overwrite, Accessor, Invoker, Implements.
*/
private ClassVisitor analyzeVisitor(int mrjVersion, String className, ClassVisitor next) {
tasks.putIfAbsent(mrjVersion, new ArrayList<>());
return new HardTargetMixinClassVisitor(tasks.get(mrjVersion), next);
}

private void stateProcessor(TrEnvironment environment) {
CommonData data = new CommonData(environment, logger);

for (Consumer<CommonData> task : tasks.get(environment.getMrjVersion())) {
for (Consumer<CommonData> task : tasks.getOrDefault(environment.getMrjVersion(), Collections.emptyList())) {
try {
task.accept(data);
} catch (RuntimeException e) {
Expand All @@ -111,11 +123,54 @@ private void stateProcessor(TrEnvironment environment) {
}
}

/**
* Hard-target: Shadow, Overwrite, Accessor, Invoker, Implements.
*/
private final class AnalyzeVisitorProvider implements TinyRemapper.AnalyzeVisitorProvider {
@Override
public ClassVisitor insertAnalyzeVisitor(int mrjVersion, String className, ClassVisitor next) {
return new HardTargetMixinClassVisitor(tasks.computeIfAbsent(mrjVersion, k -> new ConcurrentLinkedQueue<>()), next);
}

@Override
public ClassVisitor insertAnalyzeVisitor(int mrjVersion, String className, ClassVisitor next, InputTag[] inputTags) {
if (inputTagFilter == null || inputTags == null) {
return insertAnalyzeVisitor(mrjVersion, className, next);
} else {
for (InputTag tag : inputTags) {
if (inputTagFilter.test(tag)) {
return insertAnalyzeVisitor(mrjVersion, className, next);
}
}

return next;
}
}
}

/**
* Soft-target: Mixin, Invoker, Accessor, Inject, ModifyArg, ModifyArgs, Redirect, ModifyVariable, ModifyConstant, At, Slice.
*/
public ClassVisitor preApplyVisitor(TrClass cls, ClassVisitor next) {
return new SoftTargetMixinClassVisitor(new CommonData(cls.getEnvironment(), logger), next);
private final class PreApplyVisitorProvider implements TinyRemapper.ApplyVisitorProvider {
@Override
public ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next) {
return new SoftTargetMixinClassVisitor(new CommonData(cls.getEnvironment(), logger), next);
}

@Override
public ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next, InputTag[] inputTags) {
if (inputTagFilter == null || inputTags == null) {
return insertApplyVisitor(cls, next);
} else {
for (InputTag tag : inputTags) {
if (inputTagFilter.test(tag)) {
return insertApplyVisitor(cls, next);
}
}

return next;
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2021, FabricMC
* Copyright (c) 2021, 2023, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -19,6 +19,7 @@
package net.fabricmc.tinyremapper.extension.mixin.hard;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand All @@ -41,7 +42,7 @@
import net.fabricmc.tinyremapper.extension.mixin.hard.data.SoftInterface;

public class HardTargetMixinClassVisitor extends ClassVisitor {
private final List<Consumer<CommonData>> tasks;
private final Collection<Consumer<CommonData>> tasks;
private MxClass _class;

// @Mixin
Expand All @@ -51,7 +52,7 @@ public class HardTargetMixinClassVisitor extends ClassVisitor {
// @Implements
private final List<SoftInterface> interfaces = new ArrayList<>();

public HardTargetMixinClassVisitor(List<Consumer<CommonData>> tasks, ClassVisitor delegate) {
public HardTargetMixinClassVisitor(Collection<Consumer<CommonData>> tasks, ClassVisitor delegate) {
super(Constant.ASM_VERSION, delegate);
this.tasks = Objects.requireNonNull(tasks);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2021, FabricMC
* Copyright (c) 2021, 2023, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -18,6 +18,7 @@

package net.fabricmc.tinyremapper.extension.mixin.hard;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
Expand All @@ -32,13 +33,13 @@
import net.fabricmc.tinyremapper.extension.mixin.hard.annotation.ShadowAnnotationVisitor;

class HardTargetMixinFieldVisitor extends FieldVisitor {
private final List<Consumer<CommonData>> tasks;
private final Collection<Consumer<CommonData>> tasks;
private final MxMember field;

private final boolean remap;
private final List<String> targets;

HardTargetMixinFieldVisitor(List<Consumer<CommonData>> tasks, FieldVisitor delegate, MxMember field,
HardTargetMixinFieldVisitor(Collection<Consumer<CommonData>> tasks, FieldVisitor delegate, MxMember field,
boolean remap, List<String> targets) {
super(Constant.ASM_VERSION, delegate);
this.tasks = Objects.requireNonNull(tasks);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2021, FabricMC
* Copyright (c) 2021, 2023, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -18,6 +18,7 @@

package net.fabricmc.tinyremapper.extension.mixin.hard;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
Expand All @@ -35,13 +36,13 @@
import net.fabricmc.tinyremapper.extension.mixin.hard.annotation.ShadowAnnotationVisitor;

class HardTargetMixinMethodVisitor extends MethodVisitor {
private final List<Consumer<CommonData>> data;
private final Collection<Consumer<CommonData>> data;
private final MxMember method;

private final boolean remap;
private final List<String> targets;

HardTargetMixinMethodVisitor(List<Consumer<CommonData>> data, MethodVisitor delegate, MxMember method, boolean remap, List<String> targets) {
HardTargetMixinMethodVisitor(Collection<Consumer<CommonData>> data, MethodVisitor delegate, MxMember method, boolean remap, List<String> targets) {
super(Constant.ASM_VERSION, delegate);
this.data = Objects.requireNonNull(data);
this.method = Objects.requireNonNull(method);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2021, FabricMC
* Copyright (c) 2021, 2023, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -42,14 +42,14 @@
* do not lower the first character of the remaining part.
*/
public class AccessorAnnotationVisitor extends AnnotationVisitor {
private final List<Consumer<CommonData>> tasks;
private final Collection<Consumer<CommonData>> tasks;
private final MxMember method;
private final List<String> targets;

private boolean remap;
private boolean isSoftTarget;

public AccessorAnnotationVisitor(List<Consumer<CommonData>> tasks, AnnotationVisitor delegate, MxMember method, boolean remap, List<String> targets) {
public AccessorAnnotationVisitor(Collection<Consumer<CommonData>> tasks, AnnotationVisitor delegate, MxMember method, boolean remap, List<String> targets) {
super(Constant.ASM_VERSION, delegate);

this.tasks = Objects.requireNonNull(tasks);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2021, FabricMC
* Copyright (c) 2021, 2023, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -81,7 +81,7 @@ public AnnotationVisitor visitAnnotation(String name, String descriptor) {
}
}

public static void visitMethod(List<Consumer<CommonData>> tasks, MxMember method, List<SoftInterface> interfaces) {
public static void visitMethod(Collection<Consumer<CommonData>> tasks, MxMember method, List<SoftInterface> interfaces) {
tasks.add(data -> new SoftImplementsMappable(data, method, interfaces).result());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2021, FabricMC
* Copyright (c) 2021, 2023, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -40,14 +40,14 @@
* do not lower the first character of the remaining part.
*/
public class InvokerAnnotationVisitor extends AnnotationVisitor {
private final List<Consumer<CommonData>> tasks;
private final Collection<Consumer<CommonData>> tasks;
private final MxMember method;
private final List<String> targets;

private boolean remap;
private boolean isSoftTarget;

public InvokerAnnotationVisitor(List<Consumer<CommonData>> tasks, AnnotationVisitor delegate, MxMember method, boolean remap, List<String> targets) {
public InvokerAnnotationVisitor(Collection<Consumer<CommonData>> tasks, AnnotationVisitor delegate, MxMember method, boolean remap, List<String> targets) {
super(Constant.ASM_VERSION, delegate);

this.tasks = Objects.requireNonNull(tasks);
Expand Down
Loading

0 comments on commit d16a1ac

Please sign in to comment.