Fix GTNH MixinExtras generator crashing in dev env with NEI

This commit is contained in:
makamys committed 2023-10-21 11:50:21 +02:00
1 parent d5c96de372
commit fe11aee64c
3 files changed
+48 -3

No files matched your search

@@ -28,6 +28,7 @@ public class CompatCore implements IFMLLoadingPlugin {
try {
Class.forName("com.gtnewhorizon.mixinextras.MixinExtrasBootstrap").getMethod("init").invoke(null);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed to initialize MixinExtrasBootstrap");
}
}
@@ -2,6 +2,7 @@ package io.github.legacymoddingmc.unimixins.compat.asm;
import static io.github.legacymoddingmc.unimixins.compat.CompatCore.LOGGER;
import io.github.legacymoddingmc.unimixins.compat.util.LaunchClassLoaderUtils;
import makamys.mixingasm.api.MixinSafeTransformer;
import makamys.mixingasm.api.TransformerInclusions;
import net.minecraft.launchwrapper.IClassTransformer;
@@ -24,12 +25,21 @@ public class LegacyGTNHMixinExtrasGenerator implements IClassTransformer {
if(name.startsWith("com.gtnewhorizon.mixinextras")) {
try {
LOGGER.trace("Generating class " + name);
return Launch.classLoader.getClassBytes("data.gtnhmixinextras." + name);
byte[] bytes = Launch.classLoader.getClassBytes("data.gtnhmixinextras." + name);
if(bytes != null) {
// If NEI is on the class path before CCL (which would cause the game to crash immediately in
// production, but is possible in a dev env), ClassHeirachyTransformer will try to call getClassBytes,
// and if it fails, it will call Class.forName for the class being loaded, causing a
// ClassCircularityError.
// So we need to populate the cache to avoid that from happening.
LaunchClassLoaderUtils.putInResourceCache(name, bytes);
return bytes;
}
} catch (IOException e) {
throw new RuntimeException("Failed to generate class " + name);
}
} else {
return basicClass;
}
return basicClass;
}
}
@@ -0,0 +1,34 @@
package io.github.legacymoddingmc.unimixins.compat.util;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;
public class LaunchClassLoaderUtils {
private static Map<String,byte[]> resourceCache;
private static Set<String> negativeResourceCache;
static {
try {
Field resourceCacheField = LaunchClassLoader.class.getDeclaredField("resourceCache");
resourceCacheField.setAccessible(true);
resourceCache = (Map<String, byte[]>) resourceCacheField.get(Launch.classLoader);
Field negativeResourceCacheField = LaunchClassLoader.class.getDeclaredField("negativeResourceCache");
negativeResourceCacheField.setAccessible(true);
negativeResourceCache = (Set<String>) negativeResourceCacheField.get(Launch.classLoader);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/** Use with care, ArchaicFix weakens the resource cache once it receives the LoadComplete event. */
public static void putInResourceCache(String key, byte[] value) {
resourceCache.put(key, value);
negativeResourceCache.remove(key);
}
}