diff options
| author | 2016-08-12 19:59:54 +0200 | |
|---|---|---|
| committer | 2016-08-12 19:59:54 +0200 | |
| commit | 6032f55ede3c1b550cfd83b2f768926c992d9a98 (patch) | |
| tree | 75aa7866c2dd635d03798819ea0e691ec85f2247 /src/main/java/cuchaz/enigma/mapping | |
| parent | Implement Enigma directory format (#1) (diff) | |
| download | enigma-fork-6032f55ede3c1b550cfd83b2f768926c992d9a98.tar.gz enigma-fork-6032f55ede3c1b550cfd83b2f768926c992d9a98.tar.xz enigma-fork-6032f55ede3c1b550cfd83b2f768926c992d9a98.zip | |
Remoe JSON directory format support and clean up others things
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping')
3 files changed, 1 insertions, 222 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/Mappings.java b/src/main/java/cuchaz/enigma/mapping/Mappings.java index 1f4ca02..538c67e 100644 --- a/src/main/java/cuchaz/enigma/mapping/Mappings.java +++ b/src/main/java/cuchaz/enigma/mapping/Mappings.java | |||
| @@ -160,6 +160,6 @@ public class Mappings { | |||
| 160 | 160 | ||
| 161 | public enum FormatType | 161 | public enum FormatType |
| 162 | { | 162 | { |
| 163 | JSON_DIRECTORY, ENIGMA_FILE, ENIGMA_DIRECTORY, SRG_FILE | 163 | ENIGMA_FILE, ENIGMA_DIRECTORY, SRG_FILE |
| 164 | } | 164 | } |
| 165 | } | 165 | } |
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java deleted file mode 100644 index 8f5bde3..0000000 --- a/src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java +++ /dev/null | |||
| @@ -1,120 +0,0 @@ | |||
| 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 | } | ||
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsJsonWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsJsonWriter.java deleted file mode 100644 index db95322..0000000 --- a/src/main/java/cuchaz/enigma/mapping/MappingsJsonWriter.java +++ /dev/null | |||
| @@ -1,101 +0,0 @@ | |||
| 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.gson.Gson; | ||
| 15 | import com.google.gson.GsonBuilder; | ||
| 16 | |||
| 17 | import java.io.*; | ||
| 18 | import java.util.ArrayList; | ||
| 19 | import java.util.Collections; | ||
| 20 | import java.util.List; | ||
| 21 | |||
| 22 | import cuchaz.enigma.json.*; | ||
| 23 | |||
| 24 | public class MappingsJsonWriter { | ||
| 25 | |||
| 26 | public void write(File file, Mappings mappings) throws IOException { | ||
| 27 | if (!file.isDirectory()) { | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | |||
| 31 | String[] entries = file.list(); | ||
| 32 | for (String s : entries) { | ||
| 33 | File currentFile = new File(file.getPath(), s); | ||
| 34 | deleteDirectory(currentFile); | ||
| 35 | } | ||
| 36 | |||
| 37 | Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
| 38 | for (ClassMapping classMapping : sorted(mappings.classes())) { | ||
| 39 | if (classMapping.getDeobfName() != null && !classMapping.getDeobfName().equalsIgnoreCase("") && !classMapping.getDeobfName().equalsIgnoreCase("null")) { | ||
| 40 | JsonClass jsonClass = new JsonClass(classMapping.getObfSimpleName(), classMapping.getDeobfName()); | ||
| 41 | write(jsonClass, classMapping); | ||
| 42 | |||
| 43 | File f = new File(file, jsonClass.getName() + ".json"); | ||
| 44 | f.getParentFile().mkdirs(); | ||
| 45 | f.createNewFile(); | ||
| 46 | PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f), Charsets.UTF_8)); | ||
| 47 | writer.write(gson.toJson(jsonClass)); | ||
| 48 | writer.close(); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | private void write(JsonClass jsonClass, ClassMapping classMapping) { | ||
| 54 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { | ||
| 55 | JsonClass innerClass = new JsonClass(classMapping.getObfSimpleName() + "$" + innerClassMapping.getObfSimpleName().replace("nome/", ""), innerClassMapping.getDeobfName()); | ||
| 56 | write(innerClass, innerClassMapping); | ||
| 57 | jsonClass.addInnerClass(innerClass); | ||
| 58 | } | ||
| 59 | |||
| 60 | for (FieldMapping fieldMapping : sorted(classMapping.fields())) { | ||
| 61 | jsonClass.addField(new JsonField(fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfType().toString())); | ||
| 62 | } | ||
| 63 | |||
| 64 | for (MethodMapping methodMapping : sorted(classMapping.methods())) { | ||
| 65 | List<JsonArgument> args = new ArrayList<>(); | ||
| 66 | for (ArgumentMapping argumentMapping : sorted(methodMapping.arguments())) { | ||
| 67 | args.add(new JsonArgument(argumentMapping.getIndex(), argumentMapping.getName())); | ||
| 68 | } | ||
| 69 | if (methodMapping.getObfName().contains("<init>") || methodMapping.getObfName().contains("<clinit>")) { | ||
| 70 | jsonClass.addConstructor(new JsonConstructor(methodMapping.getObfSignature().toString(), args, methodMapping.getObfName().contains("<clinit>"))); | ||
| 71 | } else { | ||
| 72 | jsonClass.addMethod(new JsonMethod(methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfSignature().toString(), args)); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { | ||
| 78 | List<T> out = new ArrayList<>(); | ||
| 79 | for (T t : classes) { | ||
| 80 | out.add(t); | ||
| 81 | } | ||
| 82 | Collections.sort(out); | ||
| 83 | return out; | ||
| 84 | } | ||
| 85 | |||
| 86 | public static boolean deleteDirectory(File directory) { | ||
| 87 | if (directory.exists()) { | ||
| 88 | File[] files = directory.listFiles(); | ||
| 89 | if (null != files) { | ||
| 90 | for (int i = 0; i < files.length; i++) { | ||
| 91 | if (files[i].isDirectory()) { | ||
| 92 | deleteDirectory(files[i]); | ||
| 93 | } else { | ||
| 94 | files[i].delete(); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | return (directory.delete()); | ||
| 100 | } | ||
| 101 | } | ||