namespace ClientCore.Event { public class EventData { private static long BaseUid = 0; private static long GenUid() { return (++BaseUid); } /// /// 唯一id /// public long uid { get; private set; } /// /// 回调 /// public Delegate callback { get; private set; } public EventData(Delegate d) { uid = GenUid(); callback = d; } } public class EventSystem { private static EventSystem instance = new EventSystem(); public static EventSystem Instance { get { return instance; } } private Dictionary> eventDic = new Dictionary>(128); private EventSystem() { } #region RegisterEvent public void RegisterEvent(EEvent evt, Action callback) { InterRegisterEvent(evt, callback); } public void RegisterEvent(EEvent evt, Action callback) { InterRegisterEvent(evt, callback); } public void RegisterEvent(EEvent evt, Action callback) { InterRegisterEvent(evt, callback); } public void RegisterEvent(EEvent evt, Action callback) { InterRegisterEvent(evt, callback); } public void RegisterEvent(EEvent evt, Action callback) { InterRegisterEvent(evt, callback); } private void InterRegisterEvent(EEvent evt, Delegate callback) { if (eventDic.TryGetValue(evt, out List dlist)) { if (dlist.IndexOf(callback) < 0) { dlist.Add(callback); } } else { eventDic.Add(evt, new List() { callback }); } //if (eventDic.ContainsKey(evt)) //{ // if (eventDic[evt].IndexOf(callback) < 0) // { // eventDic[evt].Add(callback); // } //} //else //{ // eventDic.Add(evt, new List() { callback }); //} } #endregion #region UnregisterEvent public void UnregisterEvent(EEvent evt, Action callback) { Delegate tempDelegate = callback; InterUnregisterEvent(evt, tempDelegate); } public void UnregisterEvent(EEvent evt, Action callback) { Delegate tempDelegate = callback; InterUnregisterEvent(evt, tempDelegate); } public void UnregisterEvent(EEvent evt, Action callback) { Delegate tempDelegate = callback; InterUnregisterEvent(evt, tempDelegate); } public void UnregisterEvent(EEvent evt, Action callback) { Delegate tempDelegate = callback; InterUnregisterEvent(evt, tempDelegate); } public void UnregisterEvent(EEvent evt, Action callback) { Delegate tempDelegate = callback; InterUnregisterEvent(evt, tempDelegate); } private void InterUnregisterEvent(EEvent evt, Delegate callback) { if (eventDic.TryGetValue(evt, out List dlist)) { dlist.Remove(callback); if (dlist.Count == 0) eventDic.Remove(evt); } //if (eventDic.ContainsKey(evt)) //{ // eventDic[evt].Remove(callback); // if (eventDic[evt].Count == 0) eventDic.Remove(evt); //} } #endregion #region PostEvent public void PostEvent(EEvent evt, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { List eventList = GetEventList(evt); if (eventList != null) { foreach (Delegate callback in eventList) { try { ((Action)callback)(arg1, arg2, arg3, arg4); } catch (Exception e) { App.log.Error(e.Message); } } } } public void PostEvent(EEvent evt, T1 arg1, T2 arg2, T3 arg3) { List eventList = GetEventList(evt); if (eventList != null) { foreach (Delegate callback in eventList) { try { ((Action)callback)(arg1, arg2, arg3); } catch (Exception e) { App.log.Error(e.Message); } } } } public void PostEvent(EEvent evt, T1 arg1, T2 arg2) { List eventList = GetEventList(evt); if (eventList != null) { foreach (Delegate callback in eventList) { try { ((Action)callback)(arg1, arg2); } catch (Exception e) { App.log.Error(e.Message); } } } } public void PostEvent(EEvent evt, T arg) { List eventList = GetEventList(evt); if (eventList != null) { foreach (Delegate callback in eventList) { try { ((Action)callback)(arg); } catch (Exception e) { App.log.Error(e.Message + ", method name : " + callback.Method); } } } } public void PostEvent(EEvent evt) { List eventList = GetEventList(evt); if (eventList != null) { foreach (Delegate callback in eventList) { try { ((Action)callback)(); } catch (Exception e) { App.log.Error(e.Message); } } } } #endregion /// /// 获取所有事件 /// /// /// private List GetEventList(EEvent evt) { if (eventDic.TryGetValue(evt,out List dlist)) { if (null != dlist) { return dlist; } } //if (eventDic.ContainsKey(evt)) //{ // List tempList = eventDic[evt]; // if (null != tempList) // { // return tempList; // } //} return null; } } }