Cleanup EntryPoint code

- Don't return an int from listen(). An enum is preferred.
- Make parseLine() static, and pass Parameters to it.

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax 2022-10-27 20:12:39 -04:00 committed by TheKodeToad
parent 107fa6b4f7
commit 99d9868116

View File

@ -52,6 +52,7 @@
package org.prismlauncher; package org.prismlauncher;
import org.prismlauncher.exception.ParseException; import org.prismlauncher.exception.ParseException;
import org.prismlauncher.launcher.Launcher; import org.prismlauncher.launcher.Launcher;
import org.prismlauncher.launcher.LauncherFactory; import org.prismlauncher.launcher.LauncherFactory;
@ -64,102 +65,114 @@ import java.nio.charset.StandardCharsets;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public final class EntryPoint { public final class EntryPoint {
private static final Logger LOGGER = Logger.getLogger("EntryPoint"); private static final Logger LOGGER = Logger.getLogger("EntryPoint");
private final Parameters params = new Parameters(); private final Parameters params = new Parameters();
public static void main(String[] args) { public static void main(String[] args) {
EntryPoint listener = new EntryPoint(); EntryPoint listener = new EntryPoint();
int retCode = listener.listen(); ExitCode exitCode = listener.listen();
if (retCode != 0) { if (exitCode != ExitCode.NORMAL) {
LOGGER.info("Exiting with " + retCode); LOGGER.warning("Exiting with " + exitCode);
System.exit(retCode); System.exit(exitCode.numericalCode);
} }
} }
private Action parseLine(String inData) throws ParseException { private static PreLaunchAction parseLine(String inData, Parameters params) throws ParseException {
if (inData.isEmpty()) if (inData.isEmpty())
throw new ParseException("Unexpected empty string!"); throw new ParseException("Unexpected empty string!");
String first = inData; String first = inData;
String second = null; String second = null;
int splitPoint = inData.indexOf(' '); int splitPoint = inData.indexOf(' ');
if (splitPoint != -1) { if (splitPoint != -1) {
first = first.substring(0, splitPoint); first = first.substring(0, splitPoint);
second = inData.substring(splitPoint + 1); second = inData.substring(splitPoint + 1);
} }
switch (first) { switch (first) {
case "launch": case "launch":
return Action.LAUNCH; return PreLaunchAction.LAUNCH;
case "abort": case "abort":
return Action.ABORT; return PreLaunchAction.ABORT;
default: default:
if (second == null || second.isEmpty()) if (second == null || second.isEmpty())
throw new ParseException("Error while parsing:" + inData); throw new ParseException("Error while parsing:" + inData);
params.add(first, second); params.add(first, second);
return Action.PROCEED; return PreLaunchAction.PROCEED;
} }
} }
public int listen() { public ExitCode listen() {
Action action = Action.PROCEED; PreLaunchAction preLaunchAction = PreLaunchAction.PROCEED;
try (BufferedReader reader = new BufferedReader(new InputStreamReader( try (BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in, System.in,
StandardCharsets.UTF_8 StandardCharsets.UTF_8
))) { ))) {
String line; String line;
while (action == Action.PROCEED) { while (preLaunchAction == PreLaunchAction.PROCEED) {
if ((line = reader.readLine()) != null) { if ((line = reader.readLine()) != null) {
action = parseLine(line); preLaunchAction = parseLine(line, this.params);
} else { } else {
action = Action.ABORT; preLaunchAction = PreLaunchAction.ABORT;
} }
} }
} catch (IOException | ParseException e) { } catch (IOException | ParseException e) {
LOGGER.log(Level.SEVERE, "Launcher abort due to exception:", e); LOGGER.log(Level.SEVERE, "Launcher abort due to exception:", e);
return 1; return ExitCode.ERROR;
} }
// Main loop // Main loop
if (action == Action.ABORT) { if (preLaunchAction == PreLaunchAction.ABORT) {
LOGGER.info("Launch aborted by the launcher."); LOGGER.info("Launch aborted by the launcher.");
return 1; return ExitCode.ERROR;
} }
try { try {
Launcher launcher = LauncherFactory.createLauncher(params); Launcher launcher = LauncherFactory.createLauncher(params);
launcher.launch(); launcher.launch();
return 0; return ExitCode.NORMAL;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOGGER.log(Level.SEVERE, "Wrong argument.", e); LOGGER.log(Level.SEVERE, "Wrong argument.", e);
return 1; return ExitCode.ERROR;
} catch (Throwable e) { } catch (Throwable e) {
LOGGER.log(Level.SEVERE, "Exception caught from launcher.", e); LOGGER.log(Level.SEVERE, "Exception caught from launcher.", e);
return 1; return ExitCode.ERROR;
} }
} }
private enum Action { private enum PreLaunchAction {
PROCEED, PROCEED,
LAUNCH, LAUNCH,
ABORT ABORT
} }
private enum ExitCode {
NORMAL(0),
ERROR(1);
private final int numericalCode;
ExitCode(int numericalCode) {
this.numericalCode = numericalCode;
}
}
} }