From 5b813c2ae4bbf5c7e1733b45d0d52db85060e0dd Mon Sep 17 00:00:00 2001 From: gegy1000 Date: Thu, 5 Jul 2018 09:08:45 +0200 Subject: Use previous save state to delete old mapping files --- .../translators/TranslationMethodVisitor.java | 3 + .../java/cuchaz/enigma/mapping/ClassMapping.java | 18 +++++ .../enigma/mapping/MappingsEnigmaWriter.java | 87 ++++++++-------------- 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java index 4e60e87d..0141b45e 100644 --- a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java +++ b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java @@ -178,6 +178,9 @@ public class TranslationMethodVisitor extends MethodVisitor { String typeName = desc.getTypeEntry().getSimpleName().replace("$", ""); typeName = typeName.substring(0, 1).toUpperCase(Locale.ROOT) + typeName.substring(1); nameBuilder.append(typeName); + } else { + System.err.println("Encountered invalid argument type descriptor " + desc.toString()); + nameBuilder.append("Unk"); } if (!argument || methodEntry.getDesc().getArgumentDescs().size() > 1) { nameBuilder.append(nameIndex); diff --git a/src/main/java/cuchaz/enigma/mapping/ClassMapping.java b/src/main/java/cuchaz/enigma/mapping/ClassMapping.java index 1e6c3a19..be9e7416 100644 --- a/src/main/java/cuchaz/enigma/mapping/ClassMapping.java +++ b/src/main/java/cuchaz/enigma/mapping/ClassMapping.java @@ -517,6 +517,14 @@ public class ClassMapping implements Comparable { return deobfFullName != null ? new ClassEntry(deobfFullName) : null; } + public boolean isObfuscated() { + return this.deobfName == null || this.deobfName.equals(this.obfFullName); + } + + public String getSaveName() { + return this.isObfuscated() ? this.obfFullName : this.deobfName; + } + public boolean isDirty() { return isDirty; } @@ -564,4 +572,14 @@ public class ClassMapping implements Comparable { this.deobfFullName = deobName; return this; } + + @Override + public int hashCode() { + return this.obfFullName.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof ClassMapping && ((ClassMapping) obj).obfFullName.equals(this.obfFullName); + } } diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java index 4e29a465..231893d5 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java @@ -14,9 +14,7 @@ package cuchaz.enigma.mapping; import com.google.common.base.Charsets; import java.io.*; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; public class MappingsEnigmaWriter { @@ -33,70 +31,51 @@ public class MappingsEnigmaWriter { if (!target.exists() && !target.mkdirs()) throw new IOException("Cannot create mapping directory!"); + Mappings previousState = mappings.getPreviousState(); for (ClassMapping classMapping : sorted(mappings.classes())) { - if (!classMapping.isDirty()) + if (!classMapping.isDirty()) { continue; - this.deletePreviousClassMapping(target, classMapping); - File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); - File result; - if (classMapping.getDeobfName() == null) - result = obFile; - else { - // Make sure that old version of the file doesn't exist - if (obFile.exists()) - obFile.delete(); - result = new File(target, classMapping.getDeobfName() + ".mapping"); } - if (!result.getParentFile().exists()) - result.getParentFile().mkdirs(); + if (previousState != null) { + ClassMapping previousClass = previousState.classesByObf.get(classMapping.getObfFullName()); + if (previousClass != null) { + File previousFile = new File(target, previousClass.getSaveName() + ".mapping"); + if (previousFile.exists() && !previousFile.delete()) { + System.err.println("Failed to delete old class mapping " + previousFile.getName()); + } + } + } + + File result = new File(target, classMapping.getSaveName() + ".mapping"); + + File packageFile = result.getParentFile(); + if (!packageFile.exists()) { + packageFile.mkdirs(); + } result.createNewFile(); - PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(result), Charsets.UTF_8)); - write(outputWriter, classMapping, 0); - outputWriter.close(); + + try (PrintWriter outputWriter = new PrintWriter(new BufferedWriter(new FileWriter(result)))) { + write(outputWriter, classMapping, 0); + } } // Remove dropped mappings - if (mappings.getPreviousState() != null) { - List droppedClassMappings = new ArrayList<>(mappings.getPreviousState().classes()); - List classMappings = new ArrayList<>(mappings.classes()); - droppedClassMappings.removeAll(classMappings); - for (ClassMapping classMapping : droppedClassMappings) { - File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); - File result; - if (classMapping.getDeobfName() == null) - result = obFile; - else { - // Make sure that old version of the file doesn't exist - if (obFile.exists()) - obFile.delete(); - result = new File(target, classMapping.getDeobfName() + ".mapping"); + if (previousState != null) { + Set droppedClassMappings = new HashSet<>(previousState.classes()); + droppedClassMappings.removeAll(mappings.classes()); + for (ClassMapping droppedMapping : droppedClassMappings) { + File result = new File(target, droppedMapping.getSaveName() + ".mapping"); + if (!result.exists()) { + continue; + } + if (!result.delete()) { + System.err.println("Failed to delete dropped class mapping " + result.getName()); } - if (result.exists()) - result.delete(); } } } - private void deletePreviousClassMapping(File target, ClassMapping classMapping) { - File prevFile = null; - // Deob rename - if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() != null && !classMapping.getPreviousDeobfName().equals(classMapping.getDeobfName())) { - prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); - } - // Deob to ob rename - else if (classMapping.getDeobfName() == null && classMapping.getPreviousDeobfName() != null) { - prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); - } - // Ob to Deob rename - else if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() == null) { - prevFile = new File(target, classMapping.getObfFullName() + ".mapping"); - } - - if (prevFile != null && prevFile.exists()) - prevFile.delete(); - } - public void write(PrintWriter out, Mappings mappings) throws IOException { for (ClassMapping classMapping : sorted(mappings.classes())) { write(out, classMapping, 0); -- cgit v1.2.3