mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-25 22:44:32 +00:00
Fix classloading properly. ModLoadingClassLoader doesn't work
with the proper delegation model. Abandoned it, in favour of injecting locators into the Transformer classloader directly.
This commit is contained in:
1 parent
58c78560fa
commit
1b40618f02
27 files changed
+135
-544
No files matched your search
@@ -66,7 +66,7 @@ public class AutomaticEventSubscriber
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
LOGGER.error(LOADING, "Failed to load mod class {} for @EventBusSubscriber annotation", ad.getClassType(), e);
|
||||
LOGGER.fatal(LOADING, "Failed to load mod class {} for @EventBusSubscriber annotation", ad.getClassType(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ public final class FMLWorldPersistenceHook implements WorldPersistenceHooks.Worl
|
||||
}
|
||||
if (!Objects.equals(modVersion, MavenVersionStringHelper.artifactVersionToString(container.get().getModInfo().getVersion())))
|
||||
{
|
||||
LOGGER.info(WORLDPERSISTENCE,"This world was saved with mod {} version {} and it is now at version {}, things may not work well", modId, modVersion, MavenVersionStringHelper.artifactVersionToString(container.get().getModInfo().getVersion()));
|
||||
LOGGER.warn(WORLDPERSISTENCE,"This world was saved with mod {} version {} and it is now at version {}, things may not work well", modId, modVersion, MavenVersionStringHelper.artifactVersionToString(container.get().getModInfo().getVersion()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ import net.minecraftforge.fml.loading.StringUtils;
|
||||
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;
|
||||
import org.apache.commons.lang3.text.ExtendedMessageFormat;
|
||||
import org.apache.commons.lang3.text.FormatFactory;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.text.FieldPosition;
|
||||
import java.text.Format;
|
||||
@@ -33,6 +35,7 @@ import java.util.Objects;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class ForgeI18n {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static Map<String,String> i18n;
|
||||
private static Map<String,FormatFactory> customFactories;
|
||||
|
||||
@@ -77,6 +80,7 @@ public class ForgeI18n {
|
||||
}
|
||||
|
||||
public static void loadLanguageData(final Map<String, String> properties) {
|
||||
LOGGER.debug("Loading I18N data entries: {}", properties.size());
|
||||
i18n = properties;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,12 +42,14 @@ public class LaunchTesting
|
||||
final MarkerFilter axformFilter= MarkerFilter.createFilter("AXFORM", Filter.Result.DENY, Filter.Result.NEUTRAL);
|
||||
final MarkerFilter eventbusFilter = MarkerFilter.createFilter("EVENTBUS", Filter.Result.DENY, Filter.Result.NEUTRAL);
|
||||
final MarkerFilter distxformFilter = MarkerFilter.createFilter("DISTXFORM", Filter.Result.DENY, Filter.Result.NEUTRAL);
|
||||
// final MarkerFilter scannerFilter = MarkerFilter.createFilter("SCAN", Filter.Result.DENY, Filter.Result.NEUTRAL);
|
||||
final LoggerContext logcontext = LoggerContext.getContext(false);
|
||||
logcontext.getConfiguration().addFilter(classloadingFilter);
|
||||
logcontext.getConfiguration().addFilter(launchpluginFilter);
|
||||
logcontext.getConfiguration().addFilter(axformFilter);
|
||||
logcontext.getConfiguration().addFilter(eventbusFilter);
|
||||
logcontext.getConfiguration().addFilter(distxformFilter);
|
||||
// logcontext.getConfiguration().addFilter(axformFilter);
|
||||
// logcontext.getConfiguration().addFilter(eventbusFilter);
|
||||
// logcontext.getConfiguration().addFilter(distxformFilter);
|
||||
// logcontext.getConfiguration().addFilter(scannerFilter);
|
||||
logcontext.updateLoggers();
|
||||
|
||||
String assets = System.getenv().getOrDefault("assetDirectory", "assets");
|
||||
|
||||
@@ -49,7 +49,6 @@ public class ModLoader
|
||||
private static ModLoader INSTANCE;
|
||||
private final TransformingClassLoader launchClassLoader;
|
||||
private final LoadingModList loadingModList;
|
||||
private final ModLoadingClassLoader modClassLoader;
|
||||
|
||||
private final List<ModLoadingException> loadingExceptions;
|
||||
private ModLoader()
|
||||
@@ -57,10 +56,8 @@ public class ModLoader
|
||||
INSTANCE = this;
|
||||
this.launchClassLoader = FMLLoader.getLaunchClassLoader();
|
||||
this.loadingModList = FMLLoader.getLoadingModList();
|
||||
this.modClassLoader = new ModLoadingClassLoader(this.launchClassLoader);
|
||||
this.loadingExceptions = FMLLoader.getLoadingModList().
|
||||
getErrors().stream().flatMap(ModLoadingException::fromEarlyException).collect(Collectors.toList());
|
||||
Thread.currentThread().setContextClassLoader(this.modClassLoader);
|
||||
}
|
||||
|
||||
public static ModLoader get()
|
||||
@@ -80,10 +77,10 @@ public class ModLoader
|
||||
}
|
||||
final Stream<ModContainer> modContainerStream = loadingModList.getModFiles().stream().
|
||||
map(ModFileInfo::getFile).
|
||||
map(mf -> buildMods(mf, modClassLoader)).
|
||||
map(mf -> buildMods(mf, launchClassLoader)).
|
||||
flatMap(Collection::stream);
|
||||
if (!loadingExceptions.isEmpty()) {
|
||||
LOGGER.error(CORE, "Failed to initialize mod containers");
|
||||
LOGGER.fatal(CORE, "Failed to initialize mod containers");
|
||||
throw new LoadingFailedException(loadingExceptions);
|
||||
}
|
||||
modList.setLoadedMods(modContainerStream.collect(Collectors.toList()));
|
||||
@@ -102,7 +99,7 @@ public class ModLoader
|
||||
event.dispatch(this::accumulateErrors);
|
||||
}
|
||||
if (!loadingExceptions.isEmpty()) {
|
||||
LOGGER.error("Failed to complete lifecycle event {}, {} errors found", event, loadingExceptions.size());
|
||||
LOGGER.fatal("Failed to complete lifecycle event {}, {} errors found", event, loadingExceptions.size());
|
||||
throw new LoadingFailedException(loadingExceptions);
|
||||
}
|
||||
}
|
||||
@@ -110,7 +107,7 @@ public class ModLoader
|
||||
loadingExceptions.addAll(errors);
|
||||
}
|
||||
|
||||
private List<ModContainer> buildMods(final ModFile modFile, final ModLoadingClassLoader modClassLoader)
|
||||
private List<ModContainer> buildMods(final ModFile modFile, final TransformingClassLoader modClassLoader)
|
||||
{
|
||||
final Map<String, IModInfo> modInfoMap = modFile.getModFileInfo().getMods().stream().collect(Collectors.toMap(IModInfo::getModId, Function.identity()));
|
||||
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
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.net.URL;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static net.minecraftforge.fml.Logging.LOADING;
|
||||
|
||||
public class ModLoadingClassLoader extends TransformingClassLoader
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
static {
|
||||
ClassLoader.registerAsParallelCapable();
|
||||
}
|
||||
|
||||
private final Predicate<String> classLoadingPredicate;
|
||||
private final TransformingClassLoader tcl;
|
||||
|
||||
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 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;
|
||||
|
||||
LOGGER.debug(LOADING, "Loading class {}", name);
|
||||
if (!classLoadingPredicate.test(name)) {
|
||||
LOGGER.debug(LOADING, "Delegating to parent {}", name);
|
||||
return tcl.loadClass(name);
|
||||
}
|
||||
return tcl.loadClass(name, this::locateResource);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected URL findResource(String name)
|
||||
{
|
||||
return locateResource(name);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user