Skip to content

Commit 99c80bd

Browse files
committed
Commit or something
1 parent 347dec7 commit 99c80bd

File tree

5 files changed

+174
-155
lines changed

5 files changed

+174
-155
lines changed

src/main/java/org/mcphackers/mcp/MCPConfig.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class MCPConfig {
2121
public static final String LWJGL_UTIL = LIB + "lwjgl_util.jar";
2222
public static final String JINPUT = LIB + "jinput.jar";
2323
public static final String NATIVES = LIB + "natives";
24-
public static final String CLIENT_TINY_OUT = TEMP + "client_remapped.jar";
25-
public static final String SERVER_TINY_OUT = TEMP + "server_remapped.jar";
24+
public static final String CLIENT_TINY_OUT = TEMP + "client_deobf.jar";
25+
public static final String SERVER_TINY_OUT = TEMP + "server_deobf.jar";
2626
public static final String CLIENT_EXC_OUT = TEMP + "client_exc.jar";
2727
public static final String SERVER_EXC_OUT = TEMP + "server_exc.jar";
2828
public static final String CLIENT_SRC = TEMP + "client_src.zip";
@@ -37,6 +37,8 @@ public class MCPConfig {
3737
public static final String SERVER_REOBF_JAR = TEMP + "server_reobf.jar";
3838
public static final String CLIENT_MAPPINGS_RO = TEMP + "client_reobf.tiny";
3939
public static final String SERVER_MAPPINGS_RO = TEMP + "server_reobf.tiny";
40+
public static final String CLIENT_MAPPINGS_DO = TEMP + "client_deobf.tiny";
41+
public static final String SERVER_MAPPINGS_DO = TEMP + "server_deobf.tiny";
4042
public static final String CLIENT_SOURCES = SRC + "minecraft";
4143
public static final String SERVER_SOURCES = SRC + "minecraft_server";
4244
public static final String CLIENT_BIN = BIN + "minecraft";

src/main/java/org/mcphackers/mcp/tasks/TaskDecompile.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
5-
import java.io.OutputStream;
6-
import java.io.PrintStream;
5+
import java.nio.file.FileSystems;
76
import java.nio.file.Files;
87
import java.nio.file.Path;
98
import java.nio.file.Paths;
109

11-
import codechicken.diffpatch.cli.CliOperation;
1210
import org.mcphackers.mcp.MCP;
1311
import org.mcphackers.mcp.MCPConfig;
1412
import org.mcphackers.mcp.ProgressInfo;
@@ -17,15 +15,19 @@
1715
import org.mcphackers.mcp.tools.constants.GLConstants;
1816
import org.mcphackers.mcp.tools.constants.MathConstants;
1917
import org.mcphackers.mcp.tools.fernflower.Decompiler;
18+
import org.mcphackers.mcp.tools.mappings.MappingUtil;
2019
import org.mcphackers.mcp.tools.mcinjector.MCInjector;
21-
import org.mcphackers.mcp.tools.tiny.Remapper;
20+
21+
import codechicken.diffpatch.cli.CliOperation;
2222
import codechicken.diffpatch.cli.PatchOperation;
23+
import net.fabricmc.mappingio.tree.MemoryMappingTree;
2324

2425
public class TaskDecompile extends Task {
2526

2627
private final Decompiler decompiler;
2728
private TaskUpdateMD5 md5Task;
2829
private TaskRecompile recompTask;
30+
private MemoryMappingTree mappingTree = new MemoryMappingTree();
2931

3032
private static final int REMAP = 1;
3133
private static final int EXCEPTOR = 2;
@@ -57,6 +59,7 @@ public void doTask() throws Exception {
5759
Path srcPath = Paths.get(chooseFromSide(MCPConfig.CLIENT_SOURCES, MCPConfig.SERVER_SOURCES));
5860
Path patchesPath = Paths.get(chooseFromSide(MCPConfig.CLIENT_PATCHES, MCPConfig.SERVER_PATCHES));
5961
Path mappings = Paths.get(chooseFromSide(MCPConfig.CLIENT_MAPPINGS, MCPConfig.SERVER_MAPPINGS));
62+
Path deobfMappings = Paths.get(chooseFromSide(MCPConfig.CLIENT_MAPPINGS_DO, MCPConfig.SERVER_MAPPINGS_DO));
6063

6164
boolean hasLWJGL = side == CLIENT;
6265

@@ -73,7 +76,17 @@ public void doTask() throws Exception {
7376
switch (step) {
7477
case REMAP:
7578
if (Files.exists(mappings)) {
76-
Remapper.remap(mappings, originalJar, Paths.get(tinyOut), true, getLibraryPaths(side));
79+
MappingUtil.readMappings(mappings, mappingTree);
80+
MappingUtil.modifyMappings(mappingTree, FileSystems.newFileSystem(originalJar, null).getPath("/"), className -> {
81+
if (mappingTree.getClass(className) == null) {
82+
if(className.lastIndexOf("/") < 0) {
83+
return "net/minecraft/src/" + className;
84+
}
85+
}
86+
return null;
87+
});
88+
MappingUtil.writeMappings(deobfMappings, mappingTree);
89+
MappingUtil.remap(mappings, originalJar, Paths.get(tinyOut), true, getLibraryPaths(side));
7790
}
7891
else {
7992
Files.copy(originalJar, Paths.get(tinyOut));
@@ -122,7 +135,7 @@ public void doTask() throws Exception {
122135
}
123136
}
124137
}
125-
138+
126139
private static void patch(Path base, Path out, Path patches, TaskInfo info) throws IOException {
127140
ByteArrayOutputStream logger = new ByteArrayOutputStream();
128141
PatchOperation patchOperation = PatchOperation.builder()
@@ -135,8 +148,7 @@ private static void patch(Path base, Path out, Path patches, TaskInfo info) thro
135148
CliOperation.Result<PatchOperation.PatchesSummary> result = patchOperation.operate();
136149
if (result.exit != 0) {
137150
info.addInfo(logger.toString());
138-
info.addInfo("Patching failed!");
139-
throw new IOException("Could not apply patches!");
151+
throw new IOException("Patching failed!");
140152
}
141153
}
142154

src/main/java/org/mcphackers/mcp/tasks/TaskReobfuscate.java

Lines changed: 43 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
import java.io.BufferedReader;
44
import java.io.IOException;
5-
import java.nio.file.FileVisitResult;
65
import java.nio.file.Files;
76
import java.nio.file.Path;
87
import java.nio.file.Paths;
9-
import java.nio.file.SimpleFileVisitor;
10-
import java.nio.file.attribute.BasicFileAttributes;
11-
import java.util.Collections;
128
import java.util.HashMap;
139
import java.util.Map;
1410

@@ -17,26 +13,22 @@
1713
import org.mcphackers.mcp.tasks.info.TaskInfo;
1814
import org.mcphackers.mcp.tools.FileUtil;
1915
import 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;
2418
import net.fabricmc.mappingio.adapter.MappingNsCompleter;
2519
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
26-
import net.fabricmc.mappingio.format.Tiny2Reader;
27-
import net.fabricmc.mappingio.format.Tiny2Writer;
2820
import net.fabricmc.mappingio.tree.MappingTree;
2921
import net.fabricmc.mappingio.tree.MemoryMappingTree;
3022

3123
public 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));
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package org.mcphackers.mcp.tools.mappings;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.nio.file.FileVisitResult;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.SimpleFileVisitor;
9+
import java.nio.file.attribute.BasicFileAttributes;
10+
import java.util.function.BiConsumer;
11+
import java.util.function.Function;
12+
import java.util.regex.Pattern;
13+
14+
import org.objectweb.asm.ClassReader;
15+
16+
import net.fabricmc.mappingio.MappedElementKind;
17+
import net.fabricmc.mappingio.format.Tiny2Reader;
18+
import net.fabricmc.mappingio.format.Tiny2Writer;
19+
import net.fabricmc.mappingio.tree.MemoryMappingTree;
20+
import net.fabricmc.tinyremapper.IMappingProvider;
21+
import net.fabricmc.tinyremapper.NonClassCopyMode;
22+
import net.fabricmc.tinyremapper.OutputConsumerPath;
23+
import net.fabricmc.tinyremapper.TinyRemapper;
24+
import net.fabricmc.tinyremapper.TinyUtils;
25+
26+
public class MappingUtil {
27+
28+
private static final Pattern MC_LV_PATTERN = Pattern.compile("\\$\\$\\d+");
29+
30+
public static void readMappings(Path mappings, MemoryMappingTree mappingTree) throws IOException {
31+
try (BufferedReader reader = Files.newBufferedReader(mappings)) {
32+
Tiny2Reader.read(reader, mappingTree);
33+
}
34+
}
35+
36+
public static void writeMappings(Path mappings, MemoryMappingTree mappingTree) throws IOException {
37+
try (Tiny2Writer writer = new Tiny2Writer(Files.newBufferedWriter(mappings), false)) {
38+
mappingTree.accept(writer);
39+
}
40+
}
41+
42+
public static void modifyMappings(MemoryMappingTree mappingTree, Path classPath, Function<String, String> getDstName) throws IOException {
43+
do {
44+
if (mappingTree.visitHeader()) mappingTree.visitNamespaces(mappingTree.getSrcNamespace(), mappingTree.getDstNamespaces());
45+
46+
if (mappingTree.visitContent()) {
47+
Files.walkFileTree(classPath, new SimpleFileVisitor<Path>() {
48+
@Override
49+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
50+
if (file.toString().endsWith(".class")) {
51+
ClassReader classReader = new ClassReader(Files.readAllBytes(file));
52+
String className = classReader.getClassName();
53+
if (mappingTree.visitClass(className)) {
54+
String dstName = getDstName.apply(className);
55+
if(dstName != null) {
56+
mappingTree.visitDstName(MappedElementKind.CLASS, 0, dstName);
57+
}
58+
}
59+
}
60+
return super.visitFile(file, attrs);
61+
}
62+
});
63+
}
64+
} while (!mappingTree.visitEnd());
65+
}
66+
67+
public static void remap(Path mappings, Path input, Path output, Path... cp) throws IOException {
68+
remap(mappings, input, output, false, cp);
69+
}
70+
71+
public static void remap(Path mappings, Path input, Path output, boolean deobf, Path... cp) throws IOException {
72+
TinyRemapper remapper = null;
73+
String[] names = new String[] {"official", "named"};
74+
75+
if(!deobf) {
76+
names = new String[] {"named", "official"};
77+
}
78+
79+
try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) {
80+
remapper = applyMappings(TinyUtils.createTinyMappingProvider(mappings, names[0], names[1]), input, outputConsumer, cp);
81+
if(deobf) outputConsumer.addNonClassFiles(input, NonClassCopyMode.FIX_META_INF, remapper);
82+
} finally {
83+
if (remapper != null) {
84+
remapper.finish();
85+
}
86+
}
87+
}
88+
89+
private static TinyRemapper applyMappings(IMappingProvider mappings, Path input, BiConsumer<String, byte[]> consumer, Path... classpath) {
90+
TinyRemapper remapper = TinyRemapper.newRemapper()
91+
.renameInvalidLocals(false)
92+
.rebuildSourceFilenames(true)
93+
.invalidLvNamePattern(MC_LV_PATTERN)
94+
.withMappings(mappings)
95+
.fixPackageAccess(false)
96+
.threads(Runtime.getRuntime().availableProcessors() - 3)
97+
.rebuildSourceFilenames(true)
98+
.build();
99+
100+
remapper.readClassPath(classpath);
101+
remapper.readInputs(input);
102+
remapper.apply(consumer);
103+
104+
return remapper;
105+
}
106+
107+
}

0 commit comments

Comments
 (0)