diff options
| author | 2016-08-12 19:23:54 +0200 | |
|---|---|---|
| committer | 2016-08-12 19:23:54 +0200 | |
| commit | c4970cc4addedd4565cf8c3ed9ea92b6a4487e0c (patch) | |
| tree | a6f00a59cd0d5bc41014768506d9c4d3aad48de8 /src/main/java/cuchaz/enigma/mapping | |
| parent | Allow exporting mappings as SRG or Enigma (diff) | |
| download | enigma-fork-c4970cc4addedd4565cf8c3ed9ea92b6a4487e0c.tar.gz enigma-fork-c4970cc4addedd4565cf8c3ed9ea92b6a4487e0c.tar.xz enigma-fork-c4970cc4addedd4565cf8c3ed9ea92b6a4487e0c.zip | |
Implement Enigma directory format (#1)
Others changes:
~ Rework File menu
~ Force UTF-8 for all I/O operations
~ Enigma now detect the original file format and use the correct one when you save a mapping
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/Mappings.java | 19 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java (renamed from src/main/java/cuchaz/enigma/mapping/MappingsReaderOld.java) | 48 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java (renamed from src/main/java/cuchaz/enigma/mapping/MappingsOldWriter.java) | 61 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java (renamed from src/main/java/cuchaz/enigma/mapping/MappingsReader.java) | 13 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsJsonWriter.java (renamed from src/main/java/cuchaz/enigma/mapping/MappingsWriter.java) | 9 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java | 7 |
6 files changed, 123 insertions, 34 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/Mappings.java b/src/main/java/cuchaz/enigma/mapping/Mappings.java index b51e1a4..1f4ca02 100644 --- a/src/main/java/cuchaz/enigma/mapping/Mappings.java +++ b/src/main/java/cuchaz/enigma/mapping/Mappings.java | |||
| @@ -24,8 +24,15 @@ public class Mappings { | |||
| 24 | 24 | ||
| 25 | protected Map<String, ClassMapping> classesByObf; | 25 | protected Map<String, ClassMapping> classesByObf; |
| 26 | protected Map<String, ClassMapping> classesByDeobf; | 26 | protected Map<String, ClassMapping> classesByDeobf; |
| 27 | private final FormatType originMapping; | ||
| 27 | 28 | ||
| 28 | public Mappings() { | 29 | public Mappings() |
| 30 | { | ||
| 31 | this(FormatType.ENIGMA_DIRECTORY); | ||
| 32 | } | ||
| 33 | |||
| 34 | public Mappings(FormatType originMapping) { | ||
| 35 | this.originMapping = originMapping; | ||
| 29 | this.classesByObf = Maps.newHashMap(); | 36 | this.classesByObf = Maps.newHashMap(); |
| 30 | this.classesByDeobf = Maps.newHashMap(); | 37 | this.classesByDeobf = Maps.newHashMap(); |
| 31 | } | 38 | } |
| @@ -145,4 +152,14 @@ public class Mappings { | |||
| 145 | } | 152 | } |
| 146 | return mappingChain; | 153 | return mappingChain; |
| 147 | } | 154 | } |
| 155 | |||
| 156 | public FormatType getOriginMappingFormat() | ||
| 157 | { | ||
| 158 | return originMapping; | ||
| 159 | } | ||
| 160 | |||
| 161 | public enum FormatType | ||
| 162 | { | ||
| 163 | JSON_DIRECTORY, ENIGMA_FILE, ENIGMA_DIRECTORY, SRG_FILE | ||
| 164 | } | ||
| 148 | } | 165 | } |
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsReaderOld.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java index 776d908..70f3f18 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsReaderOld.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java | |||
| @@ -1,23 +1,49 @@ | |||
| 1 | package cuchaz.enigma.mapping; | 1 | package cuchaz.enigma.mapping; |
| 2 | 2 | ||
| 3 | import com.google.common.base.Charsets; | ||
| 3 | import com.google.common.collect.Queues; | 4 | import com.google.common.collect.Queues; |
| 5 | import cuchaz.enigma.throwables.MappingConflict; | ||
| 6 | import cuchaz.enigma.throwables.MappingParseException; | ||
| 4 | 7 | ||
| 5 | import java.io.BufferedReader; | 8 | import java.io.*; |
| 6 | import java.io.IOException; | ||
| 7 | import java.io.Reader; | ||
| 8 | import java.util.Deque; | 9 | import java.util.Deque; |
| 9 | 10 | ||
| 10 | import cuchaz.enigma.throwables.MappingConflict; | 11 | public class MappingsEnigmaReader |
| 11 | import cuchaz.enigma.throwables.MappingParseException; | 12 | { |
| 12 | 13 | ||
| 13 | public class MappingsReaderOld { | 14 | public Mappings read(File file) throws IOException, MappingParseException { |
| 15 | Mappings mappings; | ||
| 14 | 16 | ||
| 15 | public Mappings read(Reader in) throws IOException, MappingParseException { | 17 | // Multiple file |
| 16 | return read(new BufferedReader(in)); | 18 | if (file.isDirectory()) |
| 19 | { | ||
| 20 | mappings = new Mappings(Mappings.FormatType.ENIGMA_DIRECTORY); | ||
| 21 | readDirectory(mappings, file); | ||
| 22 | } | ||
| 23 | else | ||
| 24 | { | ||
| 25 | mappings = new Mappings(); | ||
| 26 | readFile(mappings, new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8))); | ||
| 27 | } | ||
| 28 | return mappings; | ||
| 17 | } | 29 | } |
| 18 | 30 | ||
| 19 | public Mappings read(BufferedReader in) throws IOException, MappingParseException { | 31 | public void readDirectory(Mappings mappings, File directory) throws IOException, MappingParseException { |
| 20 | Mappings mappings = new Mappings(); | 32 | File[] files = directory.listFiles(); |
| 33 | if (files != null) { | ||
| 34 | for (File file : files) { | ||
| 35 | if (file.isFile()) | ||
| 36 | readFile(mappings, new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8))); | ||
| 37 | else if (file.isDirectory()) | ||
| 38 | readDirectory(mappings, file.getAbsoluteFile()); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | else | ||
| 42 | throw new IOException("Cannot access directory" + directory.getAbsolutePath()); | ||
| 43 | } | ||
| 44 | |||
| 45 | public Mappings readFile(Mappings mappings, BufferedReader in) throws IOException, MappingParseException { | ||
| 46 | |||
| 21 | Deque<Object> mappingStack = Queues.newArrayDeque(); | 47 | Deque<Object> mappingStack = Queues.newArrayDeque(); |
| 22 | 48 | ||
| 23 | int lineNumber = 0; | 49 | int lineNumber = 0; |
| @@ -96,7 +122,7 @@ public class MappingsReaderOld { | |||
| 96 | e.printStackTrace(); | 122 | e.printStackTrace(); |
| 97 | } | 123 | } |
| 98 | } | 124 | } |
| 99 | 125 | in.close(); | |
| 100 | return mappings; | 126 | return mappings; |
| 101 | } | 127 | } |
| 102 | 128 | ||
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsOldWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java index 8b766a3..a3b0616 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsOldWriter.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | |||
| @@ -10,17 +10,66 @@ | |||
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | package cuchaz.enigma.mapping; | 11 | package cuchaz.enigma.mapping; |
| 12 | 12 | ||
| 13 | import java.io.IOException; | 13 | import com.google.common.base.Charsets; |
| 14 | import java.io.PrintWriter; | 14 | |
| 15 | import java.io.Writer; | 15 | import java.io.*; |
| 16 | import java.util.ArrayList; | 16 | import java.util.ArrayList; |
| 17 | import java.util.Collections; | 17 | import java.util.Collections; |
| 18 | import java.util.List; | 18 | import java.util.List; |
| 19 | 19 | ||
| 20 | public class MappingsOldWriter { | 20 | public class MappingsEnigmaWriter { |
| 21 | 21 | ||
| 22 | public void write(Writer out, Mappings mappings) throws IOException { | 22 | public void write(File out, Mappings mappings, boolean isDirectoryFormat) throws IOException { |
| 23 | write(new PrintWriter(out), mappings); | 23 | if (!isDirectoryFormat) |
| 24 | { | ||
| 25 | PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), Charsets.UTF_8)); | ||
| 26 | write(outputWriter, mappings); | ||
| 27 | outputWriter.close(); | ||
| 28 | } | ||
| 29 | else | ||
| 30 | writeAsDirectory(out, mappings); | ||
| 31 | } | ||
| 32 | |||
| 33 | public void writeAsDirectory(File target, Mappings mappings) throws IOException { | ||
| 34 | if (!target.exists() && !target.mkdirs()) | ||
| 35 | throw new IOException("Cannot create mapping directory!"); | ||
| 36 | |||
| 37 | for (ClassMapping classMapping : sorted(mappings.classes())) { | ||
| 38 | File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); | ||
| 39 | File result; | ||
| 40 | if (classMapping.getDeobfName() == null) | ||
| 41 | result = obFile; | ||
| 42 | else | ||
| 43 | { | ||
| 44 | // Make sure that old version of the file doesn't exist | ||
| 45 | if (obFile.exists()) | ||
| 46 | obFile.delete(); | ||
| 47 | result = new File(target, classMapping.getDeobfName() + ".mapping"); | ||
| 48 | } | ||
| 49 | |||
| 50 | if (!result.getParentFile().exists()) | ||
| 51 | result.getParentFile().mkdirs(); | ||
| 52 | result.createNewFile(); | ||
| 53 | PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(result), Charsets.UTF_8)); | ||
| 54 | write(outputWriter, classMapping, 0); | ||
| 55 | outputWriter.close(); | ||
| 56 | } | ||
| 57 | |||
| 58 | // Remove empty directories | ||
| 59 | File[] fileList = target.listFiles(); | ||
| 60 | if (fileList != null) | ||
| 61 | { | ||
| 62 | for (File file : fileList) | ||
| 63 | { | ||
| 64 | if (file.isDirectory()) | ||
| 65 | { | ||
| 66 | File[] childFiles = file.listFiles(); | ||
| 67 | if (childFiles != null && childFiles.length == 0) | ||
| 68 | file.delete(); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 24 | } | 73 | } |
| 25 | 74 | ||
| 26 | public void write(PrintWriter out, Mappings mappings) throws IOException { | 75 | public void write(PrintWriter out, Mappings mappings) throws IOException { |
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java index b2b6d09..8f5bde3 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsReader.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java | |||
| @@ -10,22 +10,20 @@ | |||
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | package cuchaz.enigma.mapping; | 11 | package cuchaz.enigma.mapping; |
| 12 | 12 | ||
| 13 | import com.google.common.base.Charsets; | ||
| 13 | import com.google.common.io.Files; | 14 | import com.google.common.io.Files; |
| 14 | import com.google.gson.Gson; | 15 | import com.google.gson.Gson; |
| 15 | import com.google.gson.GsonBuilder; | 16 | import com.google.gson.GsonBuilder; |
| 16 | 17 | ||
| 17 | import java.io.BufferedReader; | 18 | import java.io.*; |
| 18 | import java.io.File; | ||
| 19 | import java.io.FileReader; | ||
| 20 | import java.io.IOException; | ||
| 21 | 19 | ||
| 22 | import cuchaz.enigma.json.JsonClass; | 20 | import cuchaz.enigma.json.JsonClass; |
| 23 | import cuchaz.enigma.throwables.MappingConflict; | 21 | import cuchaz.enigma.throwables.MappingConflict; |
| 24 | 22 | ||
| 25 | public class MappingsReader { | 23 | public class MappingsJsonReader { |
| 26 | 24 | ||
| 27 | public Mappings read(File in) throws IOException { | 25 | public Mappings read(File in) throws IOException { |
| 28 | Mappings mappings = new Mappings(); | 26 | Mappings mappings = new Mappings(Mappings.FormatType.JSON_DIRECTORY); |
| 29 | readDirectory(mappings, in); | 27 | readDirectory(mappings, in); |
| 30 | return mappings; | 28 | return mappings; |
| 31 | } | 29 | } |
| @@ -35,7 +33,8 @@ public class MappingsReader { | |||
| 35 | if (fList != null) { | 33 | if (fList != null) { |
| 36 | for (File file : fList) { | 34 | for (File file : fList) { |
| 37 | if (file.isFile() && Files.getFileExtension(file.getName()).equalsIgnoreCase("json")) { | 35 | if (file.isFile() && Files.getFileExtension(file.getName()).equalsIgnoreCase("json")) { |
| 38 | readFile(mappings, new BufferedReader(new FileReader(file))); | 36 | readFile(mappings, new BufferedReader(new InputStreamReader(new FileInputStream(file), |
| 37 | Charsets.UTF_8))); | ||
| 39 | } else if (file.isDirectory()) { | 38 | } else if (file.isDirectory()) { |
| 40 | readDirectory(mappings, file.getAbsoluteFile()); | 39 | readDirectory(mappings, file.getAbsoluteFile()); |
| 41 | } | 40 | } |
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsJsonWriter.java index 4793166..db95322 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsWriter.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsJsonWriter.java | |||
| @@ -10,19 +10,18 @@ | |||
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | package cuchaz.enigma.mapping; | 11 | package cuchaz.enigma.mapping; |
| 12 | 12 | ||
| 13 | import com.google.common.base.Charsets; | ||
| 13 | import com.google.gson.Gson; | 14 | import com.google.gson.Gson; |
| 14 | import com.google.gson.GsonBuilder; | 15 | import com.google.gson.GsonBuilder; |
| 15 | 16 | ||
| 16 | import java.io.File; | 17 | import java.io.*; |
| 17 | import java.io.FileWriter; | ||
| 18 | import java.io.IOException; | ||
| 19 | import java.util.ArrayList; | 18 | import java.util.ArrayList; |
| 20 | import java.util.Collections; | 19 | import java.util.Collections; |
| 21 | import java.util.List; | 20 | import java.util.List; |
| 22 | 21 | ||
| 23 | import cuchaz.enigma.json.*; | 22 | import cuchaz.enigma.json.*; |
| 24 | 23 | ||
| 25 | public class MappingsWriter { | 24 | public class MappingsJsonWriter { |
| 26 | 25 | ||
| 27 | public void write(File file, Mappings mappings) throws IOException { | 26 | public void write(File file, Mappings mappings) throws IOException { |
| 28 | if (!file.isDirectory()) { | 27 | if (!file.isDirectory()) { |
| @@ -44,7 +43,7 @@ public class MappingsWriter { | |||
| 44 | File f = new File(file, jsonClass.getName() + ".json"); | 43 | File f = new File(file, jsonClass.getName() + ".json"); |
| 45 | f.getParentFile().mkdirs(); | 44 | f.getParentFile().mkdirs(); |
| 46 | f.createNewFile(); | 45 | f.createNewFile(); |
| 47 | FileWriter writer = new FileWriter(f); | 46 | PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f), Charsets.UTF_8)); |
| 48 | writer.write(gson.toJson(jsonClass)); | 47 | writer.write(gson.toJson(jsonClass)); |
| 49 | writer.close(); | 48 | writer.close(); |
| 50 | } | 49 | } |
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java index 64da709..a05b915 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java | |||
| @@ -1,10 +1,9 @@ | |||
| 1 | package cuchaz.enigma.mapping; | 1 | package cuchaz.enigma.mapping; |
| 2 | 2 | ||
| 3 | import com.google.common.base.Charsets; | ||
| 3 | import cuchaz.enigma.Deobfuscator; | 4 | import cuchaz.enigma.Deobfuscator; |
| 4 | 5 | ||
| 5 | import java.io.File; | 6 | import java.io.*; |
| 6 | import java.io.FileWriter; | ||
| 7 | import java.io.IOException; | ||
| 8 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
| 9 | import java.util.Collections; | 8 | import java.util.Collections; |
| 10 | import java.util.List; | 9 | import java.util.List; |
| @@ -22,7 +21,7 @@ public class MappingsSRGWriter { | |||
| 22 | 21 | ||
| 23 | Mappings mappings = deobfuscator.getMappings(); | 22 | Mappings mappings = deobfuscator.getMappings(); |
| 24 | 23 | ||
| 25 | FileWriter writer = new FileWriter(file); | 24 | PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8)); |
| 26 | List<String> fieldMappings = new ArrayList<>(); | 25 | List<String> fieldMappings = new ArrayList<>(); |
| 27 | List<String> methodMappings = new ArrayList<>(); | 26 | List<String> methodMappings = new ArrayList<>(); |
| 28 | for (ClassMapping classMapping : sorted(mappings.classes())) { | 27 | for (ClassMapping classMapping : sorted(mappings.classes())) { |