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 --- .../enigma/mapping/MappingsEnigmaWriter.java | 87 ++++++++-------------- 1 file changed, 33 insertions(+), 54 deletions(-) (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java') diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java index 4e29a46..231893d 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