1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| public class EventSys : ISystem { private static String ms_lock = "lock"; private List<EventSysCallBack> m_allHander = new List<EventSysCallBack> (); private List<EventSysCallBack>[] m_eventArray = new List<EventSysCallBack>[(int)EEventType.Count]; private int m_eventNum = 0;
private Queue m_eventQueue = new Queue(); public static EventSys Instance = null;
private bool m_hasInit = false; public override void Init() { if(m_hasInit) { return; }
m_hasInit = true; m_eventQueue.Clear(); Instance = this; m_eventNum = (int)EEventType.Count; }
public override void SysUpdate() { lock (ms_lock) { while (m_eventQueue.Count > 0) { EventParamData data = m_eventQueue.Dequeue() as EventParamData; EEventType eventId = data.GetEventId(); SendEvent(eventId, data.GetParam1(), data.GetParam2()); } } }
public void AddAllHander(EventSysCallBack callBack) { lock (ms_lock) { m_allHander.Add (callBack); } }
public void AddUintHander(uint eventid, object instance, EventSysCallBack_ callBack) { AddHander((EEventType)eventid, (EEventType eventType, object param1, object param2) => { callBack((uint)eventType, param1, param2); }); } public void AddHander(EEventType eventId, EventSysCallBack callBack) { lock (ms_lock) {
if ((int)eventId < m_eventNum) { var eventList = m_eventArray[(int)eventId]; if (eventList != null) { int count = eventList.Count; for (int i = 0; i < count; i++) { if(callBack == eventList[i]) { return; } } eventList.Add(callBack); return; } }
List<EventSysCallBack> t = new List<EventSysCallBack>(); t.Add(callBack); m_eventArray[(int)eventId] = t; } }
public void RemoveHander(object target) { lock (ms_lock) { for (int i = m_eventNum - 1; i >= 0; i--) { var eventList = m_eventArray[i]; if (eventList != null) { var eventCount = eventList.Count; for(int j = eventCount - 1; j >= 0; j--) { if(target == eventList[j].Target) { eventList.RemoveAt(j); } } } } } }
public void AddEvent(EEventType eventId, object param1=null, object param2=null) { lock (ms_lock) { m_eventQueue.Enqueue( new EventParamData(eventId, param1, param2 ) ); } }
public void AddUintEvent(uint eventId, object param1 = null, object param2 = null) { AddEvent((EEventType)eventId, param1, param2); }
public void AddEventNow(EEventType eventId, object param1=null, object param2=null) { SendEvent (eventId, param1, param2); }
public void AddUintEventNow(uint eventId, object param1 = null, object param2 = null) { AddEventNow((EEventType)eventId, param1, param2); }
private void SendEvent(EEventType eventId, object param1, object param2) { for(int i = 0; i < m_allHander.Count; i++) { try { m_allHander[i](eventId, param1, param2); } catch (Exception e) { CNetSys.Instance.SendImportantLogWithBugly(true, "[Exception]" + e.ToString()); } }
int targetIndex = (int)eventId; if(targetIndex < m_eventNum) { var eventList = m_eventArray[targetIndex]; if(eventList != null) { int count = eventList.Count; for(int i = 0; i < count; i++) { try { if(eventList != null && i < eventList.Count) { eventList[i](eventId, param1, param2); } } catch (System.Exception e) { CNetSys.Instance.SendImportantLogWithBugly(true, "[Exception]" + e.ToString()); } } } } } }
|