Cleanup launcher classes
Cleanup a bunch of the code in launcher classes - Migrate the majority of the reflection to ReflectionUtils - Decrease logic in AbstractLauncher - Add logging to launcher classes at FINE level - make mcParams in AbstractLauncher an immutable list to prevent runtime manipulation - StandardLauncher instead copies the list to modify it Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
@ -55,81 +55,66 @@
|
||||
|
||||
package org.prismlauncher.launcher.impl;
|
||||
|
||||
|
||||
import org.prismlauncher.exception.ParseException;
|
||||
import org.prismlauncher.launcher.Launcher;
|
||||
import org.prismlauncher.utils.Parameters;
|
||||
import org.prismlauncher.utils.StringUtils;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public abstract class AbstractLauncher implements Launcher {
|
||||
|
||||
private static final int DEFAULT_WINDOW_WIDTH = 854;
|
||||
|
||||
private static final int DEFAULT_WINDOW_HEIGHT = 480;
|
||||
|
||||
// parameters, separated from ParamBucket
|
||||
protected final List<String> mcParams;
|
||||
private final String mainClass;
|
||||
|
||||
// secondary parameters
|
||||
protected final int width;
|
||||
|
||||
protected final int height;
|
||||
|
||||
protected final boolean maximize;
|
||||
|
||||
protected final String serverAddress, serverPort;
|
||||
protected final String serverAddress;
|
||||
|
||||
protected final ClassLoader classLoader;
|
||||
protected final String serverPort;
|
||||
|
||||
protected final String mainClassName;
|
||||
|
||||
protected AbstractLauncher(Parameters params) {
|
||||
classLoader = ClassLoader.getSystemClassLoader();
|
||||
this.mcParams = Collections.unmodifiableList(params.getList("param", new ArrayList<String>()));
|
||||
this.mainClassName = params.getString("mainClass", "net.minecraft.client.Minecraft");
|
||||
|
||||
mcParams = params.getList("param", new ArrayList<String>());
|
||||
mainClass = params.getString("mainClass", "net.minecraft.client.Minecraft");
|
||||
|
||||
serverAddress = params.getString("serverAddress", null);
|
||||
serverPort = params.getString("serverPort", null);
|
||||
this.serverAddress = params.getString("serverAddress", null);
|
||||
this.serverPort = params.getString("serverPort", null);
|
||||
|
||||
String windowParams = params.getString("windowParams", null);
|
||||
|
||||
if ("max".equals(windowParams) || windowParams == null) {
|
||||
maximize = windowParams != null;
|
||||
this.maximize = "max".equalsIgnoreCase(windowParams);
|
||||
|
||||
width = DEFAULT_WINDOW_WIDTH;
|
||||
height = DEFAULT_WINDOW_HEIGHT;
|
||||
} else {
|
||||
maximize = false;
|
||||
|
||||
if (windowParams != null && !"max".equalsIgnoreCase(windowParams)) {
|
||||
String[] sizePair = StringUtils.splitStringPair('x', windowParams);
|
||||
|
||||
|
||||
if (sizePair != null) {
|
||||
try {
|
||||
width = Integer.parseInt(sizePair[0]);
|
||||
height = Integer.parseInt(sizePair[1]);
|
||||
return;
|
||||
} catch (NumberFormatException ignored) {
|
||||
this.width = Integer.parseInt(sizePair[0]);
|
||||
this.height = Integer.parseInt(sizePair[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ParseException(String.format("Could not parse window parameters from '%s'", windowParams), e);
|
||||
}
|
||||
} else {
|
||||
throw new ParseException(String.format("Invalid window size parameters '%s'. Format: [height]x[width]", windowParams));
|
||||
}
|
||||
|
||||
throw new ParseException("Invalid window size parameter value: " + windowParams);
|
||||
} else {
|
||||
this.width = DEFAULT_WINDOW_WIDTH;
|
||||
this.height = DEFAULT_WINDOW_HEIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
protected Class<?> loadMain() throws ClassNotFoundException {
|
||||
return classLoader.loadClass(mainClass);
|
||||
}
|
||||
|
||||
protected void loadAndInvokeMain() throws Throwable {
|
||||
invokeMain(loadMain());
|
||||
}
|
||||
|
||||
protected void invokeMain(Class<?> mainClass) throws Throwable {
|
||||
MethodHandle method = MethodHandles.lookup().findStatic(mainClass, "main", MethodType.methodType(void.class, String[].class));
|
||||
|
||||
method.invokeExact(mcParams.toArray(new String[0]));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -58,9 +58,17 @@ package org.prismlauncher.launcher.impl;
|
||||
import org.prismlauncher.launcher.Launcher;
|
||||
import org.prismlauncher.launcher.LauncherProvider;
|
||||
import org.prismlauncher.utils.Parameters;
|
||||
import org.prismlauncher.utils.ReflectionUtils;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
public final class StandardLauncher extends AbstractLauncher {
|
||||
private static final Logger LOGGER = Logger.getLogger("LegacyLauncher");
|
||||
|
||||
|
||||
public StandardLauncher(Parameters params) {
|
||||
super(params);
|
||||
@ -78,21 +86,27 @@ public final class StandardLauncher extends AbstractLauncher {
|
||||
// the following often breaks linux screen setups
|
||||
// mcparams.add("--fullscreen");
|
||||
|
||||
if (!maximize) {
|
||||
mcParams.add("--width");
|
||||
mcParams.add(Integer.toString(width));
|
||||
mcParams.add("--height");
|
||||
mcParams.add(Integer.toString(height));
|
||||
List<String> launchParameters = new ArrayList<>(this.mcParams);
|
||||
|
||||
if (!this.maximize) {
|
||||
launchParameters.add("--width");
|
||||
launchParameters.add(Integer.toString(width));
|
||||
launchParameters.add("--height");
|
||||
launchParameters.add(Integer.toString(height));
|
||||
}
|
||||
|
||||
if (serverAddress != null) {
|
||||
mcParams.add("--server");
|
||||
mcParams.add(serverAddress);
|
||||
mcParams.add("--port");
|
||||
mcParams.add(serverPort);
|
||||
if (this.serverAddress != null) {
|
||||
launchParameters.add("--server");
|
||||
launchParameters.add(serverAddress);
|
||||
launchParameters.add("--port");
|
||||
launchParameters.add(serverPort);
|
||||
}
|
||||
|
||||
loadAndInvokeMain();
|
||||
LOGGER.info("Launching minecraft using the main class entrypoint");
|
||||
|
||||
MethodHandle method = ReflectionUtils.findMainEntrypoint(this.mainClassName);
|
||||
|
||||
method.invokeExact((Object[]) launchParameters.toArray(new String[0]));
|
||||
}
|
||||
|
||||
|
||||
|
@ -60,14 +60,11 @@ import org.prismlauncher.launcher.Launcher;
|
||||
import org.prismlauncher.launcher.LauncherProvider;
|
||||
import org.prismlauncher.launcher.impl.AbstractLauncher;
|
||||
import org.prismlauncher.utils.Parameters;
|
||||
import org.prismlauncher.utils.ReflectionUtils;
|
||||
|
||||
import java.applet.Applet;
|
||||
import java.io.File;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
@ -87,7 +84,7 @@ public final class LegacyLauncher extends AbstractLauncher {
|
||||
|
||||
private final String appletClass;
|
||||
|
||||
private final boolean noApplet;
|
||||
private final boolean usesApplet;
|
||||
|
||||
private final String cwd;
|
||||
|
||||
@ -100,8 +97,9 @@ public final class LegacyLauncher extends AbstractLauncher {
|
||||
appletClass = params.getString("appletClass", "net.minecraft.client.MinecraftApplet");
|
||||
|
||||
List<String> traits = params.getList("traits", Collections.<String>emptyList());
|
||||
noApplet = traits.contains("noapplet");
|
||||
usesApplet = !traits.contains("noapplet");
|
||||
|
||||
//noinspection AccessOfSystemProperties
|
||||
cwd = System.getProperty("user.dir");
|
||||
}
|
||||
|
||||
@ -109,74 +107,40 @@ public final class LegacyLauncher extends AbstractLauncher {
|
||||
return new LegacyLauncherProvider();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a field that looks like a Minecraft base folder in a supplied class
|
||||
*
|
||||
* @param clazz the class to scan
|
||||
*
|
||||
* @return The found field.
|
||||
*/
|
||||
private static Field getMinecraftGameDirField(Class<?> clazz) {
|
||||
// Field we're looking for is always
|
||||
// private static File obfuscatedName = null;
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
// Has to be File
|
||||
if (field.getType() != File.class)
|
||||
continue;
|
||||
|
||||
// And Private Static.
|
||||
if (!Modifier.isStatic(field.getModifiers()) || !Modifier.isPrivate(field.getModifiers()))
|
||||
continue;
|
||||
|
||||
return field;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void launch() throws Throwable {
|
||||
Class<?> main = loadMain();
|
||||
Field gameDirField = getMinecraftGameDirField(main);
|
||||
Class<?> main = ClassLoader.getSystemClassLoader().loadClass(this.mainClassName);
|
||||
Field gameDirField = ReflectionUtils.getMinecraftGameDirField(main);
|
||||
|
||||
if (gameDirField == null) {
|
||||
LOGGER.warning("Could not find Mineraft path field.");
|
||||
LOGGER.warning("Could not find Minecraft path field");
|
||||
} else {
|
||||
gameDirField.setAccessible(true);
|
||||
gameDirField.set(null, new File(cwd));
|
||||
gameDirField.set(null /* field is static, so instance is null */, new File(cwd));
|
||||
}
|
||||
|
||||
if (!noApplet) {
|
||||
LOGGER.info("Launching with applet wrapper...");
|
||||
if (this.usesApplet) {
|
||||
LOGGER.info("Launching legacy minecraft using applet wrapper...");
|
||||
|
||||
try {
|
||||
Class<?> appletClass = classLoader.loadClass(this.appletClass);
|
||||
|
||||
MethodHandle constructor = MethodHandles.lookup().findConstructor(appletClass, MethodType.methodType(void.class));
|
||||
Applet applet = (Applet) constructor.invoke();
|
||||
|
||||
LegacyFrame window = new LegacyFrame(title, applet);
|
||||
LegacyFrame window = new LegacyFrame(title, ReflectionUtils.createAppletClass(this.appletClass));
|
||||
|
||||
window.start(
|
||||
user,
|
||||
session,
|
||||
width,
|
||||
height,
|
||||
maximize,
|
||||
serverAddress,
|
||||
serverPort,
|
||||
mcParams.contains("--demo")
|
||||
this.user,
|
||||
this.session,
|
||||
this.width, this.height, this.maximize,
|
||||
this.serverAddress, this.serverPort,
|
||||
this.mcParams.contains("--demo")
|
||||
);
|
||||
|
||||
return;
|
||||
} catch (Throwable e) {
|
||||
LOGGER.log(Level.SEVERE, "Applet wrapper failed:", e);
|
||||
|
||||
LOGGER.warning("Falling back to using main class.");
|
||||
LOGGER.log(Level.SEVERE, "Running applet wrapper failed with exception", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LOGGER.info("Launching legacy minecraft using the main class entrypoint");
|
||||
MethodHandle method = ReflectionUtils.findMainEntrypoint(main);
|
||||
|
||||
invokeMain(main);
|
||||
method.invokeExact((Object[]) mcParams.toArray(new String[0]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user