22
33import java .io .BufferedReader ;
44import java .io .IOException ;
5- import java .nio .file .FileVisitResult ;
65import java .nio .file .Files ;
76import java .nio .file .Path ;
87import java .nio .file .Paths ;
9- import java .nio .file .SimpleFileVisitor ;
10- import java .nio .file .attribute .BasicFileAttributes ;
11- import java .util .Collections ;
128import java .util .HashMap ;
139import java .util .Map ;
1410
1713import org .mcphackers .mcp .tasks .info .TaskInfo ;
1814import org .mcphackers .mcp .tools .FileUtil ;
1915import org .mcphackers .mcp .tools .Util ;
20- import org .mcphackers .mcp .tools .tiny .Remapper ;
21- import org .objectweb .asm .ClassReader ;
16+ import org .mcphackers .mcp .tools .mappings .MappingUtil ;
2217
23- import net .fabricmc .mappingio .MappedElementKind ;
2418import net .fabricmc .mappingio .adapter .MappingNsCompleter ;
2519import net .fabricmc .mappingio .adapter .MappingSourceNsSwitch ;
26- import net .fabricmc .mappingio .format .Tiny2Reader ;
27- import net .fabricmc .mappingio .format .Tiny2Writer ;
2820import net .fabricmc .mappingio .tree .MappingTree ;
2921import net .fabricmc .mappingio .tree .MemoryMappingTree ;
3022
3123public class TaskReobfuscate extends Task {
32- private final Map <String , String > recompHashes = new HashMap <>();
33- private final Map <String , String > originalHashes = new HashMap <>();
24+ private Map <String , String > recompHashes = new HashMap <>();
25+ private Map <String , String > originalHashes = new HashMap <>();
3426
35- public MemoryMappingTree mappingTree = new MemoryMappingTree ();
27+ private MemoryMappingTree mappingTree = new MemoryMappingTree ();
3628
37- private final Map <String , String > reobfPackages = new HashMap <>();
29+ private Map <String , String > reobfPackages = new HashMap <>();
3830
39- private final TaskUpdateMD5 md5Task = new TaskUpdateMD5 (side , info );
31+ private TaskUpdateMD5 md5Task = new TaskUpdateMD5 (side , info );
4032
4133 public TaskReobfuscate (int side , TaskInfo info ) {
4234 super (side , info );
@@ -49,7 +41,7 @@ public void doTask() throws Exception {
4941 Path reobfBin = Paths .get (chooseFromSide (MCPConfig .CLIENT_BIN , MCPConfig .SERVER_BIN ));
5042 Path reobfDir = Paths .get (chooseFromSide (MCPConfig .CLIENT_REOBF , MCPConfig .SERVER_REOBF ));
5143 Path reobfMappings = Paths .get (chooseFromSide (MCPConfig .CLIENT_MAPPINGS_RO , MCPConfig .SERVER_MAPPINGS_RO ));
52- Path deobfMappings = Paths .get (chooseFromSide (MCPConfig .CLIENT_MAPPINGS , MCPConfig .SERVER_MAPPINGS ));
44+ Path deobfMappings = Paths .get (chooseFromSide (MCPConfig .CLIENT_MAPPINGS_DO , MCPConfig .SERVER_MAPPINGS_DO ));
5345
5446 step ();
5547 md5Task .updateMD5 (true );
@@ -63,13 +55,26 @@ public void doTask() throws Exception {
6355
6456 step ();
6557 if (hasMappings ) {
66- readDeobfuscationMappings ();
67- writeReobfuscationMappings ();
58+ MappingUtil .readMappings (deobfMappings , mappingTree );
59+ flipMappingTree ();
60+ MappingUtil .modifyMappings (mappingTree , reobfBin , className -> {
61+ if (mappingTree .getClass (className ) == null ) { // Class isn't present in original mappings
62+ String packageName = className .lastIndexOf ("/" ) >= 0 ? className .substring (0 , className .lastIndexOf ("/" ) + 1 ) : null ;
63+ String obfPackage = reobfPackages .get (packageName );
64+ if (obfPackage == null ) {
65+ obfPackage = "" ;
66+ }
67+ return obfPackage + (className .lastIndexOf ("/" ) >= 0 ? className .substring (className .lastIndexOf ("/" ) + 1 ) : className );
68+ }
69+ return null ; // Returning null skips remapping this class
70+ });
71+ MappingUtil .writeMappings (reobfMappings , mappingTree );
72+
6873 }
6974
7075 Files .deleteIfExists (reobfJar );
7176 if (hasMappings ) {
72- Remapper .remap (reobfMappings , reobfBin , reobfJar , TaskDecompile .getLibraryPaths (side ));
77+ MappingUtil .remap (reobfMappings , reobfBin , reobfJar , TaskDecompile .getLibraryPaths (side ));
7378 }
7479 else {
7580 FileUtil .compress (reobfBin , reobfJar );
@@ -81,6 +86,26 @@ public void doTask() throws Exception {
8186 }
8287 }
8388
89+ private void flipMappingTree () throws IOException {
90+ ((MappingTree )mappingTree ).getClasses ().stream ().forEach (classEntry -> {
91+ String obfName = classEntry .getName ("official" );
92+ String deobfName = classEntry .getName ("named" );
93+ String obfPackage = obfName .lastIndexOf ("/" ) >= 0 ? obfName .substring (0 , obfName .lastIndexOf ("/" ) + 1 ) : "" ;
94+ String deobfPackage = deobfName .lastIndexOf ("/" ) >= 0 ? deobfName .substring (0 , deobfName .lastIndexOf ("/" ) + 1 ) : "" ;
95+ if (!reobfPackages .containsKey (deobfPackage )) {
96+ reobfPackages .put (deobfPackage , obfPackage );
97+ }
98+ });
99+
100+ Map namespaces = new HashMap ();
101+ namespaces .put ("named" , "official" );
102+ MemoryMappingTree namedTree = new MemoryMappingTree ();
103+ MappingNsCompleter nsCompleter = new MappingNsCompleter (namedTree , namespaces );
104+ MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch (nsCompleter , "named" );
105+ mappingTree .accept (nsSwitch );
106+ mappingTree = namedTree ;
107+ }
108+
84109 @ Override
85110 public ProgressInfo getProgress () {
86111 int total = 100 ;
@@ -106,75 +131,6 @@ public ProgressInfo getProgress() {
106131 }
107132 }
108133
109- private void writeReobfuscationMappings () throws IOException {
110-
111- Path reobfBin = Paths .get (chooseFromSide (MCPConfig .CLIENT_BIN , MCPConfig .SERVER_BIN ));
112- Path mappings = Paths .get (chooseFromSide (MCPConfig .CLIENT_MAPPINGS_RO , MCPConfig .SERVER_MAPPINGS_RO ));
113-
114- ((MappingTree )mappingTree ).getClasses ().stream ().forEach (classEntry -> {
115- String obfName = classEntry .getName ("official" );
116- String deobfName = classEntry .getName ("named" );
117- String obfPackage = obfName .lastIndexOf ("/" ) >= 0 ? obfName .substring (0 , obfName .lastIndexOf ("/" ) + 1 ) : "" ;
118- String deobfPackage = deobfName .lastIndexOf ("/" ) >= 0 ? deobfName .substring (0 , deobfName .lastIndexOf ("/" ) + 1 ) : "" ;
119- if (!reobfPackages .containsKey (deobfPackage )) {
120- reobfPackages .put (deobfPackage , obfPackage );
121- }
122- });
123-
124- Map namespaces = new HashMap ();
125- namespaces .put ("named" , "official" );
126- MemoryMappingTree namedTree = new MemoryMappingTree ();
127- MappingNsCompleter nsCompleter = new MappingNsCompleter (namedTree , namespaces );
128- MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch (nsCompleter , "named" );
129- mappingTree .accept (nsSwitch );
130- mappingTree = namedTree ;
131-
132- do {
133- if (mappingTree .visitHeader ()) mappingTree .visitNamespaces ("named" , Collections .singletonList ("official" ));
134-
135- if (mappingTree .visitContent ()) {
136- Files .walkFileTree (reobfBin , new SimpleFileVisitor <Path >() {
137- @ Override
138- public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
139- if (file .toString ().endsWith (".class" )) {
140- ClassReader classReader = new ClassReader (Files .readAllBytes (file ));
141- String className = classReader .getClassName ();
142- if (mappingTree .getClass (className ) == null ) { // Class isn't present in original mappings
143- if (mappingTree .visitClass (className )) {
144- String packageName = className .lastIndexOf ("/" ) >= 0 ? className .substring (0 , className .lastIndexOf ("/" ) + 1 ) : null ;
145- String obfPackage = reobfPackages .get (packageName );
146- if (obfPackage == null ) {
147- obfPackage = "" ;
148- }
149- String clsName = obfPackage + (className .lastIndexOf ("/" ) >= 0 ? className .substring (className .lastIndexOf ("/" ) + 1 ) : className );
150- mappingTree .visitDstName (MappedElementKind .CLASS , 0 , clsName );
151-
152- if (mappingTree .visitElementContent (MappedElementKind .CLASS )) {
153- // could do members or class comment here
154- }
155- }
156- }
157- }
158- return super .visitFile (file , attrs );
159- }
160- });
161- }
162- } while (!mappingTree .visitEnd ());
163-
164- try (Tiny2Writer writer = new Tiny2Writer (Files .newBufferedWriter (mappings ), false )) {
165- mappingTree .accept (writer );
166- }
167- }
168-
169- private void readDeobfuscationMappings () throws IOException {
170- Path mappings = Paths .get (chooseFromSide (MCPConfig .CLIENT_MAPPINGS , MCPConfig .SERVER_MAPPINGS ));
171-
172- try (BufferedReader reader = Files .newBufferedReader (mappings )) {
173- Tiny2Reader .read (reader , mappingTree );
174- }
175-
176- }
177-
178134 private void gatherMD5Hashes (boolean reobf ) throws IOException {
179135 Path md5 = Paths .get (reobf ? chooseFromSide (MCPConfig .CLIENT_MD5_RO , MCPConfig .SERVER_MD5_RO )
180136 : chooseFromSide (MCPConfig .CLIENT_MD5 , MCPConfig .SERVER_MD5 ));
0 commit comments