From c4970cc4addedd4565cf8c3ed9ea92b6a4487e0c Mon Sep 17 00:00:00 2001 From: Thomas Guillemard Date: Fri, 12 Aug 2016 19:23:54 +0200 Subject: 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--- .../cuchaz/enigma/mapping/MappingsJsonReader.java | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsJsonReader.java') 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 @@ +/******************************************************************************* + * Copyright (c) 2015 Jeff Martin. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser General Public + * License v3.0 which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl.html + *
+ * Contributors:
+ * Jeff Martin - initial API and implementation
+ ******************************************************************************/
+package cuchaz.enigma.mapping;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Files;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import java.io.*;
+
+import cuchaz.enigma.json.JsonClass;
+import cuchaz.enigma.throwables.MappingConflict;
+
+public class MappingsJsonReader {
+
+ public Mappings read(File in) throws IOException {
+ Mappings mappings = new Mappings(Mappings.FormatType.JSON_DIRECTORY);
+ readDirectory(mappings, in);
+ return mappings;
+ }
+
+ public void readDirectory(Mappings mappings, File in) throws IOException {
+ File[] fList = in.listFiles();
+ if (fList != null) {
+ for (File file : fList) {
+ if (file.isFile() && Files.getFileExtension(file.getName()).equalsIgnoreCase("json")) {
+ readFile(mappings, new BufferedReader(new InputStreamReader(new FileInputStream(file),
+ Charsets.UTF_8)));
+ } else if (file.isDirectory()) {
+ readDirectory(mappings, file.getAbsoluteFile());
+ }
+ }
+ }
+ }
+
+ public void readFile(Mappings mappings, BufferedReader in) throws IOException {
+ StringBuilder buf = new StringBuilder();
+ String line;
+ while ((line = in.readLine()) != null) {
+ buf.append(line);
+ }
+
+ Gson gson = new GsonBuilder().setPrettyPrinting().create();
+ JsonClass jsonClass = gson.fromJson(buf.toString(), JsonClass.class);
+ try {
+ load(null, jsonClass, mappings);
+ } catch (MappingConflict e) {
+ e.printStackTrace();
+ }
+ in.close();
+ }
+
+ public void load(ClassMapping parent, JsonClass jsonClass, Mappings mappings) throws MappingConflict {
+ ClassMapping classMapping = readClass(jsonClass.getObf(), jsonClass.getName());
+ if (parent != null) {
+ parent.addInnerClassMapping(classMapping);
+ } else {
+ mappings.addClassMapping(classMapping);
+ }
+ jsonClass.getField().forEach(jsonField -> classMapping.addFieldMapping(readField(jsonField.getObf(), jsonField.getName(), jsonField.getType())));
+
+ jsonClass.getConstructors().forEach(jsonConstructor -> {
+ MethodMapping methodMapping = readMethod(jsonConstructor.isStatics() ? "