Merge pull request #486 from icelimetea/refactor-java-launcher
Refactor some parts of NewLaunch
This commit is contained in:
commit
bd946c78f3
@ -16,136 +16,123 @@ package org.multimc;/*
|
|||||||
|
|
||||||
import org.multimc.onesix.OneSixLauncher;
|
import org.multimc.onesix.OneSixLauncher;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.BufferedReader;
|
||||||
import java.nio.charset.Charset;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class EntryPoint
|
public class EntryPoint
|
||||||
{
|
{
|
||||||
private enum Action
|
|
||||||
{
|
private static final Logger LOGGER = Logger.getLogger("EntryPoint");
|
||||||
Proceed,
|
|
||||||
Launch,
|
private final ParamBucket params = new ParamBucket();
|
||||||
Abort
|
|
||||||
}
|
private org.multimc.Launcher launcher;
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
EntryPoint listener = new EntryPoint();
|
EntryPoint listener = new EntryPoint();
|
||||||
|
|
||||||
int retCode = listener.listen();
|
int retCode = listener.listen();
|
||||||
|
|
||||||
if (retCode != 0)
|
if (retCode != 0)
|
||||||
{
|
{
|
||||||
System.out.println("Exiting with " + retCode);
|
LOGGER.info("Exiting with " + retCode);
|
||||||
|
|
||||||
System.exit(retCode);
|
System.exit(retCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Action parseLine(String inData) throws ParseException
|
private Action parseLine(String inData) throws ParseException
|
||||||
{
|
{
|
||||||
String[] pair = inData.split(" ", 2);
|
String[] tokens = inData.split("\\s+", 2);
|
||||||
|
|
||||||
if(pair.length == 1)
|
if (tokens.length == 0)
|
||||||
{
|
throw new ParseException("Unexpected empty string!");
|
||||||
String command = pair[0];
|
|
||||||
if (pair[0].equals("launch"))
|
switch (tokens[0]) {
|
||||||
|
case "launch": {
|
||||||
return Action.Launch;
|
return Action.Launch;
|
||||||
|
}
|
||||||
|
|
||||||
else if (pair[0].equals("abort"))
|
case "abort": {
|
||||||
return Action.Abort;
|
return Action.Abort;
|
||||||
|
}
|
||||||
|
|
||||||
else throw new ParseException("Error while parsing:" + pair[0]);
|
case "launcher": {
|
||||||
}
|
if (tokens.length != 2)
|
||||||
|
throw new ParseException("Expected 2 tokens, got " + tokens.length);
|
||||||
|
|
||||||
if(pair.length != 2)
|
if (tokens[1].equals("onesix")) {
|
||||||
throw new ParseException("Pair length is not 2.");
|
launcher = new OneSixLauncher();
|
||||||
|
|
||||||
String command = pair[0];
|
LOGGER.info("Using onesix launcher.");
|
||||||
String param = pair[1];
|
|
||||||
|
return Action.Proceed;
|
||||||
|
} else {
|
||||||
|
throw new ParseException("Invalid launcher type: " + tokens[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
if (tokens.length != 2)
|
||||||
|
throw new ParseException("Error while parsing:" + inData);
|
||||||
|
|
||||||
|
params.add(tokens[0], tokens[1]);
|
||||||
|
|
||||||
if(command.equals("launcher"))
|
|
||||||
{
|
|
||||||
if(param.equals("onesix"))
|
|
||||||
{
|
|
||||||
m_launcher = new OneSixLauncher();
|
|
||||||
Utils.log("Using onesix launcher.");
|
|
||||||
Utils.log();
|
|
||||||
return Action.Proceed;
|
return Action.Proceed;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
throw new ParseException("Invalid launcher type: " + param);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_params.add(command, param);
|
|
||||||
//System.out.println(command + " : " + param);
|
|
||||||
return Action.Proceed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int listen()
|
public int listen()
|
||||||
{
|
{
|
||||||
BufferedReader buffer;
|
Action action = Action.Proceed;
|
||||||
try
|
|
||||||
{
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||||
buffer = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
|
System.in,
|
||||||
} catch (UnsupportedEncodingException e)
|
StandardCharsets.UTF_8
|
||||||
{
|
))) {
|
||||||
System.err.println("For some reason, your java does not support UTF-8. Consider living in the current century.");
|
String line;
|
||||||
e.printStackTrace();
|
|
||||||
|
while (action == Action.Proceed) {
|
||||||
|
if ((line = reader.readLine()) != null) {
|
||||||
|
action = parseLine(line);
|
||||||
|
} else {
|
||||||
|
action = Action.Abort;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException | ParseException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Launcher ABORT due to exception:", e);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
boolean isListening = true;
|
|
||||||
boolean isAborted = false;
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while (isListening)
|
if (action == Action.Abort)
|
||||||
{
|
{
|
||||||
String inData;
|
LOGGER.info("Launch aborted by the launcher.");
|
||||||
try
|
|
||||||
{
|
|
||||||
// Read from the pipe one line at a time
|
|
||||||
inData = buffer.readLine();
|
|
||||||
if (inData != null)
|
|
||||||
{
|
|
||||||
Action a = parseLine(inData);
|
|
||||||
if(a == Action.Abort)
|
|
||||||
{
|
|
||||||
isListening = false;
|
|
||||||
isAborted = true;
|
|
||||||
}
|
|
||||||
if(a == Action.Launch)
|
|
||||||
{
|
|
||||||
isListening = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
isListening = false;
|
|
||||||
isAborted = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
System.err.println("Launcher ABORT due to IO exception:");
|
|
||||||
e.printStackTrace();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
catch (ParseException e)
|
|
||||||
{
|
|
||||||
System.err.println("Launcher ABORT due to PARSE exception:");
|
|
||||||
e.printStackTrace();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(isAborted)
|
|
||||||
{
|
|
||||||
System.err.println("Launch aborted by the launcher.");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(m_launcher != null)
|
|
||||||
|
if (launcher != null)
|
||||||
{
|
{
|
||||||
return m_launcher.launch(m_params);
|
return launcher.launch(params);
|
||||||
}
|
}
|
||||||
System.err.println("No valid launcher implementation specified.");
|
|
||||||
|
LOGGER.log(Level.SEVERE, "No valid launcher implementation specified.");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ParamBucket m_params = new ParamBucket();
|
private enum Action {
|
||||||
private org.multimc.Launcher m_launcher;
|
Proceed,
|
||||||
|
Launch,
|
||||||
|
Abort
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,5 +18,5 @@ package org.multimc;
|
|||||||
|
|
||||||
public interface Launcher
|
public interface Launcher
|
||||||
{
|
{
|
||||||
abstract int launch(ParamBucket params);
|
int launch(ParamBucket params);
|
||||||
}
|
}
|
||||||
|
@ -19,62 +19,62 @@ package org.multimc;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class ParamBucket
|
public class ParamBucket
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private final Map<String, List<String>> paramsMap = new HashMap<>();
|
||||||
|
|
||||||
public void add(String key, String value)
|
public void add(String key, String value)
|
||||||
{
|
{
|
||||||
List<String> coll = null;
|
paramsMap.computeIfAbsent(key, k -> new ArrayList<>())
|
||||||
if(!m_params.containsKey(key))
|
.add(value);
|
||||||
{
|
|
||||||
coll = new ArrayList<String>();
|
|
||||||
m_params.put(key, coll);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
coll = m_params.get(key);
|
|
||||||
}
|
|
||||||
coll.add(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> all(String key) throws NotFoundException
|
public List<String> all(String key) throws NotFoundException
|
||||||
{
|
{
|
||||||
if(!m_params.containsKey(key))
|
List<String> params = paramsMap.get(key);
|
||||||
|
|
||||||
|
if (params == null)
|
||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
return m_params.get(key);
|
|
||||||
|
return params;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> allSafe(String key, List<String> def)
|
public List<String> allSafe(String key, List<String> def)
|
||||||
{
|
{
|
||||||
if(!m_params.containsKey(key) || m_params.get(key).size() < 1)
|
List<String> params = paramsMap.get(key);
|
||||||
{
|
|
||||||
|
if (params == null || params.isEmpty())
|
||||||
return def;
|
return def;
|
||||||
}
|
|
||||||
return m_params.get(key);
|
return params;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> allSafe(String key)
|
public List<String> allSafe(String key)
|
||||||
{
|
{
|
||||||
return allSafe(key, new ArrayList<String>());
|
return allSafe(key, new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String first(String key) throws NotFoundException
|
public String first(String key) throws NotFoundException
|
||||||
{
|
{
|
||||||
List<String> list = all(key);
|
List<String> list = all(key);
|
||||||
if(list.size() < 1)
|
|
||||||
{
|
if (list.isEmpty())
|
||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
}
|
|
||||||
return list.get(0);
|
return list.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String firstSafe(String key, String def)
|
public String firstSafe(String key, String def)
|
||||||
{
|
{
|
||||||
if(!m_params.containsKey(key) || m_params.get(key).size() < 1)
|
List<String> params = paramsMap.get(key);
|
||||||
{
|
|
||||||
|
if (params == null || params.isEmpty())
|
||||||
return def;
|
return def;
|
||||||
}
|
|
||||||
return m_params.get(key).get(0);
|
return params.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String firstSafe(String key)
|
public String firstSafe(String key)
|
||||||
@ -82,5 +82,4 @@ public class ParamBucket
|
|||||||
return firstSafe(key, "");
|
return firstSafe(key, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<String, List<String>> m_params = new HashMap<String, List<String>>();
|
|
||||||
}
|
}
|
||||||
|
@ -16,21 +16,10 @@
|
|||||||
|
|
||||||
package org.multimc;
|
package org.multimc;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLClassLoader;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipFile;
|
|
||||||
|
|
||||||
public class Utils
|
public class Utils
|
||||||
{
|
{
|
||||||
@ -93,27 +82,5 @@ public class Utils
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Log to the launcher console
|
|
||||||
*
|
|
||||||
* @param message A String containing the message
|
|
||||||
* @param level A String containing the level name. See MinecraftLauncher::getLevel()
|
|
||||||
*/
|
|
||||||
public static void log(String message, String level)
|
|
||||||
{
|
|
||||||
// Kinda dirty
|
|
||||||
String tag = "!![" + level + "]!";
|
|
||||||
System.out.println(tag + message.replace("\n", "\n" + tag));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void log(String message)
|
|
||||||
{
|
|
||||||
log(message, "Launcher");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void log()
|
|
||||||
{
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,14 +19,18 @@ import org.multimc.*;
|
|||||||
|
|
||||||
import java.applet.Applet;
|
import java.applet.Applet;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class OneSixLauncher implements Launcher
|
public class OneSixLauncher implements Launcher
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger("OneSixLauncher");
|
||||||
|
|
||||||
// parameters, separated from ParamBucket
|
// parameters, separated from ParamBucket
|
||||||
private List<String> libraries;
|
private List<String> libraries;
|
||||||
private List<String> mcparams;
|
private List<String> mcparams;
|
||||||
@ -104,7 +108,7 @@ public class OneSixLauncher implements Launcher
|
|||||||
|
|
||||||
if (f == null)
|
if (f == null)
|
||||||
{
|
{
|
||||||
System.err.println("Could not find Minecraft path field.");
|
LOGGER.warning("Could not find Minecraft path field.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -113,8 +117,12 @@ public class OneSixLauncher implements Launcher
|
|||||||
}
|
}
|
||||||
} catch (Exception e)
|
} catch (Exception e)
|
||||||
{
|
{
|
||||||
System.err.println("Could not set base folder. Failed to find/access Minecraft main class:");
|
LOGGER.log(
|
||||||
e.printStackTrace(System.err);
|
Level.SEVERE,
|
||||||
|
"Could not set base folder. Failed to find/access Minecraft main class:",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +130,7 @@ public class OneSixLauncher implements Launcher
|
|||||||
|
|
||||||
if(!traits.contains("noapplet"))
|
if(!traits.contains("noapplet"))
|
||||||
{
|
{
|
||||||
Utils.log("Launching with applet wrapper...");
|
LOGGER.info("Launching with applet wrapper...");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Class<?> MCAppletClass = cl.loadClass(appletClass);
|
Class<?> MCAppletClass = cl.loadClass(appletClass);
|
||||||
@ -132,10 +140,9 @@ public class OneSixLauncher implements Launcher
|
|||||||
return 0;
|
return 0;
|
||||||
} catch (Exception e)
|
} catch (Exception e)
|
||||||
{
|
{
|
||||||
Utils.log("Applet wrapper failed:", "Error");
|
LOGGER.log(Level.SEVERE, "Applet wrapper failed:", e);
|
||||||
e.printStackTrace(System.err);
|
|
||||||
Utils.log();
|
LOGGER.warning("Falling back to using main class.");
|
||||||
Utils.log("Falling back to using main class.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,8 +154,8 @@ public class OneSixLauncher implements Launcher
|
|||||||
return 0;
|
return 0;
|
||||||
} catch (Exception e)
|
} catch (Exception e)
|
||||||
{
|
{
|
||||||
Utils.log("Failed to invoke the Minecraft main class:", "Fatal");
|
LOGGER.log(Level.SEVERE, "Failed to invoke the Minecraft main class:", e);
|
||||||
e.printStackTrace(System.err);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -185,8 +192,8 @@ public class OneSixLauncher implements Launcher
|
|||||||
mc = cl.loadClass(mainClass);
|
mc = cl.loadClass(mainClass);
|
||||||
} catch (ClassNotFoundException e)
|
} catch (ClassNotFoundException e)
|
||||||
{
|
{
|
||||||
System.err.println("Failed to find Minecraft main class:");
|
LOGGER.log(Level.SEVERE, "Failed to find Minecraft main class:", e);
|
||||||
e.printStackTrace(System.err);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,8 +204,8 @@ public class OneSixLauncher implements Launcher
|
|||||||
meth = mc.getMethod("main", String[].class);
|
meth = mc.getMethod("main", String[].class);
|
||||||
} catch (NoSuchMethodException e)
|
} catch (NoSuchMethodException e)
|
||||||
{
|
{
|
||||||
System.err.println("Failed to acquire the main method:");
|
LOGGER.log(Level.SEVERE, "Failed to acquire the main method:", e);
|
||||||
e.printStackTrace(System.err);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,8 +217,8 @@ public class OneSixLauncher implements Launcher
|
|||||||
meth.invoke(null, (Object) paramsArray);
|
meth.invoke(null, (Object) paramsArray);
|
||||||
} catch (Exception e)
|
} catch (Exception e)
|
||||||
{
|
{
|
||||||
System.err.println("Failed to start Minecraft:");
|
LOGGER.log(Level.SEVERE, "Failed to start Minecraft:", e);
|
||||||
e.printStackTrace(System.err);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -226,8 +233,8 @@ public class OneSixLauncher implements Launcher
|
|||||||
processParams(params);
|
processParams(params);
|
||||||
} catch (NotFoundException e)
|
} catch (NotFoundException e)
|
||||||
{
|
{
|
||||||
System.err.println("Not enough arguments.");
|
LOGGER.log(Level.SEVERE, "Not enough arguments!");
|
||||||
e.printStackTrace(System.err);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,4 +252,5 @@ public class OneSixLauncher implements Launcher
|
|||||||
return launchWithMainClass();
|
return launchWithMainClass();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user