Skip to content

Commit

Permalink
Fixing bugs from debug result. 2.0 Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoPangxie732 committed Jan 22, 2021
1 parent d8f0375 commit f2652d4
Show file tree
Hide file tree
Showing 21 changed files with 409 additions and 423 deletions.
22 changes: 0 additions & 22 deletions src/main/java/cn/maxpixel/mcdecompiler/CustomizeInfo.java

This file was deleted.

41 changes: 0 additions & 41 deletions src/main/java/cn/maxpixel/mcdecompiler/DefaultedInfo.java

This file was deleted.

133 changes: 99 additions & 34 deletions src/main/java/cn/maxpixel/mcdecompiler/Deobfuscator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,142 @@

package cn.maxpixel.mcdecompiler;

import cn.maxpixel.mcdecompiler.decompiler.Decompilers;
import cn.maxpixel.mcdecompiler.decompiler.IDecompiler;
import cn.maxpixel.mcdecompiler.decompiler.IExternalJarDecompiler;
import cn.maxpixel.mcdecompiler.decompiler.ILibRecommendedDecompiler;
import cn.maxpixel.mcdecompiler.deobfuscator.AbstractDeobfuscator;
import cn.maxpixel.mcdecompiler.deobfuscator.ProguardDeobfuscator;
import cn.maxpixel.mcdecompiler.decompiler.*;
import cn.maxpixel.mcdecompiler.deobfuscator.*;
import cn.maxpixel.mcdecompiler.util.FileUtil;
import cn.maxpixel.mcdecompiler.util.Utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Deobfuscator {
private static final Logger LOGGER = LogManager.getLogger();
private final AbstractDeobfuscator deobfuscator;
private final String version;
private final Info.SideType type;
private String version;
private Info.SideType type;
public Deobfuscator(String version, Info.SideType type) {
this.version = Objects.requireNonNull(version, "version cannot be null!");
this.type = Objects.requireNonNull(type, "type cannot be null!");
this.deobfuscator = new ProguardDeobfuscator(version, type);
// switch(mapping) {
// case PROGUARD:
// deobfuscator = new ProguardDeobfuscator(version, type);
// break;
// default:
// throw new IllegalArgumentException("MappingType " + mapping + " is not supported now");
// }
}
public Deobfuscator(String mappingPath) {
switch(getMappingType(Objects.requireNonNull(mappingPath, "mappingPath cannot be null!"))) {
case PROGUARD:
this.deobfuscator = new ProguardDeobfuscator(mappingPath);
break;
case SRG:
this.deobfuscator = new SrgDeobfuscator(mappingPath);
break;
case CSRG:
this.deobfuscator = new CsrgDeobfuscator(mappingPath);
break;
case TSRG:
this.deobfuscator = new TsrgDeobfuscator(mappingPath);
break;
default:
throw new IllegalArgumentException("This type of mapping is currently unsupported");
}
}
private Info.MappingType getMappingType(String mappingPath) {
try(Stream<String> lines = Files.lines(Paths.get(mappingPath), StandardCharsets.UTF_8).filter(s -> !s.startsWith("#")).limit(2)) {
List<String> list = lines.collect(Collectors.toList());
String s = list.get(1);
if(s.startsWith(" ")) return Info.MappingType.PROGUARD;
else if(s.startsWith("\t")) return Info.MappingType.TSRG;
s = list.get(0);
if(s.startsWith("PK: ") || s.startsWith("CL: ") || s.startsWith("FD: ") || s.startsWith("MD: ")) return Info.MappingType.SRG;
else return Info.MappingType.CSRG;
} catch (IOException e) {
throw Utils.wrapInRuntime(e);
}
}
public void deobfuscate() {
if(version == null && type == null) {
deobfuscate(Properties.get(Properties.Key.INPUT_JAR), Properties.getOutputDeobfuscatedJarPath());
} else {
Objects.requireNonNull(version, "version cannot be null!");
Objects.requireNonNull(type, "type cannot be null!");
deobfuscate(Properties.getDownloadedMcJarPath(version, type),
Properties.getOutputDeobfuscatedJarPath(version, type));
}
}
public void deobfuscate(Path input, Path output) {
Path tempPath = Properties.get(Properties.Key.TEMP_DIR);
FileUtil.deleteDirectory(tempPath);
FileUtil.ensureDirectoryExist(tempPath);
Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtil.deleteDirectory(tempPath)));
deobfuscator.deobfuscate(input, output);
}
public void decompile(Info.DecompilerType decompilerType) {
LOGGER.info("Decompiling using \"{}\"", decompilerType);
try {
Path tempPath = InfoProviders.get().getTempPath();
FileUtil.deleteDirectory(tempPath);
Files.createDirectories(tempPath);
Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtil.deleteDirectory(tempPath)));
} catch (FileAlreadyExistsException ignored) {
Path mappedClasses = Properties.getTempMappedClassesPath();
Path decompileDir = Properties.getOutputDecompiledDirectory(version, type);
Files.createDirectories(decompileDir);
Path decompilerJarPath = Properties.getTempDecompilerPath().toAbsolutePath().normalize();
IDecompiler decompiler = Decompilers.get(decompilerType);
Path libDownloadPath = Properties.getDownloadedLibPath().toAbsolutePath().normalize();
FileUtil.ensureDirectoryExist(libDownloadPath);
if(decompiler instanceof IExternalJarDecompiler) ((IExternalJarDecompiler) decompiler).extractDecompilerTo(decompilerJarPath);
if(decompiler instanceof ILibRecommendedDecompiler && version != null)
((ILibRecommendedDecompiler) decompiler).downloadLib(libDownloadPath, version);
switch(decompiler.getSourceType()) {
case DIRECTORY:
Path decompileClasses = Properties.getTempDecompileClassesPath();
FileUtil.copyDirectory(mappedClasses.resolve("net"), decompileClasses);
try(Stream<Path> mjDirs = Files.list(mappedClasses.resolve("com").resolve("mojang")).filter(p -> !(p.endsWith("authlib") ||
p.endsWith("bridge") || p.endsWith("brigadier") || p.endsWith("datafixers") || p.endsWith("serialization") ||
p.endsWith("util")))) {
Path decompiledMj = decompileClasses.resolve("com").resolve("mojang");
mjDirs.forEach(p -> FileUtil.copyDirectory(p, decompiledMj));
}
decompiler.decompile(decompileClasses.toAbsolutePath().normalize(), decompileDir.toAbsolutePath().normalize());
break;
case FILE:
decompiler.decompile(Properties.getOutputDeobfuscatedJarPath(version, type).toAbsolutePath().normalize(),
decompileDir.toAbsolutePath().normalize());
break;
}
} catch (IOException e) {
e.printStackTrace();
LOGGER.fatal("Error when decompiling", e);
}
deobfuscator.deobfuscate(Paths.get(InfoProviders.get().getMcJarPath(version, type)), Paths.get(InfoProviders.get().getDeobfuscateJarPath(version, type)));
}
public void decompile(Info.DecompilerType type) {
LOGGER.info("Decompiling");
public void decompileCustomized(String customizedDecompilerName) {
LOGGER.info("Decompiling using customized decompiler \"{}\"", customizedDecompilerName);
try {
Path mappedClasses = InfoProviders.get().getTempMappedClassesPath();
Path decompileDir = Paths.get(InfoProviders.get().getDecompileDirectory(version, this.type));
Path mappedClasses = Properties.getTempMappedClassesPath();
Path decompileDir = Properties.getOutputDecompiledDirectory(version, type);
Files.createDirectories(decompileDir);
Path decompilerJarPath = InfoProviders.get().getTempDecompilerPath(type).toAbsolutePath().normalize();
IDecompiler decompiler = Decompilers.get(type);
Path libDownloadPath = Paths.get(InfoProviders.get().getLibDownloadPath()).toAbsolutePath().normalize();
Path decompilerJarPath = Properties.getTempDecompilerPath().toAbsolutePath().normalize();
ICustomizedDecompiler decompiler = Decompilers.getCustomized(customizedDecompilerName);
Path libDownloadPath = Properties.getDownloadedLibPath().toAbsolutePath().normalize();
FileUtil.ensureDirectoryExist(libDownloadPath);
if(decompiler instanceof IExternalJarDecompiler) ((IExternalJarDecompiler) decompiler).extractDecompilerTo(decompilerJarPath);
if(decompiler instanceof ILibRecommendedDecompiler) ((ILibRecommendedDecompiler) decompiler).downloadLib(libDownloadPath, version);
if(decompiler instanceof ILibRecommendedDecompiler && version != null)
((ILibRecommendedDecompiler) decompiler).downloadLib(libDownloadPath, version);
switch(decompiler.getSourceType()) {
case DIRECTORY:
Path decompileClasses = InfoProviders.get().getTempDecompileClassesPath();
Path decompileClasses = Properties.getTempDecompileClassesPath();
FileUtil.copyDirectory(mappedClasses.resolve("net"), decompileClasses);
FileUtil.copyDirectory(mappedClasses.resolve("com").resolve("mojang"), decompileClasses.resolve("com"));
decompiler.decompile(decompileClasses.toAbsolutePath().normalize(), decompileDir.toAbsolutePath().normalize());
break;
case FILE:
decompiler.decompile(Paths.get(InfoProviders.get().getDeobfuscateJarPath(version, this.type)).toAbsolutePath().normalize(),
decompiler.decompile(Properties.getOutputDeobfuscatedJarPath(version, type).toAbsolutePath().normalize(),
decompileDir.toAbsolutePath().normalize());
break;
}
} catch (IOException e) {
e.printStackTrace();
LOGGER.fatal("Error when decompiling using customized decompiler", e);
}
}
}
Loading

0 comments on commit f2652d4

Please sign in to comment.