mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-22 13:09:59 +00:00
Change how Forge itself is located to be loaded similar to a real mod.
Fixes Minecraft/Forge annotation data missing. Fix Capability injection.
This commit is contained in:
1 parent
d11a67499d
commit
4879b0611a
18 files changed
+304
-349
No files matched your search
@@ -19,13 +19,11 @@
|
||||
|
||||
package net.minecraftforge.fml;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.event.ModelRegistryEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.capabilities.CapabilityManager;
|
||||
import net.minecraftforge.fml.language.IModInfo;
|
||||
import net.minecraftforge.fml.loading.DefaultModInfos;
|
||||
import net.minecraftforge.fml.loading.FMLLoader;
|
||||
import net.minecraftforge.fml.loading.LoadingModList;
|
||||
import net.minecraftforge.fml.loading.moddiscovery.ModFile;
|
||||
@@ -34,7 +32,6 @@ import net.minecraftforge.registries.GameData;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -76,18 +73,6 @@ public class ModLoader
|
||||
|
||||
public void loadMods() {
|
||||
final ModList modList = ModList.of(loadingModList.getModFiles().stream().map(ModFileInfo::getFile).collect(Collectors.toList()), loadingModList.getMods());
|
||||
ModContainer forgeModContainer;
|
||||
try
|
||||
{
|
||||
forgeModContainer = (ModContainer)Class.forName("net.minecraftforge.common.ForgeModContainer", true, modClassLoader).
|
||||
getConstructor(ModLoadingClassLoader.class).newInstance(modClassLoader);
|
||||
}
|
||||
catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException | InstantiationException | InvocationTargetException e)
|
||||
{
|
||||
LOGGER.error(CORE,"Unable to load the Forge Mod Container", e);
|
||||
loadingExceptions.add(new ModLoadingException(DefaultModInfos.forgeModInfo, ModLoadingStage.VALIDATE, "fml.modloading.failedtoloadforge", e));
|
||||
forgeModContainer = null;
|
||||
}
|
||||
if (!this.loadingExceptions.isEmpty()) {
|
||||
throw new LoadingFailedException(loadingExceptions);
|
||||
}
|
||||
@@ -99,10 +84,8 @@ public class ModLoader
|
||||
LOGGER.error(CORE, "Failed to initialize mod containers");
|
||||
throw new LoadingFailedException(loadingExceptions);
|
||||
}
|
||||
modList.setLoadedMods(Streams.concat(Stream.of(forgeModContainer), modContainerStream).collect(Collectors.toList()));
|
||||
modList.setLoadedMods(modContainerStream.collect(Collectors.toList()));
|
||||
dispatchAndHandleError(LifecycleEventProvider.CONSTRUCT);
|
||||
WorldPersistenceHooks.addHook(new FMLWorldPersistenceHook());
|
||||
WorldPersistenceHooks.addHook((WorldPersistenceHooks.WorldPersistenceHook)forgeModContainer.getMod());
|
||||
GameData.fireCreateRegistryEvents();
|
||||
CapabilityManager.INSTANCE.injectCapabilities(modList.getAllScanData());
|
||||
dispatchAndHandleError(LifecycleEventProvider.PREINIT);
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-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
|
||||
*/
|
||||
|
||||
package net.minecraftforge.fml.config;
|
||||
|
||||
public class ModConfig
|
||||
{
|
||||
}
|
||||
@@ -36,8 +36,7 @@ import java.util.Objects;
|
||||
public class DefaultModInfos
|
||||
{
|
||||
static {
|
||||
FileConfig minecraftmod;
|
||||
FileConfig forgemod;
|
||||
FileConfig minecraftmod; FileConfig forgemod;
|
||||
try
|
||||
{
|
||||
final URI jarFileURI = DefaultModInfos.class.getClassLoader().getResource("minecraftmod.toml").toURI();
|
||||
@@ -46,28 +45,24 @@ public class DefaultModInfos
|
||||
FileSystems.newFileSystem(jarFileURI, new HashMap<>());
|
||||
}
|
||||
minecraftmod = FileConfig.of(Paths.get(jarFileURI));
|
||||
forgemod = FileConfig.of(Paths.get(DefaultModInfos.class.getClassLoader().getResource("forgemod.toml").toURI()));
|
||||
minecraftmod.load();
|
||||
forgemod.load();
|
||||
}
|
||||
catch (IOException | URISyntaxException | NullPointerException e)
|
||||
{
|
||||
throw new RuntimeException("Missing toml configs for minecraft and forge!", e);
|
||||
}
|
||||
minecraftModInfo = new ModInfo(null, minecraftmod);
|
||||
forgeModInfo = new ModInfo(null, forgemod);
|
||||
|
||||
}
|
||||
|
||||
public static final IModInfo minecraftModInfo;
|
||||
public static final IModInfo forgeModInfo;
|
||||
|
||||
// no construction
|
||||
private DefaultModInfos() {}
|
||||
|
||||
public static List<IModInfo> getModInfos()
|
||||
{
|
||||
return Arrays.asList(minecraftModInfo, forgeModInfo);
|
||||
return Arrays.asList(minecraftModInfo);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -41,9 +41,11 @@ public class FMLConfig
|
||||
configSpec.defineInList("side", Dist.CLIENT.name(), Arrays.stream(Dist.values()).map(Enum::name).collect(Collectors.toList()));
|
||||
configSpec.defineInRange("maxframerate", 60, 10, 120);
|
||||
configSpec.defineInRange("minframerate", 60, 10, 120);
|
||||
/* Tests that we know work and shouldn't be done in runtime.
|
||||
configSpec.defineInList(Arrays.asList("tasty","flavour"), Dist.CLIENT.name(), Arrays.stream(Dist.values()).map(Enum::name).collect(Collectors.toList()));
|
||||
configSpec.defineInList(Arrays.asList("tasty","teaser"), Dist.CLIENT.name(), Arrays.stream(Dist.values()).map(Enum::name).collect(Collectors.toList()));
|
||||
configSpec.define("longstring", StringUtils.repeat("AAAA", 10000), s->s!=null && ((String)s).length()>0);
|
||||
*/
|
||||
}
|
||||
|
||||
private CommentedFileConfig configData;
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-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
|
||||
*/
|
||||
|
||||
package net.minecraftforge.fml.loading.moddiscovery;
|
||||
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.fml.loading.FMLEnvironment;
|
||||
import net.minecraftforge.fml.loading.moddiscovery.IModLocator;
|
||||
import net.minecraftforge.fml.loading.moddiscovery.ModFile;
|
||||
import net.minecraftforge.versions.forge.ForgeVersion;
|
||||
import net.minecraftforge.versions.mcp.MCPVersion;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.jar.Manifest;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static net.minecraftforge.fml.Logging.SCAN;
|
||||
|
||||
/*
|
||||
* Locate Forge in the classpath, this works similar to ClasspathLocator.
|
||||
* Forge/Minecraft are in the following formats:
|
||||
* Runtime:
|
||||
* %lib%/net/minecraftforge/forge/{ver}/forge-{ver}.jar or ./forge-{ver}-universal.jar - Client is in libs, Server is in root
|
||||
* %lib%/net/minecraftforge/forge/{ver}/forge-{ver}-{client|server}.jar - Forge, Vanilla classes, Patched
|
||||
* %lib%/net/minecraft/{client|server}/{mcver}/{client|server}-{mcver}-srg.jar - Vanilla classes, SRG, Unpatched
|
||||
*
|
||||
* UserDev:
|
||||
* Forge and vanilla is in a single jar file, patched, on the classpath.
|
||||
*
|
||||
* Eclipse:
|
||||
* bin/{sourceset}/
|
||||
*
|
||||
* Intellij: TODO: Verify, I don't use intellij
|
||||
* out/production/resources/../classes/ - Resources are a separate folder so we have to navigate to classes
|
||||
*
|
||||
* Gradle:
|
||||
* build/classes/{language}/{sourceset}
|
||||
* build/resources/{sourceset}
|
||||
*
|
||||
*/
|
||||
public class ForgeLocator implements IModLocator
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final String FORGE_META = "forgemod.toml";
|
||||
private static final String MAIN_SOURCESET = "main";
|
||||
private static final String LIB_RESOURCE = "org/objectweb/asm/Opcodes.class";
|
||||
private static final String LIB_PATH = "org/ow2/asm/asm".replace('/', File.separatorChar);
|
||||
private List<Path> roots = new ArrayList<>();
|
||||
private boolean filterClasses = false;
|
||||
|
||||
public ForgeLocator() { }
|
||||
|
||||
@Override
|
||||
public List<ModFile> scanMods()
|
||||
{
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
URL url = loader.getResource(FORGE_META);
|
||||
if (url == null)
|
||||
throw new IllegalStateException("Could not locate \"" + FORGE_META + "\", Forge is correupt/not on classpath!");
|
||||
|
||||
File base = getRoot(url, FORGE_META);
|
||||
if (base == null)
|
||||
throw new IllegalStateException("Could not determine root for Forge resource: " + url);
|
||||
|
||||
url = loader.getResource(LIB_RESOURCE);
|
||||
if (url == null)
|
||||
throw new IllegalStateException("Could not locate \"" + LIB_RESOURCE + "\" to locate libraries directory.");
|
||||
File libs = getRoot(url, LIB_RESOURCE);
|
||||
//If In maven style the libraries folder we need to drop the file name, and version folder
|
||||
if (libs != null && libs.getParentFile().getParentFile().getAbsolutePath().endsWith(LIB_PATH))
|
||||
{
|
||||
int count = 2 + LIB_PATH.split(Pattern.quote("" + File.separatorChar)).length;
|
||||
for (int x = 0; x < count; x++)
|
||||
libs = libs.getParentFile();
|
||||
}
|
||||
else
|
||||
{
|
||||
libs = null;
|
||||
}
|
||||
|
||||
LOGGER.info("Forge Locator: ");
|
||||
LOGGER.info(" Forge: " + base);
|
||||
LOGGER.info(" Libraries: " + libs);
|
||||
|
||||
Path path = base.toPath();
|
||||
if (base.isDirectory())
|
||||
{ // Development workspace.
|
||||
int count = path.getNameCount();
|
||||
//Eclipse: /bin/{sourceset}/
|
||||
if (count >= 2 && MAIN_SOURCESET.equals(path.getName(count - 1).toString()) && "bin".equals(path.getName(count - 2).toString()))
|
||||
{
|
||||
roots.add(path);
|
||||
roots.add(path.getParent().resolve("userdev")); //Do I need this?
|
||||
} //Intellij: /out/production/resources/ & /out/production/classes/
|
||||
else if (count >= 1 && "resources".equals(path.getName(count - 1).toString()))
|
||||
{
|
||||
roots.add(path.getParent().resolve("classes"));
|
||||
roots.add(path);
|
||||
} //Gradle: build/resources/{sourceset} & build/classes/{language}/{sourceset}
|
||||
else if (count >= 2 && MAIN_SOURCESET.equals(path.getName(count - 1).toString()) && "resources".equals(path.getName(count - 2).toString()))
|
||||
{
|
||||
Path classes = path.getParent().getParent().resolve("classes");
|
||||
roots.add(classes.resolve(MAIN_SOURCESET));
|
||||
roots.add(classes.resolve("userdev"));
|
||||
roots.add(path);
|
||||
roots.add(path.getParent().resolve("userdev"));
|
||||
}
|
||||
filterClasses = true;
|
||||
}
|
||||
else if (base.isFile())
|
||||
{
|
||||
if (libs == null) //User dev workspace.
|
||||
{
|
||||
roots.add(path);
|
||||
}
|
||||
else //Standard install
|
||||
{
|
||||
String dist = FMLEnvironment.dist == Dist.CLIENT ? "client" : "server";
|
||||
String version = MCPVersion.getMCVersion() + "-" + ForgeVersion.getVersion();
|
||||
roots.add(path); // universal
|
||||
roots.add(libs.toPath().resolve(Paths.get(ForgeVersion.getGroup().replace('.', File.separatorChar), "forge", version, "forge-" + version + "-" + dist + ".jar"))); // Patched classes
|
||||
roots.add(libs.toPath().resolve(Paths.get("net", "minecraft", dist, MCPVersion.getMCPandMCVersion(), dist + "-" + MCPVersion.getMCPandMCVersion() + "-srg.jar"))); //Vanilla jar in srg names
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
throw new IllegalStateException("Forge path is neither File or Directory... " + base);
|
||||
|
||||
if (roots.isEmpty())
|
||||
return Collections.emptyList();
|
||||
|
||||
roots.forEach(r -> LOGGER.info(" Root: " + r));
|
||||
return Collections.singletonList(new ModFile(roots.get(0), this));
|
||||
}
|
||||
|
||||
protected File getRoot(URL url, String resource)
|
||||
{
|
||||
String path = url.getPath();
|
||||
if ("jar".equals(url.getProtocol()))
|
||||
{
|
||||
int idx = path.indexOf("!/");
|
||||
path = path.substring(0, idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Strip resource path off the end
|
||||
path = path.substring(0, path.length() - resource.length());
|
||||
}
|
||||
|
||||
if (path.startsWith("file:"))
|
||||
path = path.substring(5);
|
||||
|
||||
return new File(path).getAbsoluteFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name()
|
||||
{
|
||||
return "forge locator";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path findPath(final ModFile modFile, String... path)
|
||||
{
|
||||
if (path.length < 1)
|
||||
throw new IllegalArgumentException("Missing path");
|
||||
|
||||
if (path.length == 2 && "META-INF".equals(path[0]) && "mods.toml".equals(path[1]))
|
||||
path = FORGE_META.split("/");
|
||||
|
||||
if (filterClasses && path[path.length - 1].endsWith(".class"))
|
||||
path[path.length - 1] = path[path.length - 1] + "THIS_FILE_WILL_NEVER_EXITS_DIRTY_HAX";
|
||||
|
||||
String[] tail = Arrays.copyOfRange(path, 1, path.length);
|
||||
for (Path root : roots)
|
||||
{
|
||||
Path target = root.resolve(root.getFileSystem().getPath(path[0], tail));
|
||||
if (Files.exists(target))
|
||||
return target;
|
||||
}
|
||||
|
||||
return modFile.getFilePath().resolve(modFile.getFilePath().getFileSystem().getPath(path[0], tail));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scanFile(final ModFile modFile, final Consumer<Path> pathConsumer)
|
||||
{
|
||||
final Set<String> visited = new HashSet<>();
|
||||
roots.stream().forEach(root ->
|
||||
{
|
||||
LOGGER.debug(SCAN,"Scanning Forge: " + root);
|
||||
try (Stream<Path> files = Files.find(root, Integer.MAX_VALUE, (p, a) -> p.getNameCount() > 0 && p.getFileName().toString().endsWith(".class")))
|
||||
{
|
||||
files.forEach(f -> {
|
||||
String relative = root.relativize(f).toString(); //TODO: Test jared files...
|
||||
if (visited.add(relative))
|
||||
pathConsumer.accept(Paths.get(relative));
|
||||
});
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
LOGGER.debug(SCAN,"Forge scan complete");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "{Forge locator}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Manifest> findManifest(Path file)
|
||||
{
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -82,7 +82,7 @@ public class ModFileResourcePack extends AbstractResourcePack
|
||||
map(path -> root.relativize(path.toAbsolutePath())).
|
||||
filter(path -> path.getNameCount() > 1 && path.getNameCount() - 1 <= maxDepth). // Make sure the depth is within bounds, ignoring domain
|
||||
filter(path -> !path.toString().endsWith(".mcmeta")). // Ignore .mcmeta files
|
||||
filter(path -> path.subpath(1, path.getNameCount()).startsWith(inputPath)). // Make sure the target path is inside this one (again ignoring domain)
|
||||
filter(path -> path.subpath(1, path.getNameCount()).startsWith(inputPath)). // Make sure the target path is inside this one (again ignoring domain)
|
||||
filter(path -> filter.test(path.getFileName().toString())). // Test the file name against the predicate
|
||||
// Finally we need to form the RL, so use the first name as the domain, and the rest as the path
|
||||
// It is VERY IMPORTANT that we do not rely on Path.toString as this is inconsistent between operating systems
|
||||
|
||||
Reference in new issue
Block a user