Skip to content

Commit a8066e0

Browse files
Close #211 : Add --skip-when-absent as the temporary solution
1 parent 5986d0f commit a8066e0

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

modules/api/src/main/java/cn/maxpixel/mcdecompiler/api/MinecraftDecompiler.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public MinecraftDecompiler(Options options) {
7575
}
7676

7777
public void deobfuscate() {
78+
if (options.skipWhenAbsent() && deobfuscator == null) {
79+
LOGGER.info("Skipping deobfuscation as mappings are absent");
80+
return;
81+
}
7882
try {
7983
deobfuscator.deobfuscate(options.inputJar(), options.outputJar());
8084
} catch (IOException e) {
@@ -92,13 +96,15 @@ public void decompile(String decompilerName) {
9296
public void decompile(String decompilerName, @Nullable Path incrementalJar) {
9397
var decompiler = Decompilers.get(decompilerName);
9498
if (decompiler == null) throw new IllegalArgumentException("Decompiler \"" + decompilerName + "\" does not exist");
95-
if (Files.notExists(options.outputJar())) deobfuscate();
96-
if (deobfuscator.toDecompile.isEmpty()) {
97-
LOGGER.info("Nothing to decompile, skipping decompilation");
98-
return;
99+
if (deobfuscator != null) {
100+
if (Files.notExists(options.outputJar())) deobfuscate();
101+
if (deobfuscator.toDecompile.isEmpty()) {
102+
LOGGER.info("Nothing to decompile, skipping decompilation");
103+
return;
104+
}
99105
}
100106
LOGGER.info("Decompiling using \"{}\"", decompiler.name());
101-
var inputJar = options.outputJar();
107+
var inputJar = deobfuscator == null ? options.inputJar() : options.outputJar();
102108
var outputDir = options.outputDecompDir();
103109
try (FileSystem jarFs = JarUtil.createZipFs(inputJar)) {
104110
if (incrementalJar == null) FileUtil.deleteIfExists(outputDir);
@@ -110,6 +116,7 @@ public void decompile(String decompilerName, @Nullable Path incrementalJar) {
110116
ObjectOpenHashSet<Path> libs = options.bundledLibs().map(ObjectOpenHashSet::new).orElseGet(() ->
111117
DownloadingUtil.downloadLibraries(options.version(), libDownloadPath));
112118
if (incrementalJar != null && decompiler.getSourceType() == IDecompiler.SourceType.DIRECTORY) {
119+
if (deobfuscator == null) throw new UnsupportedOperationException();// FIXME: I guess no one use this, so just throw uoe before refactoring
113120
try (FileSystem incrementalFs = JarUtil.createZipFs(incrementalJar);
114121
Stream<Path> paths = FileUtil.iterateFiles(incrementalFs.getPath(""))) {
115122
var toDecompile = deobfuscator.toDecompile;
@@ -153,9 +160,12 @@ public void decompile(String decompilerName, @Nullable Path incrementalJar) {
153160
if (!libs.isEmpty()) lrd.receiveLibs(libs);
154161
}
155162
switch (decompiler.getSourceType()) {
156-
case DIRECTORY -> {
163+
case DIRECTORY -> {// FIXME: temporary solution; needs refactoring
157164
Path decompileClasses = Directories.TEMP_DIR.resolve("decompileClasses").toAbsolutePath().normalize();
158-
deobfuscator.toDecompile.parallelStream().forEach(path -> FileUtil.copyFile(jarFs.getPath(path), decompileClasses.resolve(path)));
165+
if (deobfuscator != null) deobfuscator.toDecompile.parallelStream().forEach(path -> FileUtil.copyFile(jarFs.getPath(path), decompileClasses.resolve(path)));
166+
else try (Stream<Path> s = FileUtil.iterateFiles(jarFs.getPath(""))) {
167+
s.forEach(p -> FileUtil.copyFile(p, decompileClasses.resolve(p.toString())));
168+
}
159169
decompiler.decompile(decompileClasses, outputDir);
160170
}
161171
case FILE -> decompiler.decompile(inputJar, outputDir);
@@ -185,6 +195,8 @@ public static final class OptionBuilder {
185195

186196
private String namespaceTarget;
187197

198+
private boolean skipWhenAbsent;
199+
188200
public OptionBuilder(String version, SideType type) {
189201
this.version = Objects.requireNonNull(version, "version cannot be null!");
190202
this.type = Objects.requireNonNull(type, "type cannot be null!");
@@ -349,6 +361,11 @@ public OptionBuilder addExtraClasses(ObjectList<String> classes) {
349361
return this;
350362
}
351363

364+
public OptionBuilder skipRemappingWhenMappingsAreAbsent() {
365+
this.skipWhenAbsent = true;
366+
return this;
367+
}
368+
352369
public Options build() {
353370
if(this.outputJar.getParent().equals(this.outputDecompDir))
354371
throw new IllegalArgumentException("The parent directory of outputJar cannot be the same as outputDecomp");
@@ -405,6 +422,11 @@ public String namespaceTarget() {
405422
public Optional<ObjectSet<Path>> bundledLibs() {
406423
return bundledLibs;
407424
}
425+
426+
@Override
427+
public boolean skipWhenAbsent() {
428+
return skipWhenAbsent;
429+
}
408430
};
409431
}
410432
}
@@ -423,9 +445,15 @@ private ClassifiedDeobfuscator buildDeobfuscator() {
423445
} else return new ClassifiedDeobfuscator((ClassifiedMapping<PairedMapping>) mappings, deobfuscation());
424446
} else throw new UnsupportedOperationException("Unsupported yet"); // TODO
425447
}
448+
if (skipWhenAbsent() && !containsMappings(version(), type())) return null;
426449
return new ClassifiedDeobfuscator(version(), type(), deobfuscation());
427450
}
428451

452+
private static boolean containsMappings(String version, SideType type) {
453+
return VersionManifest.getSync(version).getAsJsonObject("downloads")
454+
.has(type + "_mappings");
455+
}
456+
429457
DataMap dataMap();
430458

431459
DeobfuscationOptions deobfuscation();
@@ -441,5 +469,7 @@ private ClassifiedDeobfuscator buildDeobfuscator() {
441469
String namespaceTarget();
442470

443471
Optional<ObjectSet<Path>> bundledLibs();
472+
473+
boolean skipWhenAbsent();
444474
}
445475
}

modules/cli/src/main/java/cn/maxpixel/mcdecompiler/MinecraftDecompilerCommandLine.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public static void main(String[] args) throws Throwable {
9191
ArgumentAcceptingOptionSpec<Path> incrementalDecompilationO = parser.accepts("incremental-decompilation","Try to decompile " +
9292
"incrementally. Specify a jar to compare the difference. Only works with decompilers of source type \"DIRECTORY\"")
9393
.withRequiredArg().withValuesConvertedBy(new PathConverter(PathProperties.FILE_EXISTING));
94+
OptionSpecBuilder skipWhenAbsentO = parser.accepts("skip-when-absent",
95+
"Skip remapping when mappings are absent");
9496
AbstractOptionSpec<Void> help = parser.acceptsAll(of("h", "?", "help"), "For help").forHelp();
9597

9698
for (Option option : ExtensionManager.OPTION_REGISTRY.getOptions()) {
@@ -166,6 +168,8 @@ public String valuePattern() {
166168
builder.addExtraJars(options.valuesOf(extraJarsO));
167169
builder.addExtraClasses(options.valuesOf(extraClassesO));
168170

171+
if (options.has(skipWhenAbsentO)) builder.skipRemappingWhenMappingsAreAbsent();
172+
169173
MinecraftDecompiler md = new MinecraftDecompiler(builder.build());
170174
md.deobfuscate();
171175

0 commit comments

Comments
 (0)