Skip to content

Commit

Permalink
Close #68
Browse files Browse the repository at this point in the history
Remove `lib-recommended` option of user-defined decompiler. Now user-defined decompiler is always lib-recommended
Add `params` to UniqueMapping
  • Loading branch information
XiaoPangxie732 committed Aug 15, 2022
1 parent 5a2235c commit 7baf3b1
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 150 deletions.
6 changes: 1 addition & 5 deletions decompiler/decompiler.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
# Required, your decompiler executable file(.jar, .exe, etc.). Absolute path and relative path are both OK.
decompiler-file=fernflower.jar

# Optional, if the decompiler works better with the libraries used by Minecraft, change the value to true.
# Only works when the "--version" command-line option is specified or version is detected from the jar file.
lib-recommended=true

# Required, sets the input to the decompiler should be file or directory. Options are "FILE" and "DIRECTORY"(case-insensitive).
# "FILE" will input the full Minecraft jar, so you need to set your decompiler to skip lib classes.
source-type=DIRECTORY
Expand All @@ -18,4 +14,4 @@ source-type=DIRECTORY
# %source%: The input file/directory(specified by source-type).
# %target%: The output directory
# NOTE: %lib_all% and %lib_repeat% only works when "lib-recommended" option is set to true
args=-log=TRACE -dgs=1 -asc=1 -udv=0 -ump=0 -rsy=1 -aoa=1 -e=%lib_repeat% %source% %target%
args=-log=TRACE -dgs=1 -asc=1 -udv=0 -ump=0 -rsy=1 -aoa=1 -e=%lib_repeat% %source% %target%
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ForkJoinPool;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
Expand All @@ -62,7 +61,6 @@ public class MinecraftDecompiler {
private static final Logger LOGGER = Logging.getLogger();
public static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
.proxy(ProxySelector.of((InetSocketAddress) MinecraftDecompilerCommandLine.INTERNAL_PROXY.address()))
.executor(ForkJoinPool.commonPool())
.connectTimeout(Duration.ofSeconds(10L))
.build();

Expand Down Expand Up @@ -102,13 +100,13 @@ private void decompile0(IDecompiler decompiler, ObjectSet<String> paths, Path in
try(FileSystem jarFs = JarUtil.createZipFs(inputJar, false)) {
FileUtil.deleteIfExists(outputDir);
Files.createDirectories(outputDir);
Path libDownloadPath = Properties.DOWNLOAD_DIR.resolve("libs").toAbsolutePath().normalize();
Files.createDirectories(libDownloadPath);
Path libDownloadPath = Files.createDirectories(Properties.DOWNLOAD_DIR.resolve("libs").toAbsolutePath().normalize());
if(decompiler instanceof IExternalResourcesDecompiler erd)
erd.extractTo(Properties.TEMP_DIR.toAbsolutePath().normalize());
if(decompiler instanceof ILibRecommendedDecompiler lrd) {
options.bundledLibs().ifPresentOrElse(lrd::receiveLibs, LambdaUtil.unwrap(() -> {
if(options.version() != null) lrd.downloadLib(libDownloadPath, options.version());
if(options.version() != null)
lrd.receiveLibs(DownloadUtil.downloadLibraries(options.version(), libDownloadPath));
}));
}
switch (decompiler.getSourceType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

public class MinecraftDecompilerCommandLine {
static {
System.setProperty("org.openjdk.java.util.stream.tripwire", "true");
System.setProperty("org.openjdk.java.util.stream.tripwire", Boolean.toString(Info.IS_DEV));
}
private static final Logger LOGGER = Logging.getLogger("CommandLine");
public static final Proxy INTERNAL_PROXY = Info.IS_DEV ?
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import cn.maxpixel.mcdecompiler.util.DownloadUtil;
import cn.maxpixel.mcdecompiler.util.Logging;
import cn.maxpixel.mcdecompiler.util.Utils;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import it.unimi.dsi.fastutil.objects.ObjectSets;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand All @@ -34,10 +37,11 @@
import java.nio.file.StandardCopyOption;
import java.util.logging.Level;

public class CFRDecompiler extends AbstractLibRecommendedDecompiler implements IExternalResourcesDecompiler {
public class CFRDecompiler implements IExternalResourcesDecompiler, ILibRecommendedDecompiler {
private static final String VERSION = Properties.getProperty("CFR-Version", "cfr.version");
private static final URI RESOURCE = URI.create("https://repo1.maven.org/maven2/org/benf/cfr/" + VERSION + "/cfr-" + VERSION + ".jar");
private static final URI RESOURCE_HASH = URI.create("https://repo1.maven.org/maven2/org/benf/cfr/" + VERSION + "/cfr-" + VERSION + ".jar.sha1");
private ObjectSet<String> libs = ObjectSets.emptySet();
private Path decompilerJarPath;
private ExternalJarClassLoader cl;

Expand All @@ -55,9 +59,10 @@ public void decompile(@NotNull Path source, @NotNull Path target) throws IOExcep
if(cl == null) cl = new ExternalJarClassLoader(new URL[] {decompilerJarPath.toUri().toURL()}, getClass().getClassLoader());
Thread thread = (Thread) cl.loadClass("cn.maxpixel.mcdecompiler.decompiler.thread.CFRDecompileThread")
.getConstructor(String.class, String.class, String.class)
.newInstance(source.toString(), target.toString(), String.join(Info.PATH_SEPARATOR, listLibs()));
.newInstance(source.toString(), target.toString(), String.join(Info.PATH_SEPARATOR, libs));
thread.start();
while(thread.isAlive()) Thread.onSpinWait();
cl.close();
} catch(ReflectiveOperationException e) {
Logging.getLogger().log(Level.SEVERE, "Failed to load CFR", e);
throw Utils.wrapInRuntime(e);
Expand All @@ -70,4 +75,10 @@ public void extractTo(Path extractPath) throws IOException {
Files.copy(DownloadUtil.getRemoteResource(Properties.getDownloadedDecompilerPath(Info.DecompilerType.CFR), RESOURCE, RESOURCE_HASH),
decompilerJarPath, StandardCopyOption.REPLACE_EXISTING);
}

@Override
public void receiveLibs(@NotNull ObjectSet<Path> libs) {
this.libs = libs.stream().map(p -> p.toAbsolutePath().normalize().toString())
.collect(ObjectOpenHashSet.toSetWithExpectedSize(libs.size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ private static UserDefinedDecompiler findUserDefined() {
"decompiler-file is a required property");
String sourceType = Objects.requireNonNull(decompilerProperties.getProperty("source-type"),
"source-type is a required property").toUpperCase(Locale.ROOT);
String libRecommended = decompilerProperties.getProperty("lib-recommended", "false");
String[] args = Objects.requireNonNull(decompilerProperties.getProperty("args"), "args is a required property")
.split(" ");
return new UserDefinedDecompiler(IDecompiler.SourceType.valueOf(sourceType), Path.of("decompiler", decompilerPath).
toAbsolutePath().normalize(), ObjectImmutableList.of(args), Boolean.parseBoolean(libRecommended));
toAbsolutePath().normalize(), ObjectImmutableList.of(args));
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Error occurred when constructing the user-defined decompiler", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void decompile(@NotNull Path source, @NotNull Path target) throws IOExcep
.getConstructor(File.class, File.class).newInstance(source.toFile(), target.toFile());
thread.start();
while(thread.isAlive()) Thread.onSpinWait();
cl.close();
} catch(ReflectiveOperationException e) {
Logging.getLogger().log(Level.SEVERE, "Failed to load FernFlower", e);
throw Utils.wrapInRuntime(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import cn.maxpixel.mcdecompiler.util.DownloadUtil;
import cn.maxpixel.mcdecompiler.util.Logging;
import cn.maxpixel.mcdecompiler.util.Utils;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import org.jetbrains.annotations.NotNull;

import java.io.File;
Expand All @@ -35,11 +36,12 @@
import java.nio.file.StandardCopyOption;
import java.util.logging.Level;

public class ForgeFlowerDecompiler extends AbstractLibRecommendedDecompiler implements IExternalResourcesDecompiler {
public class ForgeFlowerDecompiler implements IExternalResourcesDecompiler, ILibRecommendedDecompiler {
private static final String VERSION = Properties.getProperty("ForgeFlower-Version", "forgeflower.version");
private static final URI RESOURCE = URI.create("https://maven.minecraftforge.net/net/minecraftforge/forgeflower/" + VERSION + "/forgeflower-" + VERSION + ".jar");
private static final URI RESOURCE_HASH = URI.create("https://maven.minecraftforge.net/net/minecraftforge/forgeflower/" + VERSION + "/forgeflower-" + VERSION + ".jar.sha1");
public static final String FERNFLOWER_ABSTRACT_PARAMETER_NAMES = "fernflower_abstract_parameter_names.txt";
private File[] libs = new File[0];
private Path decompilerJarPath;
private ExternalJarClassLoader cl;

Expand Down Expand Up @@ -69,12 +71,18 @@ public void decompile(@NotNull Path source, @NotNull Path target) throws IOExcep
else sources = new File[] {source.toFile()};
Thread thread = (Thread) cl.loadClass("cn.maxpixel.mcdecompiler.decompiler.thread.ForgeFlowerDecompileThread")
.getConstructor(File[].class, File[].class, File.class)
.newInstance(sources, listLibs().stream().map(File::new).toArray(File[]::new), target.toFile());
.newInstance(sources, libs, target.toFile());
thread.start();
while(thread.isAlive()) Thread.onSpinWait();
cl.close();
} catch(ReflectiveOperationException e) {
Logging.getLogger().log(Level.SEVERE, "Failed to load ForgeFlower", e);
throw Utils.wrapInRuntime(e);
}
}

@Override
public void receiveLibs(@NotNull ObjectSet<Path> libs) {
this.libs = libs.stream().map(Path::toFile).toArray(File[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,13 @@
package cn.maxpixel.mcdecompiler.decompiler;

import it.unimi.dsi.fastutil.objects.ObjectSet;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.file.Path;

/**
* A decompiler implements this interface is lib-recommended.<br>
*
* The decompiler implementation uses {@link ILibRecommendedDecompiler#downloadLib(Path, String)} to download all Minecraft libraries.
* But you need to download it yourself by implementing this method.<br>
*
* If you don't want to implement the download method yourself but still lib-recommended,
* you can extend {@link AbstractLibRecommendedDecompiler} instead of implementing this interface.
* @see AbstractLibRecommendedDecompiler
* A decompiler implements this interface is lib-recommended
*/
public interface ILibRecommendedDecompiler extends IDecompiler {
/**
* Download all libraries.
* @param libDir Where the libs download to.
* @param version Minecraft version uses to deobfuscate.
* @throws IOException When IO error occurs.
* @see AbstractLibRecommendedDecompiler#listLibs()
*/
void downloadLib(Path libDir, String version) throws IOException;

void receiveLibs(ObjectSet<Path> libs);
void receiveLibs(@NotNull ObjectSet<Path> libs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@
import cn.maxpixel.mcdecompiler.util.FileUtil;
import cn.maxpixel.mcdecompiler.util.Utils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectList;
import it.unimi.dsi.fastutil.objects.ObjectLists;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

public class UserDefinedDecompiler extends AbstractLibRecommendedDecompiler {
public class UserDefinedDecompiler implements ILibRecommendedDecompiler {
public static final UserDefinedDecompiler NONE = new UserDefinedDecompiler() {
@Override
public void decompile(@NotNull Path source, @NotNull Path target) {
throw new RuntimeException("User decompiler not found. Make sure you put correct config file in \"decompiler\" directory");
throw new RuntimeException("User decompiler not found. Please make sure you have put correct config file in \"decompiler\" directory");
}
@Override
public void downloadLib(Path libDir, String version) {}
};
private SourceType sourceType;
private Path decompilerPath;
private List<String> options;
private boolean libRecommended;
private ObjectList<String> libs = ObjectLists.emptyList();

private UserDefinedDecompiler() {}

UserDefinedDecompiler(@NotNull SourceType sourceType, Path decompilerPath, @NotNull List<String> options, boolean libRecommended) {
UserDefinedDecompiler(@NotNull SourceType sourceType, @NotNull Path decompilerPath, @NotNull List<String> options) {
this.sourceType = sourceType;
this.decompilerPath = FileUtil.requireExist(decompilerPath);
this.options = options;
this.libRecommended = libRecommended;
}

@Override
Expand All @@ -67,18 +67,14 @@ public void decompile(@NotNull Path source, @NotNull Path target) throws IOExcep
Utils.waitForProcess(Runtime.getRuntime().exec(arrayList.toArray(new String[0])));
}

private ObjectArrayList<String> resolveArgs(Path source, Path target, List<String> options) {
private ObjectArrayList<String> resolveArgs(@NotNull Path source, @NotNull Path target, @NotNull List<String> options) {
ObjectArrayList<String> resolvedOptions = new ObjectArrayList<>();
List<String> libs = listLibs();
for(int i = 0; i < options.size(); i++) {
String s = options.get(i);
if(!libRecommended && (s.contains("%lib_all%") || s.contains("%lib_repeat%")))
throw new IllegalArgumentException("lib-recommended option is set to false, if you want to use %lib_all% or %lib_repeat% " +
"variable, please change lib-recommended option to true(in decompiler.properties)");
if(s.contains("%source%")) s = s.replace("%source%", source.toString());
if(s.contains("%target%")) s = s.replace("%target%", target.toString());
if(libRecommended && s.contains("%lib_all%")) s = s.replace("%lib_all%", String.join(Info.PATH_SEPARATOR, libs));
if(libRecommended && s.contains("%lib_repeat%")) {
if(s.contains("%lib_all%")) s = s.replace("%lib_all%", String.join(Info.PATH_SEPARATOR, libs));
if(s.contains("%lib_repeat%")) {
for(int j = 0; j < libs.size(); j++) {
if(s.equals("%lib_repeat%")) {
resolvedOptions.add(options.get(i - 1));
Expand All @@ -91,7 +87,8 @@ private ObjectArrayList<String> resolveArgs(Path source, Path target, List<Strin
}

@Override
public void downloadLib(Path libDir, String version) throws IOException {
if(libRecommended) super.downloadLib(libDir, version);
public void receiveLibs(@NotNull ObjectSet<Path> libs) {
this.libs = libs.stream().map(p -> p.toAbsolutePath().normalize().toString())
.collect(ObjectArrayList.toListWithExpectedSize(libs.size()));
}
}
Loading

0 comments on commit 7baf3b1

Please sign in to comment.