Files
DanhengServer-OpenSource/GameServer/Server/Packet/HandlerManager.cs
2024-02-25 16:45:05 +08:00

41 lines
1.0 KiB
C#

using EggLink.DanhengServer.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace EggLink.DanhengServer.Server.Packet
{
public class HandlerManager
{
public Dictionary<int, Handler> handlers = [];
public HandlerManager()
{
var classes = Assembly.GetExecutingAssembly().GetTypes(); // Get all classes in the assembly
foreach (var cls in classes)
{
var attribute = (Opcode)Attribute.GetCustomAttribute(cls, typeof(Opcode));
if (attribute != null)
{
handlers.Add(attribute.CmdId, (Handler)Activator.CreateInstance(cls));
}
}
}
public Handler? GetHandler(int cmdId)
{
try
{
return handlers[cmdId];
} catch
{
return null;
}
}
}
}