Skip to content

Commit fd56b9b

Browse files
authored
Migrate from AccessWidener to ClassTweaker. (#1094)
Class Tweaker is a superset of Access Widener and is intended to offer more kinds of data driven bytecode manipulation. The fabric mod json V1 definition accessWidener can now supply Class Tweakers as well. The old AW file format remains supported.
1 parent 9efe3fd commit fd56b9b

File tree

13 files changed

+61
-63
lines changed

13 files changed

+61
-63
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ dependencies {
113113
include 'org.ow2.sat4j:org.ow2.sat4j.core:2.3.6'
114114
include 'org.ow2.sat4j:org.ow2.sat4j.pb:2.3.6'
115115
include "net.fabricmc:tiny-remapper:0.11.2"
116-
include "net.fabricmc:access-widener:2.1.0"
116+
include "net.fabricmc:class-tweaker:0.2"
117117
include "net.fabricmc:mapping-io:0.7.1"
118118

119119
development "io.github.llamalad7:mixinextras-fabric:$mixin_extras_version"
@@ -203,7 +203,7 @@ tasks.register('fatJar', ShadowJar) {
203203
configurations = [project.configurations.include]
204204

205205
relocate 'org.sat4j', 'net.fabricmc.loader.impl.lib.sat4j'
206-
relocate 'net.fabricmc.accesswidener', 'net.fabricmc.loader.impl.lib.accesswidener'
206+
relocate 'net.fabricmc.classtweaker', 'net.fabricmc.loader.impl.lib.classtweaker'
207207
relocate 'net.fabricmc.tinyremapper', 'net.fabricmc.loader.impl.lib.tinyremapper'
208208
relocate 'net.fabricmc.mappingio', 'net.fabricmc.loader.impl.lib.mappingio'
209209

minecraft/src/main/java/net/fabricmc/loader/impl/game/minecraft/launchwrapper/FabricTweaker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private void init() {
140140
loader.freeze();
141141

142142
launchClassLoader.registerTransformer(FabricClassTransformer.class.getName());
143-
FabricLoaderImpl.INSTANCE.loadAccessWideners();
143+
FabricLoaderImpl.INSTANCE.loadClassTweakers();
144144

145145
// Setup Mixin environment
146146
MixinBootstrap.init();

src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
import org.jetbrains.annotations.VisibleForTesting;
3939
import org.objectweb.asm.Opcodes;
4040

41-
import net.fabricmc.accesswidener.AccessWidener;
42-
import net.fabricmc.accesswidener.AccessWidenerReader;
4341
import net.fabricmc.api.EnvType;
42+
import net.fabricmc.classtweaker.api.ClassTweaker;
43+
import net.fabricmc.classtweaker.api.ClassTweakerReader;
4444
import net.fabricmc.loader.api.LanguageAdapter;
4545
import net.fabricmc.loader.api.MappingResolver;
4646
import net.fabricmc.loader.api.ModContainer;
@@ -90,7 +90,7 @@ public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader {
9090

9191
private final Map<String, LanguageAdapter> adapterMap = new HashMap<>();
9292
private final EntrypointStorage entrypointStorage = new EntrypointStorage();
93-
private final AccessWidener accessWidener = new AccessWidener();
93+
private final ClassTweaker classTweaker = ClassTweaker.newInstance();
9494

9595
private final ObjectShare objectShare = new ObjectShareImpl();
9696

@@ -517,21 +517,21 @@ private void setupMods() {
517517
}
518518
}
519519

520-
public void loadAccessWideners() {
521-
AccessWidenerReader accessWidenerReader = new AccessWidenerReader(accessWidener);
520+
public void loadClassTweakers() {
521+
ClassTweakerReader ctReader = ClassTweakerReader.create(classTweaker);
522522

523523
for (net.fabricmc.loader.api.ModContainer modContainer : getAllMods()) {
524524
LoaderModMetadata modMetadata = (LoaderModMetadata) modContainer.getMetadata();
525-
String accessWidener = modMetadata.getAccessWidener();
526-
if (accessWidener == null) continue;
525+
String location = modMetadata.getClassTweaker();
526+
if (location == null) continue;
527527

528-
Path path = modContainer.findPath(accessWidener).orElse(null);
529-
if (path == null) throw new RuntimeException(String.format("Missing accessWidener file %s from mod %s", accessWidener, modContainer.getMetadata().getId()));
528+
Path path = modContainer.findPath(location).orElse(null);
529+
if (path == null) throw new RuntimeException(String.format("Missing classTweaker file %s from mod %s", location, modContainer.getMetadata().getId()));
530530

531531
try (BufferedReader reader = Files.newBufferedReader(path)) {
532-
accessWidenerReader.read(reader, FabricLauncherBase.getLauncher().getMappingConfiguration().getRuntimeNamespace());
532+
ctReader.read(reader, FabricLauncherBase.getLauncher().getMappingConfiguration().getRuntimeNamespace());
533533
} catch (Exception e) {
534-
throw new RuntimeException("Failed to read accessWidener file from mod " + modMetadata.getId(), e);
534+
throw new RuntimeException("Failed to read classTweaker file from mod " + modMetadata.getId(), e);
535535
}
536536
}
537537
}
@@ -591,8 +591,8 @@ public void prepareModInit(Path newRunDir, Object gameInstance) {
591591
}
592592
}
593593

594-
public AccessWidener getAccessWidener() {
595-
return accessWidener;
594+
public ClassTweaker getClassTweaker() {
595+
return classTweaker;
596596
}
597597

598598
/**

src/main/java/net/fabricmc/loader/impl/discovery/BuiltinMetadataWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public Collection<String> getMixinConfigs(EnvType type) {
158158
}
159159

160160
@Override
161-
public String getAccessWidener() {
161+
public String getClassTweaker() {
162162
return null;
163163
}
164164

src/main/java/net/fabricmc/loader/impl/discovery/RuntimeModRemapper.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@
3939

4040
import org.objectweb.asm.commons.Remapper;
4141

42-
import net.fabricmc.accesswidener.AccessWidener;
43-
import net.fabricmc.accesswidener.AccessWidenerClassVisitor;
44-
import net.fabricmc.accesswidener.AccessWidenerReader;
45-
import net.fabricmc.accesswidener.AccessWidenerRemapper;
46-
import net.fabricmc.accesswidener.AccessWidenerWriter;
42+
import net.fabricmc.classtweaker.api.ClassTweaker;
43+
import net.fabricmc.classtweaker.api.ClassTweakerReader;
44+
import net.fabricmc.classtweaker.api.ClassTweakerWriter;
45+
import net.fabricmc.classtweaker.visitors.ClassTweakerRemapperVisitor;
4746
import net.fabricmc.loader.impl.FabricLoaderImpl;
4847
import net.fabricmc.loader.impl.FormattedException;
4948
import net.fabricmc.loader.impl.launch.FabricLauncher;
@@ -90,8 +89,8 @@ public static void remap(Collection<ModCandidateImpl> modCandidates, Path tmpDir
9089
try {
9190
FabricLauncher launcher = FabricLauncherBase.getLauncher();
9291

93-
AccessWidener mergedAccessWidener = new AccessWidener();
94-
mergedAccessWidener.visitHeader(modNs);
92+
ClassTweaker mergedClassTweaker = ClassTweaker.newInstance();
93+
mergedClassTweaker.visitHeader(modNs);
9594

9695
for (ModCandidateImpl mod : modsToRemap) {
9796
RemapInfo info = new RemapInfo();
@@ -110,19 +109,19 @@ public static void remap(Collection<ModCandidateImpl> modCandidates, Path tmpDir
110109
info.outputPath = outputDir.resolve(mod.getDefaultFileName());
111110
Files.deleteIfExists(info.outputPath);
112111

113-
String accessWidener = mod.getMetadata().getAccessWidener();
112+
String classTweaker = mod.getMetadata().getClassTweaker();
114113

115-
if (accessWidener != null) {
116-
info.accessWidenerPath = accessWidener;
114+
if (classTweaker != null) {
115+
info.classTweakerPath = classTweaker;
117116

118117
try (FileSystemUtil.FileSystemDelegate jarFs = FileSystemUtil.getJarFileSystem(info.inputPath, false)) {
119118
FileSystem fs = jarFs.get();
120-
info.accessWidener = Files.readAllBytes(fs.getPath(accessWidener));
119+
info.classTweaker = Files.readAllBytes(fs.getPath(classTweaker));
121120
} catch (Throwable t) {
122-
throw new RuntimeException("Error reading access widener for mod '" +mod.getId()+ "'!", t);
121+
throw new RuntimeException("Error reading class tweaker for mod '" +mod.getId()+ "'!", t);
123122
}
124123

125-
new AccessWidenerReader(mergedAccessWidener).read(info.accessWidener);
124+
ClassTweakerReader.create(mergedClassTweaker).read(info.classTweaker, modNs);
126125
}
127126
}
128127

@@ -131,7 +130,7 @@ public static void remap(Collection<ModCandidateImpl> modCandidates, Path tmpDir
131130
.renameInvalidLocals(false)
132131
.extension(new MixinExtension(remapMixins::contains))
133132
.extraAnalyzeVisitor((mrjVersion, className, next) ->
134-
AccessWidenerClassVisitor.createClassVisitor(FabricLoaderImpl.ASM_VERSION, next, mergedAccessWidener))
133+
mergedClassTweaker.createClassVisitor(FabricLoaderImpl.ASM_VERSION, next, null))
135134
.build();
136135

137136
try {
@@ -176,8 +175,8 @@ public static void remap(Collection<ModCandidateImpl> modCandidates, Path tmpDir
176175
for (ModCandidateImpl mod : modsToRemap) {
177176
RemapInfo info = infoMap.get(mod);
178177

179-
if (info.accessWidener != null) {
180-
info.accessWidener = remapAccessWidener(info.accessWidener, remapper.getEnvironment().getRemapper(), modNs, runtimeNs);
178+
if (info.classTweaker != null) {
179+
info.classTweaker = remapClassTweaker(info.classTweaker, remapper.getEnvironment().getRemapper(), modNs, runtimeNs);
181180
}
182181
}
183182

@@ -188,12 +187,12 @@ public static void remap(Collection<ModCandidateImpl> modCandidates, Path tmpDir
188187

189188
info.outputConsumerPath.close();
190189

191-
if (info.accessWidenerPath != null) {
190+
if (info.classTweakerPath != null) {
192191
try (FileSystemUtil.FileSystemDelegate jarFs = FileSystemUtil.getJarFileSystem(info.outputPath, false)) {
193192
FileSystem fs = jarFs.get();
194193

195-
Files.delete(fs.getPath(info.accessWidenerPath));
196-
Files.write(fs.getPath(info.accessWidenerPath), info.accessWidener);
194+
Files.delete(fs.getPath(info.classTweakerPath));
195+
Files.write(fs.getPath(info.classTweakerPath), info.classTweaker);
197196
}
198197
}
199198

@@ -228,12 +227,12 @@ public static void remap(Collection<ModCandidateImpl> modCandidates, Path tmpDir
228227
}
229228
}
230229

231-
private static byte[] remapAccessWidener(byte[] input, Remapper remapper, String modNs, String runtimeNs) {
232-
AccessWidenerWriter writer = new AccessWidenerWriter();
233-
AccessWidenerRemapper remappingDecorator = new AccessWidenerRemapper(writer, remapper, modNs, runtimeNs);
234-
AccessWidenerReader accessWidenerReader = new AccessWidenerReader(remappingDecorator);
235-
accessWidenerReader.read(input, modNs);
236-
return writer.write();
230+
private static byte[] remapClassTweaker(byte[] input, Remapper remapper, String modNs, String runtimeNs) {
231+
ClassTweakerWriter writer = ClassTweakerWriter.create(ClassTweaker.CT_LATEST);
232+
ClassTweakerRemapperVisitor remappingDecorator = new ClassTweakerRemapperVisitor(writer, remapper, modNs, runtimeNs);
233+
ClassTweakerReader reader = ClassTweakerReader.create(remappingDecorator);
234+
reader.read(input, modNs);
235+
return writer.getOutput();
237236
}
238237

239238
private static List<Path> getRemapClasspath() throws IOException {
@@ -270,7 +269,7 @@ private static class RemapInfo {
270269
Path outputPath;
271270
boolean inputIsTemp;
272271
OutputConsumerPath outputConsumerPath;
273-
String accessWidenerPath;
274-
byte[] accessWidener;
272+
String classTweakerPath;
273+
byte[] classTweaker;
275274
}
276275
}

src/main/java/net/fabricmc/loader/impl/game/LoaderLibrary.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.sat4j.specs.ContradictionException;
2929
import org.spongepowered.asm.launch.MixinBootstrap;
3030

31-
import net.fabricmc.accesswidener.AccessWidener;
3231
import net.fabricmc.api.EnvType;
32+
import net.fabricmc.classtweaker.api.ClassTweaker;
3333
import net.fabricmc.loader.impl.util.UrlConversionException;
3434
import net.fabricmc.loader.impl.util.UrlUtil;
3535
import net.fabricmc.mappingio.tree.MappingTree;
@@ -40,7 +40,7 @@ enum LoaderLibrary {
4040
MAPPING_IO(MappingTree.class),
4141
SPONGE_MIXIN(MixinBootstrap.class),
4242
TINY_REMAPPER(TinyRemapper.class),
43-
ACCESS_WIDENER(AccessWidener.class),
43+
CLASS_TWEAKER(ClassTweaker.class),
4444
ASM(ClassReader.class),
4545
ASM_ANALYSIS(Analyzer.class),
4646
ASM_COMMONS(Remapper.class),

src/main/java/net/fabricmc/loader/impl/launch/knot/Knot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public ClassLoader init(String[] args) {
142142
loader.load();
143143
loader.freeze();
144144

145-
FabricLoaderImpl.INSTANCE.loadAccessWideners();
145+
FabricLoaderImpl.INSTANCE.loadClassTweakers();
146146

147147
FabricMixinBootstrap.init(getEnvironmentType(), loader);
148148
FabricLauncherBase.finishMixinBootstrapping();

src/main/java/net/fabricmc/loader/impl/metadata/LoaderModMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ default String getOldStyleLanguageAdapter() {
3939
Collection<NestedJarEntry> getJars();
4040
Collection<String> getMixinConfigs(EnvType type);
4141
/* @Nullable */
42-
String getAccessWidener();
42+
String getClassTweaker();
4343
@Override
4444
boolean loadsInEnvironment(EnvType type);
4545

src/main/java/net/fabricmc/loader/impl/metadata/V0ModMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public Collection<String> getMixinConfigs(EnvType type) {
247247
}
248248

249249
@Override
250-
public String getAccessWidener() {
250+
public String getClassTweaker() {
251251
return null; // intentional null
252252
}
253253

src/main/java/net/fabricmc/loader/impl/metadata/V1ModMetadata.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class V1ModMetadata extends AbstractModMetadata implements LoaderModMetada
5050
private final Collection<NestedJarEntry> jars;
5151
private final Collection<MixinEntry> mixins;
5252
/* @Nullable */
53-
private final String accessWidener;
53+
private final String classTweaker;
5454

5555
// Optional (dependency resolution)
5656
private Collection<ModDependency> dependencies;
@@ -75,7 +75,7 @@ final class V1ModMetadata extends AbstractModMetadata implements LoaderModMetada
7575

7676
V1ModMetadata(String id, Version version, Collection<String> provides,
7777
ModEnvironment environment, Map<String, List<EntrypointMetadata>> entrypoints, Collection<NestedJarEntry> jars,
78-
Collection<MixinEntry> mixins, /* @Nullable */ String accessWidener,
78+
Collection<MixinEntry> mixins, /* @Nullable */ String classTweaker,
7979
Collection<ModDependency> dependencies, boolean hasRequires,
8080
/* @Nullable */ String name, /* @Nullable */String description,
8181
Collection<Person> authors, Collection<Person> contributors, /* @Nullable */ContactInformation contact, Collection<String> license, IconEntry icon,
@@ -88,7 +88,7 @@ final class V1ModMetadata extends AbstractModMetadata implements LoaderModMetada
8888
this.entrypoints = Collections.unmodifiableMap(entrypoints);
8989
this.jars = Collections.unmodifiableCollection(jars);
9090
this.mixins = Collections.unmodifiableCollection(mixins);
91-
this.accessWidener = accessWidener;
91+
this.classTweaker = classTweaker;
9292
this.dependencies = Collections.unmodifiableCollection(dependencies);
9393
this.hasRequires = hasRequires;
9494
this.name = name;
@@ -244,8 +244,8 @@ public Collection<String> getMixinConfigs(EnvType type) {
244244
}
245245

246246
@Override
247-
public String getAccessWidener() {
248-
return this.accessWidener;
247+
public String getClassTweaker() {
248+
return this.classTweaker;
249249
}
250250

251251
@Override

0 commit comments

Comments
 (0)