Feat: Support Fabric 26.2 injection

- Expose OrangeX.jar through KnotClassDelegate.addCodeSource for Fabric
- Resolve 26.2 mappings by runtime class visibility instead of loader type
- Keep identity mappings for the current Mojang-named Fabric 26.2 runtime
This commit is contained in:
xiaozhou233 committed 2026-11-04 23:06:22 +08:00
1 parent 1699df9c5d
commit 40ca99f853
2 files changed
+64 -7

No files matched your search

@@ -4,6 +4,8 @@ import cn.xiaozhou233.juiceagent.api.JuiceAgent;
import cn.xiaozhou233.orangex.loader.classloader.GameClassLoaderResolver; import cn.xiaozhou233.orangex.loader.classloader.GameClassLoaderResolver;
import java.io.File; import java.io.File;
import java.lang.reflect.Method;
import java.nio.file.Path;
public class Loader { public class Loader {
public static final String VERSION = "1.0.0-SNAPSHOT"; public static final String VERSION = "1.0.0-SNAPSHOT";
@@ -47,7 +49,7 @@ public class Loader {
orangexFile = new File(OrangeXPath + "/OrangeX.jar"); orangexFile = new File(OrangeXPath + "/OrangeX.jar");
} }
System.out.println("OrangeX Injection: " + orangexFile.getAbsolutePath()); System.out.println("OrangeX Injection: " + orangexFile.getAbsolutePath());
JuiceAgent.addToClassLoader(orangexFile.getAbsolutePath(), minecraftClassLoader); addToGameClassLoader(orangexFile, minecraftClassLoader);
// Start OrangeX // Start OrangeX
try { try {
@@ -87,4 +89,44 @@ public class Loader {
return renderThread; return renderThread;
} }
/**
* Fabric's KnotClassLoader keeps its own code-source list and rejects jars
* that were added with a plain URLClassLoader-style addURL. Expose the jar
* through KnotClassDelegate#addCodeSource, which updates both the code
* source list and the internal URL loader.
*/
private static void addToGameClassLoader(File jar, ClassLoader loader) {
if (isKnotClassLoader(loader)) {
try {
addToKnotClassLoader(loader, jar.toPath());
return;
} catch (ReflectiveOperationException | RuntimeException e) {
System.err.println(
"Failed to expose " + jar.getName()
+ " to Fabric KnotClassLoader: " + e);
e.printStackTrace();
}
}
JuiceAgent.addToClassLoader(jar.getAbsolutePath(), loader);
}
private static boolean isKnotClassLoader(ClassLoader loader) {
return loader != null
&& "net.fabricmc.loader.impl.launch.knot.KnotClassLoader"
.equals(loader.getClass().getName());
}
private static void addToKnotClassLoader(ClassLoader loader, Path jar)
throws ReflectiveOperationException {
Method getDelegate = loader.getClass().getDeclaredMethod("getDelegate");
getDelegate.setAccessible(true);
Object delegate = getDelegate.invoke(loader);
Method addCodeSource = delegate.getClass()
.getMethod("addCodeSource", Path.class);
addCodeSource.setAccessible(true);
addCodeSource.invoke(delegate, jar);
}
} }
@@ -31,15 +31,16 @@ public class MappingManager {
} }
case V26_2: { case V26_2: {
if (loaderType == LoaderType.VANILLA // 26.x normally uses Mojang names. Fabric 26.2 currently ships
|| loaderType == LoaderType.MCP // empty intermediary mappings, so it also runs with Mojang
|| loaderType == LoaderType.NEOFORGE) { // names. Verify the runtime instead of trusting the loader type.
// 26.x runtimes use Mojang names for classes, fields and if (usesMojangNames(info)) {
// methods, so no obfuscation mapping is needed.
resolver = new IdentityFallbackMappingResolver(); resolver = new IdentityFallbackMappingResolver();
} else { } else {
throw new RuntimeException( throw new RuntimeException(
"Unsupported loader for 26.2: " + loaderType); "Unsupported runtime naming for 26.2 ("
+ loaderType + "): expected "
+ "net.minecraft.client.Minecraft");
} }
break; break;
@@ -54,6 +55,20 @@ public class MappingManager {
return resolver != null; return resolver != null;
} }
private static boolean usesMojangNames(RuntimeInfo info) {
ClassLoader loader = info.getMinecraftClassLoader();
if (loader == null) {
return false;
}
try {
Class.forName("net.minecraft.client.Minecraft", false, loader);
return true;
} catch (Throwable ignored) {
return false;
}
}
public static MappingResolver getResolver() { public static MappingResolver getResolver() {
if (!isInitialized()) { if (!isInitialized()) {
System.out.println("Resolver not initialized!"); System.out.println("Resolver not initialized!");