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;
|
2018-06-15 19:03:35 +00:00
|
|
|
import net.minecraftforge.fml.language.IModInfo;
|
|
|
|
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;
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
private final ModLoadingClassLoader modClassLoader;
|
|
|
|
|
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();
|
|
|
|
this.modClassLoader = new ModLoadingClassLoader(this.launchClassLoader);
|
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
|
|
|
Thread.currentThread().setContextClassLoader(this.modClassLoader);
|
|
|
|
}
|
|
|
|
|
|
|
|
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).
|
|
|
|
map(mf -> buildMods(mf, modClassLoader)).
|
2018-06-21 19:37:32 +00:00
|
|
|
flatMap(Collection::stream);
|
2018-09-29 01:07:46 +00:00
|
|
|
if (!loadingExceptions.isEmpty()) {
|
|
|
|
LOGGER.error(CORE, "Failed to initialize mod containers");
|
|
|
|
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);
|
2018-06-19 18:04:05 +00:00
|
|
|
GameData.fireCreateRegistryEvents();
|
|
|
|
CapabilityManager.INSTANCE.injectCapabilities(modList.getAllScanData());
|
2018-09-29 01:07:46 +00:00
|
|
|
dispatchAndHandleError(LifecycleEventProvider.PREINIT);
|
|
|
|
DistExecutor.runWhenOn(Dist.CLIENT, ModLoader::fireClientEvents);
|
|
|
|
dispatchAndHandleError(LifecycleEventProvider.SIDEDINIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
LOGGER.error("Failed to complete lifecycle event {}, {} errors found", event, loadingExceptions.size());
|
|
|
|
throw new LoadingFailedException(loadingExceptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private void accumulateErrors(List<ModLoadingException> errors) {
|
|
|
|
loadingExceptions.addAll(errors);
|
2018-06-15 19:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<ModContainer> buildMods(final ModFile modFile, final ModLoadingClassLoader modClassLoader)
|
|
|
|
{
|
|
|
|
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 {
|
|
|
|
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()
|
|
|
|
{
|
2018-09-29 01:07:46 +00:00
|
|
|
dispatchAndHandleError(LifecycleEventProvider.INIT);
|
|
|
|
dispatchAndHandleError(LifecycleEventProvider.POSTINIT);
|
|
|
|
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
|
|
|
}
|