Skip to content

Commit abc3da3

Browse files
Add fallback to mapping retrival in dev
1 parent d1524b2 commit abc3da3

File tree

2 files changed

+84
-27
lines changed

2 files changed

+84
-27
lines changed

src/main/java/codechicken/lib/reflect/ObfMapping.java

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
package codechicken.lib.reflect;
22

3+
import codechicken.lib.CodeChickenLib;
4+
import codechicken.lib.internal.CCLLog;
35
import com.google.common.base.Charsets;
46
import com.google.common.base.Objects;
57
import com.google.common.io.LineProcessor;
68
import com.google.common.io.Resources;
79
import net.minecraft.launchwrapper.Launch;
810
import net.minecraftforge.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper;
11+
import net.minecraftforge.fml.common.launcher.FMLTweaker;
12+
import net.minecraftforge.fml.relauncher.CoreModManager;
13+
import org.apache.commons.io.FileUtils;
14+
import org.apache.commons.io.IOUtils;
915
import org.objectweb.asm.commons.Remapper;
1016

1117
import javax.annotation.Nonnull;
1218
import java.io.File;
19+
import java.io.FileOutputStream;
20+
import java.io.FileWriter;
1321
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.OutputStream;
1424
import java.lang.reflect.Field;
25+
import java.net.MalformedURLException;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
28+
import java.nio.charset.StandardCharsets;
29+
import java.nio.file.Files;
30+
import java.util.Enumeration;
1531
import java.util.HashMap;
1632
import java.util.Map;
1733
import java.util.Map.Entry;
34+
import java.util.jar.JarEntry;
35+
import java.util.jar.JarFile;
36+
import java.util.zip.ZipEntry;
37+
import java.util.zip.ZipFile;
1838

