diff --git a/GameServer/Commands/Command.cs b/GameServer/Commands/Command.cs new file mode 100644 index 0000000..6f36f76 --- /dev/null +++ b/GameServer/Commands/Command.cs @@ -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 Commands = new(); + static readonly Logger c = new("Factory", ConsoleColor.Yellow); + + public static void LoadCommandHandlers() + { + c.Log("Loading Command Handlers..."); + + IEnumerable 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"); + } + } +} diff --git a/GameServer/Commands/TestCommand.cs b/GameServer/Commands/TestCommand.cs new file mode 100644 index 0000000..143419a --- /dev/null +++ b/GameServer/Commands/TestCommand.cs @@ -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]}"); + } + } +} diff --git a/GameServer/Packet.cs b/GameServer/Packet.cs index b62c323..c0ac8d1 100644 --- a/GameServer/Packet.cs +++ b/GameServer/Packet.cs @@ -1,6 +1,5 @@ using System.Buffers.Binary; using Common.Resources.Proto; -using Newtonsoft.Json; using Common.Utils; using ProtoBuf; using System.Reflection; diff --git a/Program.cs b/Program.cs index 8f599e5..330ffe8 100644 --- a/Program.cs +++ b/Program.cs @@ -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 args = line.Split(' ').ToList(); + args.RemoveAt(0); + + cmd.Run(null, args.ToArray()); + break; + } + } + } + } } } } \ No newline at end of file