using System; using System.Collections.Generic; using System.Reflection; using Zepheus.FiestaLib.Networking; using Zepheus.Util; namespace Zepheus.Login.Handlers { [ServerModule(Util.InitializationStage.Metadata)] public class HandlerStore { private static Dictionary> handlers; [InitializerMethod] public static bool Load() { handlers = new Dictionary>(); foreach (var info in Reflector.FindMethodsByAttribute()) { PacketHandlerAttribute attribute = info.First; MethodInfo method = info.Second; if (!handlers.ContainsKey(attribute.Header)) handlers.Add(attribute.Header, new Dictionary()); if (handlers[attribute.Header].ContainsKey(attribute.Type)) { Log.WriteLine(LogLevel.Warn, "Duplicate handler found: {0}:{1}", attribute.Header, attribute.Type); handlers[attribute.Header].Remove(attribute.Type); } handlers[attribute.Header].Add(attribute.Type, method); } int count = 0; foreach (var dict in handlers.Values) count += dict.Count; Log.WriteLine(LogLevel.Info, "{0} Handlers loaded.", count); return true; } public static MethodInfo GetHandler(byte header, byte type) { Dictionary dict; MethodInfo meth; if (handlers.TryGetValue(header, out dict)) { if (dict.TryGetValue(type, out meth)) { return meth; } } return null; } public static Action GetCallback(MethodInfo method, params object[] parameters) { return () => method.Invoke(null, parameters); } } }