From 5f379e4f426951d55f0ac4889c5207f80afcf86c Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Mon, 1 Nov 2021 13:43:03 +0000 Subject: [PATCH] Make CFR the default decompiler (#527) * Make CFR the default decompiler Expose decompiler options * Remove convention, default value is an empty map. * Checkstyle.. --- .../decompilers/DecompilationMetadata.java | 3 +- .../decompilers/cfr/LoomCFRDecompiler.java | 8 +++-- .../FabricFernFlowerDecompiler.java | 23 ++++++++------ .../loom/task/GenerateSourcesTask.java | 12 ++++++- .../net/fabricmc/loom/task/LoomTasks.java | 31 +++++++++++++------ .../test/integration/DecompileTest.groovy | 4 +-- 6 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/api/decompilers/DecompilationMetadata.java b/src/main/java/net/fabricmc/loom/api/decompilers/DecompilationMetadata.java index a4f6dcf..191a045 100644 --- a/src/main/java/net/fabricmc/loom/api/decompilers/DecompilationMetadata.java +++ b/src/main/java/net/fabricmc/loom/api/decompilers/DecompilationMetadata.java @@ -26,8 +26,9 @@ package net.fabricmc.loom.api.decompilers; import java.nio.file.Path; import java.util.Collection; +import java.util.Map; import net.fabricmc.loom.util.IOStringConsumer; -public record DecompilationMetadata(int numberOfThreads, Path javaDocs, Collection libraries, IOStringConsumer logger) { +public record DecompilationMetadata(int numberOfThreads, Path javaDocs, Collection libraries, IOStringConsumer logger, Map options) { } diff --git a/src/main/java/net/fabricmc/loom/decompilers/cfr/LoomCFRDecompiler.java b/src/main/java/net/fabricmc/loom/decompilers/cfr/LoomCFRDecompiler.java index bbfb0be..cb71513 100644 --- a/src/main/java/net/fabricmc/loom/decompilers/cfr/LoomCFRDecompiler.java +++ b/src/main/java/net/fabricmc/loom/decompilers/cfr/LoomCFRDecompiler.java @@ -32,6 +32,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.jar.Attributes; import java.util.jar.JarOutputStream; @@ -64,7 +65,10 @@ public class LoomCFRDecompiler implements LoomDecompiler { @Override public void decompile(Path compiledJar, Path sourcesDestination, Path linemapDestination, DecompilationMetadata metaData) { final String path = compiledJar.toAbsolutePath().toString(); - final Options options = OptionsImpl.getFactory().create(DECOMPILE_OPTIONS); + final Map allOptions = new HashMap<>(DECOMPILE_OPTIONS); + allOptions.putAll(metaData.options()); + + final Options options = OptionsImpl.getFactory().create(allOptions); ClassFileSourceImpl classFileSource = new ClassFileSourceImpl(options); @@ -138,7 +142,7 @@ public class LoomCFRDecompiler implements LoomDecompiler { decompiler.decompile(Paths.get("input.jar"), Paths.get("output-sources.jar"), lineMap, - new DecompilationMetadata(4, null, Collections.emptyList(), null) + new DecompilationMetadata(4, null, Collections.emptyList(), null, Collections.emptyMap()) ); LineNumberRemapper lineNumberRemapper = new LineNumberRemapper(); diff --git a/src/main/java/net/fabricmc/loom/decompilers/fernflower/FabricFernFlowerDecompiler.java b/src/main/java/net/fabricmc/loom/decompilers/fernflower/FabricFernFlowerDecompiler.java index c300944..9ad8111 100644 --- a/src/main/java/net/fabricmc/loom/decompilers/fernflower/FabricFernFlowerDecompiler.java +++ b/src/main/java/net/fabricmc/loom/decompilers/fernflower/FabricFernFlowerDecompiler.java @@ -25,6 +25,7 @@ package net.fabricmc.loom.decompilers.fernflower; import java.nio.file.Path; +import java.util.HashMap; import java.util.Map; import org.jetbrains.java.decompiler.main.Fernflower; @@ -38,21 +39,25 @@ import net.fabricmc.loom.api.decompilers.LoomDecompiler; public final class FabricFernFlowerDecompiler implements LoomDecompiler { @Override public String name() { - return "FabricFlower"; // Or something else? + return "FernFlower"; } @Override public void decompile(Path compiledJar, Path sourcesDestination, Path linemapDestination, DecompilationMetadata metaData) { - Map options = Map.of( - IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES, "1", - IFernflowerPreferences.BYTECODE_SOURCE_MAPPING, "1", - IFernflowerPreferences.REMOVE_SYNTHETIC, "1", - IFernflowerPreferences.LOG_LEVEL, "trace", - IFernflowerPreferences.THREADS, String.valueOf(metaData.numberOfThreads()), - IFernflowerPreferences.INDENT_STRING, "\t", - IFabricJavadocProvider.PROPERTY_NAME, new TinyJavadocProvider(metaData.javaDocs().toFile()) + final Map options = new HashMap<>( + Map.of( + IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES, "1", + IFernflowerPreferences.BYTECODE_SOURCE_MAPPING, "1", + IFernflowerPreferences.REMOVE_SYNTHETIC, "1", + IFernflowerPreferences.LOG_LEVEL, "trace", + IFernflowerPreferences.THREADS, String.valueOf(metaData.numberOfThreads()), + IFernflowerPreferences.INDENT_STRING, "\t", + IFabricJavadocProvider.PROPERTY_NAME, new TinyJavadocProvider(metaData.javaDocs().toFile()) + ) ); + options.putAll(metaData.options()); + IResultSaver saver = new ThreadSafeResultSaver(sourcesDestination::toFile, linemapDestination::toFile); Fernflower ff = new Fernflower(FernFlowerUtils::getBytecode, saver, options, new FernflowerLogger(metaData.logger())); diff --git a/src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java b/src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java index c8057ff..9a127d8 100644 --- a/src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java +++ b/src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java @@ -43,6 +43,7 @@ import javax.inject.Inject; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.provider.MapProperty; import org.gradle.api.provider.Property; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputFile; @@ -83,6 +84,9 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask { @Input public abstract Property getMaxMemory(); + @Input + public abstract MapProperty getOptions(); + @Inject public abstract WorkerExecutor getWorkerExecutor(); @@ -98,6 +102,7 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask { getOutputs().upToDateWhen((o) -> false); getMaxMemory().convention(4096L).finalizeValueOnRead(); + getOptions().finalizeValueOnRead(); } @TaskAction @@ -134,6 +139,8 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask { workQueue.submit(DecompileAction.class, params -> { params.getDecompilerClass().set(decompiler.getClass().getCanonicalName()); + params.getOptions().set(getOptions()); + params.getInputJar().set(getInputJar()); params.getRuntimeJar().set(getExtension().getMappingsProvider().mappedProvider.getMappedJar()); params.getSourcesDestinationJar().set(getMappedJarFileWithSuffix("-sources.jar")); @@ -182,6 +189,8 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask { public interface DecompileParams extends WorkParameters { Property getDecompilerClass(); + MapProperty getOptions(); + RegularFileProperty getInputJar(); RegularFileProperty getRuntimeJar(); RegularFileProperty getSourcesDestinationJar(); @@ -231,7 +240,8 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask { Runtime.getRuntime().availableProcessors(), getParameters().getMappings().get().getAsFile().toPath(), getLibraries(), - logger + logger, + getParameters().getOptions().get() ); decompiler.decompile( diff --git a/src/main/java/net/fabricmc/loom/task/LoomTasks.java b/src/main/java/net/fabricmc/loom/task/LoomTasks.java index 8847741..e914046 100644 --- a/src/main/java/net/fabricmc/loom/task/LoomTasks.java +++ b/src/main/java/net/fabricmc/loom/task/LoomTasks.java @@ -34,7 +34,6 @@ import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.api.decompilers.LoomDecompiler; import net.fabricmc.loom.configuration.ide.RunConfigSettings; import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl; -import net.fabricmc.loom.decompilers.fernflower.FabricFernFlowerDecompiler; import net.fabricmc.loom.util.Constants; public final class LoomTasks { @@ -129,7 +128,7 @@ public final class LoomTasks { return; } - File inputJar = mappingsProvider.mappedProvider.getMappedJar(); + File mappedJar = mappingsProvider.mappedProvider.getMappedJar(); if (mappingsProvider.hasUnpickDefinitions()) { File outputJar = mappingsProvider.mappedProvider.getUnpickedJar(); @@ -140,21 +139,33 @@ public final class LoomTasks { unpickJarTask.getOutputJar().set(outputJar); }); - inputJar = outputJar; + mappedJar = outputJar; } + final File inputJar = mappedJar; + extension.getGameDecompilers().finalizeValue(); for (LoomDecompiler decompiler : extension.getGameDecompilers().get()) { - String taskName = decompiler instanceof FabricFernFlowerDecompiler ? "genSources" : "genSourcesWith" + decompiler.name(); - // decompiler will be passed to the constructor of GenerateSourcesTask - GenerateSourcesTask generateSourcesTask = tasks.register(taskName, GenerateSourcesTask.class, decompiler).get(); - generateSourcesTask.getInputJar().set(inputJar); + String taskName = "genSourcesWith" + decompiler.name(); + // Decompiler will be passed to the constructor of GenerateSourcesTask + tasks.register(taskName, GenerateSourcesTask.class, decompiler).configure(task -> { + task.setDescription("Decompile minecraft using %s.".formatted(decompiler.name())); + task.setGroup(Constants.TaskGroup.FABRIC); + task.getInputJar().set(inputJar); - if (mappingsProvider.hasUnpickDefinitions()) { - generateSourcesTask.dependsOn(tasks.getByName("unpickJar")); - } + if (mappingsProvider.hasUnpickDefinitions()) { + 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")); + }); }); } } diff --git a/src/test/groovy/net/fabricmc/loom/test/integration/DecompileTest.groovy b/src/test/groovy/net/fabricmc/loom/test/integration/DecompileTest.groovy index daa2a51..d2ed4b2 100644 --- a/src/test/groovy/net/fabricmc/loom/test/integration/DecompileTest.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/integration/DecompileTest.groovy @@ -45,8 +45,8 @@ class DecompileTest extends Specification implements GradleProjectTestTrait { where: decompiler | task | version - 'fernflower' | "genSources" | DEFAULT_GRADLE - 'fernflower' | "genSources" | PRE_RELEASE_GRADLE + 'fernflower' | "genSourcesWithFernFlower" | DEFAULT_GRADLE + 'fernflower' | "genSourcesWithFernFlower" | PRE_RELEASE_GRADLE 'cfr' | "genSourcesWithCfr" | DEFAULT_GRADLE 'cfr' | "genSourcesWithCfr" | PRE_RELEASE_GRADLE }