using System.Collections.Generic; namespace XGame.Framework { public delegate void EventDelegate(int eventId, object args); public sealed partial class EventModule : IEventModule, System.IDisposable { Dictionary> dic; public EventModule() { dic = new Dictionary>(); } public void AddListener(int eventId, EventDelegate handler) { Assert.IsNotNull(handler, $"EventModule.AddListener handler is null, eventId: {eventId}"); if (!dic.TryGetValue(eventId, out var handlers)) { handlers = new HashSet(); dic[eventId] = handlers; } Assert.IsFalse(handlers.Contains(handler), $"EventModule.AddListener Handler Allready Exists!! {handler.Method.DeclaringType.FullName}::{handler.Method.Name}"); handlers.Add(handler); } public void RemoveListener(int eventId, EventDelegate handler) { Assert.IsNotNull(handler, $"EventModule.RemoveListener handler is null, eventId: {eventId}"); if (dic.TryGetValue(eventId, out var handlers)) { Assert.IsTrue(handlers.Contains(handler), $"EventModule.RemoveListener Handler Doesn't Exists!! {handler.Method.DeclaringType.FullName}::{handler.Method.Name}"); handlers.Remove(handler); if (handlers.Count <= 0) dic.Remove(eventId); } } public void Notify(int eventId) { Notify(eventId, null); } public void Notify(int eventId, object args) { if (dic.TryGetValue(eventId, out var handlers)) { var list = ListPool.Acquire(); list.AddRange(handlers); foreach (var handler in list) { try { #if UNITY_EDITOR if (handlers.Contains(handler)) #endif { handler?.Invoke(eventId, args); } } catch (System.Exception e) { Log.Exception($"EventModule.Notify({eventId}) Error: \n" + $"Occur: {handler.Method?.DeclaringType?.FullName + "::" + handler.Method?.Name},\n", e); } } ListPool.Recycle(list); } } public void Dispose() { dic.Clear(); } } }