Tweak library locating code. Works well on vanilla and dev now.

This commit is contained in:
cpw 2018-10-04 23:24:02 -04:00
parent bdcc7d966f
commit 2b23eb96fb
5 changed files with 65 additions and 60 deletions

View File

@ -63,7 +63,7 @@ public class FMLModContainer extends ModContainer
try
{
modClass = Class.forName(className, true, modClassLoader);
LOGGER.error(LOADING,"Loaded modclass {} with {}", modClass.getName(), modClass.getClassLoader());
LOGGER.debug(LOADING,"Loaded modclass {} with {}", modClass.getName(), modClass.getClassLoader());
}
catch (Throwable e)
{

View File

@ -23,6 +23,7 @@ import cpw.mods.modlauncher.api.IEnvironment;
import cpw.mods.modlauncher.api.ILaunchHandlerService;
import cpw.mods.modlauncher.api.ITransformingClassLoader;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.relauncher.libraries.LibraryManager;
import net.minecraftforge.versions.forge.ForgeVersion;
import net.minecraftforge.versions.mcp.MCPVersion;
import org.apache.logging.log4j.LogManager;
@ -37,26 +38,7 @@ import java.util.concurrent.Callable;
public class FMLClientLaunchProvider extends FMLCommonLaunchHandler implements ILaunchHandlerService
{
private static final Logger LOGGER = LogManager.getLogger();
private static final Path forgePath;
private static final Path patchedBinariesPath;
private static final Path srgMcPath;
static {
Path forgePath1 = null;
Path patchedBinariesPath1 = null;
Path srgMcPath1 = null;
try {
forgePath1 = Paths.get(FMLClientLaunchProvider.class.getProtectionDomain().getCodeSource().getLocation().toURI());
patchedBinariesPath1 = forgePath1.resolveSibling("forge-"+MCPVersion.getMCVersion()+"-"+ForgeVersion.getVersion()+"-client.jar");
Path libs = forgePath1.getParent().getParent().getParent().getParent().getParent();
srgMcPath1 = libs.resolve(Paths.get("net","minecraft", "client", MCPVersion.getMCPandMCVersion(), "client-"+MCPVersion.getMCPandMCVersion()+"-srg.jar")).toAbsolutePath();
} catch (URISyntaxException e) {
}
forgePath = forgePath1;
patchedBinariesPath = patchedBinariesPath1;
srgMcPath = srgMcPath1;
}
@Override
public String name()
{
@ -66,20 +48,23 @@ public class FMLClientLaunchProvider extends FMLCommonLaunchHandler implements I
@Override
public Path[] identifyTransformationTargets()
{
Path libsPath = findLibsPath();
Path patchedBinariesPath = libsPath.resolve(Paths.get("net","minecraftforge","forge",MCPVersion.getMCVersion()+"-"+ForgeVersion.getVersion(),"forge-"+MCPVersion.getMCVersion()+"-"+ForgeVersion.getVersion()+"-client.jar"));
Path srgMcPath = libsPath.resolve(Paths.get("net","minecraft", "client", MCPVersion.getMCPandMCVersion(), "client-"+MCPVersion.getMCPandMCVersion()+"-srg.jar"));
LOGGER.info("SRG MC at {} is {}", srgMcPath.toString(), Files.exists(srgMcPath) ? "present" : "missing");
LOGGER.info("Forge patches at {} is {}", patchedBinariesPath.toString(), Files.exists(patchedBinariesPath) ? "present" : "missing");
LOGGER.info("Forge at {} is {}", forgePath.toString(), Files.exists(forgePath) ? "present" : "missing");
if (!(Files.exists(srgMcPath) && Files.exists(patchedBinariesPath) && Files.exists(forgePath))) {
LOGGER.info("Forge at {} is {}", getForgePath().toString(), Files.exists(getForgePath()) ? "present" : "missing");
if (!(Files.exists(srgMcPath) && Files.exists(patchedBinariesPath) && Files.exists(getForgePath()))) {
throw new RuntimeException("Failed to find patched jars");
}
return new Path[] {forgePath, patchedBinariesPath, srgMcPath};
return super.commonLibPaths(new Path[] {getForgePath(), patchedBinariesPath, srgMcPath});
}
@Override
public Callable<Void> launchService(String[] arguments, ITransformingClassLoader launchClassLoader)
{
return () -> {
super.beforeStart(launchClassLoader, forgePath);
super.beforeStart(launchClassLoader);
launchClassLoader.addTargetPackageFilter(getPackagePredicate());
Class.forName("net.minecraft.client.main.Main", true, launchClassLoader.getInstance()).getMethod("main", String[].class).invoke(null, (Object)arguments);
return null;

View File

@ -19,25 +19,38 @@
package net.minecraftforge.fml.loading;
import com.google.common.collect.ObjectArrays;
import cpw.mods.modlauncher.api.IEnvironment;
import cpw.mods.modlauncher.api.ITransformingClassLoader;
import net.minecraftforge.api.distmarker.Dist;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import static net.minecraftforge.fml.Logging.CORE;
public abstract class FMLCommonLaunchHandler
{
private static final Logger LOGGER = LogManager.getLogger();
private static final List<String> SKIPPACKAGES = Arrays.asList(
// standard libs
"joptsimple.", "org.lwjgl.", "com.mojang.", "com.google.", "org.apache.commons.", "io.netty.",
"joptsimple.", "org.lwjgl.", "com.mojang.guava.", "com.google.", "org.apache.commons.", "io.netty.",
"org.apache.logging.log4j.", "org.apache.http.", "org.apache.maven.", "org.objectweb.asm.",
"paulscode.sound.", "com.ibm.icu.", "sun.", "gnu.trove.", "com.electronwill.nightconfig.",
"net.minecraftforge.fml.loading.", "net.minecraftforge.fml.language.", "net.minecraftforge.versions.",
"net.minecraftforge.eventbus.", "net.minecraftforge.api."
);
private Path forgePath;
protected Predicate<String> getPackagePredicate() {
return cn -> SKIPPACKAGES.stream().noneMatch(cn::startsWith);
}
@ -47,10 +60,46 @@ public abstract class FMLCommonLaunchHandler
}
Path findLibsPath() {
final Path asm = findJarPathFor("org/objectweb/asm/Opcodes.class", "asm");
// go up SIX parents to find the libs dir
final Path libs = asm.getParent().getParent().getParent().getParent().getParent().getParent();
LOGGER.debug(CORE, "Found probable library path {}", libs);
return libs;
}
Path findJarPathFor(final String className, final String jarName) {
final URL resource = getClass().getClassLoader().getResource(className);
try {
Path path;
final URI uri = resource.toURI();
if (uri.getSchemeSpecificPart().contains("!")) {
path = Paths.get(new URI(uri.getSchemeSpecificPart().split("!")[0]));
} else {
path = Paths.get(new URI("file:///"+uri.getSchemeSpecificPart().substring(0, uri.getSchemeSpecificPart().length()-className.length())));
}
LOGGER.debug(CORE, "Found JAR {} at path {}", jarName, path.toString());
return path;
} catch (URISyntaxException e) {
LOGGER.error(CORE, "Failed to find JAR for class {} - {}", className, jarName);
throw new RuntimeException("Unable to locate "+className+" - "+jarName, e);
}
}
Path[] commonLibPaths(Path[] extras) {
final Path realms = findJarPathFor("com/mojang/realmsclient/RealmsVersion.class", "realms");
return ObjectArrays.concat(extras, realms);
}
Path getForgePath() {
if (forgePath == null) {
forgePath = findJarPathFor("net/minecraftforge/versions/forge/ForgeVersion.class", "forge");
LOGGER.debug(CORE, "Found forge path {}", forgePath);
}
return forgePath;
}
public abstract Dist getDist();
protected void beforeStart(ITransformingClassLoader launchClassLoader, Path forgePath)
protected void beforeStart(ITransformingClassLoader launchClassLoader)
{
FMLLoader.beforeStart(launchClassLoader, forgePath);
FMLLoader.beforeStart(launchClassLoader, getForgePath());
}
}

View File

@ -47,24 +47,10 @@ public class FMLDevClientLaunchProvider extends FMLCommonLaunchHandler implement
return "fmldevclient";
}
private static final Path myPath;
static
{
try
{
myPath = Paths.get(FMLDevClientLaunchProvider.class.getProtectionDomain().getCodeSource().getLocation().toURI());
}
catch (URISyntaxException e)
{
throw new RuntimeException("HUH?");
}
}
@Override
public Path[] identifyTransformationTargets()
{
return new Path[] { myPath };
return super.commonLibPaths(new Path[] {getForgePath()});
}
@Override
@ -72,7 +58,7 @@ public class FMLDevClientLaunchProvider extends FMLCommonLaunchHandler implement
{
return () -> {
LOGGER.debug(CORE, "Launching minecraft in {} with arguments {}", launchClassLoader, arguments);
super.beforeStart(launchClassLoader, myPath);
super.beforeStart(launchClassLoader);
launchClassLoader.addTargetPackageFilter(getPackagePredicate());
Class.forName("net.minecraft.client.main.Main", true, launchClassLoader.getInstance()).getMethod("main", String[].class).invoke(null, (Object)arguments);
return null;

View File

@ -39,7 +39,6 @@ import static net.minecraftforge.fml.Logging.CORE;
public class FMLDevServerLaunchProvider extends FMLCommonLaunchHandler implements ILaunchHandlerService
{
private static final Logger LOGGER = LogManager.getLogger();
@Override
@ -48,24 +47,10 @@ public class FMLDevServerLaunchProvider extends FMLCommonLaunchHandler implement
return "fmldevserver";
}
private static final Path myPath;
static
{
try
{
myPath = Paths.get(FMLDevServerLaunchProvider.class.getProtectionDomain().getCodeSource().getLocation().toURI());
}
catch (URISyntaxException e)
{
throw new RuntimeException("HUH?");
}
}
@Override
public Path[] identifyTransformationTargets()
{
return new Path[] { myPath };
return super.commonLibPaths(new Path[] { getForgePath() });
}
@Override
@ -73,7 +58,7 @@ public class FMLDevServerLaunchProvider extends FMLCommonLaunchHandler implement
{
return () -> {
LOGGER.debug(CORE, "Launching minecraft in {} with arguments {}", launchClassLoader, arguments);
super.beforeStart(launchClassLoader, myPath);
super.beforeStart(launchClassLoader);
launchClassLoader.addTargetPackageFilter(getPackagePredicate());
Thread.currentThread().setContextClassLoader(launchClassLoader.getInstance());
Class.forName("net.minecraft.server.MinecraftServer", true, launchClassLoader.getInstance()).getMethod("main", String[].class).invoke(null, (Object)arguments);