From 1e2e8e7e568305eb12e3c892327046490431e677 Mon Sep 17 00:00:00 2001 From: Gamebuster19901 Date: Sat, 20 Jul 2024 00:32:43 -0400 Subject: [PATCH] Push changes --- build.gradle | 11 +-- gradle.properties | 4 +- .../wildermods/workspace/Dependencies.java | 3 +- .../workspace/decomp/DecompilerBuilder.java | 90 +++++++++++++++++++ .../decomp/WilderWorkspaceDecompiler.java | 61 +++++++++++++ .../WilderWorkspaceJavadocProvider.java | 51 +++++++++++ .../workspace/tasks/DecompileJarsTask.java | 21 +++-- .../workspace/tasks/GenerateSourcesTask.java | 7 ++ .../util/GradlePrintStreamLogger.java | 11 ++- 9 files changed, 237 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/wildermods/workspace/decomp/DecompilerBuilder.java create mode 100644 src/main/java/com/wildermods/workspace/decomp/WilderWorkspaceDecompiler.java create mode 100644 src/main/java/com/wildermods/workspace/decomp/WilderWorkspaceJavadocProvider.java create mode 100644 src/main/java/com/wildermods/workspace/tasks/GenerateSourcesTask.java diff --git a/build.gradle b/build.gradle index b0e7d53..5ae353d 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,7 @@ dependencies { implementation group: 'org.apache.commons', name: 'commons-lang3', version: commonsLangVersion implementation group: 'org.apache.commons', name: 'commons-text', version: commonsTextVersion implementation group: 'commons-io', name: 'commons-io', version: commonsIOVersion + implementation group: 'net.fabricmc', name: 'fabric-loom', version: loomVersion implementation group: 'org.vineflower', name: 'vineflower', version: vineFlowerVersion } @@ -37,6 +38,7 @@ task processSource(type: Sync) { inputs.property 'commonsLangVersion', commonsLangVersion inputs.property 'commonsIOVersion', commonsIOVersion inputs.property 'commonsTextVersion', commonsTextVersion + inputs.property 'loomVersion', loomVersion inputs.property 'vineFlowerVersion', vineFlowerVersion filter(ReplaceTokens, tokens: [ @@ -44,6 +46,7 @@ task processSource(type: Sync) { commonsLangVersion: commonsLangVersion, commonsTextVersion: commonsTextVersion, commonsIOVersion: commonsIOVersion, + loomVersion: loomVersion, vineFlowerVersion: vineFlowerVersion ]) } @@ -53,14 +56,6 @@ compileJava { dependsOn processSource } -sourceSets { - main { - java { - srcDirs = ["$buildDir/processedSrc"] - } - } -} - def pom2; publishing { diff --git a/gradle.properties b/gradle.properties index b863e44..0933bdc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,4 +2,6 @@ workspaceVersion = 4.0.0.0 commonsLangVersion = 3.14.0 commonsTextVersion = 1.12.0 commonsIOVersion = 2.16.1 -vineFlowerVersion = 1.10.1 \ No newline at end of file +fernFlowerVersion = 2.0.0 +vineFlowerVersion = 1.10.1 +loomVersion = 1.7.2 \ No newline at end of file diff --git a/src/main/java/com/wildermods/workspace/Dependencies.java b/src/main/java/com/wildermods/workspace/Dependencies.java index d62b4ef..a0afa92 100644 --- a/src/main/java/com/wildermods/workspace/Dependencies.java +++ b/src/main/java/com/wildermods/workspace/Dependencies.java @@ -7,7 +7,8 @@ enum Dependencies implements Dependency { COMMONS_IO("commons-io", "commons-io", "@commonsIOVersion@"), COMMONS_LANG("org.apache.commons", "commons-lang3", "@commonsLangVersion@"), COMMONS_TEXT("org.apache.commons", "commons-text", "@commonsTextVersion@"), - VINEFLOWER("org.vineflower", "vineflower", "@vineFlowerVersion@") + LOOM("net.fabricmc", "fabric-loom", "@loomVersion@"), + VINEFLOWER("org.vineflower", "vineflower", "@vineFlowerVersion@"); ; diff --git a/src/main/java/com/wildermods/workspace/decomp/DecompilerBuilder.java b/src/main/java/com/wildermods/workspace/decomp/DecompilerBuilder.java new file mode 100644 index 0000000..36a9c13 --- /dev/null +++ b/src/main/java/com/wildermods/workspace/decomp/DecompilerBuilder.java @@ -0,0 +1,90 @@ +package com.wildermods.workspace.decomp; + +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +import org.gradle.api.Task; + +import com.wildermods.workspace.util.GradlePrintStreamLogger; + +import net.fabricmc.loom.api.decompilers.DecompilationMetadata; +import net.fabricmc.loom.util.IOStringConsumer; + +public class DecompilerBuilder { + + private int numberOfThreads = 4; + private Path javadocs; + private Path decompDest; + private Collection jarsToDecomp = new HashSet(); + private Collection libraries = new HashSet(); + private IOStringConsumer logger; + private Map options = new HashMap<>(); + + public DecompilerBuilder() {} + + public DecompilerBuilder(Task task) { + setLogger(new GradlePrintStreamLogger(task)); + } + + public DecompilerBuilder setThreadCount(int threadCount) { + if(threadCount > 0 && threadCount <= Runtime.getRuntime().availableProcessors()) { + numberOfThreads = threadCount; + } + return this; + } + + public DecompilerBuilder setJavadocs(Path javadocs) { + this.javadocs = javadocs; + return this; + } + + public DecompilerBuilder setDecompDest(Path decompDest) { + this.decompDest = decompDest; + return this; + } + + public DecompilerBuilder addJarsToDecomp(Path... jars) { + this.jarsToDecomp.addAll(Arrays.asList(jars)); + return this; + } + + public DecompilerBuilder addLibraries(Path... libraries) { + this.libraries.addAll(Arrays.asList(libraries)); + return this; + } + + public DecompilerBuilder setLogger(IOStringConsumer logger) { + this.logger = logger; + return this; + } + + public DecompilerBuilder setOption(String key, String value) { + options.put(key, value); + return this; + } + + public DecompilationMetadata getMetaData() { + return new DecompilationMetadata(numberOfThreads, javadocs, libraries, logger, options); + } + + public Collection getJarsToDecomp() { + return jarsToDecomp; + } + + public Path getDecompDest() { + return decompDest; + } + + public Path getLinemapDest() { + return decompDest.getParent().resolve("linemaps"); + } + + public WilderWorkspaceDecompiler build() { + decompDest.getClass(); //throw if null + return new WilderWorkspaceDecompiler(this); + } +} diff --git a/src/main/java/com/wildermods/workspace/decomp/WilderWorkspaceDecompiler.java b/src/main/java/com/wildermods/workspace/decomp/WilderWorkspaceDecompiler.java new file mode 100644 index 0000000..ce27915 --- /dev/null +++ b/src/main/java/com/wildermods/workspace/decomp/WilderWorkspaceDecompiler.java @@ -0,0 +1,61 @@ +package com.wildermods.workspace.decomp; + +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import org.jetbrains.java.decompiler.main.Fernflower; +import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; +import org.jetbrains.java.decompiler.main.extern.IResultSaver; + +import com.wildermods.workspace.util.GradlePrintStreamLogger; + +import net.fabricmc.loom.decompilers.vineflower.ThreadSafeResultSaver; +import net.fabricmc.loom.api.decompilers.DecompilationMetadata; + +public class WilderWorkspaceDecompiler { + + private final Fernflower ff; + private final DecompilerBuilder builder; + private final DecompilationMetadata metaData; + + WilderWorkspaceDecompiler(DecompilerBuilder builder) { + this.builder = builder; + this.metaData = builder.getMetaData(); + + 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 WilderWorkspaceJavadocProvider(metaData.javaDocs().toFile()) + ) + ); + + options.putAll(metaData.options()); + + IResultSaver saver = new ThreadSafeResultSaver(builder.getDecompDest()::toFile, builder.getLinemapDest()::toFile); + ff = new Fernflower(saver, options, (GradlePrintStreamLogger)metaData.logger()); + + for(Path library : metaData.libraries()) { + ff.addLibrary(library.toFile()); + } + + for(Path compiledJar : builder.getJarsToDecomp()) { + ff.addSource(compiledJar.toFile()); + } + + } + + public void decompile() { + try { + ff.decompileContext(); + } + finally { + ff.clearContext(); + } + } + +} diff --git a/src/main/java/com/wildermods/workspace/decomp/WilderWorkspaceJavadocProvider.java b/src/main/java/com/wildermods/workspace/decomp/WilderWorkspaceJavadocProvider.java new file mode 100644 index 0000000..4a441f6 --- /dev/null +++ b/src/main/java/com/wildermods/workspace/decomp/WilderWorkspaceJavadocProvider.java @@ -0,0 +1,51 @@ +package com.wildermods.workspace.decomp; + +import java.io.File; + +import org.jetbrains.java.decompiler.struct.StructClass; +import org.jetbrains.java.decompiler.struct.StructField; +import org.jetbrains.java.decompiler.struct.StructMethod; + +import net.fabricmc.loom.decompilers.vineflower.TinyJavadocProvider; + +public class WilderWorkspaceJavadocProvider extends TinyJavadocProvider { + + private final TinyJavadocProvider parent; + + public WilderWorkspaceJavadocProvider(File tinyFile) { + super(tinyFile); + if(tinyFile == null) { + parent = null; + } + else { + parent = new TinyJavadocProvider(tinyFile); + } + } + + @Override + public String getClassDoc(StructClass structClass) { + if(parent == null) { + return ""; + } + return parent.getClassDoc(structClass); + } + + @Override + public String getFieldDoc(StructClass structClass, StructField structField) { + if(parent == null) { + return ""; + } + return parent.getFieldDoc(structClass, structField); + } + + @Override + public String getMethodDoc(StructClass structClass, StructMethod structMethod) { + if(parent == null) { + return ""; + } + return parent.getMethodDoc(structClass, structMethod); + } + + + +} diff --git a/src/main/java/com/wildermods/workspace/tasks/DecompileJarsTask.java b/src/main/java/com/wildermods/workspace/tasks/DecompileJarsTask.java index c36b531..044435a 100644 --- a/src/main/java/com/wildermods/workspace/tasks/DecompileJarsTask.java +++ b/src/main/java/com/wildermods/workspace/tasks/DecompileJarsTask.java @@ -6,17 +6,12 @@ import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; -import java.util.Map; - import org.gradle.api.DefaultTask; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.TaskAction; -import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler; -import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler.SaveType; -import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; -import org.jetbrains.java.decompiler.util.JrtFinder; -import com.wildermods.workspace.util.GradlePrintStreamLogger; +import com.wildermods.workspace.decomp.DecompilerBuilder; +import com.wildermods.workspace.decomp.WilderWorkspaceDecompiler; public class DecompileJarsTask extends DefaultTask { @@ -28,8 +23,10 @@ public class DecompileJarsTask extends DefaultTask { @TaskAction public void decompile() throws IOException { - ConsoleDecompiler consoleDecompiler = new ConsoleDecompiler(Path.of(decompDir).normalize().toAbsolutePath().toFile(), Map.of(IFernflowerPreferences.INCLUDE_JAVA_RUNTIME, JrtFinder.CURRENT), new GradlePrintStreamLogger(this), SaveType.FOLDER) {}; //constructor is protected, have to subclass... + Path compiledDir = Path.of(this.compiledDir); + DecompilerBuilder b = new DecompilerBuilder(this); + b.setDecompDest(Path.of(decompDir)); /* * Add jars to decompile @@ -53,7 +50,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO case "scratchpad.jar": case "wildermyth.jar": System.out.println("Adding " + file.toAbsolutePath().normalize().toString() + " as input for the decompiler."); - consoleDecompiler.addSource(file.normalize().toAbsolutePath().toFile()); + b.addJarsToDecomp(file.normalize().toAbsolutePath()); return FileVisitResult.CONTINUE; } return FileVisitResult.CONTINUE; @@ -86,13 +83,15 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO return FileVisitResult.CONTINUE; } System.out.println("Adding " + file.toAbsolutePath().normalize().toString() + " as library for the decompiler."); - consoleDecompiler.addLibrary(file.normalize().toAbsolutePath().toFile()); + b.addLibraries(file.normalize().toAbsolutePath()); return FileVisitResult.CONTINUE; } }); - consoleDecompiler.decompileContext(); + WilderWorkspaceDecompiler decompiler = b.build(); + decompiler.decompile(); + } public String getCompiledDir() { diff --git a/src/main/java/com/wildermods/workspace/tasks/GenerateSourcesTask.java b/src/main/java/com/wildermods/workspace/tasks/GenerateSourcesTask.java new file mode 100644 index 0000000..66154fa --- /dev/null +++ b/src/main/java/com/wildermods/workspace/tasks/GenerateSourcesTask.java @@ -0,0 +1,7 @@ +package com.wildermods.workspace.tasks; + +import org.gradle.api.DefaultTask; + +public class GenerateSourcesTask extends DefaultTask { + +} diff --git a/src/main/java/com/wildermods/workspace/util/GradlePrintStreamLogger.java b/src/main/java/com/wildermods/workspace/util/GradlePrintStreamLogger.java index de0a330..e264601 100644 --- a/src/main/java/com/wildermods/workspace/util/GradlePrintStreamLogger.java +++ b/src/main/java/com/wildermods/workspace/util/GradlePrintStreamLogger.java @@ -1,10 +1,14 @@ package com.wildermods.workspace.util; +import java.io.IOException; + import org.gradle.api.Task; import org.gradle.api.logging.LogLevel; import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger; -public class GradlePrintStreamLogger extends IFernflowerLogger { +import net.fabricmc.loom.util.IOStringConsumer; + +public class GradlePrintStreamLogger extends IFernflowerLogger implements IOStringConsumer { private final Task task; @@ -53,4 +57,9 @@ public void writeMessage(String message, Severity severity, Throwable t) { } } + @Override + public void accept(String data) throws IOException { + writeMessage(data, Severity.INFO); + } + }