Cleanup splitting string into a pair
Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
parent
609b30110b
commit
ac5b74301e
@ -54,10 +54,12 @@
|
|||||||
|
|
||||||
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;
|
||||||
import org.prismlauncher.utils.Parameters;
|
import org.prismlauncher.utils.Parameters;
|
||||||
|
import org.prismlauncher.utils.StringUtils;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -66,58 +68,47 @@ 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");
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ExitCode exitCode = listen();
|
ExitCode exitCode = listen();
|
||||||
|
|
||||||
if (exitCode != ExitCode.NORMAL) {
|
if (exitCode != ExitCode.NORMAL) {
|
||||||
LOGGER.warning("Exiting with " + exitCode);
|
LOGGER.warning("Exiting with " + exitCode);
|
||||||
|
|
||||||
System.exit(exitCode.numericalCode);
|
System.exit(exitCode.numericalCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PreLaunchAction parseLine(String inData, Parameters params) throws ParseException {
|
private static PreLaunchAction parseLine(String input, Parameters params) throws ParseException {
|
||||||
if (inData.isEmpty())
|
if (input.isEmpty())
|
||||||
throw new ParseException("Unexpected empty string!");
|
throw new ParseException("Unexpected empty string!");
|
||||||
|
|
||||||
String first = inData;
|
|
||||||
String second = null;
|
if ("launch".equalsIgnoreCase(input)) {
|
||||||
int splitPoint = inData.indexOf(' ');
|
return PreLaunchAction.LAUNCH;
|
||||||
|
} else if ("abort".equalsIgnoreCase(input)) {
|
||||||
if (splitPoint != -1) {
|
return PreLaunchAction.ABORT;
|
||||||
first = first.substring(0, splitPoint);
|
} else {
|
||||||
second = inData.substring(splitPoint + 1);
|
String[] pair = StringUtils.splitStringPair(' ', input);
|
||||||
}
|
if (pair == null)
|
||||||
|
throw new ParseException("Error while parsing:" + input);
|
||||||
switch (first) {
|
|
||||||
case "launch":
|
params.add(pair[0], pair[1]);
|
||||||
return PreLaunchAction.LAUNCH;
|
|
||||||
case "abort":
|
return PreLaunchAction.PROCEED;
|
||||||
return PreLaunchAction.ABORT;
|
|
||||||
default:
|
|
||||||
if (second == null || second.isEmpty())
|
|
||||||
throw new ParseException("Error while parsing:" + inData);
|
|
||||||
|
|
||||||
params.add(first, second);
|
|
||||||
|
|
||||||
return PreLaunchAction.PROCEED;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ExitCode listen() {
|
private static ExitCode listen() {
|
||||||
Parameters parameters = new Parameters();
|
Parameters parameters = new Parameters();
|
||||||
PreLaunchAction preLaunchAction = PreLaunchAction.PROCEED;
|
PreLaunchAction preLaunchAction = PreLaunchAction.PROCEED;
|
||||||
|
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) {
|
||||||
System.in,
|
|
||||||
StandardCharsets.UTF_8
|
|
||||||
))) {
|
|
||||||
String line;
|
String line;
|
||||||
|
|
||||||
while (preLaunchAction == PreLaunchAction.PROCEED) {
|
while (preLaunchAction == PreLaunchAction.PROCEED) {
|
||||||
if ((line = reader.readLine()) != null) {
|
if ((line = reader.readLine()) != null) {
|
||||||
preLaunchAction = parseLine(line, parameters);
|
preLaunchAction = parseLine(line, parameters);
|
||||||
@ -127,49 +118,50 @@ public final class EntryPoint {
|
|||||||
}
|
}
|
||||||
} 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 ExitCode.ERROR;
|
return ExitCode.ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
if (preLaunchAction == PreLaunchAction.ABORT) {
|
if (preLaunchAction == PreLaunchAction.ABORT) {
|
||||||
LOGGER.info("Launch aborted by the launcher.");
|
LOGGER.info("Launch aborted by the launcher.");
|
||||||
|
|
||||||
return ExitCode.ERROR;
|
return ExitCode.ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Launcher launcher = LauncherFactory.createLauncher(parameters);
|
Launcher launcher = LauncherFactory.createLauncher(parameters);
|
||||||
|
|
||||||
launcher.launch();
|
launcher.launch();
|
||||||
|
|
||||||
return ExitCode.NORMAL;
|
return ExitCode.NORMAL;
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Wrong argument.", e);
|
LOGGER.log(Level.SEVERE, "Wrong argument.", e);
|
||||||
|
|
||||||
return ExitCode.ERROR;
|
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 ExitCode.ERROR;
|
return ExitCode.ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum PreLaunchAction {
|
private enum PreLaunchAction {
|
||||||
PROCEED,
|
PROCEED,
|
||||||
LAUNCH,
|
LAUNCH,
|
||||||
ABORT
|
ABORT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private enum ExitCode {
|
private enum ExitCode {
|
||||||
NORMAL(0),
|
NORMAL(0),
|
||||||
ERROR(1);
|
ERROR(1);
|
||||||
|
|
||||||
private final int numericalCode;
|
private final int numericalCode;
|
||||||
|
|
||||||
ExitCode(int numericalCode) {
|
ExitCode(int numericalCode) {
|
||||||
this.numericalCode = numericalCode;
|
this.numericalCode = numericalCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,7 @@ package org.prismlauncher.launcher.impl;
|
|||||||
import org.prismlauncher.exception.ParseException;
|
import org.prismlauncher.exception.ParseException;
|
||||||
import org.prismlauncher.launcher.Launcher;
|
import org.prismlauncher.launcher.Launcher;
|
||||||
import org.prismlauncher.utils.Parameters;
|
import org.prismlauncher.utils.Parameters;
|
||||||
|
import org.prismlauncher.utils.StringUtils;
|
||||||
|
|
||||||
import java.lang.invoke.MethodHandle;
|
import java.lang.invoke.MethodHandle;
|
||||||
import java.lang.invoke.MethodHandles;
|
import java.lang.invoke.MethodHandles;
|
||||||
@ -101,13 +102,13 @@ public abstract class AbstractLauncher implements Launcher {
|
|||||||
height = DEFAULT_WINDOW_HEIGHT;
|
height = DEFAULT_WINDOW_HEIGHT;
|
||||||
} else {
|
} else {
|
||||||
maximize = false;
|
maximize = false;
|
||||||
|
|
||||||
int byIndex = windowParams.indexOf('x');
|
String[] sizePair = StringUtils.splitStringPair('x', windowParams);
|
||||||
|
|
||||||
if (byIndex != -1) {
|
if (sizePair != null) {
|
||||||
try {
|
try {
|
||||||
width = Integer.parseInt(windowParams.substring(0, byIndex));
|
width = Integer.parseInt(sizePair[0]);
|
||||||
height = Integer.parseInt(windowParams.substring(byIndex + 1));
|
height = Integer.parseInt(sizePair[1]);
|
||||||
return;
|
return;
|
||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
|
15
libraries/launcher/org/prismlauncher/utils/StringUtils.java
Normal file
15
libraries/launcher/org/prismlauncher/utils/StringUtils.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package org.prismlauncher.utils;
|
||||||
|
|
||||||
|
|
||||||
|
public final class StringUtils {
|
||||||
|
private StringUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
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) };
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user