Make CFR the default decompiler (#527)

* Make CFR the default decompiler
Expose decompiler options

* Remove convention, default value is an empty map.

* Checkstyle..
dev/0.11
modmuss50 2021-11-01 13:43:03 +00:00 committed by GitHub
parent d01c20f049
commit 5f379e4f42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 25 deletions

View File

@ -26,8 +26,9 @@ package net.fabricmc.loom.api.decompilers;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
import net.fabricmc.loom.util.IOStringConsumer; import net.fabricmc.loom.util.IOStringConsumer;
public record DecompilationMetadata(int numberOfThreads, Path javaDocs, Collection<Path> libraries, IOStringConsumer logger) { public record DecompilationMetadata(int numberOfThreads, Path javaDocs, Collection<Path> libraries, IOStringConsumer logger, Map<String, String> options) {
} }

View File

@ -32,6 +32,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import java.util.jar.JarOutputStream; import java.util.jar.JarOutputStream;
@ -64,7 +65,10 @@ public class LoomCFRDecompiler implements LoomDecompiler {
@Override @Override
public void decompile(Path compiledJar, Path sourcesDestination, Path linemapDestination, DecompilationMetadata metaData) { public void decompile(Path compiledJar, Path sourcesDestination, Path linemapDestination, DecompilationMetadata metaData) {
final String path = compiledJar.toAbsolutePath().toString(); final String path = compiledJar.toAbsolutePath().toString();
final Options options = OptionsImpl.getFactory().create(DECOMPILE_OPTIONS); final Map<String, String> allOptions = new HashMap<>(DECOMPILE_OPTIONS);
allOptions.putAll(metaData.options());
final Options options = OptionsImpl.getFactory().create(allOptions);
ClassFileSourceImpl classFileSource = new ClassFileSourceImpl(options); ClassFileSourceImpl classFileSource = new ClassFileSourceImpl(options);
@ -138,7 +142,7 @@ public class LoomCFRDecompiler implements LoomDecompiler {
decompiler.decompile(Paths.get("input.jar"), decompiler.decompile(Paths.get("input.jar"),
Paths.get("output-sources.jar"), Paths.get("output-sources.jar"),
lineMap, lineMap,
new DecompilationMetadata(4, null, Collections.emptyList(), null) new DecompilationMetadata(4, null, Collections.emptyList(), null, Collections.emptyMap())
); );
LineNumberRemapper lineNumberRemapper = new LineNumberRemapper(); LineNumberRemapper lineNumberRemapper = new LineNumberRemapper();

View File

@ -25,6 +25,7 @@
package net.fabricmc.loom.decompilers.fernflower; package net.fabricmc.loom.decompilers.fernflower;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.jetbrains.java.decompiler.main.Fernflower; import org.jetbrains.java.decompiler.main.Fernflower;
@ -38,12 +39,13 @@ import net.fabricmc.loom.api.decompilers.LoomDecompiler;
public final class FabricFernFlowerDecompiler implements LoomDecompiler { public final class FabricFernFlowerDecompiler implements LoomDecompiler {
@Override @Override
public String name() { public String name() {
return "FabricFlower"; // Or something else? return "FernFlower";
} }
@Override @Override
public void decompile(Path compiledJar, Path sourcesDestination, Path linemapDestination, DecompilationMetadata metaData) { public void decompile(Path compiledJar, Path sourcesDestination, Path linemapDestination, DecompilationMetadata metaData) {
Map<String, Object> options = Map.of( final Map<String, Object> options = new HashMap<>(
Map.of(
IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES, "1", IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES, "1",
IFernflowerPreferences.BYTECODE_SOURCE_MAPPING, "1", IFernflowerPreferences.BYTECODE_SOURCE_MAPPING, "1",
IFernflowerPreferences.REMOVE_SYNTHETIC, "1", IFernflowerPreferences.REMOVE_SYNTHETIC, "1",
@ -51,8 +53,11 @@ public final class FabricFernFlowerDecompiler implements LoomDecompiler {
IFernflowerPreferences.THREADS, String.valueOf(metaData.numberOfThreads()), IFernflowerPreferences.THREADS, String.valueOf(metaData.numberOfThreads()),
IFernflowerPreferences.INDENT_STRING, "\t", IFernflowerPreferences.INDENT_STRING, "\t",
IFabricJavadocProvider.PROPERTY_NAME, new TinyJavadocProvider(metaData.javaDocs().toFile()) IFabricJavadocProvider.PROPERTY_NAME, new TinyJavadocProvider(metaData.javaDocs().toFile())
)
); );
options.putAll(metaData.options());
IResultSaver saver = new ThreadSafeResultSaver(sourcesDestination::toFile, linemapDestination::toFile); IResultSaver saver = new ThreadSafeResultSaver(sourcesDestination::toFile, linemapDestination::toFile);
Fernflower ff = new Fernflower(FernFlowerUtils::getBytecode, saver, options, new FernflowerLogger(metaData.logger())); Fernflower ff = new Fernflower(FernFlowerUtils::getBytecode, saver, options, new FernflowerLogger(metaData.logger()));

View File

@ -43,6 +43,7 @@ import javax.inject.Inject;
import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.RegularFileProperty; import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property; import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile; import org.gradle.api.tasks.InputFile;
@ -83,6 +84,9 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
@Input @Input
public abstract Property<Long> getMaxMemory(); public abstract Property<Long> getMaxMemory();
@Input
public abstract MapProperty<String, String> getOptions();
@Inject @Inject
public abstract WorkerExecutor getWorkerExecutor(); public abstract WorkerExecutor getWorkerExecutor();
@ -98,6 +102,7 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
getOutputs().upToDateWhen((o) -> false); getOutputs().upToDateWhen((o) -> false);
getMaxMemory().convention(4096L).finalizeValueOnRead(); getMaxMemory().convention(4096L).finalizeValueOnRead();
getOptions().finalizeValueOnRead();
} }
@TaskAction @TaskAction
@ -134,6 +139,8 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
workQueue.submit(DecompileAction.class, params -> { workQueue.submit(DecompileAction.class, params -> {
params.getDecompilerClass().set(decompiler.getClass().getCanonicalName()); params.getDecompilerClass().set(decompiler.getClass().getCanonicalName());
params.getOptions().set(getOptions());
params.getInputJar().set(getInputJar()); params.getInputJar().set(getInputJar());
params.getRuntimeJar().set(getExtension().getMappingsProvider().mappedProvider.getMappedJar()); params.getRuntimeJar().set(getExtension().getMappingsProvider().mappedProvider.getMappedJar());
params.getSourcesDestinationJar().set(getMappedJarFileWithSuffix("-sources.jar")); params.getSourcesDestinationJar().set(getMappedJarFileWithSuffix("-sources.jar"));
@ -182,6 +189,8 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
public interface DecompileParams extends WorkParameters { public interface DecompileParams extends WorkParameters {
Property<String> getDecompilerClass(); Property<String> getDecompilerClass();
MapProperty<String, String> getOptions();
RegularFileProperty getInputJar(); RegularFileProperty getInputJar();
RegularFileProperty getRuntimeJar(); RegularFileProperty getRuntimeJar();
RegularFileProperty getSourcesDestinationJar(); RegularFileProperty getSourcesDestinationJar();
@ -231,7 +240,8 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(),
getParameters().getMappings().get().getAsFile().toPath(), getParameters().getMappings().get().getAsFile().toPath(),
getLibraries(), getLibraries(),
logger logger,
getParameters().getOptions().get()
); );
decompiler.decompile( decompiler.decompile(

View File

@ -34,7 +34,6 @@ import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.api.decompilers.LoomDecompiler; import net.fabricmc.loom.api.decompilers.LoomDecompiler;
import net.fabricmc.loom.configuration.ide.RunConfigSettings; import net.fabricmc.loom.configuration.ide.RunConfigSettings;
import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl; import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
import net.fabricmc.loom.decompilers.fernflower.FabricFernFlowerDecompiler;
import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.Constants;
public final class LoomTasks { public final class LoomTasks {
@ -129,7 +128,7 @@ public final class LoomTasks {
return; return;
} }
File inputJar = mappingsProvider.mappedProvider.getMappedJar(); File mappedJar = mappingsProvider.mappedProvider.getMappedJar();
if (mappingsProvider.hasUnpickDefinitions()) { if (mappingsProvider.hasUnpickDefinitions()) {
File outputJar = mappingsProvider.mappedProvider.getUnpickedJar(); File outputJar = mappingsProvider.mappedProvider.getUnpickedJar();
@ -140,21 +139,33 @@ public final class LoomTasks {
unpickJarTask.getOutputJar().set(outputJar); unpickJarTask.getOutputJar().set(outputJar);
}); });
inputJar = outputJar; mappedJar = outputJar;
} }
final File inputJar = mappedJar;
extension.getGameDecompilers().finalizeValue(); extension.getGameDecompilers().finalizeValue();
for (LoomDecompiler decompiler : extension.getGameDecompilers().get()) { for (LoomDecompiler decompiler : extension.getGameDecompilers().get()) {
String taskName = decompiler instanceof FabricFernFlowerDecompiler ? "genSources" : "genSourcesWith" + decompiler.name(); String taskName = "genSourcesWith" + decompiler.name();
// decompiler will be passed to the constructor of GenerateSourcesTask // Decompiler will be passed to the constructor of GenerateSourcesTask
GenerateSourcesTask generateSourcesTask = tasks.register(taskName, GenerateSourcesTask.class, decompiler).get(); tasks.register(taskName, GenerateSourcesTask.class, decompiler).configure(task -> {
generateSourcesTask.getInputJar().set(inputJar); task.setDescription("Decompile minecraft using %s.".formatted(decompiler.name()));
task.setGroup(Constants.TaskGroup.FABRIC);
task.getInputJar().set(inputJar);
if (mappingsProvider.hasUnpickDefinitions()) { if (mappingsProvider.hasUnpickDefinitions()) {
generateSourcesTask.dependsOn(tasks.getByName("unpickJar")); task.dependsOn(tasks.getByName("unpickJar"));
}
} }
}); });
} }
tasks.register("genSources", task -> {
task.setDescription("Decompile minecraft using the default decompiler.");
task.setGroup(Constants.TaskGroup.FABRIC);
task.dependsOn(project.getTasks().getByName("genSourcesWithCfr"));
});
});
}
} }

View File

@ -45,8 +45,8 @@ class DecompileTest extends Specification implements GradleProjectTestTrait {
where: where:
decompiler | task | version decompiler | task | version
'fernflower' | "genSources" | DEFAULT_GRADLE 'fernflower' | "genSourcesWithFernFlower" | DEFAULT_GRADLE
'fernflower' | "genSources" | PRE_RELEASE_GRADLE 'fernflower' | "genSourcesWithFernFlower" | PRE_RELEASE_GRADLE
'cfr' | "genSourcesWithCfr" | DEFAULT_GRADLE 'cfr' | "genSourcesWithCfr" | DEFAULT_GRADLE
'cfr' | "genSourcesWithCfr" | PRE_RELEASE_GRADLE 'cfr' | "genSourcesWithCfr" | PRE_RELEASE_GRADLE
} }