mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-06-21 00:03:18 +02:00
updated CommandManager.cs to use attributes instead of Classes
This commit is contained in:
parent
4be4ee34cb
commit
838b31cb1f
12
Server/CommandAttribute.cs
Normal file
12
Server/CommandAttribute.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
namespace Server;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
||||||
|
public sealed class CommandAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public CommandAttribute(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
}
|
10
Server/CommandException.cs
Normal file
10
Server/CommandException.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace Server;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
public class CommandException : Exception
|
||||||
|
{
|
||||||
|
public CommandException(string message) : base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
73
Server/CommandLibrary.cs
Normal file
73
Server/CommandLibrary.cs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
using System.Net.Sockets;
|
||||||
|
using DX86;
|
||||||
|
using Server.Commands;
|
||||||
|
|
||||||
|
namespace Server;
|
||||||
|
|
||||||
|
public class CommandLibrary
|
||||||
|
{
|
||||||
|
[Command("example")]
|
||||||
|
public static string ExampleCommand(string[] args, TcpClient? client, TcpServer? socket) =>
|
||||||
|
$"Example command executed with arguments: {string.Join(", ", args)}";
|
||||||
|
|
||||||
|
#region Basic Commands
|
||||||
|
|
||||||
|
[Command("help")]
|
||||||
|
public static string HelpCommand() =>
|
||||||
|
"Available commands:\n" +
|
||||||
|
"1. help - Show this help message\n" +
|
||||||
|
"2. exit - Exit the server\n" +
|
||||||
|
"3. user - User management\n" +
|
||||||
|
"4. settings - Server settings\n" +
|
||||||
|
"5. start - Start the server\n" +
|
||||||
|
"6. stop - Stop the server\n";
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Client Commands
|
||||||
|
|
||||||
|
[Command("login")]
|
||||||
|
public static string LoginCommand(string[] args, TcpClient? client, TcpServer? socket)
|
||||||
|
{
|
||||||
|
if (args.Length < 2)
|
||||||
|
throw new CommandException("Missing arguments: usage is login <username> <password>");
|
||||||
|
|
||||||
|
string username = args[0];
|
||||||
|
string password = args[1];
|
||||||
|
|
||||||
|
if (username == "test" && password == "1234")
|
||||||
|
{
|
||||||
|
if (client != null)
|
||||||
|
{
|
||||||
|
socket?.LoggedInClients.Add(client, username);
|
||||||
|
return "Login successful";
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new CommandException("No client connection detected.");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new CommandException("Invalid username or password.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("logout")]
|
||||||
|
public static string LogoutCommand(TcpClient? client, TcpServer? socket)
|
||||||
|
{
|
||||||
|
if (client != null && socket?.LoggedInClients.ContainsKey(client) == true)
|
||||||
|
{
|
||||||
|
socket.LoggedInClients.Remove(client);
|
||||||
|
return "Logout successful";
|
||||||
|
}
|
||||||
|
throw new CommandException("No client provided or not logged in");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Administration Commands
|
||||||
|
|
||||||
|
[Command("get")]
|
||||||
|
public static string GetCommand(string[] args, TcpClient? client, TcpServer? socket) =>
|
||||||
|
$"not implemented yet, args: {string.Join(", ", args)}";
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
@ -8,7 +8,7 @@ namespace Server;
|
|||||||
|
|
||||||
public class CommandManager
|
public class CommandManager
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, ICommand> _commands = new();
|
private readonly Dictionary<string, MethodInfo> _commands = new();
|
||||||
|
|
||||||
public CommandManager()
|
public CommandManager()
|
||||||
{
|
{
|
||||||
@ -17,38 +17,84 @@ public class CommandManager
|
|||||||
|
|
||||||
private void LoadCommands()
|
private void LoadCommands()
|
||||||
{
|
{
|
||||||
// Get all types implementing ICommand
|
var methods = Assembly.GetExecutingAssembly()
|
||||||
var commandTypes = Assembly.GetExecutingAssembly()
|
|
||||||
.GetTypes()
|
.GetTypes()
|
||||||
.Where(t => typeof(ICommand).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
|
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Static))
|
||||||
|
.Where(m => m.GetCustomAttribute<CommandAttribute>() != null);
|
||||||
|
|
||||||
foreach (var type in commandTypes)
|
foreach (var method in methods)
|
||||||
{
|
{
|
||||||
// Create an instance of the command
|
var attr = method.GetCustomAttribute<CommandAttribute>()!;
|
||||||
if (Activator.CreateInstance(type) is ICommand command)
|
_commands[attr.Name.ToLower()] = method;
|
||||||
{
|
Program.messageSender.Log($"[COMMANDMANAGER] Registered command: {attr.Name}");
|
||||||
// Use the Executor property as the key
|
|
||||||
_commands[command.Executor] = command;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void ExecuteCommand(string executor, TcpClient? client = null, TcpServer? clientSocket = null, string cid = "", params string[] args)
|
public void ExecuteCommand(string executor, TcpClient? client = null, TcpServer? clientSocket = null, string cid = "", params string[] args)
|
||||||
|
{
|
||||||
|
if (_commands.TryGetValue(executor.ToLower(), out var method))
|
||||||
{
|
{
|
||||||
if (_commands.TryGetValue(executor, out var command))
|
try
|
||||||
{
|
{
|
||||||
JsonMessage returnMessage = new JsonMessage();
|
var parameters = method.GetParameters();
|
||||||
returnMessage.cid = cid;
|
var arguments = new object?[parameters.Length];
|
||||||
returnMessage.cmd = command.Exec(args:args, client:client, clientSocket:clientSocket);
|
|
||||||
|
|
||||||
string sendClientMessage = JsonSerializer.Serialize(returnMessage);
|
for (int i = 0; i < parameters.Length; i++)
|
||||||
|
{
|
||||||
|
var param = parameters[i];
|
||||||
|
|
||||||
clientSocket.SendMessageAsync(client:client, message:sendClientMessage+"\n");
|
if (param.ParameterType == typeof(string[]))
|
||||||
//clientSocket.SendMessageAsync(client:client, message:command.Exec(args:args, client:client, clientSocket:clientSocket));
|
arguments[i] = args;
|
||||||
|
else if (param.ParameterType == typeof(TcpClient))
|
||||||
|
arguments[i] = client;
|
||||||
|
else if (param.ParameterType == typeof(TcpServer))
|
||||||
|
arguments[i] = clientSocket;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Program.messageSender.Warn($"[COMMANDMANAGER] Unknown parameter type '{param.ParameterType.Name}' in command '{executor}'. Defaulting to null.");
|
||||||
|
arguments[i] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Program.messageSender.Debug($"[COMMANDMANAGER] Executing command '{executor}'...");
|
||||||
|
string result = method.Invoke(null, arguments)?.ToString() ?? "";
|
||||||
|
|
||||||
|
var returnMessage = new JsonMessage
|
||||||
|
{
|
||||||
|
cid = cid,
|
||||||
|
cmd = result
|
||||||
|
};
|
||||||
|
|
||||||
|
var sendClientMessage = JsonSerializer.Serialize(returnMessage);
|
||||||
|
clientSocket?.SendMessageAsync(client, sendClientMessage + "\n");
|
||||||
|
Program.messageSender.Debug($"[COMMANDMANAGER] Response sent to client.");
|
||||||
|
}
|
||||||
|
catch (TargetInvocationException ex) when (ex.InnerException is CommandException cmdEx)
|
||||||
|
{
|
||||||
|
Program.messageSender.Warn($"[COMMANDMANAGER] Command '{executor}' failed: {cmdEx.Message}");
|
||||||
|
|
||||||
|
var returnMessage = new JsonMessage
|
||||||
|
{
|
||||||
|
cid = cid,
|
||||||
|
cmd = $"Error: {cmdEx.Message}"
|
||||||
|
};
|
||||||
|
|
||||||
|
var sendClientMessage = JsonSerializer.Serialize(returnMessage);
|
||||||
|
clientSocket?.SendMessageAsync(client, sendClientMessage + "\n");
|
||||||
|
}
|
||||||
|
catch (TargetInvocationException ex)
|
||||||
|
{
|
||||||
|
var actualError = ex.InnerException?.Message ?? ex.Message;
|
||||||
|
Program.messageSender.Error($"[COMMANDMANAGER] Unexpected error in command '{executor}': {actualError}");
|
||||||
|
Program.messageSender.Debug($"[COMMANDMANAGER] Stack Trace: {ex.InnerException?.StackTrace ?? ex.StackTrace}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Program.messageSender.Error($"[COMMANDMANAGER] Failed to invoke command '{executor}': {ex.Message}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Program.messageSender.Log($"[COMMANDMANAGER] Command '{executor}' not found.");
|
Program.messageSender.Warn($"[COMMANDMANAGER] Unknown command '{executor}' received.");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user