Inceptum/src/main/java/io/gitlab/jfronny/inceptum/cli/LaunchCommand.java
2021-10-31 16:59:25 +01:00

64 lines
2.5 KiB
Java

package io.gitlab.jfronny.inceptum.cli;
import io.gitlab.jfronny.inceptum.Inceptum;
import io.gitlab.jfronny.inceptum.model.inceptum.CommandArguments;
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;
import java.util.ArrayList;
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) {
Inceptum.LOGGER.error("You must provide an instance name or path");
return;
}
String pArg = args.last();
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) {
instance.gameArgsCustom = instance.gameArgsCustom == null ? new ArrayList<>() : new ArrayList<>(instance.gameArgsCustom);
instance.gameArgsCustom.addAll(args.after(1));
}
if (args.length >= 2 && args.contains("server"))
ServerLauncher.launch(instanceDir, instance);
else ClientLauncher.launch(instanceDir, instance);
}
}