package io.gitlab.jfronny.inceptum.cli; import io.gitlab.jfronny.inceptum.Inceptum; 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; import java.util.Locale; public class LaunchCommand extends Command { public LaunchCommand() { super("Launches the game. Optionally specify \"server\" or \"client\"", "run", "launch", "start"); } @Override public void invoke(String[] args) { if (args.length < 2) { Inceptum.LOGGER.error("You must provide an instance name or path"); return; } String pArg = args.length == 2 ? args[1] : args[2]; 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 > 3) { instance.gameArgsCustom = instance.gameArgsCustom == null ? new ArrayList<>() : new ArrayList<>(instance.gameArgsCustom); for (int i = 3, argsLength = args.length; i < argsLength; i++) { instance.gameArgsCustom.add(args[i]); } } if (args.length >= 3 && "server".equals(args[1].toLowerCase(Locale.ROOT))) ServerLauncher.launch(instanceDir, instance); else ClientLauncher.launch(instanceDir, instance); } }