From 717e024a4bb42eedc13bcf4095a5e9fefbcca61b Mon Sep 17 00:00:00 2001 From: FalsePattern <30945458+FalsePattern@users.noreply.github.com> Date: Mon, 5 Sep 2022 21:52:49 +0200 Subject: [PATCH] MCUrlClassPath rogue classloading fix --- .../lib/internal/impl/mixin/UCPImpl.java | 41 ++++++------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/falsepattern/lib/internal/impl/mixin/UCPImpl.java b/src/main/java/com/falsepattern/lib/internal/impl/mixin/UCPImpl.java index 1a2d196..ed9995e 100644 --- a/src/main/java/com/falsepattern/lib/internal/impl/mixin/UCPImpl.java +++ b/src/main/java/com/falsepattern/lib/internal/impl/mixin/UCPImpl.java @@ -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()); } } }