2018-06-06 15:37:56 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2019-02-10 22:57:03 +00:00
|
|
|
* Copyright (c) 2016-2019.
|
2018-06-06 15:37:56 +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
|
|
|
|
*/
|
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
package net.minecraftforge.fml;
|
2018-06-06 15:37:56 +00:00
|
|
|
|
2019-02-10 16:44:22 +00:00
|
|
|
import net.minecraftforge.fml.loading.FMLConfig;
|
2019-01-14 03:51:36 +00:00
|
|
|
import net.minecraftforge.forgespi.language.ModFileScanData;
|
2018-06-19 18:04:05 +00:00
|
|
|
import net.minecraftforge.fml.loading.FMLLoader;
|
2018-06-06 15:37:56 +00:00
|
|
|
import net.minecraftforge.fml.loading.moddiscovery.ModFile;
|
2018-06-11 01:12:46 +00:00
|
|
|
import net.minecraftforge.fml.loading.moddiscovery.ModFileInfo;
|
2018-06-06 15:37:56 +00:00
|
|
|
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;
|
2018-06-19 18:04:05 +00:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2018-06-06 15:37:56 +00:00
|
|
|
|
2018-06-11 01:12:46 +00:00
|
|
|
import java.util.Collection;
|
2018-06-06 15:37:56 +00:00
|
|
|
import java.util.List;
|
2018-06-11 01:12:46 +00:00
|
|
|
import java.util.Map;
|
2018-06-19 18:04:05 +00:00
|
|
|
import java.util.Objects;
|
2018-06-11 01:12:46 +00:00
|
|
|
import java.util.Optional;
|
2018-06-19 18:04:05 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ForkJoinPool;
|
2019-02-06 06:42:21 +00:00
|
|
|
import java.util.concurrent.ForkJoinWorkerThread;
|
2019-01-20 04:26:25 +00:00
|
|
|
import java.util.function.BiConsumer;
|
2018-06-21 19:37:32 +00:00
|
|
|
import java.util.function.Consumer;
|
2018-06-11 01:12:46 +00:00
|
|
|
import java.util.function.Function;
|
2018-06-06 15:37:56 +00:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2018-06-19 18:04:05 +00:00
|
|
|
import static net.minecraftforge.fml.Logging.LOADING;
|
|
|
|
|
2018-06-06 15:37:56 +00:00
|
|
|
/**
|
2018-06-19 18:04:05 +00:00
|
|
|
* Master list of all mods - game-side version. This is classloaded in the game scope and
|
|
|
|
* can dispatch game level events as a result.
|
2018-06-06 15:37:56 +00:00
|
|
|
*/
|
|
|
|
public class ModList
|
|
|
|
{
|
2018-08-27 17:10:07 +00:00
|
|
|
private static Logger LOGGER = LogManager.getLogger();
|
2018-06-11 13:56:44 +00:00
|
|
|
private static ModList INSTANCE;
|
2018-06-11 01:12:46 +00:00
|
|
|
private final List<ModFileInfo> modFiles;
|
2018-06-06 15:37:56 +00:00
|
|
|
private final List<ModInfo> sortedList;
|
2018-06-11 01:12:46 +00:00
|
|
|
private final Map<String, ModFileInfo> fileById;
|
|
|
|
private List<ModContainer> mods;
|
|
|
|
private Map<String, ModContainer> indexedMods;
|
2019-02-10 16:44:22 +00:00
|
|
|
private ForkJoinPool modLoadingThreadPool;
|
2018-06-19 18:04:05 +00:00
|
|
|
private List<ModFileScanData> modFileScanData;
|
2018-06-06 15:37:56 +00:00
|
|
|
|
2018-06-11 13:56:44 +00:00
|
|
|
private ModList(final List<ModFile> modFiles, final List<ModInfo> sortedList)
|
2018-06-06 15:37:56 +00:00
|
|
|
{
|
2018-06-11 01:12:46 +00:00
|
|
|
this.modFiles = modFiles.stream().map(ModFile::getModFileInfo).map(ModFileInfo.class::cast).collect(Collectors.toList());
|
2018-09-29 01:07:46 +00:00
|
|
|
this.sortedList = sortedList.stream().
|
2018-06-15 19:03:35 +00:00
|
|
|
map(ModInfo.class::cast).
|
|
|
|
collect(Collectors.toList());
|
2018-06-11 01:12:46 +00:00
|
|
|
this.fileById = this.modFiles.stream().map(ModFileInfo::getMods).flatMap(Collection::stream).
|
|
|
|
map(ModInfo.class::cast).
|
|
|
|
collect(Collectors.toMap(ModInfo::getModId, ModInfo::getOwningFile));
|
2019-02-10 16:44:22 +00:00
|
|
|
final int loadingThreadCount = FMLConfig.loadingThreadCount();
|
|
|
|
LOGGER.debug(LOADING, "Using {} threads for parallel mod-loading", loadingThreadCount);
|
|
|
|
modLoadingThreadPool = new ForkJoinPool(loadingThreadCount, ModList::newForkJoinWorkerThread, null, false);
|
2018-06-06 15:37:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:56:44 +00:00
|
|
|
public static ModList of(List<ModFile> modFiles, List<ModInfo> sortedList)
|
|
|
|
{
|
|
|
|
INSTANCE = new ModList(modFiles, sortedList);
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
|
|
|
|
2019-01-20 04:26:25 +00:00
|
|
|
static BiConsumer<LifecycleEventProvider.LifecycleEvent, Consumer<List<ModLoadingException>>> inlineDispatcher = (event, errors) -> ModList.get().dispatchSynchronousEvent(event, errors);
|
|
|
|
|
|
|
|
static BiConsumer<LifecycleEventProvider.LifecycleEvent, Consumer<List<ModLoadingException>>> parallelDispatcher = (event, errors) -> ModList.get().dispatchParallelEvent(event, errors);
|
|
|
|
|
2018-06-11 13:56:44 +00:00
|
|
|
public static ModList get() {
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
2018-06-06 15:37:56 +00:00
|
|
|
|
2019-02-10 16:44:22 +00:00
|
|
|
private static ForkJoinWorkerThread newForkJoinWorkerThread(ForkJoinPool pool) {
|
|
|
|
ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
|
|
|
|
thread.setName("modloading-worker-" + thread.getPoolIndex());
|
|
|
|
// The default sets it to the SystemClassloader, so copy the current one.
|
|
|
|
thread.setContextClassLoader(Thread.currentThread().getContextClassLoader());
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:12:46 +00:00
|
|
|
public List<ModFileInfo> getModFiles()
|
2018-06-06 15:37:56 +00:00
|
|
|
{
|
|
|
|
return modFiles;
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:12:46 +00:00
|
|
|
public ModFileInfo getModFileById(String modid)
|
|
|
|
{
|
|
|
|
return this.fileById.get(modid);
|
|
|
|
}
|
|
|
|
|
2019-01-20 04:26:25 +00:00
|
|
|
private void dispatchSynchronousEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent, final Consumer<List<ModLoadingException>> errorHandler) {
|
2019-01-27 03:18:28 +00:00
|
|
|
LOGGER.debug(LOADING, "Dispatching synchronous event {}", lifecycleEvent);
|
2019-01-20 04:26:25 +00:00
|
|
|
FMLLoader.getLanguageLoadingProvider().forEach(lp->lp.consumeLifecycleEvent(()->lifecycleEvent));
|
|
|
|
this.mods.forEach(m->m.transitionState(lifecycleEvent, errorHandler));
|
|
|
|
FMLLoader.getLanguageLoadingProvider().forEach(lp->lp.consumeLifecycleEvent(()->lifecycleEvent));
|
|
|
|
}
|
|
|
|
private void dispatchParallelEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent, final Consumer<List<ModLoadingException>> errorHandler) {
|
2019-01-27 03:18:28 +00:00
|
|
|
LOGGER.debug(LOADING, "Dispatching parallel event {}", lifecycleEvent);
|
2018-12-31 21:33:54 +00:00
|
|
|
FMLLoader.getLanguageLoadingProvider().forEach(lp->lp.consumeLifecycleEvent(()->lifecycleEvent));
|
2019-01-19 02:10:22 +00:00
|
|
|
DeferredWorkQueue.clear();
|
2018-06-19 18:04:05 +00:00
|
|
|
try
|
|
|
|
{
|
2018-09-29 01:07:46 +00:00
|
|
|
modLoadingThreadPool.submit(()->this.mods.parallelStream().forEach(m->m.transitionState(lifecycleEvent, errorHandler))).get();
|
2018-06-19 18:04:05 +00:00
|
|
|
}
|
|
|
|
catch (InterruptedException | ExecutionException e)
|
|
|
|
{
|
|
|
|
LOGGER.error(LOADING, "Encountered an exception during parallel processing", e);
|
|
|
|
}
|
2019-01-19 02:10:22 +00:00
|
|
|
DeferredWorkQueue.runTasks(lifecycleEvent.fromStage(), errorHandler);
|
2018-12-31 21:33:54 +00:00
|
|
|
FMLLoader.getLanguageLoadingProvider().forEach(lp->lp.consumeLifecycleEvent(()->lifecycleEvent));
|
2018-06-11 01:12:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:26:25 +00:00
|
|
|
void setLoadedMods(final List<ModContainer> modContainers)
|
2018-06-11 01:12:46 +00:00
|
|
|
{
|
|
|
|
this.mods = modContainers;
|
|
|
|
this.indexedMods = modContainers.stream().collect(Collectors.toMap(ModContainer::getModId, Function.identity()));
|
|
|
|
}
|
|
|
|
|
2018-06-19 18:04:05 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T> Optional<T> getModObjectById(String modId)
|
2018-06-11 13:56:44 +00:00
|
|
|
{
|
2018-06-19 18:04:05 +00:00
|
|
|
return getModContainerById(modId).map(ModContainer::getMod).map(o -> (T) o);
|
2018-06-11 13:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<? extends ModContainer> getModContainerById(String modId)
|
2018-06-11 01:12:46 +00:00
|
|
|
{
|
|
|
|
return Optional.ofNullable(this.indexedMods.get(modId));
|
|
|
|
}
|
2019-02-06 06:42:21 +00:00
|
|
|
|
2018-09-18 04:04:02 +00:00
|
|
|
public Optional<? extends ModContainer> getModContainerByObject(Object obj)
|
|
|
|
{
|
|
|
|
return mods.stream().filter(mc -> mc.getMod() == obj).findFirst();
|
|
|
|
}
|
2018-06-11 13:56:44 +00:00
|
|
|
|
|
|
|
public List<ModInfo> getMods()
|
|
|
|
{
|
|
|
|
return this.sortedList;
|
|
|
|
}
|
2018-06-15 19:03:35 +00:00
|
|
|
|
|
|
|
public boolean isLoaded(String modTarget)
|
|
|
|
{
|
|
|
|
return this.indexedMods.containsKey(modTarget);
|
|
|
|
}
|
2018-06-19 18:04:05 +00:00
|
|
|
|
|
|
|
public int size()
|
|
|
|
{
|
|
|
|
return mods.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<ModFileScanData> getAllScanData()
|
|
|
|
{
|
|
|
|
if (modFileScanData == null)
|
|
|
|
{
|
|
|
|
modFileScanData = this.sortedList.stream().
|
|
|
|
map(ModInfo::getOwningFile).
|
|
|
|
filter(Objects::nonNull).
|
|
|
|
map(ModFileInfo::getFile).
|
|
|
|
map(ModFile::getScanResult).
|
|
|
|
collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
return modFileScanData;
|
|
|
|
|
|
|
|
}
|
2018-06-21 19:37:32 +00:00
|
|
|
|
|
|
|
public void forEachModFile(Consumer<ModFile> fileConsumer)
|
|
|
|
{
|
|
|
|
modFiles.stream().map(ModFileInfo::getFile).forEach(fileConsumer);
|
|
|
|
}
|
2019-03-31 20:36:49 +00:00
|
|
|
|
|
|
|
public void forEachModContainer(BiConsumer<String, ModContainer> modContainerConsumer) {
|
|
|
|
indexedMods.forEach(modContainerConsumer);
|
|
|
|
}
|
2018-06-06 15:37:56 +00:00
|
|
|
}
|