mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2025-12-14 07:24:50 +01:00
extending command for user and prevent err in build ship menu
This commit is contained in:
@@ -30,8 +30,30 @@ public class ArgumentAttribute : Attribute
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum CommandUsage
|
||||
{
|
||||
None = 0,
|
||||
Console = 1,
|
||||
User = 2
|
||||
}
|
||||
|
||||
public abstract class Command
|
||||
{
|
||||
public virtual CommandUsage Usage
|
||||
{
|
||||
get
|
||||
{
|
||||
var usage = CommandUsage.None;
|
||||
if (GetType().GetMethod(nameof(Execute), [typeof(Dictionary<string, string>), typeof(Connection)])?.DeclaringType == GetType())
|
||||
usage |= CommandUsage.User;
|
||||
if (GetType().GetMethod(nameof(Execute), [typeof(Dictionary<string, string>)])?.DeclaringType == GetType())
|
||||
usage |= CommandUsage.Console;
|
||||
|
||||
return usage;
|
||||
}
|
||||
}
|
||||
|
||||
readonly Dictionary<string, PropertyInfo> argsProperties;
|
||||
|
||||
public Command()
|
||||
@@ -50,6 +72,11 @@ public abstract class Command
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Execute(Dictionary<string, string> args, Connection connection)
|
||||
{
|
||||
Execute(args);
|
||||
}
|
||||
|
||||
protected T Parse<T>(string? value, T fallback = default!)
|
||||
{
|
||||
var tryParseMethod = typeof(T).GetMethod("TryParse", [typeof(string), typeof(T).MakeByRefType()]);
|
||||
@@ -72,10 +99,12 @@ public static class CommandHandlerFactory
|
||||
public static readonly List<Command> Commands = new List<Command>();
|
||||
|
||||
static readonly Dictionary<string, Action<Dictionary<string, string>>> commandFunctions;
|
||||
static readonly Dictionary<string, Action<Dictionary<string, string>, Connection>> commandFunctionsConn;
|
||||
|
||||
static CommandHandlerFactory()
|
||||
{
|
||||
commandFunctions = new Dictionary<string, Action<Dictionary<string, string>>>(StringComparer.OrdinalIgnoreCase);
|
||||
commandFunctionsConn = new Dictionary<string, Action<Dictionary<string, string>, Connection>>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
RegisterCommands(Assembly.GetExecutingAssembly());
|
||||
}
|
||||
@@ -91,24 +120,23 @@ public static class CommandHandlerFactory
|
||||
if (commandAttribute != null)
|
||||
{
|
||||
var commandInstance = (Command)Activator.CreateInstance(commandType)!;
|
||||
commandFunctions[commandAttribute.Name] = commandInstance.Execute;
|
||||
|
||||
if (commandInstance.Usage.HasFlag(CommandUsage.Console))
|
||||
commandFunctions[commandAttribute.Name] = commandInstance.Execute;
|
||||
if (commandInstance.Usage.HasFlag(CommandUsage.User))
|
||||
commandFunctionsConn[commandAttribute.Name] = commandInstance.Execute;
|
||||
|
||||
Commands.Add(commandInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void HandleCommand(string commandLine)
|
||||
public static void HandleCommand(string commandLine, Connection? connection = null)
|
||||
{
|
||||
var parts = commandLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length == 0)
|
||||
return;
|
||||
|
||||
if (!commandFunctions.TryGetValue(parts[0], out var command))
|
||||
{
|
||||
Logger.c.Warn($"Unknown command: {parts[0]}");
|
||||
return;
|
||||
}
|
||||
|
||||
var arguments = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
for (var i = 1; i < parts.Length; i++)
|
||||
{
|
||||
@@ -117,6 +145,25 @@ public static class CommandHandlerFactory
|
||||
arguments[argParts[0]] = argParts[1];
|
||||
}
|
||||
|
||||
command(arguments);
|
||||
if (connection is not null)
|
||||
{
|
||||
if (!(commandFunctionsConn).TryGetValue(parts[0], out var command))
|
||||
{
|
||||
Logger.c.Warn($"Unknown command: {parts[0]}");
|
||||
return;
|
||||
}
|
||||
|
||||
command(arguments, connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(commandFunctions).TryGetValue(parts[0], out var command))
|
||||
{
|
||||
Logger.c.Warn($"Unknown command: {parts[0]}");
|
||||
return;
|
||||
}
|
||||
|
||||
command(arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user