1939
public class ObfMapping {
2040

2141
public static class ObfRemapper extends Remapper {
2242

23-
private HashMap<String, String> fields = new HashMap<>();
24-
private HashMap<String, String> funcs = new HashMap<>();
43+
private final HashMap<String, String> fields = new HashMap<>();
44+
private final HashMap<String, String> funcs = new HashMap<>();
2545

2646
@SuppressWarnings ("unchecked")
2747
public ObfRemapper() {
@@ -93,9 +113,53 @@ public static class MCPRemapper extends Remapper implements LineProcessor<Void>
93113

94114
public static File[] getConfFiles() {
95115

116+
File notchSrg;
117+
File csvDir;
118+
File mappings = new File(Launch.minecraftHome, "mappings");
96119
// check for GradleStart system vars
97-
File notchSrg = new File(System.getProperty("net.minecraftforge.gradle.GradleStart.srg.notch-srg"));
98-
File csvDir = new File(System.getProperty("net.minecraftforge.gradle.GradleStart.csvDir"));
120+
String notchSrgPath = System.getProperty("net.minecraftforge.gradle.GradleStart.srg.notch-srg");
121+
String csvDirPath = System.getProperty("net.minecraftforge.gradle.GradleStart.csvDir");
122+
123+
if (notchSrgPath != null) {
124+
notchSrg = new File(notchSrgPath);
125+
} else {
126+
mappings.mkdir();
127+
notchSrg = new File(mappings, "deobf_data-1.12.2.tsrg");
128+
try {
129+
JarFile universalJar = new JarFile(new File(FMLTweaker.getJarLocation()));
130+
JarEntry entry = universalJar.getJarEntry("deobf_data-1.12.2.tsrg");
131+
IOUtils.copy(universalJar.getInputStream(entry), new FileOutputStream(notchSrg));
132+
universalJar.close();
133+
} catch (IOException e) {
134+
CCLLog.logger.fatal("Failed to get mapping file from universal jar.", e);
135+
}
136+
}
137+
138+
if (csvDirPath != null) {
139+
csvDir = new File(csvDirPath);
140+
} else {
141+
mappings.mkdir();
142+
csvDir = mappings;
143+
File mappingZip = new File(mappings, "mcp_stable-39-1.12.zip");
144+
try {
145+
FileUtils.copyURLToFile(new URI("https://maven.minecraftforge.net/de/oceanlabs/mcp/mcp_stable/39-1.12/mcp_stable-39-1.12.zip").toURL(), mappingZip);
146+
try (ZipFile zipFile = new ZipFile(mappingZip)) {
147+
Enumeration<? extends ZipEntry> entries = zipFile.entries();
148+
while (entries.hasMoreElements()) {
149+
ZipEntry entry = entries.nextElement();
150+
File entryDestination = new File(mappings, entry.getName());
151+
try (InputStream in = zipFile.getInputStream(entry);
152+
OutputStream out = Files.newOutputStream(entryDestination.toPath())
153+
) {
154+
IOUtils.copy(in, out);
155+
}
156+
157+
}
158+
}
159+
} catch (URISyntaxException | IOException e) {
160+
CCLLog.logger.fatal("Failed to download mcp mapping file.", e);
161+
}
162+
}
99163

100164
if (notchSrg.exists() && csvDir.exists()) {
101165
File fieldCsv = new File(csvDir, "fields.csv");
@@ -109,17 +173,17 @@ public static File[] getConfFiles() {
109173
throw new RuntimeException("Failed to grab mappings from GradleStart args.");
110174
}
111175

112-
private HashMap<String, String> fields = new HashMap<>();
113-
private HashMap<String, String> funcs = new HashMap<>();
176+
private final HashMap<String, String> fields = new HashMap<>();
177+
private final HashMap<String, String> funcs = new HashMap<>();
114178

115179
public MCPRemapper() {
116180

117181
File[] mappings = getConfFiles();
118182
try {
119-
Resources.readLines(mappings[1].toURI().toURL(), Charsets.UTF_8, this);
120-
Resources.readLines(mappings[2].toURI().toURL(), Charsets.UTF_8, this);
183+
Resources.readLines(mappings[1].toURI().toURL(), StandardCharsets.UTF_8, this);
184+
Resources.readLines(mappings[2].toURI().toURL(), StandardCharsets.UTF_8, this);
121185
} catch (IOException e) {
122-
e.printStackTrace();
186+
CCLLog.logger.fatal("Failed to read mapping csv files.");
123187
}
124188
}
125189

@@ -169,12 +233,7 @@ public static void loadMCPRemapper() {
169233
public static final boolean obfuscated;
170234

171235
static {
172-
boolean obf = true;
173-
try {
174-
obf = Launch.classLoader.getClassBytes("net.minecraft.world.World") == null;
175-
} catch (IOException ignored) {
176-
}
177-
obfuscated = obf;
236+
obfuscated = !(boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
178237
}
179238

180239
public static void init() {
@@ -259,11 +318,10 @@ public String javaClass() {
259318
@Override
260319
public boolean equals(Object obj) {
261320

262-
if (!(obj instanceof ObfMapping)) {
321+
if (!(obj instanceof ObfMapping desc)) {
263322
return false;
264323
}
265324

266-
ObfMapping desc = (ObfMapping) obj;
267325
return s_owner.equals(desc.s_owner) && s_name.equals(desc.s_name) && s_desc.equals(desc.s_desc);
268326
}
269327

@@ -276,10 +334,10 @@ public int hashCode() {
276334
@Override
277335
public String toString() {
278336

279-
if (s_name.length() == 0) {
337+
if (s_name.isEmpty()) {
280338
return "[" + s_owner + "]";
281339
}
282-
if (s_desc.length() == 0) {
340+
if (s_desc.isEmpty()) {
283341
return "[" + s_owner + "." + s_name + "]";
284342
}
285343
return "[" + (isMethod() ? methodDesc() : fieldDesc()) + "]";
@@ -297,7 +355,7 @@ public String fieldDesc() {
297355

298356
public boolean isClass() {
299357

300-
return s_name.length() == 0;
358+
return s_name.isEmpty();
301359
}
302360

303361
public boolean isMethod() {
@@ -326,7 +384,7 @@ public ObfMapping map(Remapper mapper) {
326384

327385
if (isMethod()) {
328386
s_desc = mapper.mapMethodDesc(s_desc);
329-
} else if (s_desc.length() > 0) {
387+
} else if (!s_desc.isEmpty()) {
330388
s_desc = mapper.mapDesc(s_desc);
331389
}
332390

src/main/java/codechicken/lib/reflect/ReflectionManager.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package codechicken.lib.reflect;
22

3+
import com.cleanroommc.hackery.ReflectionHackery;
4+
import net.minecraftforge.common.util.EnumHelper;
35
import org.objectweb.asm.Type;
46

57
import java.lang.reflect.*;
@@ -232,7 +234,7 @@ public static boolean hasField(ObfMapping mapping) {
232234
public static <R> R getField(ObfMapping mapping, Object instance, Class<R> clazz) {
233235
try {
234236
Field field = getField(mapping);
235-
return (R) field.get(instance);
237+
return (R) ReflectionHackery.getField(field, instance);
236238
} catch (Exception e) {
237239
throw new RuntimeException(e);
238240
}
@@ -282,12 +284,9 @@ public static void removeFinal(Field field) {
282284
if ((field.getModifiers() & Modifier.FINAL) == 0) {
283285
return;
284286
}
287+
285288
try {
286-
if (modifiersField == null) {
287-
modifiersField = getField(new ObfMapping("java/lang/reflect/Field", "modifiers"));
288-
modifiersField.setAccessible(true);
289-
}
290-
modifiersField.set(field, field.getModifiers() & ~Modifier.FINAL);
289+
ReflectionHackery.stripFieldOfFinalModifier(field);
291290
} catch (Exception e) {
292291
throw new RuntimeException(e);
293292
}

0 commit comments

Comments
 (0)