mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2025-12-12 22:44:36 +01:00
renames
This commit is contained in:
@@ -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)!;
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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" };
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public static class InputSystem
|
|||||||
|
|
||||||
if (string.IsNullOrEmpty(command)) continue;
|
if (string.IsNullOrEmpty(command)) continue;
|
||||||
|
|
||||||
CommandHandler.HandleCommand(command);
|
CommandHandlerFactory.HandleCommand(command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user