Skip to content

Commit f00529c

Browse files
committed
Refactor analyze, state and apply hooks
1 parent 34de558 commit f00529c

3 files changed

Lines changed: 51 additions & 45 deletions

File tree

src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@
6363

6464
import net.fabricmc.tinyremapper.IMappingProvider.MappingAcceptor;
6565
import net.fabricmc.tinyremapper.IMappingProvider.Member;
66-
import net.fabricmc.tinyremapper.api.StateProcessor;
66+
import net.fabricmc.tinyremapper.api.TrClass;
6767
import net.fabricmc.tinyremapper.api.TrEnvironment;
6868
import net.fabricmc.tinyremapper.api.TrMember;
6969
import net.fabricmc.tinyremapper.api.TrMember.MemberType;
70-
import net.fabricmc.tinyremapper.api.WrapperFunction;
7170

7271
public class TinyRemapper {
7372
public static class Builder {
@@ -156,8 +155,17 @@ public Builder renameInvalidLocals(boolean value) {
156155
return this;
157156
}
158157

158+
@Deprecated
159159
public Builder extraAnalyzeVisitor(ClassVisitor visitor) {
160-
extraAnalyzeVisitor = visitor;
160+
return extraAnalyzeVisitor((mrjVersion, className, next) -> {
161+
if (next != null) throw new UnsupportedOperationException("can't chain fixed instance analyze visitors");
162+
163+
return visitor;
164+
});
165+
}
166+
167+
public Builder extraAnalyzeVisitor(AnalyzeVisitorProvider provider) {
168+
extraAnalyzeVisitors.add(provider);
161169
return this;
162170
}
163171

@@ -171,13 +179,13 @@ public Builder extraRemapper(Remapper remapper) {
171179
return this;
172180
}
173181

174-
public Builder extraPreApplyVisitor(WrapperFunction func) {
175-
this.preApplyVisitors = func;
182+
public Builder extraPreApplyVisitor(ApplyVisitorProvider provider) {
183+
preApplyVisitors.add(provider);
176184
return this;
177185
}
178186

179-
public Builder extraPostApplyVisitor(WrapperFunction func) {
180-
this.postApplyVisitors = func;
187+
public Builder extraPostApplyVisitor(ApplyVisitorProvider provider) {
188+
this.postApplyVisitors.add(provider);
181189
return this;
182190
}
183191

@@ -188,7 +196,8 @@ public TinyRemapper build() {
188196
propagateBridges, propagateRecordComponents,
189197
removeFrames, ignoreConflicts, resolveMissing, checkPackageAccess || fixPackageAccess, fixPackageAccess,
190198
rebuildSourceFilenames, skipLocalMapping, renameInvalidLocals,
191-
extraAnalyzeVisitor, stateProcessors, extraRemapper, preApplyVisitors, postApplyVisitors);
199+
extraAnalyzeVisitors, stateProcessors, preApplyVisitors, postApplyVisitors,
200+
extraRemapper);
192201

193202
return remapper;
194203
}
@@ -209,10 +218,23 @@ public TinyRemapper build() {
209218
private boolean rebuildSourceFilenames = false;
210219
private boolean skipLocalMapping = false;
211220
private boolean renameInvalidLocals = false;
212-
private ClassVisitor extraAnalyzeVisitor;
221+
private final List<AnalyzeVisitorProvider> extraAnalyzeVisitors = new ArrayList<>();
213222
private final List<StateProcessor> stateProcessors = new ArrayList<>();
223+
private final List<ApplyVisitorProvider> preApplyVisitors = new ArrayList<>();
224+
private final List<ApplyVisitorProvider> postApplyVisitors = new ArrayList<>();
214225
private Remapper extraRemapper;
215-
private WrapperFunction preApplyVisitors, postApplyVisitors;
226+
}
227+
228+
public interface AnalyzeVisitorProvider {
229+
ClassVisitor insertAnalyzeVisitor(int mrjVersion, String className, ClassVisitor next);
230+
}
231+
232+
public interface StateProcessor {
233+
void process(TrEnvironment env);
234+
}
235+
236+
public interface ApplyVisitorProvider {
237+
ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next);
216238
}
217239

218240
private TinyRemapper(Collection<IMappingProvider> mappingProviders, boolean ignoreFieldDesc,
@@ -228,8 +250,9 @@ private TinyRemapper(Collection<IMappingProvider> mappingProviders, boolean igno
228250
boolean rebuildSourceFilenames,
229251
boolean skipLocalMapping,
230252
boolean renameInvalidLocals,
231-
ClassVisitor extraAnalyzeVisitor, List<StateProcessor> stateProcessors, Remapper extraRemapper,
232-
WrapperFunction preApplyVisitors, WrapperFunction postApplyVisitors) {
253+
List<AnalyzeVisitorProvider> extraAnalyzeVisitors, List<StateProcessor> stateProcessors,
254+
List<ApplyVisitorProvider> preApplyVisitors, List<ApplyVisitorProvider> postApplyVisitors,
255+
Remapper extraRemapper) {
233256
this.mappingProviders = mappingProviders;
234257
this.ignoreFieldDesc = ignoreFieldDesc;
235258
this.threadCount = threadCount > 0 ? threadCount : Math.max(Runtime.getRuntime().availableProcessors(), 2);
@@ -247,11 +270,11 @@ private TinyRemapper(Collection<IMappingProvider> mappingProviders, boolean igno
247270
this.rebuildSourceFilenames = rebuildSourceFilenames;
248271
this.skipLocalMapping = skipLocalMapping;
249272
this.renameInvalidLocals = renameInvalidLocals;
250-
this.extraAnalyzeVisitor = extraAnalyzeVisitor;
273+
this.extraAnalyzeVisitors = extraAnalyzeVisitors;
251274
this.stateProcessors = stateProcessors;
252-
this.extraRemapper = extraRemapper;
253275
this.preApplyVisitors = preApplyVisitors;
254276
this.postApplyVisitors = postApplyVisitors;
277+
this.extraRemapper = extraRemapper;
255278
}
256279

257280
public static Builder newRemapper() {
@@ -520,11 +543,16 @@ private ClassInstance analyze(boolean isInput, InputTag[] tags, Path srcPath, Pa
520543

521544
final ClassInstance ret = new ClassInstance(this, isInput, tags, srcPath, isInput ? data : null);
522545

523-
reader.accept(new ClassVisitor(Opcodes.ASM9, extraAnalyzeVisitor) {
546+
reader.accept(new ClassVisitor(Opcodes.ASM9) {
524547
@Override
525548
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
526549
int mrjVersion = analyzeMrjVersion(file, name);
527550
ret.init(mrjVersion, name, signature, superName, access, interfaces);
551+
552+
for (int i = extraAnalyzeVisitors.size() - 1; i >= 0; i--) {
553+
cv = extraAnalyzeVisitors.get(i).insertAnalyzeVisitor(mrjVersion, name, cv);
554+
}
555+
528556
super.visit(version, access, name, signature, superName, interfaces);
529557
}
530558

@@ -998,17 +1026,14 @@ private byte[] apply(final ClassInstance cls) {
9981026
visitor = new CheckClassAdapter(visitor);
9991027
}
10001028

1001-
MrjState state = cls.getContext();
1002-
AsmRemapper remapper = cls.getContext().remapper;
1003-
1004-
if (postApplyVisitors != null) {
1005-
visitor = postApplyVisitors.wrap(visitor, remapper, state);
1029+
for (int i = postApplyVisitors.size() - 1; i >= 0; i--) {
1030+
visitor = postApplyVisitors.get(i).insertApplyVisitor(cls, visitor);
10061031
}
10071032

1008-
visitor = new AsmClassRemapper(visitor, remapper, rebuildSourceFilenames, checkPackageAccess, skipLocalMapping, renameInvalidLocals);
1033+
visitor = new AsmClassRemapper(visitor, cls.getContext().remapper, rebuildSourceFilenames, checkPackageAccess, skipLocalMapping, renameInvalidLocals);
10091034

1010-
if (preApplyVisitors != null) {
1011-
visitor = preApplyVisitors.wrap(visitor, remapper, state);
1035+
for (int i = preApplyVisitors.size() - 1; i >= 0; i--) {
1036+
visitor = preApplyVisitors.get(i).insertApplyVisitor(cls, visitor);
10121037
}
10131038

10141039
reader.accept(visitor, flags);
@@ -1251,8 +1276,10 @@ public void propagate(TrMember m, String newName) {
12511276
private final boolean rebuildSourceFilenames;
12521277
private final boolean skipLocalMapping;
12531278
private final boolean renameInvalidLocals;
1254-
private final ClassVisitor extraAnalyzeVisitor;
1279+
private final List<AnalyzeVisitorProvider> extraAnalyzeVisitors;
12551280
private final List<StateProcessor> stateProcessors;
1281+
private final List<ApplyVisitorProvider> preApplyVisitors;
1282+
private final List<ApplyVisitorProvider> postApplyVisitors;
12561283
final Remapper extraRemapper;
12571284

12581285
final AtomicReference<Map<InputTag, InputTag[]>> singleInputTags = new AtomicReference<>(Collections.emptyMap()); // cache for tag -> { tag }
@@ -1278,7 +1305,6 @@ public void propagate(TrMember m, String newName) {
12781305
final boolean ignoreFieldDesc;
12791306
private final int threadCount;
12801307
private final ExecutorService threadPool;
1281-
final WrapperFunction preApplyVisitors, postApplyVisitors;
12821308

12831309
private volatile boolean dirty = true; // volatile to make the state debug asserts more reliable, shouldn't actually see concurrent modifications
12841310
private Map<ClassInstance, byte[]> outputBuffer;

src/main/java/net/fabricmc/tinyremapper/api/StateProcessor.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/java/net/fabricmc/tinyremapper/api/WrapperFunction.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)