Add ability to specify additional tiny remapper options in remapJar (#292)

* Add ability to specify additional tiny remapper options in remapJar

* Imports go brr

* Fix checkstyle

Co-authored-by: modmuss50 <modmuss50@gmail.com>
dev/0.11
i509VCB 2020-12-21 15:02:39 -06:00 committed by GitHub
parent e20993daf8
commit b0860c36d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -29,8 +29,11 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.gradle.api.Action;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.file.FileCollection; import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFileProperty; import org.gradle.api.file.RegularFileProperty;
@ -59,6 +62,7 @@ public class RemapJarTask extends Jar {
private final RegularFileProperty input; private final RegularFileProperty input;
private final Property<Boolean> addNestedDependencies; private final Property<Boolean> addNestedDependencies;
private final Property<Boolean> remapAccessWidener; private final Property<Boolean> remapAccessWidener;
private final List<Action<TinyRemapper.Builder>> remapOptions = new ArrayList<>();
public JarRemapper jarRemapper; public JarRemapper jarRemapper;
private FileCollection classpath; private FileCollection classpath;
@ -107,6 +111,11 @@ public class RemapJarTask extends Jar {
} }
} }
// Apply any requested options to tiny remapper
for (Action<TinyRemapper.Builder> remapOption : this.remapOptions) {
remapOption.execute(remapperBuilder);
}
project.getLogger().lifecycle(":remapping " + input.getFileName()); project.getLogger().lifecycle(":remapping " + input.getFileName());
StringBuilder rc = new StringBuilder("Remap classpath: "); StringBuilder rc = new StringBuilder("Remap classpath: ");
@ -181,6 +190,9 @@ public class RemapJarTask extends Jar {
} }
} }
// Add remap options to the jar remapper
jarRemapper.addOptions(this.remapOptions);
jarRemapper.scheduleRemap(input, output) jarRemapper.scheduleRemap(input, output)
.supplyAccessWidener((remapData, remapper) -> { .supplyAccessWidener((remapData, remapper) -> {
if (getRemapAccessWidener().getOrElse(false) && extension.accessWidener != null) { if (getRemapAccessWidener().getOrElse(false) && extension.accessWidener != null) {
@ -251,6 +263,10 @@ public class RemapJarTask extends Jar {
return remapAccessWidener; return remapAccessWidener;
} }
public void remapOptions(Action<TinyRemapper.Builder> action) {
this.remapOptions.add(action);
}
public RemapJarTask classpath(FileCollection collection) { public RemapJarTask classpath(FileCollection collection) {
if (this.classpath == null) { if (this.classpath == null) {
this.classpath = collection; this.classpath = collection;

View File

@ -34,6 +34,7 @@ import java.util.Set;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import org.gradle.api.Action;
import org.objectweb.asm.commons.Remapper; import org.objectweb.asm.commons.Remapper;
import net.fabricmc.stitch.util.Pair; import net.fabricmc.stitch.util.Pair;
@ -46,6 +47,7 @@ public class JarRemapper {
private final List<IMappingProvider> mappingProviders = new ArrayList<>(); private final List<IMappingProvider> mappingProviders = new ArrayList<>();
private final Set<Path> classPath = new HashSet<>(); private final Set<Path> classPath = new HashSet<>();
private final List<RemapData> remapData = new ArrayList<>(); private final List<RemapData> remapData = new ArrayList<>();
private List<Action<TinyRemapper.Builder>> remapOptions;
public void addMappings(IMappingProvider mappingProvider) { public void addMappings(IMappingProvider mappingProvider) {
mappingProviders.add(mappingProvider); mappingProviders.add(mappingProvider);
@ -65,6 +67,12 @@ public class JarRemapper {
TinyRemapper.Builder remapperBuilder = TinyRemapper.newRemapper(); TinyRemapper.Builder remapperBuilder = TinyRemapper.newRemapper();
mappingProviders.forEach(remapperBuilder::withMappings); mappingProviders.forEach(remapperBuilder::withMappings);
if (remapOptions != null) {
for (Action<TinyRemapper.Builder> remapOption : remapOptions) {
remapOption.execute(remapperBuilder);
}
}
TinyRemapper remapper = remapperBuilder.build(); TinyRemapper remapper = remapperBuilder.build();
Path[] remapClasspath = classPath.stream() Path[] remapClasspath = classPath.stream()
@ -102,6 +110,10 @@ public class JarRemapper {
remapData.forEach(RemapData::complete); remapData.forEach(RemapData::complete);
} }
public void addOptions(List<Action<TinyRemapper.Builder>> remapOptions) {
this.remapOptions = remapOptions;
}
public static class RemapData { public static class RemapData {
public final Path input; public final Path input;
public final Path output; public final Path output;