From f54e2f76c5044bcc39f7c2907c701184befa6497 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Thu, 9 Jan 2025 20:55:55 +0100 Subject: [PATCH] Remake some cli stuff --- .../client/ClientCommandManager.java | 109 ++++-------------- .../com/soulfiremc/client/cli/CLIManager.java | 21 +++- .../client/cli/SFCommandDefinition.java | 12 +- .../brigadier/ClientConsoleCommandSource.java | 34 ------ .../server/ServerCommandManager.java | 40 +++++++ .../brigadier/ServerConsoleCommandSource.java | 14 ++- 6 files changed, 103 insertions(+), 127 deletions(-) delete mode 100644 server/src/main/java/com/soulfiremc/brigadier/ClientConsoleCommandSource.java diff --git a/client/src/main/java/com/soulfiremc/client/ClientCommandManager.java b/client/src/main/java/com/soulfiremc/client/ClientCommandManager.java index 8466b112a..e68886059 100644 --- a/client/src/main/java/com/soulfiremc/client/ClientCommandManager.java +++ b/client/src/main/java/com/soulfiremc/client/ClientCommandManager.java @@ -17,109 +17,50 @@ */ package com.soulfiremc.client; -import com.mojang.brigadier.Command; -import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.soulfiremc.brigadier.ClientConsoleCommandSource; import com.soulfiremc.brigadier.CommandSource; import com.soulfiremc.brigadier.PlatformCommandManager; +import com.soulfiremc.client.cli.CLIManager; import com.soulfiremc.client.grpc.RPCClient; -import com.soulfiremc.client.settings.ClientSettingsManager; -import com.soulfiremc.grpc.generated.*; -import lombok.Getter; +import com.soulfiremc.grpc.generated.CommandCompletionRequest; +import com.soulfiremc.grpc.generated.CommandRequest; +import com.soulfiremc.grpc.generated.InstanceCommandScope; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.List; -import java.util.UUID; - -import static com.soulfiremc.client.ClientBrigadierHelper.help; -import static com.soulfiremc.client.ClientBrigadierHelper.literal; @Slf4j @RequiredArgsConstructor(onConstructor_ = @Inject) public class ClientCommandManager implements PlatformCommandManager { - @Getter - private final CommandDispatcher dispatcher = new CommandDispatcher<>(); private final RPCClient rpcClient; - private final ClientSettingsManager clientSettingsManager; - - @SuppressWarnings("ResultOfMethodCallIgnored") - @PostConstruct - public void postConstruct() { - dispatcher.register( - literal("start-attack") - .executes( - help( - "Start a attack using the current settings", - c -> { - var cliInstance = rpcClient.instanceStubBlocking() - .listInstances(InstanceListRequest.newBuilder().build()) - .getInstancesList() - .stream() - .filter(instance -> instance.getFriendlyName().equals("cli-attack")) - .map(InstanceListResponse.Instance::getId) - .map(UUID::fromString) - .findFirst(); - - var cliInstanceId = cliInstance.orElseGet(() -> UUID.fromString(rpcClient.instanceStubBlocking() - .createInstance( - InstanceCreateRequest.newBuilder().setFriendlyName("cli-attack").build()) - .getId())); - - rpcClient.instanceStubBlocking().updateInstanceConfig(InstanceUpdateConfigRequest.newBuilder() - .setId(cliInstanceId.toString()) - .setConfig(clientSettingsManager.exportSettingsProto(cliInstanceId)) - .build()); - - rpcClient.instanceStubBlocking().changeInstanceState(InstanceStateChangeRequest.newBuilder() - .setId(cliInstanceId.toString()) - .setState(InstanceState.RUNNING) - .build()); - - log.info("Attack started"); - - return Command.SINGLE_SUCCESS; - }))); - } + private final CLIManager cliManager; @Override public int execute(String command, CommandSource source) { - try { - if (isClientCommand(command)) { - log.debug("Executing command {} on client", command); - return dispatcher.execute(command, new ClientConsoleCommandSource()); - } else { - log.debug("Executing command {} on server", command); - return rpcClient - .commandStubBlocking() - .executeCommand(CommandRequest.newBuilder().setCommand(command).build()) - .getCode(); - } - } catch (CommandSyntaxException e) { - log.error("An error occurred while trying to execute a command.", e); - return 1; - } - } - - private boolean isClientCommand(String command) { - var spaceIndex = command.indexOf(' '); - var commandName = spaceIndex == -1 ? command : command.substring(0, spaceIndex); - return dispatcher.getRoot().getChild(commandName) != null; + log.debug("Executing command {} on server", command); + return rpcClient + .commandStubBlocking() + .executeCommand(CommandRequest.newBuilder() + .setInstance(InstanceCommandScope.newBuilder() + .setInstanceId(cliManager.cliInstanceId().toString()) + .build()) + .setCommand(command) + .build()) + .getCode(); } @Override public List getCompletionSuggestions(String command, CommandSource source) { - try { - return rpcClient - .commandStubBlocking() - .tabCompleteCommand(CommandCompletionRequest.newBuilder().setCommand(command).build()) - .getSuggestionsList(); - } catch (Exception e) { - log.error("An error occurred while trying to perform tab completion.", e); - return List.of(); - } + log.debug("Getting completion suggestions for command {} on server", command); + return rpcClient + .commandStubBlocking() + .tabCompleteCommand(CommandCompletionRequest.newBuilder() + .setInstance(InstanceCommandScope.newBuilder() + .setInstanceId(cliManager.cliInstanceId().toString()) + .build()) + .setCommand(command) + .build()) + .getSuggestionsList(); } } diff --git a/client/src/main/java/com/soulfiremc/client/cli/CLIManager.java b/client/src/main/java/com/soulfiremc/client/cli/CLIManager.java index bf2b5b504..c87f54894 100644 --- a/client/src/main/java/com/soulfiremc/client/cli/CLIManager.java +++ b/client/src/main/java/com/soulfiremc/client/cli/CLIManager.java @@ -27,10 +27,7 @@ import com.soulfiremc.client.ClientCommandManager; import com.soulfiremc.client.grpc.RPCClient; import com.soulfiremc.client.settings.ClientSettingsManager; -import com.soulfiremc.grpc.generated.ClientDataRequest; -import com.soulfiremc.grpc.generated.ComboOption; -import com.soulfiremc.grpc.generated.DoubleSetting; -import com.soulfiremc.grpc.generated.IntSetting; +import com.soulfiremc.grpc.generated.*; import com.soulfiremc.server.settings.PropertyKey; import com.soulfiremc.server.util.SFPathConstants; import com.soulfiremc.server.util.structs.CommandHistoryManager; @@ -40,6 +37,7 @@ import org.pf4j.PluginManager; import picocli.CommandLine; +import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; @@ -56,6 +54,7 @@ public class CLIManager { private final ExecutorService threadPool = Executors.newCachedThreadPool(); private final ShutdownManager shutdownManager; private final ClientSettingsManager clientSettingsManager; + private UUID cliInstanceId; public CLIManager(RPCClient rpcClient, PluginManager pluginManager) { injector.register(CLIManager.class, this); @@ -74,6 +73,20 @@ private static String escapeFormatSpecifiers(String input) { } public void initCLI(String[] args) { + var cliInstance = rpcClient.instanceStubBlocking() + .listInstances(InstanceListRequest.newBuilder().build()) + .getInstancesList() + .stream() + .filter(instance -> instance.getFriendlyName().equals("cli-attack")) + .map(InstanceListResponse.Instance::getId) + .map(UUID::fromString) + .findFirst(); + + cliInstanceId = cliInstance.orElseGet(() -> UUID.fromString(rpcClient.instanceStubBlocking() + .createInstance( + InstanceCreateRequest.newBuilder().setFriendlyName("cli-attack").build()) + .getId())); + var soulFireCommand = new SFCommandDefinition(this); var commandLine = new CommandLine(soulFireCommand); soulFireCommand.commandLine(commandLine); diff --git a/client/src/main/java/com/soulfiremc/client/cli/SFCommandDefinition.java b/client/src/main/java/com/soulfiremc/client/cli/SFCommandDefinition.java index 8a29aed49..3e09dee53 100644 --- a/client/src/main/java/com/soulfiremc/client/cli/SFCommandDefinition.java +++ b/client/src/main/java/com/soulfiremc/client/cli/SFCommandDefinition.java @@ -17,9 +17,9 @@ */ package com.soulfiremc.client.cli; -import com.soulfiremc.brigadier.ClientConsoleCommandSource; import com.soulfiremc.brigadier.GenericTerminalConsole; import com.soulfiremc.builddata.BuildData; +import com.soulfiremc.grpc.generated.InstanceUpdateConfigRequest; import com.soulfiremc.server.account.AuthType; import com.soulfiremc.server.proxy.ProxyType; import lombok.Getter; @@ -82,6 +82,7 @@ public class SFCommandDefinition implements Callable { hidden = true) private boolean generateFlags; + @SuppressWarnings("ResultOfMethodCallIgnored") @Override public Integer call() { if (generateFlags) { @@ -121,14 +122,19 @@ public Integer call() { cliManager.clientSettingsManager().commandDefinition(this); + cliManager.rpcClient().instanceStubBlocking().updateInstanceConfig(InstanceUpdateConfigRequest.newBuilder() + .setId(cliManager.cliInstanceId().toString()) + .setConfig(cliManager.clientSettingsManager().exportSettingsProto(cliManager.cliInstanceId())) + .build()); + if (start) { - cliManager.clientCommandManager().execute("start-attack", new ClientConsoleCommandSource()); + cliManager.clientCommandManager().execute("start-attack", null); } else { log.info( "SoulFire is ready to go! Type 'start-attack' to start the attack! (Use --start to start automatically)"); } - new GenericTerminalConsole<>(cliManager.shutdownManager(), ClientConsoleCommandSource.INSTANCE, + new GenericTerminalConsole<>(cliManager.shutdownManager(), null, cliManager.clientCommandManager(), cliManager.commandHistoryManager()) .start(); diff --git a/server/src/main/java/com/soulfiremc/brigadier/ClientConsoleCommandSource.java b/server/src/main/java/com/soulfiremc/brigadier/ClientConsoleCommandSource.java deleted file mode 100644 index 7b7c29bd1..000000000 --- a/server/src/main/java/com/soulfiremc/brigadier/ClientConsoleCommandSource.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SoulFire - * Copyright (C) 2024 AlexProgrammerDE - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.soulfiremc.brigadier; - -import com.soulfiremc.server.plugins.ChatMessageLogger; -import net.kyori.adventure.text.Component; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.event.Level; - -public class ClientConsoleCommandSource implements CommandSource { - public static final ClientConsoleCommandSource INSTANCE = new ClientConsoleCommandSource(); - private static final Logger log = LoggerFactory.getLogger("Console"); - - @Override - public void sendMessage(Level level, Component message) { - log.atLevel(level).setMessage("{}").addArgument(() -> ChatMessageLogger.ANSI_MESSAGE_SERIALIZER.serialize(message)).log(); - } -} diff --git a/server/src/main/java/com/soulfiremc/server/ServerCommandManager.java b/server/src/main/java/com/soulfiremc/server/ServerCommandManager.java index 6a09e675e..0d9826394 100644 --- a/server/src/main/java/com/soulfiremc/server/ServerCommandManager.java +++ b/server/src/main/java/com/soulfiremc/server/ServerCommandManager.java @@ -28,6 +28,7 @@ import com.soulfiremc.brigadier.CommandHelpWrapper; import com.soulfiremc.brigadier.PlatformCommandManager; import com.soulfiremc.brigadier.RedirectHelpWrapper; +import com.soulfiremc.server.api.AttackLifecycle; import com.soulfiremc.server.api.InternalPlugin; import com.soulfiremc.server.api.SoulFireAPI; import com.soulfiremc.server.api.event.lifecycle.CommandManagerInitEvent; @@ -464,6 +465,45 @@ public boolean isDone() { c.getSource().sendWarn("No task was running!"); } + return Command.SINGLE_SUCCESS; + })))); + dispatcher.register( + literal("start-attack") + .executes( + help( + "Makes selected instances start an attack", + c -> + forEveryAttack( + c, + attack -> { + attack.switchToState(AttackLifecycle.RUNNING); + + return Command.SINGLE_SUCCESS; + })))); + dispatcher.register( + literal("pause-attack") + .executes( + help( + "Makes selected instances pause their", + c -> + forEveryAttack( + c, + attack -> { + attack.switchToState(AttackLifecycle.PAUSED); + + return Command.SINGLE_SUCCESS; + })))); + dispatcher.register( + literal("stop-attack") + .executes( + help( + "Makes selected instances stop their attack", + c -> + forEveryAttack( + c, + attack -> { + attack.switchToState(AttackLifecycle.STOPPED); + return Command.SINGLE_SUCCESS; })))); diff --git a/server/src/main/java/com/soulfiremc/server/brigadier/ServerConsoleCommandSource.java b/server/src/main/java/com/soulfiremc/server/brigadier/ServerConsoleCommandSource.java index 4bc1e82c9..426bcbaf7 100644 --- a/server/src/main/java/com/soulfiremc/server/brigadier/ServerConsoleCommandSource.java +++ b/server/src/main/java/com/soulfiremc/server/brigadier/ServerConsoleCommandSource.java @@ -17,13 +17,23 @@ */ package com.soulfiremc.server.brigadier; -import com.soulfiremc.brigadier.ClientConsoleCommandSource; +import com.soulfiremc.server.plugins.ChatMessageLogger; import com.soulfiremc.server.user.PermissionContext; import com.soulfiremc.server.user.ServerCommandSource; +import net.kyori.adventure.text.Component; import net.kyori.adventure.util.TriState; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.event.Level; -public class ServerConsoleCommandSource extends ClientConsoleCommandSource implements ServerCommandSource { +public class ServerConsoleCommandSource implements ServerCommandSource { public static final ServerConsoleCommandSource INSTANCE = new ServerConsoleCommandSource(); + private static final Logger log = LoggerFactory.getLogger("Console"); + + @Override + public void sendMessage(Level level, Component message) { + log.atLevel(level).setMessage("{}").addArgument(() -> ChatMessageLogger.ANSI_MESSAGE_SERIALIZER.serialize(message)).log(); + } @Override public TriState getPermission(PermissionContext permission) {