Skip to content

Commit

Permalink
Push changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamebuster19901 committed Jul 20, 2024
1 parent eb27f9c commit 1e2e8e7
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 22 deletions.
11 changes: 3 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -37,13 +38,15 @@ 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: [
workspaceVersion: workspaceVersion,
commonsLangVersion: commonsLangVersion,
commonsTextVersion: commonsTextVersion,
commonsIOVersion: commonsIOVersion,
loomVersion: loomVersion,
vineFlowerVersion: vineFlowerVersion
])
}
Expand All @@ -53,14 +56,6 @@ compileJava {
dependsOn processSource
}

sourceSets {
main {
java {
srcDirs = ["$buildDir/processedSrc"]
}
}
}

def pom2;

publishing {
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
fernFlowerVersion = 2.0.0
vineFlowerVersion = 1.10.1
loomVersion = 1.7.2
3 changes: 2 additions & 1 deletion src/main/java/com/wildermods/workspace/Dependencies.java
Original file line number Diff line number Diff line change
Expand Up @@ -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@");

;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Path> jarsToDecomp = new HashSet<Path>();
private Collection<Path> libraries = new HashSet<Path>();
private IOStringConsumer logger;
private Map<String, String> 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<Path> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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();
}
}

}
Original file line number Diff line number Diff line change
@@ -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);
}



}
21 changes: 10 additions & 11 deletions src/main/java/com/wildermods/workspace/tasks/DecompileJarsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wildermods.workspace.tasks;

import org.gradle.api.DefaultTask;

public class GenerateSourcesTask extends DefaultTask {

}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}

}

0 comments on commit 1e2e8e7

Please sign in to comment.