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

65 lines
2.7 KiB
Java
Raw Normal View History

2022-09-04 21:21:24 +02:00
package io.gitlab.jfronny.inceptum.cli.commands;
2021-10-30 22:05:24 +02:00
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.cli.*;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.launcher.api.account.AccountManager;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.InstanceMeta;
2022-10-27 20:54:55 +02:00
import io.gitlab.jfronny.inceptum.launcher.system.instance.Instance;
2022-10-19 21:44:33 +02:00
import io.gitlab.jfronny.inceptum.launcher.system.launch.*;
2022-10-27 20:54:55 +02:00
import io.gitlab.jfronny.inceptum.launcher.system.setup.Steps;
2021-10-30 22:05:24 +02:00
import java.io.IOException;
2022-09-04 21:21:24 +02:00
import java.util.*;
import java.util.stream.Stream;
2021-10-30 22:05:24 +02:00
2022-01-04 23:32:05 +01:00
public class LaunchCommand extends BaseInstanceCommand {
private final boolean server;
private final boolean restart;
2021-10-30 22:05:24 +02:00
public LaunchCommand() {
super("Launches an instance of the game (client by default). Non-blocking (batch commands will continue if this is ran)",
2022-01-04 23:32:05 +01:00
"[game arguments...]",
2022-09-30 18:07:18 +02:00
List.of("run", "instance.launch", "start"),
List.of(
new LaunchCommand("Explicitly launch a client", "client", false, false),
new LaunchCommand("Launch a server", "server", true, false,
new LaunchCommand("Restart the server if it stops", "restart", true, true)
2022-09-04 21:21:24 +02:00
)
));
this.server = false;
this.restart = false;
}
private LaunchCommand(String help, String name, boolean server, boolean restart, Command... subCommands) {
2022-01-04 23:32:05 +01:00
super(help, "[game arguments...]", List.of(name), Arrays.asList(subCommands));
this.server = server;
this.restart = restart;
2021-10-30 22:05:24 +02:00
}
@Override
2022-10-19 21:44:33 +02:00
protected void invoke(CommandArgs args, Instance instance) throws IOException, LaunchException {
if (instance.isSetupLocked()) {
Utils.LOGGER.error("This instance is still being set up");
2021-10-30 22:05:24 +02:00
return;
}
if (instance.isRunningLocked()) {
Utils.LOGGER.error("This instance is already running");
2022-01-04 12:51:51 +01:00
return;
2021-10-30 22:05:24 +02:00
}
if (args.length > 1) {
InstanceMeta meta = instance.meta();
meta.checkArguments();
meta.arguments = meta.arguments
.withClient(Stream.concat(meta.arguments.client().stream(), args.after(0).stream()).toList())
.withServer(Stream.concat(meta.arguments.server().stream(), args.after(0).stream()).toList());
2021-11-24 19:53:06 +01:00
}
Steps.reDownload(instance, Steps.createProcessState());
if (server) {
2022-10-19 21:44:33 +02:00
InstanceLauncher.launch(instance, LaunchType.Server, restart, AccountManager.NULL_AUTH);
2022-09-04 21:21:24 +02:00
} else {
2021-11-24 19:53:06 +01:00
AccountManager.loadAccounts();
InstanceLauncher.launchClient(instance);
2021-10-30 23:22:16 +02:00
}
2021-10-30 22:05:24 +02:00
}
}