Command parser that probably works

This commit is contained in:
Kyle873
2023-05-29 11:17:34 -04:00
parent 6a090b2917
commit ca558f76bd
4 changed files with 101 additions and 3 deletions

View File

@@ -0,0 +1,61 @@
using Common.Utils;
using System.Reflection;
namespace PemukulPaku.GameServer.Commands
{
public abstract class Command
{
public static readonly Logger c = new("Command", ConsoleColor.Cyan);
public string Name = string.Empty;
public virtual void Run(Session? session, string[] args)
{
}
}
[AttributeUsage(AttributeTargets.Class)]
public class CommandHandler : Attribute
{
public string Name { get; }
public CommandHandler(string name)
{
Name = name;
}
}
public static class CommandFactory
{
public static readonly List<Command> Commands = new();
static readonly Logger c = new("Factory", ConsoleColor.Yellow);
public static void LoadCommandHandlers()
{
c.Log("Loading Command Handlers...");
IEnumerable<Type> classes = from t in Assembly.GetExecutingAssembly().GetTypes()
select t;
foreach ((Type t, CommandHandler attr) in from Type? t in classes.ToList()
let attrs = (Attribute[])t.GetCustomAttributes(typeof(CommandHandler), false)
where attrs.Length > 0
let attr = (CommandHandler)attrs[0]
select (t, attr))
{
Command? cmd = (Command)Activator.CreateInstance(t)!;
if (cmd is not null)
{
cmd.Name = attr.Name;
Commands.Add(cmd);
#if DEBUG
c.Log($"Loaded Command Handler {t.Name} for Command \"{cmd.Name}\"");
#endif
}
}
c.Log("Finished loading Commands");
}
}
}

View File

@@ -0,0 +1,19 @@
using Common;
namespace PemukulPaku.GameServer.Commands
{
[CommandHandler("test")]
public class TestCommand : Command
{
public override void Run(Session? session, string[] args)
{
if (args.Length < 3)
{
c.Error("Not enough arguments");
return;
}
c.Log($"Testing {args[0]} {args[1]} {args[2]}");
}
}
}

View File

@@ -1,6 +1,5 @@
using System.Buffers.Binary;
using Common.Resources.Proto;
using Newtonsoft.Json;
using Common.Utils;
using ProtoBuf;
using System.Reflection;

View File

@@ -3,7 +3,7 @@ using System.Net.NetworkInformation;
using PemukulPaku.GameServer;
using Common.Database;
using PemukulPaku.GameServer.Game;
using Common.Utils.ExcelReader;
using PemukulPaku.GameServer.Commands;
namespace PemukulPaku
{
@@ -18,13 +18,32 @@ namespace PemukulPaku
Global.config.Gameserver.Host = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback && i.OperationalStatus == OperationalStatus.Up).First().GetIPProperties().UnicastAddresses.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First().Address.ToString();
CommandFactory.LoadCommandHandlers();
PacketFactory.LoadPacketHandlers();
new Thread(HttpServer.Program.Main).Start();
_ = Server.GetInstance();
Player Player = new(User.FromName("test"));
Console.Read();
while (true)
{
string? line = Console.ReadLine();
if (!string.IsNullOrEmpty(line))
{
foreach (Command cmd in CommandFactory.Commands)
{
if (line.StartsWith(cmd.Name.ToLower()))
{
List<string> args = line.Split(' ').ToList();
args.RemoveAt(0);
cmd.Run(null, args.ToArray());
break;
}
}
}
}
}
}
}