MCUrlClassPath rogue classloading fix

This commit is contained in:
FalsePattern committed 2022-09-05 21:52:49 +02:00
1 parent afb7fc0e57
commit 717e024a4b
1 file changed
+13 -28
@@ -21,20 +21,17 @@
package com.falsepattern.lib.internal.impl.mixin;
import com.falsepattern.lib.mixin.MinecraftURLClassPath;
import lombok.val;
import sun.misc.URLClassPath;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModClassLoader;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.io.IOException;
public final class UCPImpl {
private static final Object ucp;
private static final Method addURL;
private static final URLClassPath ucp;
private static final boolean GRIMOIRE;
static {
@@ -43,45 +40,33 @@ public final class UCPImpl {
new String[]{"io.github.crucible.grimoire.Grimoire", "io.github.crucible.grimoire.common.GrimoireCore"};
for (val className : knownGrimoireClassNames) {
try {
Class.forName(className, false, MinecraftURLClassPath.class.getClassLoader());
grimoire = true;
break;
} catch (ClassNotFoundException ignored) {
if (Launch.classLoader.getClassBytes(className) != null) {
grimoire = true;
break;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
GRIMOIRE = grimoire;
if (!GRIMOIRE) {
try {
val modClassLoaderField = Loader.class.getDeclaredField("modClassLoader");
modClassLoaderField.setAccessible(true);
val loaderInstanceField = Loader.class.getDeclaredField("instance");
loaderInstanceField.setAccessible(true);
val mainClassLoaderField = ModClassLoader.class.getDeclaredField("mainClassLoader");
mainClassLoaderField.setAccessible(true);
val ucpField = LaunchClassLoader.class.getSuperclass().getDeclaredField("ucp");
ucpField.setAccessible(true);
Object loader = loaderInstanceField.get(null);
val modClassLoader = (ModClassLoader) modClassLoaderField.get(loader);
val mainClassLoader = (LaunchClassLoader) mainClassLoaderField.get(modClassLoader);
ucp = ucpField.get(mainClassLoader);
addURL = ucp.getClass().getDeclaredMethod("addURL", URL.class);
} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
ucp = (URLClassPath) ucpField.get(Launch.classLoader);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
}
} else {
ucp = null;
addURL = null;
System.err.println("Grimoire detected, disabling jar loading utility");
}
}
public static void addJar(File pathToJar) throws Exception {
if (!GRIMOIRE) {
addURL.invoke(ucp, pathToJar.toURI().toURL());
ucp.addURL(pathToJar.toURI().toURL());
}
}
}