Skip to content

Commit a56b26f

Browse files
authored
stage2/fabric: Support custom FabricLoader impl subclasses in injectFakeMod
This is the case with e.g. Lunar Client's custom Fabric Loader implementation. Linear: EM-3472 GitHub: #28
1 parent ffd1e87 commit a56b26f

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

stage2/fabric/src/main/java/gg/essential/loader/stage2/EssentialLoader.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,30 @@ private Object createCandidate(Path path, URL url, Object metadata) throws Class
347347
}
348348
}
349349

350+
private Class<?> findFabricLoaderClass(FabricLoader fabricLoader) throws ClassNotFoundException {
351+
Class<?> clazz = fabricLoader.getClass();
352+
Exception ex = null;
353+
while (clazz != null) {
354+
try {
355+
clazz.getDeclaredField("modMap");
356+
clazz.getDeclaredField("mods");
357+
clazz.getDeclaredField("entrypointStorage");
358+
clazz.getDeclaredField("adapterMap");
359+
return clazz;
360+
} catch (NoSuchFieldException e) {
361+
if (ex == null) ex = e;
362+
else ex.addSuppressed(e); // Accumulate exceptions to provide more context
363+
364+
clazz = clazz.getSuperclass();
365+
}
366+
}
367+
throw new ClassNotFoundException("Could not find the required fields [modMap, mods, entrypointStorage, adapterMap] anywhere in the class hierarchy of FabricLoader.getInstance()", ex);
368+
}
369+
350370
@SuppressWarnings("unchecked")
351371
private void injectFakeMod(final Path path, final URL url, final ModMetadata metadata) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException, InstantiationException {
352372
FabricLoader fabricLoader = FabricLoader.getInstance();
353-
Class<? extends FabricLoader> fabricLoaderClass = fabricLoader.getClass();
373+
Class<?> fabricLoaderClass = findFabricLoaderClass(fabricLoader);
354374
Class<?> ModContainerImpl;
355375
try {
356376
// fabric-loader 0.12

0 commit comments

Comments
 (0)