104 lines
4.4 KiB
Java
104 lines
4.4 KiB
Java
package io.gitlab.jfronny.inceptum.frontend.cli.commands;
|
|
|
|
import io.gitlab.jfronny.inceptum.Inceptum;
|
|
import io.gitlab.jfronny.inceptum.frontend.cli.BaseInstanceCommand;
|
|
import io.gitlab.jfronny.inceptum.frontend.cli.Command;
|
|
import io.gitlab.jfronny.inceptum.frontend.cli.CommandArgs;
|
|
import io.gitlab.jfronny.inceptum.util.install.Steps;
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta;
|
|
import io.gitlab.jfronny.inceptum.util.InstanceLock;
|
|
import io.gitlab.jfronny.inceptum.util.MetaHolder;
|
|
import io.gitlab.jfronny.inceptum.util.Utils;
|
|
import io.gitlab.jfronny.inceptum.util.api.account.AccountManager;
|
|
import io.gitlab.jfronny.inceptum.util.InstanceLauncher;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class LaunchCommand extends BaseInstanceCommand {
|
|
private final boolean server;
|
|
private final boolean restart;
|
|
|
|
public LaunchCommand() {
|
|
super("Launches an instance of the game (client by default). Non-blocking (batch commands will continue if this is ran)",
|
|
"[game arguments...]",
|
|
List.of("run", "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)
|
|
)
|
|
));
|
|
this.server = false;
|
|
this.restart = false;
|
|
}
|
|
|
|
private LaunchCommand(String help, String name, boolean server, boolean restart, Command... subCommands) {
|
|
super(help, "[game arguments...]", List.of(name), Arrays.asList(subCommands));
|
|
this.server = server;
|
|
this.restart = restart;
|
|
}
|
|
|
|
@Override
|
|
protected void invoke(CommandArgs args, Path instancePath, InstanceMeta meta) {
|
|
if (args.length == 0) {
|
|
Utils.LOGGER.error("You must provide an instance name or path");
|
|
return;
|
|
}
|
|
String pArg = args.get(0);
|
|
Path instanceDir = Files.exists(Path.of(pArg)) ? Path.of(pArg) : MetaHolder.INSTANCE_DIR.resolve(pArg);
|
|
if (!Files.exists(instanceDir.resolve("instance.json"))) {
|
|
Utils.LOGGER.error("Not a valid instance");
|
|
return;
|
|
}
|
|
if (InstanceLock.isSetupLocked(instanceDir)) {
|
|
Utils.LOGGER.error("This instance is still being set up");
|
|
return;
|
|
}
|
|
try {
|
|
if (InstanceLock.isRunningLocked(instanceDir)) {
|
|
Utils.LOGGER.error("This instance is already running");
|
|
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 > 1) {
|
|
if (instance.arguments == null) instance.arguments = new InstanceMeta.Arguments();
|
|
instance.arguments.client = instance.arguments.client == null ? new ArrayList<>() : new ArrayList<>(instance.arguments.client);
|
|
instance.arguments.server = instance.arguments.server == null ? new ArrayList<>() : new ArrayList<>(instance.arguments.server);
|
|
instance.arguments.jvm = instance.arguments.jvm == null ? new ArrayList<>() : new ArrayList<>(instance.arguments.jvm);
|
|
instance.arguments.client.addAll(args.after(0));
|
|
instance.arguments.server.addAll(args.after(0));
|
|
}
|
|
try {
|
|
Steps.reDownload(instanceDir, Steps.createProcessState());
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
if (server) {
|
|
try {
|
|
InstanceLauncher.launch(instanceDir, instance, InstanceLauncher.LaunchType.Server, restart, AccountManager.NULL_AUTH);
|
|
} catch (InstanceLauncher.LaunchException | IOException e) {
|
|
Inceptum.showError("Could not launch instance", e);
|
|
}
|
|
}
|
|
else {
|
|
AccountManager.loadAccounts();
|
|
InstanceLauncher.launchClient(instanceDir, instance);
|
|
}
|
|
}
|
|
}
|