Skip to content

Commit

Permalink
Remake some cli stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Jan 9, 2025
1 parent 1484b30 commit f54e2f7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 127 deletions.
109 changes: 25 additions & 84 deletions client/src/main/java/com/soulfiremc/client/ClientCommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandSource> {
@Getter
private final CommandDispatcher<CommandSource> 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<String> 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();
}
}
21 changes: 17 additions & 4 deletions client/src/main/java/com/soulfiremc/client/cli/CLIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,6 +82,7 @@ public class SFCommandDefinition implements Callable<Integer> {
hidden = true)
private boolean generateFlags;

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public Integer call() {
if (generateFlags) {
Expand Down Expand Up @@ -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();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f54e2f7

Please sign in to comment.