Namespace refactor, basic Impl for PacketHandlers

This commit is contained in:
Kyle873
2023-05-26 03:49:47 -04:00
parent 3426d8ebdc
commit 511c623dd6
4 changed files with 61 additions and 7 deletions

View File

@@ -3,8 +3,9 @@ using Common.Resources.Proto;
using Newtonsoft.Json; using Newtonsoft.Json;
using Common.Utils; using Common.Utils;
using ProtoBuf; using ProtoBuf;
using System.Reflection;
namespace PemukulPaku.Gameserver namespace PemukulPaku.GameServer
{ {
public class Packet public class Packet
{ {
@@ -47,4 +48,56 @@ namespace PemukulPaku.Gameserver
} }
} }
} }
public class PacketCmdId : Attribute
{
public CmdId Id { get; }
public PacketCmdId(CmdId id)
{
Id = id;
}
}
public interface IPacketHandler
{
public void Handle(Session session, Packet packet);
}
public static class PacketFactory
{
public static readonly Dictionary<CmdId, IPacketHandler> Handlers = new();
static readonly Logger c = new("PKT", ConsoleColor.Yellow);
public static void LoadPacketHandlers()
{
c.Log("Loading Packet Handlers...");
IEnumerable<Type> classes = from t in Assembly.GetExecutingAssembly().GetTypes()
select t;
foreach ((Type t, PacketCmdId attr) in from Type? t in classes.ToList()
let attrs = (Attribute[])t.GetCustomAttributes(typeof(PacketCmdId), false)
where attrs.Length > 0
let attr = (PacketCmdId)attrs[0]
where !Handlers.ContainsKey(attr.Id)
select (t, attr))
{
Handlers.Add(attr.Id, (IPacketHandler)Activator.CreateInstance(t));
c.Log($"Loaded PacketHandler {t.Name} for Packet Type {attr.Id}");
}
c.Log("Finished Loading Packet Handlers");
}
}
[PacketCmdId(CmdId.PlayerLoginReq)]
public class PlayerLoginReqHandler : IPacketHandler
{
public void Handle(Session session, Packet packet)
{
throw new NotImplementedException();
}
}
} }

View File

@@ -3,7 +3,7 @@ using System.Net;
using Common; using Common;
using Common.Utils; using Common.Utils;
namespace PemukulPaku.Gameserver namespace PemukulPaku.GameServer
{ {
public class Server public class Server
{ {
@@ -11,7 +11,8 @@ namespace PemukulPaku.Gameserver
public readonly Dictionary<string, Session> Sessions = new(); public readonly Dictionary<string, Session> Sessions = new();
private static Server? Instance; private static Server? Instance;
public static Server GetInstance() { public static Server GetInstance()
{
return Instance ??= new Server(); return Instance ??= new Server();
} }

View File

@@ -1,8 +1,7 @@
using System.Net.Sockets; using System.Net.Sockets;
using Common.Utils; using Common.Utils;
using Newtonsoft.Json;
namespace PemukulPaku.Gameserver namespace PemukulPaku.GameServer
{ {
public class Session public class Session
{ {

View File

@@ -1,7 +1,7 @@
using Common.Resources.Proto; using Common.Resources.Proto;
using Common; using Common;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using PemukulPaku.Gameserver; using PemukulPaku.GameServer;
namespace PemukulPaku namespace PemukulPaku
{ {
@@ -19,6 +19,7 @@ namespace PemukulPaku
}; };
new Thread(HttpServer.Program.Main).Start(); new Thread(HttpServer.Program.Main).Start();
PacketFactory.LoadPacketHandlers();
_ = Server.GetInstance(); _ = Server.GetInstance();
Console.Read(); Console.Read();