2018-06-06 15:37:56 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 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
|
|
|
|
*/
|
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
package net.minecraftforge.fml;
|
2018-06-06 15:37:56 +00:00
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
import com.google.common.collect.Streams;
|
2018-06-19 18:04:05 +00:00
|
|
|
import net.minecraftforge.fml.language.ModFileScanData;
|
2018-06-15 19:03:35 +00:00
|
|
|
import net.minecraftforge.fml.loading.DefaultModInfos;
|
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;
|
|
|
|
import java.util.concurrent.FutureTask;
|
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;
|
2018-06-19 18:04:05 +00:00
|
|
|
private ForkJoinPool modLoadingThreadPool = new ForkJoinPool();
|
|
|
|
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-06-15 19:03:35 +00:00
|
|
|
this.sortedList = Streams.concat(DefaultModInfos.getModInfos().stream(), sortedList.stream()).
|
|
|
|
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));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ModList get() {
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
2018-06-06 15:37:56 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void dispatchLifeCycleEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent) {
|
2018-06-19 18:04:05 +00:00
|
|
|
FMLLoader.getLanguageLoadingProvider().forEach(lp->lp.preLifecycleEvent(lifecycleEvent));
|
|
|
|
DeferredWorkQueue.deferredWorkQueue.clear();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
modLoadingThreadPool.submit(()->this.mods.parallelStream().forEach(m->m.transitionState(lifecycleEvent))).get();
|
|
|
|
}
|
|
|
|
catch (InterruptedException | ExecutionException e)
|
|
|
|
{
|
|
|
|
LOGGER.error(LOADING, "Encountered an exception during parallel processing", e);
|
|
|
|
}
|
|
|
|
DeferredWorkQueue.deferredWorkQueue.forEach(FutureTask::run);
|
|
|
|
FMLLoader.getLanguageLoadingProvider().forEach(lp->lp.postLifecycleEvent(lifecycleEvent));
|
2018-06-11 01:12:46 +00:00
|
|
|
final List<ModContainer> erroredContainers = this.mods.stream().filter(m -> m.getCurrentState() == ModLoadingStage.ERROR).collect(Collectors.toList());
|
|
|
|
if (!erroredContainers.isEmpty()) {
|
2018-06-19 18:04:05 +00:00
|
|
|
throw new RuntimeException("Errored containers found!", erroredContainers.get(0).modLoadingError.get(0));
|
2018-06-11 01:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
public 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));
|
|
|
|
}
|
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);
|
|
|
|
}
|
2018-06-06 15:37:56 +00:00
|
|
|
}
|