using AxibugEmuOnline.Client.ClientCore; using System; using System.Collections.Generic; namespace AxibugEmuOnline.Client.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 Eventer { private static Eventer instance = new Eventer(); public static Eventer Instance { get { return instance; } } private Dictionary> eventDic = new Dictionary>(128); private Eventer() { } #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.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.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.ToString()); } } } } 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.ToString()); } } } } #endregion /// /// 获取所有事件 /// /// /// private List GetEventList(EEvent evt) { if (eventDic.ContainsKey(evt)) { List tempList = eventDic[evt]; if (null != tempList) { return tempList; } } return null; } } }