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;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class commandHandler : Attribute
public class CommandHandler : Attribute
{
public string Name { get; }
public string Description { get; }
public string Example { get; }
public commandHandler(string commandName, string description, string example)
public CommandHandler(string commandName, string description, string example)
{
Name = commandName;
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>();
static readonly Dictionary<string, Action<Dictionary<string, string>>> commandFunctions;
static CommandHandler()
static CommandHandlerFactory()
{
commandFunctions = new Dictionary<string, Action<Dictionary<string, string>>>(StringComparer.OrdinalIgnoreCase);
@@ -83,11 +83,11 @@ public static class CommandHandler
public static void RegisterCommands(Assembly assembly)
{
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)
{
var commandAttribute = (commandHandler?)Attribute.GetCustomAttribute(commandType, typeof(commandHandler));
var commandAttribute = (CommandHandler?)Attribute.GetCustomAttribute(commandType, typeof(CommandHandler));
if (commandAttribute != null)
{
var commandInstance = (Command)Activator.CreateInstance(commandType)!;

View File

@@ -3,10 +3,10 @@ using System.Text;
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
{
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)
{
@@ -15,11 +15,11 @@ public class HelpCommand : Command
StringBuilder sb = new StringBuilder();
sb.AppendLine("Available Commands: ");
foreach (var command in CommandHandler.Commands)
foreach (var command in CommandHandlerFactory.Commands)
{
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;
}

View File

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

View File

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