diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java new file mode 100644 index 0000000..8f5bde3 --- /dev/null +++ b/src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java | |||
| @@ -0,0 +1,120 @@ | |||
| 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.common.base.Charsets; | ||
| 14 | import com.google.common.io.Files; | ||
| 15 | import com.google.gson.Gson; | ||
| 16 | import com.google.gson.GsonBuilder; | ||
| 17 | |||
| 18 | import java.io.*; | ||
| 19 | |||
| 20 | import cuchaz.enigma.json.JsonClass; | ||
| 21 | import cuchaz.enigma.throwables.MappingConflict; | ||
| 22 | |||
| 23 | public class MappingsJsonReader { | ||
| 24 | |||
| 25 | public Mappings read(File in) throws IOException { | ||
| 26 | Mappings mappings = new Mappings(Mappings.FormatType.JSON_DIRECTORY); | ||
| 27 | readDirectory(mappings, in); | ||
| 28 | return mappings; | ||
| 29 | } | ||
| 30 | |||
| 31 | public void readDirectory(Mappings mappings, File in) throws IOException { | ||
| 32 | File[] fList = in.listFiles(); | ||
| 33 | if (fList != null) { | ||
| 34 | for (File file : fList) { | ||
| 35 | if (file.isFile() && Files.getFileExtension(file.getName()).equalsIgnoreCase("json")) { | ||
| 36 | readFile(mappings, new BufferedReader(new InputStreamReader(new FileInputStream(file), | ||
| 37 | Charsets.UTF_8))); | ||
| 38 | } else if (file.isDirectory()) { | ||
| 39 | readDirectory(mappings, file.getAbsoluteFile()); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | public void readFile(Mappings mappings, BufferedReader in) throws IOException { | ||
| 46 | StringBuilder buf = new StringBuilder(); | ||
| 47 | String line; | ||
| 48 | while ((line = in.readLine()) != null) { | ||
| 49 | buf.append(line); | ||
| 50 | } | ||
| 51 | |||
| 52 | Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
| 53 | JsonClass jsonClass = gson.fromJson(buf.toString(), JsonClass.class); | ||
| 54 | try { | ||
| 55 | load(null, jsonClass, mappings); | ||
| 56 | } catch (MappingConflict e) { | ||
| 57 | e.printStackTrace(); | ||
| 58 | } | ||
| 59 | in.close(); | ||
| 60 | } | ||
| 61 | |||
| 62 | public void load(ClassMapping parent, JsonClass jsonClass, Mappings mappings) throws MappingConflict { | ||
| 63 | ClassMapping classMapping = readClass(jsonClass.getObf(), jsonClass.getName()); | ||
| 64 | if (parent != null) { | ||
| 65 | parent.addInnerClassMapping(classMapping); | ||
| 66 | } else { | ||
| 67 | mappings.addClassMapping(classMapping); | ||
| 68 | } | ||
| 69 | jsonClass.getField().forEach(jsonField -> classMapping.addFieldMapping(readField(jsonField.getObf(), jsonField.getName(), jsonField.getType()))); | ||
| 70 | |||
| 71 | jsonClass.getConstructors().forEach(jsonConstructor -> { | ||
| 72 | MethodMapping methodMapping = readMethod(jsonConstructor.isStatics() ? "<clinit>" : "<init>", null, jsonConstructor.getSignature()); | ||
| 73 | jsonConstructor.getArgs().forEach(jsonArgument -> { | ||
| 74 | try { | ||
| 75 | methodMapping.addArgumentMapping(readArgument(jsonArgument.getIndex(), jsonArgument.getName())); | ||
| 76 | } catch (MappingConflict e) { | ||
| 77 | e.printStackTrace(); | ||
| 78 | } | ||
| 79 | }); | ||
| 80 | classMapping.addMethodMapping(methodMapping); | ||
| 81 | }); | ||
| 82 | |||
| 83 | jsonClass.getMethod().forEach(jsonMethod -> { | ||
| 84 | MethodMapping methodMapping = readMethod(jsonMethod.getObf(), jsonMethod.getName(), jsonMethod.getSignature()); | ||
| 85 | jsonMethod.getArgs().forEach(jsonArgument -> { | ||
| 86 | try { | ||
| 87 | methodMapping.addArgumentMapping(readArgument(jsonArgument.getIndex(), jsonArgument.getName())); | ||
| 88 | } catch (MappingConflict e) { | ||
| 89 | e.printStackTrace(); | ||
| 90 | } | ||
| 91 | }); | ||
| 92 | classMapping.addMethodMapping(methodMapping); | ||
| 93 | }); | ||
| 94 | |||
| 95 | jsonClass.getInnerClass().forEach(jsonInnerClasses -> { | ||
| 96 | try { | ||
| 97 | load(classMapping, jsonInnerClasses, mappings); | ||
| 98 | } catch (MappingConflict e) { | ||
| 99 | e.printStackTrace(); | ||
| 100 | } | ||
| 101 | }); | ||
| 102 | } | ||
| 103 | |||
| 104 | private ArgumentMapping readArgument(int index, String name) { | ||
| 105 | return new ArgumentMapping(index, name); | ||
| 106 | } | ||
| 107 | |||
| 108 | private ClassMapping readClass(String obf, String deobf) { | ||
| 109 | return new ClassMapping("none/" + obf, deobf); | ||
| 110 | } | ||
| 111 | |||
| 112 | /* TEMP */ | ||
| 113 | protected FieldMapping readField(String obf, String deobf, String sig) { | ||
| 114 | return new FieldMapping(obf, new Type(sig), deobf); | ||
| 115 | } | ||
| 116 | |||
| 117 | private MethodMapping readMethod(String obf, String deobf, String sig) { | ||
| 118 | return new MethodMapping(obf, new Signature(sig), deobf); | ||
| 119 | } | ||
| 120 | } | ||