From ae383b8d4bc44962f2648099f01f2fc74838c036 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sat, 4 Dec 2021 19:53:32 +0200 Subject: Juuzify ArchitecturyLoomDecompiler (#60) * Juuzify ArchitecturyLoomDecompiler * Remove unused max memory --- .../architectury/ArchitecturyLoomDecompiler.java | 26 +++++++++++++-- .../loom/task/ArchitecturyGenerateSourcesTask.java | 39 ++++++++++++++++------ .../fabricmc/loom/task/GenerateSourcesTask.java | 9 +++-- 3 files changed, 58 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main/java/net/fabricmc/loom/api/decompilers/architectury/ArchitecturyLoomDecompiler.java b/src/main/java/net/fabricmc/loom/api/decompilers/architectury/ArchitecturyLoomDecompiler.java index 1a1f4b64..c6988b8f 100644 --- a/src/main/java/net/fabricmc/loom/api/decompilers/architectury/ArchitecturyLoomDecompiler.java +++ b/src/main/java/net/fabricmc/loom/api/decompilers/architectury/ArchitecturyLoomDecompiler.java @@ -24,12 +24,32 @@ package net.fabricmc.loom.api.decompilers.architectury; -import org.gradle.api.logging.Logger; +import org.gradle.api.Project; -import net.fabricmc.loom.task.GenerateSourcesTask; +import net.fabricmc.loom.api.decompilers.LoomDecompiler; +/** + * A decompiler definition. Differs from {@link LoomDecompiler} by allowing + * to use a project context for creating the decompiler, which lets you make + * configurable decompilers. + * + *
Note that JVM forking is not handled by this interface, and that is + * the responsibility of the decompiler implementation. + */ public interface ArchitecturyLoomDecompiler { + /** + * {@return the name of the decompiler} + * It is used for naming the source generation task ({@code genSourcesWith[name]}). + */ String name(); - void decompile(Logger logger, GenerateSourcesTask.DecompileParams params); + /** + * Creates a {@link LoomDecompiler} from a {@link Project} context. + * + *
The name of the created decompiler is not used.
+ *
+ * @param project the project context
+ * @return the created decompiler
+ */
+ LoomDecompiler create(Project project);
}
diff --git a/src/main/java/net/fabricmc/loom/task/ArchitecturyGenerateSourcesTask.java b/src/main/java/net/fabricmc/loom/task/ArchitecturyGenerateSourcesTask.java
index a1c65b32..402cee1e 100644
--- a/src/main/java/net/fabricmc/loom/task/ArchitecturyGenerateSourcesTask.java
+++ b/src/main/java/net/fabricmc/loom/task/ArchitecturyGenerateSourcesTask.java
@@ -25,15 +25,20 @@
package net.fabricmc.loom.task;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
import javax.inject.Inject;
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;
import org.gradle.api.tasks.TaskAction;
+import net.fabricmc.loom.api.decompilers.DecompilationMetadata;
import net.fabricmc.loom.api.decompilers.architectury.ArchitecturyLoomDecompiler;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.OperatingSystem;
@@ -60,20 +65,32 @@ public abstract class ArchitecturyGenerateSourcesTask extends AbstractLoomTask {
throw new UnsupportedOperationException("GenSources task requires a 64bit JVM to run due to the memory requirements.");
}
- GenerateSourcesTask.DecompileParams params = getProject().getObjects().newInstance(GenerateSourcesTask.DecompileParams.class);
-
// TODO: Need a good way to not keep a duplicated code for this
- params.getOptions().set(getOptions());
+ Path compiledJar = getInputJar().get().getAsFile().toPath();
+ Path runtimeJar = getExtension().getMappingsProvider().mappedProvider.getMappedJar().toPath();
+ Path sourcesDestination = GenerateSourcesTask.getMappedJarFileWithSuffix(getProject(), "-sources.jar").toPath();
+ Path linemapDestination = GenerateSourcesTask.getMappedJarFileWithSuffix(getProject(), "-sources.lmap").toPath();
+ DecompilationMetadata metadata = new DecompilationMetadata(
+ Runtime.getRuntime().availableProcessors(),
+ GenerateSourcesTask.getMappings(getProject(), getExtension()),
+ GenerateSourcesTask.DecompileAction.toPaths(getProject().getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES)),
+ getLogger()::info,
+ getOptions().get()
+ );
- params.getInputJar().set(getInputJar());
- params.getRuntimeJar().set(getExtension().getMappingsProvider().mappedProvider.getMappedJar());
- params.getSourcesDestinationJar().set(GenerateSourcesTask.getMappedJarFileWithSuffix(getProject(), "-sources.jar"));
- params.getLinemap().set(GenerateSourcesTask.getMappedJarFileWithSuffix(getProject(), "-sources.lmap"));
- params.getLinemapJar().set(GenerateSourcesTask.getMappedJarFileWithSuffix(getProject(), "-linemapped.jar"));
- params.getMappings().set(GenerateSourcesTask.getMappings(getProject(), getExtension()).toFile());
+ decompiler.create(getProject()).decompile(compiledJar, sourcesDestination, linemapDestination, metadata);
- params.getClassPath().setFrom(getProject().getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES));
+ // Apply linemap
+ if (Files.exists(linemapDestination)) {
+ Path linemapJar = GenerateSourcesTask.getMappedJarFileWithSuffix(getProject(), "-linemapped.jar").toPath();
- decompiler.decompile(getLogger(), params);
+ try {
+ GenerateSourcesTask.DecompileAction.remapLineNumbers(getLogger()::info, runtimeJar, linemapDestination, linemapJar);
+ Files.copy(linemapJar, runtimeJar, StandardCopyOption.REPLACE_EXISTING);
+ Files.delete(linemapJar);
+ } catch (Exception e) {
+ throw new RuntimeException("Could not remap line numbers", e);
+ }
+ }
}
}
diff --git a/src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java b/src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java
index 67ea201f..ab4f6929 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.Project;
import org.gradle.api.file.ConfigurableFileCollection;
+import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
@@ -272,7 +273,7 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
}
}
- private void remapLineNumbers(IOStringConsumer logger, Path oldCompiledJar, Path linemap, Path linemappedJarDestination) throws IOException {
+ static void remapLineNumbers(IOStringConsumer logger, Path oldCompiledJar, Path linemap, Path linemappedJarDestination) throws IOException {
LineNumberRemapper remapper = new LineNumberRemapper();
remapper.readMappings(linemap.toFile());
@@ -283,7 +284,11 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
}
private Collection