Skip to content

Commit

Permalink
Move away from using internal loader API
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Sep 14, 2021
1 parent 68ed43f commit 6fd91a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ static ResourceLocation toNative(final Key key) {
* @param player the player to identify
* @return an identified representation of the player
* @since 4.0.0
*
*/
static @NotNull Identified identified(final @NotNull Player player) {
return (Identified) player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,53 @@
*/
package net.kyori.adventure.platform.fabric.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;
import net.fabricmc.loader.launch.common.FabricLauncherBase;

/**
* Via i509VCB, a trick to get Brig onto the Knot classpath in order to properly mix in.
*
* <p>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.</p>
*
* <p>Original on GitHub at <a href="https://github.com/i509VCB/Fabric-Junkkyard/blob/ce278daa93804697c745a51af06ec812896ec2ad/src/main/java/me/i509/junkkyard/hacks/PreLaunchHacks.java">i509VCB/Fabric-Junkkyard</a></p>
*/
public class AdventurePrelaunch implements PreLaunchEntrypoint {
private static final ClassLoader KNOT_CLASSLOADER = Thread.currentThread().getContextClassLoader();
private static final Method ADD_URL_METHOD;

static {
try {
ADD_URL_METHOD = KNOT_CLASSLOADER.getClass().getMethod("addURL", URL.class);
ADD_URL_METHOD.setAccessible(true);
} catch (final ReflectiveOperationException ex) {
throw new RuntimeException("Failed to load Classloader fields", ex);
}
}

@Override
public void onPreLaunch() {
// TODO: Actually fix loader
final URL target;
try {
target = Class.forName("com.mojang.authlib.UserAuthentication").getProtectionDomain().getCodeSource().getLocation();
FabricLauncherBase.getLauncher().propose(target);
} catch (final ClassNotFoundException e) {
AdventurePrelaunch.hackilyLoadForMixin("com.mojang.authlib.UserAuthentication");
} catch (final ClassNotFoundException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException("please fix fabric loader to enable transforming libraries normally", e);
}
}

/**
* Hackily load the package which a mixin may exist within.
*
* <p>YOU SHOULD NOT TARGET A CLASS WHICH YOU MIXIN TO. (this could be done to better avoid class loads)</p>
*
* @param pathOfAClass The path of any class within the package.
* @throws ClassNotFoundException if an unknown class name is used
* @throws InvocationTargetException if an error occurs while injecting
* @throws IllegalAccessException if an error occurs while injecting
*/
static void hackilyLoadForMixin(final String pathOfAClass) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException {
final URL url = Class.forName(pathOfAClass).getProtectionDomain().getCodeSource().getLocation();
ADD_URL_METHOD.invoke(KNOT_CLASSLOADER, url);
}
}

0 comments on commit 6fd91a1

Please sign in to comment.