This commit is contained in:
rfi
2024-02-22 00:25:19 +07:00
parent 9b6673138e
commit b232088f4d
8 changed files with 18 additions and 18 deletions

View File

@@ -4,13 +4,13 @@ using System.Reflection;
namespace BLHX.Server.Game.Commands; namespace BLHX.Server.Game.Commands;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class commandHandler : Attribute public class CommandHandler : Attribute
{ {
public string Name { get; } public string Name { get; }
public string Description { get; } public string Description { get; }
public string Example { get; } public string Example { get; }
public commandHandler(string commandName, string description, string example) public CommandHandler(string commandName, string description, string example)
{ {
Name = commandName; Name = commandName;
Description = description; Description = description;
@@ -67,13 +67,13 @@ public abstract class Command
} }
} }
public static class CommandHandler public static class CommandHandlerFactory
{ {
public static readonly List<Command> Commands = new List<Command>(); 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>>> commandFunctions;
static CommandHandler() static CommandHandlerFactory()
{ {
commandFunctions = new Dictionary<string, Action<Dictionary<string, string>>>(StringComparer.OrdinalIgnoreCase); commandFunctions = new Dictionary<string, Action<Dictionary<string, string>>>(StringComparer.OrdinalIgnoreCase);
@@ -83,11 +83,11 @@ public static class CommandHandler
public static void RegisterCommands(Assembly assembly) public static void RegisterCommands(Assembly assembly)
{ {
var commandTypes = assembly.GetTypes() var commandTypes = assembly.GetTypes()
.Where(t => Attribute.IsDefined(t, typeof(commandHandler)) && typeof(Command).IsAssignableFrom(t)); .Where(t => Attribute.IsDefined(t, typeof(CommandHandler)) && typeof(Command).IsAssignableFrom(t));
foreach (var commandType in commandTypes) foreach (var commandType in commandTypes)
{ {
var commandAttribute = (commandHandler?)Attribute.GetCustomAttribute(commandType, typeof(commandHandler)); var commandAttribute = (CommandHandler?)Attribute.GetCustomAttribute(commandType, typeof(CommandHandler));
if (commandAttribute != null) if (commandAttribute != null)
{ {
var commandInstance = (Command)Activator.CreateInstance(commandType)!; var commandInstance = (Command)Activator.CreateInstance(commandType)!;

View File

@@ -3,10 +3,10 @@ using System.Text;
namespace BLHX.Server.Game.Commands; namespace BLHX.Server.Game.Commands;
[commandHandler("help", "Print out all commands with their description and example", "help")] [CommandHandler("help", "Print out all commands with their description and example", "help")]
public class HelpCommand : Command public class HelpCommand : Command
{ {
static readonly Dictionary<Type, commandHandler?> commandAttributes = new Dictionary<Type, commandHandler?>(); static readonly Dictionary<Type, CommandHandler?> commandAttributes = new Dictionary<Type, CommandHandler?>();
public override void Execute(Dictionary<string, string> args) public override void Execute(Dictionary<string, string> args)
{ {
@@ -15,11 +15,11 @@ public class HelpCommand : Command
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.AppendLine("Available Commands: "); sb.AppendLine("Available Commands: ");
foreach (var command in CommandHandler.Commands) foreach (var command in CommandHandlerFactory.Commands)
{ {
if (!commandAttributes.TryGetValue(command.GetType(), out var attr)) if (!commandAttributes.TryGetValue(command.GetType(), out var attr))
{ {
attr = command.GetType().GetCustomAttribute(typeof(commandHandler)) as commandHandler; attr = command.GetType().GetCustomAttribute(typeof(CommandHandler)) as CommandHandler;
commandAttributes[command.GetType()] = attr; commandAttributes[command.GetType()] = attr;
} }

View File

@@ -2,7 +2,7 @@
namespace BLHX.Server.Game.Commands; namespace BLHX.Server.Game.Commands;
[commandHandler("save", "Save the current state", "save")] [CommandHandler("save", "Save the current state", "save")]
public class SaveCommand : Command public class SaveCommand : Command
{ {
public override void Execute(Dictionary<string, string> args) public override void Execute(Dictionary<string, string> args)

View File

@@ -3,7 +3,7 @@ using BLHX.Server.Common.Utils;
namespace BLHX.Server.Game.Commands; namespace BLHX.Server.Game.Commands;
[commandHandler("test", "Test command", "test type=gacha")] [CommandHandler("test", "Test command", "test type=gacha")]
public class TestCommand : Command public class TestCommand : Command
{ {
static readonly string[] RarityStrings = { "Unknown", "Unused", "Normal", "Rare", "Elite", "SSR", "UR" }; static readonly string[] RarityStrings = { "Unknown", "Unused", "Normal", "Rare", "Elite", "SSR", "UR" };

View File

@@ -68,7 +68,7 @@ namespace BLHX.Server.Game
var packet = new Packet(buf[readLen..]); var packet = new Packet(buf[readLen..]);
c.Log(packet.command.ToString()); c.Log(packet.command.ToString());
(PacketHandlerDelegate? handler, PacketHandlerAttribute? attr) = PacketFactory.GetPacketHandler(packet.command); (PacketHandlerDelegate? handler, PacketHandlerAttribute? attr) = PacketHandlerFactory.GetPacketHandler(packet.command);
if (handler is not null && attr is not null) if (handler is not null && attr is not null)
{ {
handler(this, packet); handler(this, packet);

View File

@@ -17,7 +17,7 @@ namespace BLHX.Server.Game
static GameServer() static GameServer()
{ {
// Preload // Preload
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(PacketFactory).TypeHandle); System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(PacketHandlerFactory).TypeHandle);
EndPoint = new(IPAddress.Any, (int)Config.Instance.Port); EndPoint = new(IPAddress.Any, (int)Config.Instance.Port);
listener = new TcpListener(EndPoint); listener = new TcpListener(EndPoint);

View File

@@ -12,7 +12,7 @@ public static class InputSystem
if (string.IsNullOrEmpty(command)) continue; if (string.IsNullOrEmpty(command)) continue;
CommandHandler.HandleCommand(command); CommandHandlerFactory.HandleCommand(command);
} }
} }
} }

View File

@@ -29,12 +29,12 @@ namespace BLHX.Server.Game
public readonly T Decode<T>() where T : IExtensible => Serializer.Deserialize<T>(bytes.AsSpan()); public readonly T Decode<T>() where T : IExtensible => Serializer.Deserialize<T>(bytes.AsSpan());
} }
static class PacketFactory static class PacketHandlerFactory
{ {
static readonly Logger c = new(nameof(PacketFactory), ConsoleColor.DarkGreen); static readonly Logger c = new(nameof(PacketHandlerFactory), ConsoleColor.DarkGreen);
static readonly Dictionary<Command, (PacketHandlerDelegate, PacketHandlerAttribute)> handlers = []; static readonly Dictionary<Command, (PacketHandlerDelegate, PacketHandlerAttribute)> handlers = [];
static PacketFactory() static PacketHandlerFactory()
{ {
LoadPacketHandlers(); LoadPacketHandlers();
} }