Skip to content

Commit 611d909

Browse files
committed
feat(commands): IsCommandRegistered
1 parent a9cb081 commit 611d909

File tree

8 files changed

+53
-0
lines changed

8 files changed

+53
-0
lines changed

managed/src/SwiftlyS2.Core/Modules/Commands/CommandService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public void UnregisterCommand( string commandName )
8686
}
8787
}
8888

89+
public bool IsCommandRegistered( string commandName )
90+
{
91+
return NativeCommands.IsCommandRegistered(commandName);
92+
}
93+
8994
public Guid HookClientCommand( ICommandService.ClientCommandHandler handler )
9095
{
9196
var callback = new ClientCommandListenerCallback(handler, loggerFactory, profiler);

managed/src/SwiftlyS2.Generated/Natives/Commands.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ public unsafe static void UnregisterCommand(ulong callbackID) {
5353
_UnregisterCommand(callbackID);
5454
}
5555

56+
private unsafe static delegate* unmanaged<byte*, byte> _IsCommandRegistered;
57+
58+
public unsafe static bool IsCommandRegistered(string commandName) {
59+
var pool = ArrayPool<byte>.Shared;
60+
var commandNameLength = Encoding.UTF8.GetByteCount(commandName);
61+
var commandNameBuffer = pool.Rent(commandNameLength + 1);
62+
Encoding.UTF8.GetBytes(commandName, commandNameBuffer);
63+
commandNameBuffer[commandNameLength] = 0;
64+
fixed (byte* commandNameBufferPtr = commandNameBuffer) {
65+
var ret = _IsCommandRegistered(commandNameBufferPtr);
66+
pool.Return(commandNameBuffer);
67+
return ret == 1;
68+
}
69+
}
70+
5671
private unsafe static delegate* unmanaged<byte*, byte*, byte, ulong> _RegisterAlias;
5772

5873
/// <summary>

managed/src/SwiftlyS2.Shared/Modules/Commands/ICommandService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public interface ICommandService
5757
/// <param name="commandName">The command name.</param>
5858
public void UnregisterCommand( string commandName );
5959

60+
/// <summary>
61+
/// Checks if a command is registered.
62+
/// </summary>
63+
/// <param name="commandName">The command name.</param>
64+
/// <returns>Whether the command is registered.</returns>
65+
public bool IsCommandRegistered( string commandName );
66+
6067
/// <summary>
6168
/// Hooks client commands, will be fired when a player sends any command.
6269
/// </summary>

natives/server/commands.native

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Commands
33
int32 HandleCommandForPlayer = int32 playerid, string command // 1 -> not silent, 2 -> silent, -1 -> invalid player, 0 -> no command
44
uint64 RegisterCommand = string commandName, ptr callback, bool registerRaw // callback should receive (int32 playerid, string arguments_list (separated by \x01), string commandName, string prefix, bool silent), if registerRaw is false, it will not put "sw_" before the command name
55
void UnregisterCommand = uint64 callbackID
6+
bool IsCommandRegistered = string commandName
67
uint64 RegisterAlias = string aliasName, string commandName, bool registerRaw // registerRaw behaves the same as on RegisterCommand, for commandName you need to also put the "sw_" prefix if the command is registered without raw mode
78
void UnregisterAlias = uint64 callbackID
89
uint64 RegisterClientCommandsListener = ptr callback // callback should receive: int32 playerid, string commandline, return true -> ignored, return false -> supercede

src/api/server/commands/manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class IServerCommands
3737
// playerid, args, command_name, prefix, silent
3838
virtual uint64_t RegisterCommand(std::string command_name, std::function<void(int, std::vector<std::string>, std::string, std::string, bool)> handler, bool registerRaw) = 0;
3939
virtual void UnregisterCommand(uint64_t command_id) = 0;
40+
virtual bool IsCommandRegistered(std::string command_name) = 0;
4041

4142
virtual uint64_t RegisterAlias(std::string alias_command, std::string command_name, bool registerRaw) = 0;
4243
virtual void UnregisterAlias(uint64_t alias_id) = 0;

src/scripting/server/commands.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ void Bridge_Commands_UnregisterCommand(uint64_t callbackID)
6565
servercommands->UnregisterCommand(callbackID);
6666
}
6767

68+
uint8_t Bridge_Commands_IsCommandRegistered(const char* commandName)
69+
{
70+
auto servercommands = g_ifaceService.FetchInterface<IServerCommands>(SERVERCOMMANDS_INTERFACE_VERSION);
71+
return servercommands->IsCommandRegistered(commandName) ? 1 : 0;
72+
}
73+
6874
uint64_t Bridge_Commands_RegisterAlias(const char* alias, const char* command, bool registerRaw)
6975
{
7076
auto servercommands = g_ifaceService.FetchInterface<IServerCommands>(SERVERCOMMANDS_INTERFACE_VERSION);

src/server/commands/manager.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,23 @@ void CServerCommands::UnregisterCommand(uint64_t commandId)
318318
delete conCommand;
319319
}
320320

321+
bool CServerCommands::IsCommandRegistered(std::string commandName)
322+
{
323+
std::transform(commandName.begin(), commandName.end(), commandName.begin(), ::tolower);
324+
if (commandHandlers.contains(commandName))
325+
{
326+
return true;
327+
}
328+
329+
commandName = "sw_" + commandName;
330+
if (commandHandlers.contains(commandName))
331+
{
332+
return true;
333+
}
334+
335+
return false;
336+
}
337+
321338
uint64_t CServerCommands::RegisterAlias(std::string aliasCommand, std::string commandName, bool registerRaw)
322339
{
323340
std::transform(commandName.begin(), commandName.end(), commandName.begin(), ::tolower);

src/server/commands/manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class CServerCommands : public IServerCommands
3535
// playerid, args, command_name, prefix, silent
3636
virtual uint64_t RegisterCommand(std::string command_name, std::function<void(int, std::vector<std::string>, std::string, std::string, bool)> handler, bool registerRaw) override;
3737
virtual void UnregisterCommand(uint64_t command_id) override;
38+
virtual bool IsCommandRegistered(std::string command_name) override;
3839

3940
virtual uint64_t RegisterAlias(std::string alias_command, std::string command_name, bool registerRaw) override;
4041
virtual void UnregisterAlias(uint64_t alias_id) override;

0 commit comments

Comments
 (0)