diff options
| author | 2016-06-30 00:49:21 +1000 | |
|---|---|---|
| committer | 2016-06-30 00:49:21 +1000 | |
| commit | 4be005617b3b8c3578cca07c5d085d12916f0d1d (patch) | |
| tree | db163431f38703e26da417ef05eaea2b27a498b9 /src/main/java/cuchaz/enigma/mapping/MappingsReader.java | |
| parent | Some small changes to fix idea importing (diff) | |
| download | enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.gz enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.xz enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.zip | |
Json format (#2)
* Added new format
* Fixed bug
* Updated Version
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsReader.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsReader.java | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsReader.java new file mode 100644 index 0000000..c790eed --- /dev/null +++ b/src/main/java/cuchaz/enigma/mapping/MappingsReader.java | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * <p> | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.mapping; | ||
| 12 | |||
| 13 | import com.google.gson.Gson; | ||
| 14 | import com.google.gson.GsonBuilder; | ||
| 15 | |||
| 16 | import java.io.BufferedReader; | ||
| 17 | import java.io.File; | ||
| 18 | import java.io.FileReader; | ||
| 19 | import java.io.IOException; | ||
| 20 | |||
| 21 | import cuchaz.enigma.json.JsonClass; | ||
| 22 | |||
| 23 | public class MappingsReader { | ||
| 24 | |||
| 25 | public Mappings read(File in) throws IOException, MappingParseException { | ||
| 26 | Mappings mappings = new Mappings(); | ||
| 27 | readDirectory(mappings, in); | ||
| 28 | return mappings; | ||
| 29 | } | ||
| 30 | |||
| 31 | public void readDirectory(Mappings mappings, File in) throws IOException, MappingParseException { | ||
| 32 | |||
| 33 | File[] fList = in.listFiles(); | ||
| 34 | for (File file : fList) { | ||
| 35 | if (file.isFile()) { | ||
| 36 | readFile(mappings, new BufferedReader(new FileReader(file))); | ||
| 37 | } else if (file.isDirectory()) { | ||
| 38 | readDirectory(mappings, file.getAbsoluteFile()); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | public void readFile(Mappings mappings, BufferedReader in) throws IOException, MappingParseException { | ||
| 44 | |||
| 45 | String builder = ""; | ||
| 46 | String line = null; | ||
| 47 | while ((line = in.readLine()) != null) { | ||
| 48 | builder += line; | ||
| 49 | } | ||
| 50 | |||
| 51 | Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
| 52 | JsonClass jsonClass = gson.fromJson(builder, JsonClass.class); | ||
| 53 | load(null, jsonClass, mappings); | ||
| 54 | } | ||
| 55 | |||
| 56 | public void load(ClassMapping parent, JsonClass jsonClass, Mappings mappings) { | ||
| 57 | ClassMapping classMapping = readClass(jsonClass.getObf(), jsonClass.getName()); | ||
| 58 | if (parent != null) { | ||
| 59 | parent.addInnerClassMapping(classMapping); | ||
| 60 | } else { | ||
| 61 | mappings.addClassMapping(classMapping); | ||
| 62 | } | ||
| 63 | jsonClass.getField().forEach(jsonField -> classMapping.addFieldMapping(readField(jsonField.getObf(), jsonField.getName(), jsonField.getType()))); | ||
| 64 | |||
| 65 | jsonClass.getMethod().forEach(jsonMethod -> { | ||
| 66 | MethodMapping methodMapping = readMethod(jsonMethod.getObf(), jsonMethod.getName(), jsonMethod.getSignature()); | ||
| 67 | jsonMethod.getArgs().forEach(jsonArgument -> methodMapping.addArgumentMapping(readArgument(jsonArgument.getIndex(), jsonArgument.getName()))); | ||
| 68 | classMapping.addMethodMapping(methodMapping); | ||
| 69 | }); | ||
| 70 | |||
| 71 | jsonClass.getInnerClass().forEach(jsonInnerClasses -> { | ||
| 72 | load(classMapping, jsonInnerClasses, mappings); | ||
| 73 | }); | ||
| 74 | } | ||
| 75 | |||
| 76 | private ArgumentMapping readArgument(int index, String name) { | ||
| 77 | return new ArgumentMapping(index, name); | ||
| 78 | } | ||
| 79 | |||
| 80 | private ClassMapping readClass(String obf, String deobf) { | ||
| 81 | return new ClassMapping("none/" + obf, deobf); | ||
| 82 | } | ||
| 83 | |||
| 84 | /* TEMP */ | ||
| 85 | protected FieldMapping readField(String obf, String deobf, String sig) { | ||
| 86 | return new FieldMapping(obf, new Type(sig), deobf); | ||
| 87 | } | ||
| 88 | |||
| 89 | private MethodMapping readMethod(String obf, String deobf, String sig) { | ||
| 90 | return new MethodMapping(obf, new Signature(sig), deobf); | ||
| 91 | } | ||
| 92 | } | ||