A lot more cleanup

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad
2022-11-08 17:51:18 +00:00
parent f2ca9a6b31
commit 32c2ad2bbd
20 changed files with 417 additions and 861 deletions

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
@ -56,7 +55,6 @@
package org.prismlauncher;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
@ -70,63 +68,40 @@ import org.prismlauncher.utils.logging.Log;
public final class EntryPoint {
private EntryPoint() {
}
public static void main(String[] args) {
ExitCode exitCode = listen();
ExitCode code = listen();
if (exitCode != ExitCode.NORMAL) {
Log.fatal("Exiting with " + exitCode);
if (code != ExitCode.NORMAL) {
Log.fatal("Exiting with " + code);
System.exit(exitCode.numericalCode);
}
}
private static PreLaunchAction parseLine(String input, Parameters params) throws ParseException {
if (input.isEmpty())
return PreLaunchAction.PROCEED;
if ("launch".equalsIgnoreCase(input))
return PreLaunchAction.LAUNCH;
else if ("abort".equalsIgnoreCase(input))
return PreLaunchAction.ABORT;
else {
String[] pair = StringUtils.splitStringPair(' ', input);
if (pair == null)
throw new ParseException(String.format(
"Could not split input string '%s' by space. All input provided from stdin must be either 'launch', 'abort', or "
+ "in the format '[param name] [param]'.",
input));
params.add(pair[0], pair[1]);
return PreLaunchAction.PROCEED;
System.exit(code.numeric);
}
}
private static ExitCode listen() {
Parameters parameters = new Parameters();
PreLaunchAction preLaunchAction = PreLaunchAction.PROCEED;
Parameters params = new Parameters();
PreLaunchAction action = PreLaunchAction.PROCEED;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) {
String line;
while (preLaunchAction == PreLaunchAction.PROCEED) {
while (action == PreLaunchAction.PROCEED) {
if ((line = reader.readLine()) != null)
preLaunchAction = parseLine(line, parameters);
action = parseLine(line, params);
else
preLaunchAction = PreLaunchAction.ABORT;
action = PreLaunchAction.ABORT;
}
} catch (IOException | ParseException e) {
Log.fatal("Launcher abort due to exception", e);
} catch (IllegalArgumentException e) {
Log.fatal("Aborting due to wrong argument", e);
return ExitCode.ILLEGAL_ARGUMENT;
} catch (Throwable e) {
Log.fatal("Aborting due to exception", e);
return ExitCode.ABORT;
}
// Main loop
if (preLaunchAction == PreLaunchAction.ABORT) {
if (action == PreLaunchAction.ABORT) {
Log.fatal("Launch aborted by the launcher");
return ExitCode.ABORT;
@ -134,33 +109,28 @@ public final class EntryPoint {
try {
Launcher launcher;
String type = parameters.getString("launcher");
String type = params.getString("launcher");
switch (type) {
case "standard":
launcher = new StandardLauncher(parameters);
launcher = new StandardLauncher(params);
break;
case "legacy":
launcher = new LegacyLauncher(parameters);
launcher = new LegacyLauncher(params);
break;
default:
throw new IllegalArgumentException("Invalid launcher type: " + type);
}
Log.launcher("Using " + type + " launcher");
Log.blankLine();
launcher.launch();
return ExitCode.NORMAL;
} catch (IllegalArgumentException e) {
Log.fatal("Wrong argument", e);
Log.fatal("Illegal argument", e);
return ExitCode.ILLEGAL_ARGUMENT;
} catch (ReflectiveOperationException e) {
Log.fatal("Caught reflection exception from launcher", e);
return ExitCode.ERROR;
} catch (Throwable e) {
Log.fatal("Exception caught from launcher", e);
@ -168,6 +138,29 @@ public final class EntryPoint {
}
}
private static PreLaunchAction parseLine(String input, Parameters params) throws ParseException {
switch (input) {
case "":
break;
case "launch":
return PreLaunchAction.LAUNCH;
case "abort":
return PreLaunchAction.ABORT;
default:
String[] pair = StringUtils.splitStringPair(' ', input);
if (pair == null)
throw new ParseException(input, "[key] [value]");
params.add(pair[0], pair[1]);
}
return PreLaunchAction.PROCEED;
}
private enum PreLaunchAction {
PROCEED, LAUNCH, ABORT
}
@ -175,10 +168,10 @@ public final class EntryPoint {
private enum ExitCode {
NORMAL(0), ABORT(1), ERROR(2), ILLEGAL_ARGUMENT(65);
private final int numericalCode;
private final int numeric;
ExitCode(int numericalCode) {
this.numericalCode = numericalCode;
ExitCode(int numeric) {
this.numeric = numeric;
}
}

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
@ -42,23 +41,8 @@ public final class ParameterNotFoundException extends IllegalArgumentException {
private static final long serialVersionUID = 1L;
public ParameterNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public ParameterNotFoundException(Throwable cause) {
super(cause);
}
public ParameterNotFoundException(String message) {
super(message);
}
public ParameterNotFoundException() {
}
public static ParameterNotFoundException forParameterName(String parameterName) {
return new ParameterNotFoundException(String.format("Unknown parameter name '%s'", parameterName));
public ParameterNotFoundException(String key) {
super(String.format("Required parameter '%s' was not found", key));
}
}

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
@ -42,27 +41,8 @@ public final class ParseException extends IllegalArgumentException {
private static final long serialVersionUID = 1L;
public ParseException(String message) {
super(message);
}
public ParseException(String message, Throwable cause) {
super(message, cause);
}
public ParseException(Throwable cause) {
super(cause);
}
public ParseException() {
}
public static ParseException forInputString(String inputString) {
return new ParseException(String.format("Could not parse input string '%s'", inputString));
}
public static ParseException forInputString(String inputString, Throwable cause) {
return new ParseException(String.format("Could not parse input string '%s'", inputString), cause);
public ParseException(String input, String format) {
super(String.format("For input '%s' - should match '%s'", input, format));
}
}

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
@ -55,21 +54,20 @@
package org.prismlauncher.launcher.impl;
import java.util.ArrayList;
import java.util.List;
import org.prismlauncher.exception.ParseException;
import org.prismlauncher.launcher.Launcher;
import org.prismlauncher.utils.Parameters;
import org.prismlauncher.utils.StringUtils;
import java.util.ArrayList;
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;
private static final int DEFAULT_WINDOW_WIDTH = 854, DEFAULT_WINDOW_HEIGHT = 480;
// parameters, separated from ParamBucket
protected final List<String> mcParams;
protected final List<String> gameArgs;
// secondary parameters
protected final int width, height;
@ -79,34 +77,34 @@ public abstract class AbstractLauncher implements Launcher {
protected final String mainClassName;
protected AbstractLauncher(Parameters params) {
this.mcParams = params.getList("param", new ArrayList<String>());
this.mainClassName = params.getString("mainClass", "net.minecraft.client.Minecraft");
gameArgs = params.getList("param", new ArrayList<String>());
mainClassName = params.getString("mainClass", "net.minecraft.client.Minecraft");
this.serverAddress = params.getString("serverAddress", null);
this.serverPort = params.getString("serverPort", null);
serverAddress = params.getString("serverAddress", null);
serverPort = params.getString("serverPort", null);
String windowParams = params.getString("windowParams", null);
this.maximize = "max".equalsIgnoreCase(windowParams);
if ("max".equals(windowParams) || windowParams == null) {
maximize = windowParams != null;
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 {
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);
width = Integer.parseInt(sizePair[0]);
height = Integer.parseInt(sizePair[1]);
return;
} catch (NumberFormatException ignored) {
}
} else {
throw new ParseException(
String.format("Invalid window size parameters '%s'. Format: [height]x[width]", windowParams));
}
} else {
this.width = DEFAULT_WINDOW_WIDTH;
this.height = DEFAULT_WINDOW_HEIGHT;
throw new ParseException(windowParams, "[width]x[height]");
}
}

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
@ -55,11 +54,11 @@
package org.prismlauncher.launcher.impl;
import java.lang.invoke.MethodHandle;
import org.prismlauncher.utils.Parameters;
import org.prismlauncher.utils.ReflectionUtils;
import java.lang.invoke.MethodHandle;
public final class StandardLauncher extends AbstractLauncher {
public StandardLauncher(Parameters params) {
@ -69,27 +68,24 @@ public final class StandardLauncher extends AbstractLauncher {
@Override
public void launch() throws Throwable {
// window size, title and state
// FIXME: there is no good way to maximize the minecraft window from here.
// the following often breaks linux screen setups
// mcparams.add("--fullscreen");
if (!this.maximize) {
this.mcParams.add("--width");
this.mcParams.add(Integer.toString(this.width));
this.mcParams.add("--height");
this.mcParams.add(Integer.toString(this.height));
// FIXME doesn't support maximisation
if (!maximize) {
gameArgs.add("--width");
gameArgs.add(Integer.toString(width));
gameArgs.add("--height");
gameArgs.add(Integer.toString(height));
}
if (this.serverAddress != null) {
this.mcParams.add("--server");
this.mcParams.add(this.serverAddress);
this.mcParams.add("--port");
this.mcParams.add(this.serverPort);
if (serverAddress != null) {
gameArgs.add("--server");
gameArgs.add(serverAddress);
gameArgs.add("--port");
gameArgs.add(serverPort);
}
MethodHandle method = ReflectionUtils.findMainMethod(this.mainClassName);
method.invokeExact(this.mcParams.toArray(new String[0]));
// find and invoke the main method
MethodHandle method = ReflectionUtils.findMainMethod(mainClassName);
method.invokeExact(gameArgs.toArray(new String[0]));
}
}

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 flow <flowlnlnln@gmail.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
@ -55,13 +54,6 @@
package org.prismlauncher.launcher.impl.legacy;
import net.minecraft.Launcher;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.prismlauncher.utils.logging.Log;
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
@ -75,6 +67,13 @@ import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.prismlauncher.utils.logging.Log;
import net.minecraft.Launcher;
public final class LegacyFrame extends JFrame {
private static final long serialVersionUID = 1L;
@ -84,85 +83,85 @@ public final class LegacyFrame extends JFrame {
public LegacyFrame(String title, Applet applet) {
super(title);
this.launcher = new Launcher(applet);
launcher = new Launcher(applet);
applet.setStub(this.launcher);
applet.setStub(launcher);
try {
this.setIconImage(ImageIO.read(new File("icon.png")));
setIconImage(ImageIO.read(new File("icon.png")));
} catch (IOException e) {
Log.error("Unable to read Minecraft icon", e);
Log.error("Failed to read window icon", e);
}
this.addWindowListener(new ForceExitHandler());
addWindowListener(new ForceExitHandler());
}
public void start(String user, String session, int width, int height, boolean maximize, String serverAddress,
String serverPort, boolean isDemo) {
String serverPort, boolean demo) {
// Implements support for launching in to multiplayer on classic servers using a
// mpticket
// file generated by an external program and stored in the instance's root
// folder.
// mpticket file generated by an external program and stored in the instance's
// root folder.
Path instanceFolder = Paths.get("..");
Path mpticket = instanceFolder.resolve("mpticket");
Path mpticketCorrupt = instanceFolder.resolve("mpticket.corrupt");
Path mpticketFile = Paths.get(System.getProperty("user.dir"), "..", "mpticket");
Path mpticketFileCorrupt = Paths.get(System.getProperty("user.dir"), "..", "mpticket.corrupt");
if (Files.exists(mpticketFile)) {
if (Files.exists(mpticket)) {
try {
List<String> lines = Files.readAllLines(mpticketFile, StandardCharsets.UTF_8);
List<String> lines = Files.readAllLines(mpticket, StandardCharsets.UTF_8);
if (lines.size() < 3) {
Files.move(mpticketFile, mpticketFileCorrupt, StandardCopyOption.REPLACE_EXISTING);
Files.move(mpticket, mpticketCorrupt, StandardCopyOption.REPLACE_EXISTING);
Log.warning("Mpticket file is corrupted!");
Log.warning("mpticket file is corrupted");
} else {
// Assumes parameters are valid and in the correct order
this.launcher.setParameter("server", lines.get(0));
this.launcher.setParameter("port", lines.get(1));
this.launcher.setParameter("mppass", lines.get(2));
launcher.setParameter("server", lines.get(0));
launcher.setParameter("port", lines.get(1));
launcher.setParameter("mppass", lines.get(2));
}
} catch (IOException e) {
Log.error("Unable to read mpticket file", e);
Log.error("Failed to read mpticket file", e);
}
}
if (serverAddress != null) {
this.launcher.setParameter("server", serverAddress);
this.launcher.setParameter("port", serverPort);
launcher.setParameter("server", serverAddress);
launcher.setParameter("port", serverPort);
}
this.launcher.setParameter("username", user);
this.launcher.setParameter("sessionid", session);
this.launcher.setParameter("stand-alone", "true"); // Show the quit button. TODO: why won't this work?
this.launcher.setParameter("haspaid", "true"); // Some old versions need this for world saves to work.
this.launcher.setParameter("demo", isDemo ? "true" : "false");
this.launcher.setParameter("fullscreen", "false");
launcher.setParameter("username", user);
launcher.setParameter("sessionid", session);
launcher.setParameter("stand-alone", true); // Show the quit button. TODO: why won't this work?
launcher.setParameter("haspaid", true); // Some old versions need this for world saves to work.
launcher.setParameter("demo", demo);
launcher.setParameter("fullscreen", false);
this.add(this.launcher);
add(launcher);
this.launcher.setPreferredSize(new Dimension(width, height));
launcher.setPreferredSize(new Dimension(width, height));
this.pack();
pack();
this.setLocationRelativeTo(null);
this.setResizable(true);
setLocationRelativeTo(null);
setResizable(true);
if (maximize)
setExtendedState(MAXIMIZED_BOTH);
this.validate();
validate();
this.launcher.init();
this.launcher.start();
launcher.init();
launcher.start();
this.setVisible(true);
setVisible(true);
}
private final class ForceExitHandler extends WindowAdapter {
@Override
public void windowClosing(WindowEvent event) {
// FIXME better solution
new Thread(new Runnable() {
@Override
public void run() {
@ -177,9 +176,9 @@ public final class LegacyFrame extends JFrame {
}
}).start();
if (LegacyFrame.this.launcher != null) {
LegacyFrame.this.launcher.stop();
LegacyFrame.this.launcher.destroy();
if (launcher != null) {
launcher.stop();
launcher.destroy();
}
// old minecraft versions can hang without this >_<

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 flow <flowlnlnln@gmail.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
@ -56,17 +55,17 @@
package org.prismlauncher.launcher.impl.legacy;
import org.prismlauncher.launcher.impl.AbstractLauncher;
import org.prismlauncher.utils.Parameters;
import org.prismlauncher.utils.ReflectionUtils;
import org.prismlauncher.utils.logging.Log;
import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import org.prismlauncher.launcher.impl.AbstractLauncher;
import org.prismlauncher.utils.Parameters;
import org.prismlauncher.utils.ReflectionUtils;
import org.prismlauncher.utils.logging.Log;
/**
* Used to launch old versions that support applets.
*/
@ -75,51 +74,53 @@ public final class LegacyLauncher extends AbstractLauncher {
private final String user, session;
private final String title;
private final String appletClass;
private final boolean usesApplet;
private final String cwd;
private final boolean useApplet;
private final String gameDir;
public LegacyLauncher(Parameters params) {
super(params);
this.user = params.getString("userName");
this.session = params.getString("sessionId");
this.title = params.getString("windowTitle", "Minecraft");
this.appletClass = params.getString("appletClass", "net.minecraft.client.MinecraftApplet");
user = params.getString("userName");
session = params.getString("sessionId");
title = params.getString("windowTitle", "Minecraft");
appletClass = params.getString("appletClass", "net.minecraft.client.MinecraftApplet");
List<String> traits = params.getList("traits", Collections.<String>emptyList());
this.usesApplet = !traits.contains("noapplet");
useApplet = !traits.contains("noapplet");
this.cwd = System.getProperty("user.dir");
gameDir = System.getProperty("user.dir");
}
@Override
public void launch() throws Throwable {
Class<?> main = ClassLoader.getSystemClassLoader().loadClass(this.mainClassName);
Field gameDirField = ReflectionUtils.getMinecraftGameDirField(main);
Class<?> main = ClassLoader.getSystemClassLoader().loadClass(mainClassName);
Field gameDirField = ReflectionUtils.findMinecraftGameDirField(main);
if (gameDirField == null)
Log.warning("Could not find Minecraft path field");
Log.warning("Could not find Minecraft folder field");
else {
gameDirField.setAccessible(true);
gameDirField.set(null /* field is static, so instance is null */, new File(this.cwd));
gameDirField.set(null, new File(gameDir));
}
if (this.usesApplet) {
Log.launcher("Launching with applet wrapper...");
if (useApplet) {
System.setProperty("minecraft.applet.TargetDirectory", gameDir);
try {
LegacyFrame window = new LegacyFrame(this.title, ReflectionUtils.createAppletClass(this.appletClass));
LegacyFrame window = new LegacyFrame(title, ReflectionUtils.createAppletClass(appletClass));
window.start(this.user, this.session, this.width, this.height, this.maximize, this.serverAddress,
this.serverPort, this.mcParams.contains("--demo"));
window.start(user, session, width, height, maximize, serverAddress, serverPort,
gameArgs.contains("--demo"));
return;
} catch (Throwable e) {
Log.error("Running applet wrapper failed with exception; falling back to main class", e);
}
}
MethodHandle method = ReflectionUtils.findMainEntrypoint(main);
method.invokeExact(this.mcParams.toArray(new String[0]));
// find and invoke the main method, this time without size parameters
// in all versions that support applets, these are ignored
MethodHandle method = ReflectionUtils.findMainMethod(main);
method.invokeExact(gameArgs.toArray(new String[0]));
}
}

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
@ -55,40 +54,40 @@
package org.prismlauncher.utils;
import org.prismlauncher.exception.ParameterNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.prismlauncher.exception.ParameterNotFoundException;
public final class Parameters {
private final Map<String, List<String>> map = new HashMap<>();
public void add(String key, String value) {
List<String> params = this.map.get(key);
List<String> params = map.get(key);
if (params == null) {
params = new ArrayList<>();
this.map.put(key, params);
map.put(key, params);
}
params.add(value);
}
public List<String> getList(String key) throws ParameterNotFoundException {
List<String> params = this.map.get(key);
List<String> params = map.get(key);
if (params == null)
throw ParameterNotFoundException.forParameterName(key);
throw new ParameterNotFoundException(key);
return params;
}
public List<String> getList(String key, List<String> def) {
List<String> params = this.map.get(key);
List<String> params = map.get(key);
if (params == null || params.isEmpty())
return def;
@ -97,16 +96,16 @@ public final class Parameters {
}
public String getString(String key) throws ParameterNotFoundException {
List<String> list = this.getList(key);
List<String> list = getList(key);
if (list.isEmpty())
throw ParameterNotFoundException.forParameterName(key);
throw new ParameterNotFoundException(key);
return list.get(0);
}
public String getString(String key, String def) {
List<String> params = this.map.get(key);
List<String> params = map.get(key);
if (params == null || params.isEmpty())
return def;

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
@ -67,68 +66,57 @@ import org.prismlauncher.utils.logging.Log;
public final class ReflectionUtils {
private ReflectionUtils() {
}
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final ClassLoader LOADER = ClassLoader.getSystemClassLoader();
/**
* Instantiate an applet class by name
* Construct a Java applet by its class name.
*
* @param appletClassName The name of the applet class to resolve
*
* @return The instantiated applet class
*
* @throws ClassNotFoundException if the provided class name cannot be found
* @throws NoSuchMethodException if the no-args constructor cannot be found
* @throws IllegalAccessException if the constructor cannot be accessed via
* method handles
* @throws Throwable any exceptions from the class's constructor
* @param clazz The class name
* @return The applet instance
* @throws Throwable
*/
public static Applet createAppletClass(String appletClassName) throws Throwable {
Class<?> appletClass = ClassLoader.getSystemClassLoader().loadClass(appletClassName);
public static Applet createAppletClass(String clazz) throws Throwable {
Class<?> appletClass = LOADER.loadClass(clazz);
MethodHandle appletConstructor = MethodHandles.lookup().findConstructor(appletClass,
MethodType.methodType(void.class));
MethodHandle appletConstructor = LOOKUP.findConstructor(appletClass, MethodType.methodType(void.class));
return (Applet) appletConstructor.invoke();
}
/**
* Finds a field that looks like a Minecraft base folder in a supplied class
* Best guess of the game directory field within net.minecraft.client.Minecraft.
* Designed for legacy versions - newer versions do not use a static field.
*
* @param minecraftMainClass the class to scan
*
* @return The found field.
* @param clazz The class
* @return The first field matching criteria
*/
public static Field getMinecraftGameDirField(Class<?> minecraftMainClass) {
public static Field findMinecraftGameDirField(Class<?> clazz) {
Log.debug("Resolving minecraft game directory field");
// Field we're looking for is always
// private static File obfuscatedName = null;
for (Field field : minecraftMainClass.getDeclaredFields()) {
// Has to be File
// search for private static File
for (Field field : clazz.getDeclaredFields()) {
if (field.getType() != File.class) {
continue;
}
int fieldModifiers = field.getModifiers();
// Must be static
if (!Modifier.isStatic(fieldModifiers)) {
Log.debug("Rejecting field " + field.getName() + " because it is not static");
continue;
}
// Must be private
if (!Modifier.isPrivate(fieldModifiers)) {
Log.debug("Rejecting field " + field.getName() + " because it is not private");
continue;
}
// Must not be final
if (Modifier.isFinal(fieldModifiers)) {
Log.debug("Rejecting field " + field.getName() + " because it is final");
continue;
}
Log.debug("Identified field " + field.getName() + " to match conditions for minecraft game directory field");
Log.debug("Identified field " + field.getName() + " to match conditions for game directory field");
return field;
}
@ -137,51 +125,30 @@ public final class ReflectionUtils {
}
/**
* Resolve main entrypoint and returns method handle for it.
* <p>
* Resolves a method that matches the following signature <code>
* public static void main(String[] args) {
* <p>
* }
* </code>
* Gets the main method within a class.
*
* @param entrypointClass The entrypoint class to resolve the method from
*
* @return The method handle for the resolved entrypoint
*
* @throws NoSuchMethodException If no method matching the correct signature
* can be found
* @throws IllegalAccessException If method handles cannot access the entrypoint
* @param clazz The class
* @return A method matching the descriptor of a main method
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws IllegalAccessException
*/
public static MethodHandle findMainEntrypoint(Class<?> entrypointClass)
throws NoSuchMethodException, IllegalAccessException {
return MethodHandles.lookup().findStatic(entrypointClass, "main",
MethodType.methodType(void.class, String[].class));
public static MethodHandle findMainMethod(Class<?> clazz) throws NoSuchMethodException, IllegalAccessException {
return LOOKUP.findStatic(clazz, "main", MethodType.methodType(void.class, String[].class));
}
/**
* Resolve main entrypoint and returns method handle for it.
* <p>
* Resolves a method that matches the following signature <code>
* public static void main(String[] args) {
* <p>
* }
* </code>
* Gets the main method within a class by its name.
*
* @param entrypointClassName The name of the entrypoint class to resolve the
* method from
*
* @return The method handle for the resolved entrypoint
*
* @throws ClassNotFoundException If a class cannot be found with the provided
* name
* @throws NoSuchMethodException If no method matching the correct signature
* can be found
* @throws IllegalAccessException If method handles cannot access the entrypoint
* @param clazz The class name
* @return A method matching the descriptor of a main method
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws IllegalAccessException
*/
public static MethodHandle findMainMethod(String entrypointClassName)
public static MethodHandle findMainMethod(String clazz)
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException {
return findMainEntrypoint(ClassLoader.getSystemClassLoader().loadClass(entrypointClassName));
return findMainMethod(LOADER.loadClass(clazz));
}
}

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
*
* This program is free software: you can redistribute it and/or modify
@ -38,15 +37,13 @@ package org.prismlauncher.utils;
public final class StringUtils {
private StringUtils() {
}
public static String[] splitStringPair(char splitChar, String str) {
int splitPoint = str.indexOf(splitChar);
public static String[] splitStringPair(char splitChar, String input) {
int splitPoint = input.indexOf(splitChar);
if (splitPoint == -1)
return null;
return new String[] { input.substring(0, splitPoint), input.substring(splitPoint + 1) };
return new String[] { str.substring(0, splitPoint), str.substring(splitPoint + 1) };
}
}

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
@ -36,8 +36,13 @@
package org.prismlauncher.utils.logging;
public enum Level {
LAUNCHER("Launcher"), DEBUG("Debug"), INFO("Info"), MESSAGE("Message"),
WARNING("Warning"), ERROR("Error", true), FATAL("Fatal", true);
LAUNCHER("Launcher"),
DEBUG("Debug"),
INFO("Info"),
MESSAGE("Message"),
WARNING("Warning"),
ERROR("Error", true),
FATAL("Fatal", true);
String name;
boolean stderr;

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
@ -44,37 +44,12 @@ import java.io.PrintStream;
*/
public final class Log {
private static final PrintStream ERROR_PREFIX = new PrintStream(System.err) {
@Override
public void println(String x) {
error(x);
}
@Override
public void println(Object x) {
error(String.valueOf(x));
}
}, FATAL_PREFIX = new PrintStream(System.err) {
@Override
public void println(String x) {
fatal(x);
}
@Override
public void println(Object x) {
fatal(String.valueOf(x));
}
};
// original before overridden
private static final PrintStream OUT = new PrintStream(System.out), ERR = new PrintStream(System.err);
private static final PrintStream ERROR_PREFIX = new LogPrintStream(System.err, Level.ERROR),
FATAL_PREFIX = new LogPrintStream(System.err, Level.FATAL);
private static final boolean DEBUG = Boolean.getBoolean("org.prismlauncher.debug");
private Log() {
}
public static void blankLine() {
System.out.println();
}
public static void launcher(String message) {
log(message, Level.LAUNCHER);
}
@ -84,16 +59,9 @@ public final class Log {
}
public static void debug(String message) {
if (!DEBUG)
return;
log(message, Level.DEBUG);
}
public static void info(String message) {
log(message, Level.INFO);
}
public static void warning(String message) {
log(message, Level.WARNING);
}
@ -113,12 +81,16 @@ public final class Log {
}
/**
* Logs a message with the prefix !![LEVEL]!.
* Logs a message with the prefix <code>!![LEVEL]!</code>. This is picked up by
* the log viewer to give it nice colours.
*
* @param message The message
* @param level The level
*/
public static void log(String message, Level level) {
if (!DEBUG && level == Level.DEBUG)
return;
String prefix = "!![" + level.name + "]!";
// prefix first line
message = prefix + message;
@ -126,9 +98,9 @@ public final class Log {
message = message.replace("\n", "\n" + prefix);
if (level.stderr)
System.err.println(message);
ERR.println(message);
else
System.out.println(message);
OUT.println(message);
}
}

View File

@ -0,0 +1,99 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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 General Public License for more details.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module. An independent module is a module which is
* not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the
* library, but you are not obliged to do so. If you do not wish to do
* so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.utils.logging;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* Used to create a print stream that redirects to Log.
*/
final class LogPrintStream extends PrintStream {
private final Level level;
public LogPrintStream(OutputStream out, Level level) {
super(out);
this.level = level;
}
@Override
public void println(String x) {
Log.log(x, level);
}
@Override
public void println(Object x) {
println(String.valueOf(x));
}
@Override
public void println(boolean x) {
println(String.valueOf(x));
}
@Override
public void println(char x) {
println(String.valueOf(x));
}
@Override
public void println(int x) {
println(String.valueOf(x));
}
@Override
public void println(long x) {
println(String.valueOf(x));
}
@Override
public void println(float x) {
println(String.valueOf(x));
}
@Override
public void println(double x) {
println(String.valueOf(x));
}
@Override
public void println(char[] x) {
println(String.valueOf(x));
}
}