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/MappingsWriter.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/MappingsWriter.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsWriter.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsWriter.java new file mode 100644 index 0000000..aa37f16 --- /dev/null +++ b/src/main/java/cuchaz/enigma/mapping/MappingsWriter.java | |||
| @@ -0,0 +1,82 @@ | |||
| 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.File; | ||
| 17 | import java.io.FileWriter; | ||
| 18 | import java.io.IOException; | ||
| 19 | import java.util.ArrayList; | ||
| 20 | import java.util.Collections; | ||
| 21 | import java.util.List; | ||
| 22 | |||
| 23 | import cuchaz.enigma.json.*; | ||
| 24 | |||
| 25 | public class MappingsWriter { | ||
| 26 | |||
| 27 | |||
| 28 | public void write(File file, Mappings mappings) throws IOException { | ||
| 29 | if (!file.isDirectory()) { | ||
| 30 | //TODO Error | ||
| 31 | } | ||
| 32 | |||
| 33 | Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
| 34 | for (ClassMapping classMapping : sorted(mappings.classes())) { | ||
| 35 | JsonClass jsonClass = new JsonClass(classMapping.getObfSimpleName(), classMapping.getDeobfName()); | ||
| 36 | write(jsonClass, classMapping); | ||
| 37 | |||
| 38 | File f = new File(file, jsonClass.getName() + ".json"); | ||
| 39 | f.getParentFile().mkdirs(); | ||
| 40 | f.createNewFile(); | ||
| 41 | FileWriter writer = new FileWriter(f); | ||
| 42 | writer.write(gson.toJson(jsonClass)); | ||
| 43 | writer.close(); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | private void write(JsonClass jsonClass, ClassMapping classMapping) throws IOException { | ||
| 48 | if (classMapping.getDeobfName() != null && !classMapping.getDeobfName().equalsIgnoreCase("") && !classMapping.getDeobfName().equalsIgnoreCase("null")) { | ||
| 49 | |||
| 50 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { | ||
| 51 | JsonClass innerClass = new JsonClass(classMapping.getObfSimpleName() + "$" + innerClassMapping.getObfSimpleName().replace("nome/", ""), innerClassMapping.getDeobfName()); | ||
| 52 | write(innerClass, innerClassMapping); | ||
| 53 | jsonClass.addInnerClass(innerClass); | ||
| 54 | } | ||
| 55 | |||
| 56 | for (FieldMapping fieldMapping : sorted(classMapping.fields())) { | ||
| 57 | jsonClass.addField(new JsonField(fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfType().toString())); | ||
| 58 | } | ||
| 59 | |||
| 60 | for (MethodMapping methodMapping : sorted(classMapping.methods())) { | ||
| 61 | List<JsonArgument> args = new ArrayList<>(); | ||
| 62 | for (ArgumentMapping argumentMapping : sorted(methodMapping.arguments())) { | ||
| 63 | args.add(new JsonArgument(argumentMapping.getIndex(), argumentMapping.getName())); | ||
| 64 | } | ||
| 65 | if (methodMapping.getObfName().contains("<init>") || methodMapping.getObfName().contains("<clinit>")) { | ||
| 66 | jsonClass.addConstructor(new JsonConstructor(methodMapping.getObfSignature().toString(), args, methodMapping.getObfName().contains("<clinit>"))); | ||
| 67 | } else { | ||
| 68 | jsonClass.addMethod(new JsonMethod(methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfSignature().toString(), args)); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { | ||
| 75 | List<T> out = new ArrayList<T>(); | ||
| 76 | for (T t : classes) { | ||
| 77 | out.add(t); | ||
| 78 | } | ||
| 79 | Collections.sort(out); | ||
| 80 | return out; | ||
| 81 | } | ||
| 82 | } | ||