mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-16 18:45:09 +00:00
Finish up classloading system. Use ModJARURL to locate resources
and enable ModLauncher to use those instead. This allows all mods to load within the scope of the game classloader, removing weird conflicts and class discovery problems.
This commit is contained in:
1 parent
6baddc7e26
commit
fd9c83b65f
21 files changed
+360
-144
No files matched your search
@@ -49,12 +49,18 @@ public class LaunchTesting
|
||||
logcontext.getConfiguration().addFilter(eventbusFilter);
|
||||
logcontext.getConfiguration().addFilter(distxformFilter);
|
||||
logcontext.updateLoggers();
|
||||
File invsorter = new File("/home/cpw/projects/minecraft/inventorysorter/classes");
|
||||
if (invsorter.exists()) {
|
||||
System.setProperty("fml.explodedDir", "/home/cpw/projects/minecraft/inventorysorter/classes"); //TODO: Move this to a example included in our tests, not a random location...
|
||||
}
|
||||
|
||||
String assets = System.getenv().getOrDefault("assetDirectory", "assets");
|
||||
String target = System.getenv().get("target");
|
||||
String[] launchArgs = new String[]{
|
||||
"--gameDir", ".",
|
||||
"--launchTarget", target,
|
||||
"--fml.forgeVersion", System.getProperty("forge.version"),
|
||||
"--fml.mcpVersion", System.getProperty("mcp.version"),
|
||||
"--fml.mcVersion", System.getProperty("mc.version"),
|
||||
"--fml.forgeGroup", System.getProperty("forge.group")
|
||||
};
|
||||
|
||||
|
||||
if (target == null) {
|
||||
throw new IllegalArgumentException("Environment variable target must be set.");
|
||||
@@ -62,23 +68,15 @@ public class LaunchTesting
|
||||
|
||||
if (Objects.equals(target,"fmldevclient")) {
|
||||
hackNatives();
|
||||
Launcher.main("--launchTarget", target,
|
||||
"--gameDir", ".",
|
||||
"--accessToken", "blah",
|
||||
"--version", "FMLDev",
|
||||
"--assetIndex", "1.13",
|
||||
"--assetsDir", assets,
|
||||
"--userProperties", "{}",
|
||||
"--fml.forgeVersion", "24.0.0",
|
||||
"--fml.mcpVersion", "2018.11.30",
|
||||
"--fml.mcVersion", "1.13");
|
||||
} else if (Objects.equals(target, "fmldevserver")) {
|
||||
String[] launchargs = ObjectArrays.concat(new String[] {"--launchTarget", target,
|
||||
"--gameDir", ".", "--fml.forgeVersion", "24.0.0",
|
||||
"--fml.mcpVersion", "2018.11.30",
|
||||
"--fml.mcVersion", "1.13"}, args, String.class);
|
||||
Launcher.main(launchargs);
|
||||
launchArgs = ObjectArrays.concat(launchArgs, new String[] {
|
||||
"--accessToken", "blah",
|
||||
"--version", "FMLDev",
|
||||
"--assetIndex", "1.13",
|
||||
"--assetsDir", assets,
|
||||
"--userProperties", "{}"
|
||||
}, String.class);
|
||||
}
|
||||
Launcher.main(launchArgs);
|
||||
Thread.sleep(10000);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
package net.minecraftforge.fml;
|
||||
|
||||
import cpw.mods.modlauncher.TransformingClassLoader;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.event.ModelRegistryEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
@@ -40,12 +41,13 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static net.minecraftforge.fml.Logging.CORE;
|
||||
import static net.minecraftforge.fml.Logging.LOADING;
|
||||
|
||||
public class ModLoader
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static ModLoader INSTANCE;
|
||||
private final ClassLoader launchClassLoader;
|
||||
private final TransformingClassLoader launchClassLoader;
|
||||
private final LoadingModList loadingModList;
|
||||
private final ModLoadingClassLoader modClassLoader;
|
||||
|
||||
@@ -112,6 +114,7 @@ public class ModLoader
|
||||
{
|
||||
final Map<String, IModInfo> modInfoMap = modFile.getModFileInfo().getMods().stream().collect(Collectors.toMap(IModInfo::getModId, Function.identity()));
|
||||
|
||||
LOGGER.debug(LOADING, "ModContainer is {}", ModContainer.class.getClassLoader());
|
||||
return modFile.getScanResult().getTargets().entrySet().stream().
|
||||
map(e-> {
|
||||
try {
|
||||
|
||||
@@ -19,20 +19,18 @@
|
||||
|
||||
package net.minecraftforge.fml;
|
||||
|
||||
import cpw.mods.modlauncher.TransformingClassLoader;
|
||||
import net.minecraftforge.fml.loading.FMLLoader;
|
||||
import net.minecraftforge.fml.loading.ModJarURLHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.SecureClassLoader;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static net.minecraftforge.fml.Logging.LOADING;
|
||||
|
||||
public class ModLoadingClassLoader extends SecureClassLoader
|
||||
public class ModLoadingClassLoader extends TransformingClassLoader
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
@@ -41,62 +39,44 @@ public class ModLoadingClassLoader extends SecureClassLoader
|
||||
}
|
||||
|
||||
private final Predicate<String> classLoadingPredicate;
|
||||
private final TransformingClassLoader tcl;
|
||||
|
||||
protected ModLoadingClassLoader(final ClassLoader parent) {
|
||||
super(parent);
|
||||
protected ModLoadingClassLoader(final TransformingClassLoader parent) {
|
||||
super(parent, path->FMLLoader.getLoadingModList().findURLForResource(path));
|
||||
this.tcl = parent;
|
||||
this.classLoadingPredicate = FMLLoader.getClassLoaderExclusions();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected URL locateResource(final String path) {
|
||||
return FMLLoader.getLoadingModList().findURLForResource(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getResource(String name)
|
||||
{
|
||||
return super.getResource(name);
|
||||
return locateResource(name);
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
|
||||
{
|
||||
synchronized (getClassLoadingLock(name)) {
|
||||
Class<?> existingClass = tcl.getLoadedClass(name);
|
||||
if (existingClass != null) return existingClass;
|
||||
|
||||
final String className = name.replace('.', '/').concat(".class");
|
||||
final Path classResource = FMLLoader.getLoadingModList().findResource(className);
|
||||
if (classResource != null)
|
||||
{
|
||||
return findClass(name);
|
||||
}
|
||||
|
||||
return super.loadClass(name, resolve);
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException
|
||||
{
|
||||
if (!classLoadingPredicate.test(name)) {
|
||||
LOGGER.debug(LOADING, "Delegating to parent {}", name);
|
||||
return getParent().loadClass(name);
|
||||
}
|
||||
LOGGER.debug(LOADING, "Loading class {}", name);
|
||||
final String className = name.replace('.','/').concat(".class");
|
||||
final Path classResource = FMLLoader.getLoadingModList().findResource(className);
|
||||
if (classResource != null) {
|
||||
try {
|
||||
final byte[] bytes = Files.readAllBytes(classResource);
|
||||
return defineClass(name, bytes, 0, bytes.length);
|
||||
LOGGER.debug(LOADING, "Loading class {}", name);
|
||||
if (!classLoadingPredicate.test(name)) {
|
||||
LOGGER.debug(LOADING, "Delegating to parent {}", name);
|
||||
return tcl.loadClass(name);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new ClassNotFoundException("Failed to load class file " + classResource + " for "+ className, e);
|
||||
}
|
||||
} else if(getParent() != null) {
|
||||
getParent().loadClass(name);
|
||||
return tcl.loadClass(name, this::locateResource);
|
||||
}
|
||||
throw new ClassNotFoundException("Failed to find class file "+ className);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected URL findResource(String name)
|
||||
{
|
||||
return super.findResource(name);
|
||||
return locateResource(name);
|
||||
}
|
||||
}
|
||||
@@ -35,8 +35,7 @@ import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static net.minecraftforge.fml.Logging.LOADING;
|
||||
import static net.minecraftforge.fml.Logging.SCAN;
|
||||
import static net.minecraftforge.fml.Logging.*;
|
||||
|
||||
public class FMLJavaModLanguageProvider implements IModLanguageProvider
|
||||
{
|
||||
@@ -68,13 +67,14 @@ public class FMLJavaModLanguageProvider implements IModLanguageProvider
|
||||
// in the classloader of the game - the context classloader is appropriate here.
|
||||
try
|
||||
{
|
||||
final Constructor<?> constructor = Class.forName("net.minecraftforge.fml.javafmlmod.FMLModContainer", true, Thread.currentThread().getContextClassLoader()).
|
||||
getConstructor(IModInfo.class, String.class, ClassLoader.class, ModFileScanData.class);
|
||||
final Class<?> fmlContainer = Class.forName("net.minecraftforge.fml.javafmlmod.FMLModContainer", true, Thread.currentThread().getContextClassLoader());
|
||||
LOGGER.debug(LOADING, "Loading FMLModContainer from classloader {} - got {}", Thread.currentThread().getContextClassLoader(), fmlContainer.getClassLoader());
|
||||
final Constructor<?> constructor = fmlContainer.getConstructor(IModInfo.class, String.class, ClassLoader.class, ModFileScanData.class);
|
||||
return (T)constructor.newInstance(info, className, modClassLoader, modFileScanResults);
|
||||
}
|
||||
catch (NoSuchMethodException | ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e)
|
||||
{
|
||||
LOGGER.error(LOADING,"Unable to load FMLModContainer, wut?", e);
|
||||
LOGGER.fatal(LOADING,"Unable to load FMLModContainer, wut?", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user