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
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.minecraftforge.fml.loading;
|
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
import com.google.common.collect.Streams;
|
2018-06-06 15:37:56 +00:00
|
|
|
import net.minecraftforge.fml.loading.moddiscovery.BackgroundScanHandler;
|
|
|
|
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;
|
|
|
|
|
2019-01-04 16:44:57 +00:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2018-06-15 19:03:35 +00:00
|
|
|
import java.nio.file.Files;
|
2018-06-06 15:37:56 +00:00
|
|
|
import java.nio.file.Path;
|
2018-09-29 01:07:46 +00:00
|
|
|
import java.util.ArrayList;
|
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-06 15:37:56 +00:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
2018-06-19 18:04:05 +00:00
|
|
|
* Master list of all mods <em>in the loading context. This class cannot refer outside the
|
|
|
|
* loading package</em>
|
2018-06-06 15:37:56 +00:00
|
|
|
*/
|
2018-06-15 19:03:35 +00:00
|
|
|
public class LoadingModList
|
2018-06-06 15:37:56 +00:00
|
|
|
{
|
2018-06-15 19:03:35 +00:00
|
|
|
private static LoadingModList 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;
|
2018-06-06 15:37:56 +00:00
|
|
|
private BackgroundScanHandler scanner;
|
2018-09-29 01:07:46 +00:00
|
|
|
private final List<EarlyLoadingException> preLoadErrors;
|
2018-06-06 15:37:56 +00:00
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
private LoadingModList(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-09-29 01:07:46 +00:00
|
|
|
this.preLoadErrors = new ArrayList<>();
|
2018-06-06 15:37:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-29 01:07:46 +00:00
|
|
|
public static LoadingModList of(List<ModFile> modFiles, List<ModInfo> sortedList, final EarlyLoadingException earlyLoadingException)
|
2018-06-11 13:56:44 +00:00
|
|
|
{
|
2018-06-15 19:03:35 +00:00
|
|
|
INSTANCE = new LoadingModList(modFiles, sortedList);
|
2018-09-29 01:07:46 +00:00
|
|
|
if (earlyLoadingException != null)
|
|
|
|
{
|
|
|
|
INSTANCE.preLoadErrors.add(earlyLoadingException);
|
|
|
|
}
|
2018-06-11 13:56:44 +00:00
|
|
|
return INSTANCE;
|
|
|
|
}
|
|
|
|
|
2018-06-15 19:03:35 +00:00
|
|
|
public static LoadingModList get() {
|
2018-06-11 13:56:44 +00:00
|
|
|
return INSTANCE;
|
|
|
|
}
|
2018-06-06 15:37:56 +00:00
|
|
|
public void addCoreMods()
|
|
|
|
{
|
2018-06-11 01:12:46 +00:00
|
|
|
modFiles.stream().map(ModFileInfo::getFile).map(ModFile::getCoreMods).flatMap(List::stream).forEach(FMLLoader.getCoreModProvider()::addCoreMod);
|
2018-06-06 15:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addAccessTransformers()
|
|
|
|
{
|
2018-06-11 01:12:46 +00:00
|
|
|
modFiles.stream().map(ModFileInfo::getFile).forEach(mod -> mod.getAccessTransformer().ifPresent(path -> FMLLoader.addAccessTransformer(path, mod)));
|
2018-06-06 15:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addForScanning(BackgroundScanHandler backgroundScanHandler)
|
|
|
|
{
|
|
|
|
this.scanner = backgroundScanHandler;
|
2018-06-15 19:03:35 +00:00
|
|
|
backgroundScanHandler.setLoadingModList(this);
|
2018-06-11 01:12:46 +00:00
|
|
|
modFiles.stream().map(ModFileInfo::getFile).forEach(backgroundScanHandler::submitForScanning);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Path findResource(final String className)
|
|
|
|
{
|
2018-06-11 01:12:46 +00:00
|
|
|
for (ModFileInfo mf : modFiles) {
|
|
|
|
final Path resource = mf.getFile().findResource(className);
|
2018-06-15 19:03:35 +00:00
|
|
|
if (Files.exists(resource)) return resource;
|
2018-06-06 15:37:56 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2018-06-11 01:12:46 +00:00
|
|
|
|
2019-01-04 16:44:57 +00:00
|
|
|
public URL findURLForResource(final String resourceName) {
|
|
|
|
for (ModFileInfo mf : modFiles) {
|
|
|
|
final Path resource = mf.getFile().findResource(resourceName);
|
|
|
|
if (Files.exists(resource)) {
|
|
|
|
try {
|
|
|
|
return new URL("modjar://"+mf.getMods().get(0).getModId()+"/"+resourceName);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:12:46 +00:00
|
|
|
public ModFileInfo getModFileById(String modid)
|
|
|
|
{
|
|
|
|
return this.fileById.get(modid);
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:56:44 +00:00
|
|
|
public List<ModInfo> getMods()
|
|
|
|
{
|
|
|
|
return this.sortedList;
|
|
|
|
}
|
2018-09-29 01:07:46 +00:00
|
|
|
|
|
|
|
public List<EarlyLoadingException> getErrors() {
|
|
|
|
return preLoadErrors;
|
|
|
|
}
|
2018-06-06 15:37:56 +00:00
|
|
|
}
|