mirror of
https://github.com/rafi1212122/PemukulPaku
synced 2025-12-12 15:54:34 +01:00
Command parser that probably works
This commit is contained in:
61
GameServer/Commands/Command.cs
Normal file
61
GameServer/Commands/Command.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
19
GameServer/Commands/TestCommand.cs
Normal file
19
GameServer/Commands/TestCommand.cs
Normal 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]}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.Buffers.Binary;
|
||||
using Common.Resources.Proto;
|
||||
using Newtonsoft.Json;
|
||||
using Common.Utils;
|
||||
using ProtoBuf;
|
||||
using System.Reflection;
|
||||
|
||||
23
Program.cs
23
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<string> args = line.Split(' ').ToList();
|
||||
args.RemoveAt(0);
|
||||
|
||||
cmd.Run(null, args.ToArray());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user