2018-06-15 19:03:35 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2018-09-21 09:17:20 +00:00
|
|
|
* Copyright (c) 2016-2018.
|
2018-06-15 19:03:35 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2019-01-04 16:44:57 +00:00
|
|
|
import cpw.mods.modlauncher.TransformingClassLoader;
|
2018-06-21 19:37:32 +00:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
2018-06-19 18:04:05 +00:00
|
|
|
import net.minecraftforge.client.event.ModelRegistryEvent;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.common.capabilities.CapabilityManager;
|
2019-01-29 03:42:37 +00:00
|
|
|
import net.minecraftforge.fml.config.ConfigTracker;
|
|
|
|
import net.minecraftforge.fml.config.ModConfig;
|
|
|
|
import net.minecraftforge.fml.loading.FMLPaths;
|
2019-01-14 03:51:36 +00:00
|
|
|
import net.minecraftforge.forgespi.language.IModInfo;
|
2018-06-15 19:03:35 +00:00
|
|
|
import net.minecraftforge.fml.loading.FMLLoader;
|
|
|
|
import net.minecraftforge.fml.loading.LoadingModList;
|
|
|
|
import net.minecraftforge.fml.loading.moddiscovery.ModFile;
|
|
|
|
import net.minecraftforge.fml.loading.moddiscovery.ModFileInfo;
|
2018-06-19 18:04:05 +00:00
|
|
|
import net.minecraftforge.registries.GameData;
|
2019-01-10 04:57:01 +00:00
|
|
|
import net.minecraftforge.registries.ObjectHolderRegistry;
|
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.function.Function;
|
|
|
|
import java.util.stream.Collectors;
|
2018-06-21 19:37:32 +00:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
import static net.minecraftforge.fml.Logging.CORE;
|
2019-01-04 16:44:57 +00:00
|
|
|
import static net.minecraftforge.fml.Logging.LOADING;
|
2018-06-15 19:03:35 +00:00
|
|
|
|
2019-01-15 03:32:43 +00:00
|
|
|
/**
|
|
|
|
* Loads mods.
|
|
|
|
*
|
|
|
|
* Dispatch cycle is seen in {@link #loadMods()} and {@link #finishMods()}
|
|
|
|
*
|
|
|
|
* Overall sequence for loadMods is:
|
|
|
|
* <dl>
|
|
|
|
* <dt>CONSTRUCT</dt>
|
|
|
|
* <dd>Constructs the mod instance. Mods can typically setup basic environment such as Event listeners
|
|
|
|
* and Configuration specifications here.</dd>
|
|
|
|
* <dt>Automated dispatches</dt>
|
|
|
|
* <dd>Dispatches automated elements : {@link net.minecraftforge.fml.common.Mod.EventBusSubscriber},
|
|
|
|
* {@link net.minecraftforge.event.RegistryEvent}, {@link net.minecraftforge.common.capabilities.CapabilityInject}
|
|
|
|
* and others</dd>
|
|
|
|
* <dt>CONFIG_LOAD</dt>
|
|
|
|
* <dd>Dispatches ConfigLoadEvent to mods</dd>
|
|
|
|
* <dt>COMMON_SETUP</dt>
|
|
|
|
* <dd>Dispatches {@link net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent} to mods</dd>
|
|
|
|
* <dt>SIDED_SETUP</dt>
|
|
|
|
* <dd>Dispatches {@link net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent} or
|
|
|
|
* {@link net.minecraftforge.fml.event.lifecycle.FMLDedicatedServerSetupEvent} to mods</dd>
|
|
|
|
* </dl>
|
|
|
|
*
|
|
|
|
* Overall sequence for finishMods is:
|
|
|
|
* <dl>
|
|
|
|
* <dt>ENQUEUE_IMC</dt>
|
|
|
|
* <dd>Dispatches {@link net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent} to mods,
|
|
|
|
* for enqueuing {@link InterModComms} messages for other mods to receive subsequently</dd>
|
|
|
|
* <dt>PROCESS_IMC</dt>
|
|
|
|
* <dd>Dispatches {@link net.minecraftforge.fml.event.lifecycle.InterModProcessEvent} to mods,
|
|
|
|
* for processing {@link InterModComms} messages received from other mods prior to this event</dd>
|
|
|
|
* <dt>COMPLETE</dt>
|
|
|
|
* <dd>Dispatches {@link net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent} to mods,
|
|
|
|
* and completes the mod loading sequence.</dd>
|
|
|
|
* </dl>
|
|
|
|
*/
|
2018-06-15 19:03:35 +00:00
|
|
|
public class ModLoader
|
|
|
|
{
|
2018-08-27 17:10:07 +00:00
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
2018-06-15 19:03:35 +00:00
|
|
|
private static ModLoader INSTANCE;
|
2019-01-04 16:44:57 +00:00
|
|
|
private final TransformingClassLoader launchClassLoader;
|
2018-06-15 19:03:35 +00:00
|
|
|
private final LoadingModList loadingModList;
|
|
|
|
|
2018-09-29 01:07:46 +00:00
|
|
|
private final List<ModLoadingException> loadingExceptions;
|
2018-06-15 19:03:35 +00:00
|
|
|
private ModLoader()
|
|
|
|
{
|
|
|
|
INSTANCE = this;
|
|
|
|
this.launchClassLoader = FMLLoader.getLaunchClassLoader();
|
|
|
|
this.loadingModList = FMLLoader.getLoadingModList();
|
2018-09-29 01:07:46 +00:00
|
|
|
this.loadingExceptions = FMLLoader.getLoadingModList().
|
2018-10-04 04:57:08 +00:00
|
|
|
getErrors().stream().flatMap(ModLoadingException::fromEarlyException).collect(Collectors.toList());
|
2018-06-15 19:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ModLoader get()
|
|
|
|
{
|
|
|
|
return INSTANCE == null ? INSTANCE = new ModLoader() : INSTANCE;
|
|
|
|
}
|
|
|
|
|
2018-09-29 01:07:46 +00:00
|
|
|
private static Runnable fireClientEvents()
|
2018-06-19 18:04:05 +00:00
|
|
|
{
|
|
|
|
return ()->MinecraftForge.EVENT_BUS.post(new ModelRegistryEvent());
|
|
|
|
}
|
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
public void loadMods() {
|
|
|
|
final ModList modList = ModList.of(loadingModList.getModFiles().stream().map(ModFileInfo::getFile).collect(Collectors.toList()), loadingModList.getMods());
|
2018-10-04 04:57:08 +00:00
|
|
|
if (!this.loadingExceptions.isEmpty()) {
|
|
|
|
throw new LoadingFailedException(loadingExceptions);
|
|
|
|
}
|
2018-06-21 19:37:32 +00:00
|
|
|
final Stream<ModContainer> modContainerStream = loadingModList.getModFiles().stream().
|
2018-06-15 19:03:35 +00:00
|
|
|
map(ModFileInfo::getFile).
|
2019-01-06 20:46:29 +00:00
|
|
|
map(mf -> buildMods(mf, launchClassLoader)).
|
2018-06-21 19:37:32 +00:00
|
|
|
flatMap(Collection::stream);
|
2018-09-29 01:07:46 +00:00
|
|
|
if (!loadingExceptions.isEmpty()) {
|
2019-01-06 20:46:29 +00:00
|
|
|
LOGGER.fatal(CORE, "Failed to initialize mod containers");
|
2018-09-29 01:07:46 +00:00
|
|
|
throw new LoadingFailedException(loadingExceptions);
|
|
|
|
}
|
2018-12-22 00:07:39 +00:00
|
|
|
modList.setLoadedMods(modContainerStream.collect(Collectors.toList()));
|
2018-09-29 01:07:46 +00:00
|
|
|
dispatchAndHandleError(LifecycleEventProvider.CONSTRUCT);
|
2019-01-20 04:26:25 +00:00
|
|
|
GameData.fireCreateRegistryEvents(LifecycleEventProvider.CREATE_REGISTRIES, this::dispatchAndHandleError);
|
2019-01-10 04:57:01 +00:00
|
|
|
ObjectHolderRegistry.findObjectHolders();
|
2018-06-19 18:04:05 +00:00
|
|
|
CapabilityManager.INSTANCE.injectCapabilities(modList.getAllScanData());
|
2019-01-20 04:26:25 +00:00
|
|
|
GameData.fireRegistryEvents(rl->true, LifecycleEventProvider.LOAD_REGISTRIES, this::dispatchAndHandleError);
|
2019-01-29 03:42:37 +00:00
|
|
|
DistExecutor.runWhenOn(Dist.CLIENT, ()->()-> ConfigTracker.INSTANCE.loadConfigs(ModConfig.Type.CLIENT, FMLPaths.CONFIGDIR.get()));
|
2019-01-15 03:32:43 +00:00
|
|
|
dispatchAndHandleError(LifecycleEventProvider.SETUP);
|
2018-09-29 01:07:46 +00:00
|
|
|
DistExecutor.runWhenOn(Dist.CLIENT, ModLoader::fireClientEvents);
|
2019-01-15 03:32:43 +00:00
|
|
|
dispatchAndHandleError(LifecycleEventProvider.SIDED_SETUP);
|
2018-09-29 01:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void dispatchAndHandleError(LifecycleEventProvider event) {
|
|
|
|
if (!loadingExceptions.isEmpty()) {
|
|
|
|
LOGGER.error("Skipping lifecycle event {}, {} errors found.", event, loadingExceptions.size());
|
|
|
|
} else {
|
|
|
|
event.dispatch(this::accumulateErrors);
|
|
|
|
}
|
|
|
|
if (!loadingExceptions.isEmpty()) {
|
2019-01-06 20:46:29 +00:00
|
|
|
LOGGER.fatal("Failed to complete lifecycle event {}, {} errors found", event, loadingExceptions.size());
|
2018-09-29 01:07:46 +00:00
|
|
|
throw new LoadingFailedException(loadingExceptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private void accumulateErrors(List<ModLoadingException> errors) {
|
|
|
|
loadingExceptions.addAll(errors);
|
2018-06-15 19:03:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 20:46:29 +00:00
|
|
|
private List<ModContainer> buildMods(final ModFile modFile, final TransformingClassLoader modClassLoader)
|
2018-06-15 19:03:35 +00:00
|
|
|
{
|
|
|
|
final Map<String, IModInfo> modInfoMap = modFile.getModFileInfo().getMods().stream().collect(Collectors.toMap(IModInfo::getModId, Function.identity()));
|
|
|
|
|
2019-01-04 16:44:57 +00:00
|
|
|
LOGGER.debug(LOADING, "ModContainer is {}", ModContainer.class.getClassLoader());
|
2018-06-15 19:03:35 +00:00
|
|
|
return modFile.getScanResult().getTargets().entrySet().stream().
|
2018-09-29 01:07:46 +00:00
|
|
|
map(e-> {
|
|
|
|
try {
|
2019-02-07 01:44:02 +00:00
|
|
|
IModInfo info = modInfoMap.get(e.getKey());
|
|
|
|
if (info == null) {
|
|
|
|
loadingExceptions.add(new ModLoadingException(null, ModLoadingStage.CONSTRUCT, "fml.modloading.missingmetadata", null, e.getKey()));
|
|
|
|
return null;
|
|
|
|
}
|
2018-09-29 01:07:46 +00:00
|
|
|
return e.getValue().<ModContainer>loadMod(modInfoMap.get(e.getKey()), modClassLoader, modFile.getScanResult());
|
|
|
|
} catch (ModLoadingException mle) {
|
|
|
|
loadingExceptions.add(mle);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}).collect(Collectors.toList());
|
2018-06-15 19:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-19 18:04:05 +00:00
|
|
|
public void finishMods()
|
|
|
|
{
|
2019-01-15 03:32:43 +00:00
|
|
|
dispatchAndHandleError(LifecycleEventProvider.ENQUEUE_IMC);
|
|
|
|
dispatchAndHandleError(LifecycleEventProvider.PROCESS_IMC);
|
2018-09-29 01:07:46 +00:00
|
|
|
dispatchAndHandleError(LifecycleEventProvider.COMPLETE);
|
2018-06-21 19:37:32 +00:00
|
|
|
GameData.freezeData();
|
2018-06-19 18:04:05 +00:00
|
|
|
}
|
2018-06-21 19:37:32 +00:00
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
}
|