Inceptum/src/main/java/io/gitlab/jfronny/inceptum/cli/LaunchCommand.java

64 lines
2.5 KiB
Java
Raw Normal View History

2021-10-30 22:05:24 +02:00
package io.gitlab.jfronny.inceptum.cli;
import io.gitlab.jfronny.inceptum.Inceptum;
import io.gitlab.jfronny.inceptum.model.inceptum.CommandArguments;
2021-10-30 22:05:24 +02:00
import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta;
import io.gitlab.jfronny.inceptum.util.ClientLauncher;
import io.gitlab.jfronny.inceptum.util.ProcessUtils;
import io.gitlab.jfronny.inceptum.util.ServerLauncher;
import io.gitlab.jfronny.inceptum.util.Utils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
2021-10-30 23:22:16 +02:00
import java.util.ArrayList;
2021-10-30 22:05:24 +02:00
public class LaunchCommand extends Command {
public LaunchCommand() {
super("Launches the game. Optionally specify \"server\" or \"client\"", "run", "launch", "start");
}
@Override
public void invoke(CommandArguments args) {
if (args.length == 0) {
2021-10-30 22:05:24 +02:00
Inceptum.LOGGER.error("You must provide an instance name or path");
return;
}
String pArg = args.last();
2021-10-30 22:05:24 +02:00
Path instanceDir = Files.exists(Path.of(pArg)) ? Path.of(pArg) : Inceptum.INSTANCE_DIR.resolve(pArg);
if (!Files.exists(instanceDir.resolve("instance.json"))) {
Inceptum.LOGGER.error("Not a valid instance");
return;
}
if (Files.exists(instanceDir.resolve("inceptum.setup.lock"))) {
Inceptum.LOGGER.error("This instance is still being set up");
return;
}
if (Files.exists(instanceDir.resolve("inceptum.lock"))) {
try {
if (ProcessUtils.isProcessAlive(Files.readString(instanceDir.resolve("inceptum.lock")))) {
Inceptum.LOGGER.error("This instance is already being ran");
return;
}
} catch (IOException e) {
Inceptum.showError("Could not read inceptum lock", e);
return;
}
}
InstanceMeta instance;
try {
instance = Utils.loadObject(instanceDir.resolve("instance.json"), InstanceMeta.class);
} catch (IOException e) {
Inceptum.showError("Not a valid instance", e);
return;
}
if (args.length > 2) {
2021-10-30 23:22:16 +02:00
instance.gameArgsCustom = instance.gameArgsCustom == null ? new ArrayList<>() : new ArrayList<>(instance.gameArgsCustom);
instance.gameArgsCustom.addAll(args.after(1));
2021-10-30 23:22:16 +02:00
}
if (args.length >= 2 && args.contains("server"))
2021-10-30 22:05:24 +02:00
ServerLauncher.launch(instanceDir, instance);
else ClientLauncher.launch(instanceDir, instance);
}
}