diff options
| author | 2019-01-24 14:48:32 +0200 | |
|---|---|---|
| committer | 2019-01-24 13:48:32 +0100 | |
| commit | 00fcd0550fcdda621c2e4662f6ddd55ce673b931 (patch) | |
| tree | 6f9e4c24dbcc6d118fceec56adf7bf9d747a485c /src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | |
| parent | mark as 0.13.0-SNAPSHOT for preliminary development (diff) | |
| download | enigma-fork-00fcd0550fcdda621c2e4662f6ddd55ce673b931.tar.gz enigma-fork-00fcd0550fcdda621c2e4662f6ddd55ce673b931.tar.xz enigma-fork-00fcd0550fcdda621c2e4662f6ddd55ce673b931.zip | |
[WIP] Mapping rework (#91)
* Move packages
* Mapping & entry refactor: first pass
* Fix deobf -> obf tree remapping
* Resolve various issues
* Give all entries the potential for parents and treat inner classes as children
* Deobf UI tree elements
* Tests pass
* Sort mapping output
* Fix delta tracking
* Index separation and first pass for #97
* Keep track of remapped jar index
* Fix child entries not being remapped
* Drop non-root entries
* Track dropped mappings
* Fix enigma mapping ordering
* EntryTreeNode interface
* Small tweaks
* Naive full index remap on rename
* Entries can resolve to more than one root entry
* Support alternative resolution strategies
* Bridge method resolution
* Tests pass
* Fix mappings being used where there are none
* Fix methods with different descriptors being considered unique. closes #89
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | 160 |
1 files changed, 0 insertions, 160 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java deleted file mode 100644 index e3302b1..0000000 --- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java +++ /dev/null | |||
| @@ -1,160 +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 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | |||
| 12 | package cuchaz.enigma.mapping; | ||
| 13 | |||
| 14 | import com.google.common.base.Charsets; | ||
| 15 | |||
| 16 | import java.io.*; | ||
| 17 | import java.util.*; | ||
| 18 | |||
| 19 | public class MappingsEnigmaWriter { | ||
| 20 | |||
| 21 | public void write(File out, Mappings mappings, boolean isDirectoryFormat) throws IOException { | ||
| 22 | if (!isDirectoryFormat) { | ||
| 23 | PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), Charsets.UTF_8)); | ||
| 24 | write(outputWriter, mappings); | ||
| 25 | outputWriter.close(); | ||
| 26 | } else | ||
| 27 | writeAsDirectory(out, mappings); | ||
| 28 | } | ||
| 29 | |||
| 30 | public void writeAsDirectory(File target, Mappings mappings) throws IOException { | ||
| 31 | if (!target.exists() && !target.mkdirs()) | ||
| 32 | throw new IOException("Cannot create mapping directory!"); | ||
| 33 | |||
| 34 | Mappings previousState = mappings.getPreviousState(); | ||
| 35 | for (ClassMapping classMapping : sorted(mappings.classes())) { | ||
| 36 | File result = new File(target, classMapping.getSaveName() + ".mapping"); | ||
| 37 | |||
| 38 | if (!classMapping.isDirty()) { | ||
| 39 | continue; | ||
| 40 | } | ||
| 41 | |||
| 42 | if (classMapping.isEmpty()) { | ||
| 43 | if (result.exists()) { | ||
| 44 | result.delete(); | ||
| 45 | } | ||
| 46 | continue; | ||
| 47 | } | ||
| 48 | |||
| 49 | if (previousState != null) { | ||
| 50 | ClassMapping previousClass = previousState.classesByObf.get(classMapping.getObfFullName()); | ||
| 51 | File previousFile; | ||
| 52 | if (previousClass != null) { | ||
| 53 | previousFile = new File(target, previousClass.getSaveName() + ".mapping"); | ||
| 54 | } else { | ||
| 55 | previousFile = new File(target, classMapping.getObfFullName() + ".mapping"); | ||
| 56 | } | ||
| 57 | if (previousFile.exists() && !previousFile.delete()) { | ||
| 58 | System.err.println("Failed to delete old class mapping " + previousFile.getName()); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | File packageFile = result.getParentFile(); | ||
| 63 | if (!packageFile.exists()) { | ||
| 64 | packageFile.mkdirs(); | ||
| 65 | } | ||
| 66 | result.createNewFile(); | ||
| 67 | |||
| 68 | try (PrintWriter outputWriter = new PrintWriter(new BufferedWriter(new FileWriter(result)))) { | ||
| 69 | write(outputWriter, classMapping, 0); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | // Remove dropped mappings | ||
| 74 | if (previousState != null) { | ||
| 75 | Set<ClassMapping> droppedClassMappings = new HashSet<>(previousState.classes()); | ||
| 76 | droppedClassMappings.removeAll(mappings.classes()); | ||
| 77 | for (ClassMapping droppedMapping : droppedClassMappings) { | ||
| 78 | File result = new File(target, droppedMapping.getSaveName() + ".mapping"); | ||
| 79 | if (!result.exists()) { | ||
| 80 | continue; | ||
| 81 | } | ||
| 82 | if (!result.delete()) { | ||
| 83 | System.err.println("Failed to delete dropped class mapping " + result.getName()); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | public void write(PrintWriter out, Mappings mappings) throws IOException { | ||
| 90 | for (ClassMapping classMapping : sorted(mappings.classes())) { | ||
| 91 | write(out, classMapping, 0); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | protected void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { | ||
| 96 | if (classMapping.getDeobfName() == null) { | ||
| 97 | out.format("%sCLASS %s%s\n", getIndent(depth), classMapping.getObfFullName(), | ||
| 98 | classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); | ||
| 99 | } else { | ||
| 100 | out.format("%sCLASS %s %s%s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getDeobfName(), | ||
| 101 | classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); | ||
| 102 | } | ||
| 103 | |||
| 104 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { | ||
| 105 | write(out, innerClassMapping, depth + 1); | ||
| 106 | } | ||
| 107 | |||
| 108 | for (FieldMapping fieldMapping : sorted(classMapping.fields())) { | ||
| 109 | write(out, fieldMapping, depth + 1); | ||
| 110 | } | ||
| 111 | |||
| 112 | for (MethodMapping methodMapping : sorted(classMapping.methods())) { | ||
| 113 | write(out, methodMapping, depth + 1); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | private void write(PrintWriter out, FieldMapping fieldMapping, int depth) { | ||
| 118 | if (fieldMapping.getDeobfName() == null) | ||
| 119 | out.format("%sFIELD %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getObfDesc().toString(), | ||
| 120 | fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); | ||
| 121 | else | ||
| 122 | out.format("%sFIELD %s %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfDesc().toString(), | ||
| 123 | fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); | ||
| 124 | } | ||
| 125 | |||
| 126 | private void write(PrintWriter out, MethodMapping methodMapping, int depth) throws IOException { | ||
| 127 | if (methodMapping.isObfuscated()) { | ||
| 128 | out.format("%sMETHOD %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getObfDesc(), | ||
| 129 | methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); | ||
| 130 | } else { | ||
| 131 | out.format("%sMETHOD %s %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfDesc(), | ||
| 132 | methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); | ||
| 133 | } | ||
| 134 | |||
| 135 | for (LocalVariableMapping localVariableMapping : sorted(methodMapping.arguments())) { | ||
| 136 | write(out, localVariableMapping, depth + 1); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | private void write(PrintWriter out, LocalVariableMapping localVariableMapping, int depth) { | ||
| 141 | out.format("%sARG %d %s\n", getIndent(depth), localVariableMapping.getIndex(), localVariableMapping.getName()); | ||
| 142 | } | ||
| 143 | |||
| 144 | protected <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { | ||
| 145 | List<T> out = new ArrayList<>(); | ||
| 146 | for (T t : classes) { | ||
| 147 | out.add(t); | ||
| 148 | } | ||
| 149 | Collections.sort(out); | ||
| 150 | return out; | ||
| 151 | } | ||
| 152 | |||
| 153 | private String getIndent(int depth) { | ||
| 154 | StringBuilder buf = new StringBuilder(); | ||
| 155 | for (int i = 0; i < depth; i++) { | ||
| 156 | buf.append("\t"); | ||
| 157 | } | ||
| 158 | return buf.toString(); | ||
| 159 | } | ||
| 160 | } | ||