diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java index ba0720a..e1763d0 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | |||
| @@ -30,24 +30,14 @@ public class MappingsEnigmaWriter { | |||
| 30 | writeAsDirectory(out, mappings); | 30 | writeAsDirectory(out, mappings); |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | private void deleteDir(File file) { | ||
| 34 | File[] contents = file.listFiles(); | ||
| 35 | if (contents != null) { | ||
| 36 | for (File f : contents) { | ||
| 37 | deleteDir(f); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | file.delete(); | ||
| 41 | } | ||
| 42 | |||
| 43 | public void writeAsDirectory(File target, Mappings mappings) throws IOException { | 33 | public void writeAsDirectory(File target, Mappings mappings) throws IOException { |
| 44 | //TODO: Know what have changes during write to not rewrite all the things | ||
| 45 | deleteDir(target); | ||
| 46 | if (!target.exists() && !target.mkdirs()) | 34 | if (!target.exists() && !target.mkdirs()) |
| 47 | throw new IOException("Cannot create mapping directory!"); | 35 | throw new IOException("Cannot create mapping directory!"); |
| 48 | |||
| 49 | 36 | ||
| 50 | for (ClassMapping classMapping : sorted(mappings.classes())) { | 37 | for (ClassMapping classMapping : sorted(mappings.classes())) { |
| 38 | if (!classMapping.isDirty()) | ||
| 39 | continue; | ||
| 40 | this.deletePreviousClassMapping(target, classMapping); | ||
| 51 | File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); | 41 | File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); |
| 52 | File result; | 42 | File result; |
| 53 | if (classMapping.getDeobfName() == null) | 43 | if (classMapping.getDeobfName() == null) |
| @@ -68,8 +58,50 @@ public class MappingsEnigmaWriter { | |||
| 68 | outputWriter.close(); | 58 | outputWriter.close(); |
| 69 | } | 59 | } |
| 70 | 60 | ||
| 61 | // Remove dropped mappings | ||
| 62 | List<ClassMapping> droppedClassMappings = new ArrayList<>(mappings.getPreviousState().classes()); | ||
| 63 | List<ClassMapping> classMappings = new ArrayList<>(mappings.classes()); | ||
| 64 | droppedClassMappings.removeAll(classMappings); | ||
| 65 | for (ClassMapping classMapping : droppedClassMappings) | ||
| 66 | { | ||
| 67 | File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); | ||
| 68 | File result; | ||
| 69 | if (classMapping.getDeobfName() == null) | ||
| 70 | result = obFile; | ||
| 71 | else | ||
| 72 | { | ||
| 73 | // Make sure that old version of the file doesn't exist | ||
| 74 | if (obFile.exists()) | ||
| 75 | obFile.delete(); | ||
| 76 | result = new File(target, classMapping.getDeobfName() + ".mapping"); | ||
| 77 | } | ||
| 78 | if (result.exists()) | ||
| 79 | result.delete(); | ||
| 80 | } | ||
| 71 | } | 81 | } |
| 72 | 82 | ||
| 83 | private void deletePreviousClassMapping(File target, ClassMapping classMapping) { | ||
| 84 | File prevFile = null; | ||
| 85 | // Deob rename | ||
| 86 | if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() != null && !classMapping.getPreviousDeobfName().equals(classMapping.getDeobfName())) | ||
| 87 | { | ||
| 88 | prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); | ||
| 89 | } | ||
| 90 | // Deob to ob rename | ||
| 91 | else if (classMapping.getDeobfName() == null && classMapping.getPreviousDeobfName() != null) | ||
| 92 | { | ||
| 93 | prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); | ||
| 94 | } | ||
| 95 | // Ob to Deob rename | ||
| 96 | else if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() == null) | ||
| 97 | { | ||
| 98 | prevFile = new File(target, classMapping.getObfFullName() + ".mapping"); | ||
| 99 | } | ||
| 100 | |||
| 101 | if (prevFile != null && prevFile.exists()) | ||
| 102 | prevFile.delete(); | ||
| 103 | } | ||
| 104 | |||
| 73 | public void write(PrintWriter out, Mappings mappings) throws IOException { | 105 | public void write(PrintWriter out, Mappings mappings) throws IOException { |
| 74 | for (ClassMapping classMapping : sorted(mappings.classes())) { | 106 | for (ClassMapping classMapping : sorted(mappings.classes())) { |
| 75 | write(out, classMapping, 0); | 107 | write(out, classMapping, 0); |