From b31f917acf072275c646b2bab9680c775714ac0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB?= Date: Tue, 5 Jul 2022 14:17:44 -0700 Subject: [PATCH] Fix cloud in dev env --- .../com/dfsek/terra/quilt/PreLaunchHacks.java | 43 +++++++++++++++++++ .../terra/quilt/QuiltPreLaunchEntryPoint.java | 22 ++++++++++ .../quilt/src/main/resources/quilt.mod.json | 3 ++ 3 files changed, 68 insertions(+) create mode 100644 platforms/quilt/src/main/java/com/dfsek/terra/quilt/PreLaunchHacks.java create mode 100644 platforms/quilt/src/main/java/com/dfsek/terra/quilt/QuiltPreLaunchEntryPoint.java diff --git a/platforms/quilt/src/main/java/com/dfsek/terra/quilt/PreLaunchHacks.java b/platforms/quilt/src/main/java/com/dfsek/terra/quilt/PreLaunchHacks.java new file mode 100644 index 000000000..c9d42de36 --- /dev/null +++ b/platforms/quilt/src/main/java/com/dfsek/terra/quilt/PreLaunchHacks.java @@ -0,0 +1,43 @@ +package com.dfsek.terra.quilt; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; + +/** + * So you want to Mixin into Authlib/Brigadier/DataFixerUpper, on Fabric you'll need this guy. + * + *

YOU SHOULD ONLY USE THIS CLASS DURING "preLaunch" and ONLY TARGET A CLASS WHICH IS NOT ANY CLASS YOU MIXIN TO. + * + * This will likely not work on Gson because FabricLoader has some special logic related to Gson. + */ +public final class PreLaunchHacks { + private PreLaunchHacks() {} + + private static final ClassLoader KNOT_CLASSLOADER = Thread.currentThread().getContextClassLoader(); + private static final Method ADD_URL_METHOD; + + static { + Method tempAddUrlMethod = null; + try { + tempAddUrlMethod = KNOT_CLASSLOADER.getClass().getMethod("addURL", URL.class); + tempAddUrlMethod.setAccessible(true); + } catch (ReflectiveOperationException e) { + throw new RuntimeException("Failed to load Classloader fields", e); + } + + ADD_URL_METHOD = tempAddUrlMethod; + } + + /** + * Hackily load the package which a mixin may exist within. + * + * YOU SHOULD NOT TARGET A CLASS WHICH YOU MIXIN TO. + * + * @param pathOfAClass The path of any class within the package. + */ + public static void hackilyLoadForMixin(String pathOfAClass) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException { + URL url = Class.forName(pathOfAClass).getProtectionDomain().getCodeSource().getLocation(); + ADD_URL_METHOD.invoke(KNOT_CLASSLOADER, url); + } +} diff --git a/platforms/quilt/src/main/java/com/dfsek/terra/quilt/QuiltPreLaunchEntryPoint.java b/platforms/quilt/src/main/java/com/dfsek/terra/quilt/QuiltPreLaunchEntryPoint.java new file mode 100644 index 000000000..1afb1ee4e --- /dev/null +++ b/platforms/quilt/src/main/java/com/dfsek/terra/quilt/QuiltPreLaunchEntryPoint.java @@ -0,0 +1,22 @@ +package com.dfsek.terra.quilt; + +import cloud.commandframework.brigadier.BrigadierMappingBuilder; +import org.quiltmc.loader.api.ModContainer; +import org.quiltmc.loader.api.QuiltLoader; +import org.quiltmc.loader.api.entrypoint.PreLaunchEntrypoint; + +import java.lang.reflect.InvocationTargetException; + + +public class QuiltPreLaunchEntryPoint implements PreLaunchEntrypoint { + @Override + public void onPreLaunch(ModContainer mod) { + if (QuiltLoader.isDevelopmentEnvironment()) { + try { + PreLaunchHacks.hackilyLoadForMixin(BrigadierMappingBuilder.class.getName()); + } catch(ClassNotFoundException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/platforms/quilt/src/main/resources/quilt.mod.json b/platforms/quilt/src/main/resources/quilt.mod.json index 53afd8ecf..fbf0b5470 100644 --- a/platforms/quilt/src/main/resources/quilt.mod.json +++ b/platforms/quilt/src/main/resources/quilt.mod.json @@ -23,6 +23,9 @@ "entrypoints": { "init": [ "com.dfsek.terra.quilt.QuiltEntryPoint" + ], + "pre_launch": [ + "com.dfsek.terra.quilt.QuiltPreLaunchEntryPoint" ] }, "depends": [