-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathEvent.cs
430 lines (354 loc) · 10.7 KB
/
Event.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
namespace KBEngine
{
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
/*
事件模块
KBEngine插件层与Unity3D表现层通过事件来交互
*/
public class Event
{
public struct Pair
{
public object obj;
public string funcname;
public System.Reflection.MethodInfo method;
};
public struct EventObj
{
public Pair info;
public string eventname;
public object[] args;
};
static Dictionary<string, List<Pair>> events_out = new Dictionary<string, List<Pair>>();
static LinkedList<EventObj> firedEvents_out = new LinkedList<EventObj>();
static LinkedList<EventObj> doingEvents_out = new LinkedList<EventObj>();
static Dictionary<string, List<Pair>> events_in = new Dictionary<string, List<Pair>>();
static LinkedList<EventObj> firedEvents_in = new LinkedList<EventObj>();
static LinkedList<EventObj> doingEvents_in = new LinkedList<EventObj>();
static bool _isPauseOut = false;
public Event()
{
}
public static void clear()
{
events_out.Clear();
events_in.Clear();
clearFiredEvents();
}
public static void clearFiredEvents()
{
monitor_Enter(events_out);
firedEvents_out.Clear();
monitor_Exit(events_out);
doingEvents_out.Clear();
monitor_Enter(events_in);
firedEvents_in.Clear();
monitor_Exit(events_in);
doingEvents_in.Clear();
_isPauseOut = false;
}
public static void pause()
{
_isPauseOut = true;
}
public static void resume()
{
_isPauseOut = false;
processOutEvents();
}
public static bool isPause()
{
return _isPauseOut;
}
public static void monitor_Enter(object obj)
{
if(KBEngineApp.app == null)
return;
Monitor.Enter(obj);
}
public static void monitor_Exit(object obj)
{
if(KBEngineApp.app == null)
return;
Monitor.Exit(obj);
}
public static bool hasRegisterOut(string eventname)
{
return _hasRegister(events_out, eventname);
}
public static bool hasRegisterIn(string eventname)
{
return _hasRegister(events_in, eventname);
}
private static bool _hasRegister(Dictionary<string, List<Pair>> events, string eventname)
{
bool has = false;
monitor_Enter(events);
has = events.ContainsKey(eventname);
monitor_Exit(events);
return has;
}
/*
注册监听由kbe插件抛出的事件。(out = kbe->render)
通常由渲染表现层来注册, 例如:监听角色血量属性的变化, 如果UI层注册这个事件,
事件触发后就可以根据事件所附带的当前血量值来改变角色头顶的血条值。
*/
public static bool registerOut(string eventname, object obj, string funcname)
{
return register(events_out, eventname, obj, funcname);
}
/*
注册监听由渲染表现层抛出的事件(in = render->kbe)
通常由kbe插件层来注册, 例如:UI层点击登录, 此时需要触发一个事件给kbe插件层进行与服务端交互的处理。
*/
public static bool registerIn(string eventname, object obj, string funcname)
{
return register(events_in, eventname, obj, funcname);
}
private static bool register(Dictionary<string, List<Pair>> events, string eventname, object obj, string funcname)
{
deregister(events, eventname, obj, funcname);
List<Pair> lst = null;
Pair pair = new Pair();
pair.obj = obj;
pair.funcname = funcname;
pair.method = obj.GetType().GetMethod(funcname);
if(pair.method == null)
{
Dbg.ERROR_MSG("Event::register: " + obj + "not found method[" + funcname + "]");
return false;
}
monitor_Enter(events);
if(!events.TryGetValue(eventname, out lst))
{
lst = new List<Pair>();
lst.Add(pair);
//Dbg.DEBUG_MSG("Event::register: event(" + eventname + ")!");
events.Add(eventname, lst);
monitor_Exit(events);
return true;
}
//Dbg.DEBUG_MSG("Event::register: event(" + eventname + ")!");
lst.Add(pair);
monitor_Exit(events);
return true;
}
public static bool deregisterOut(string eventname, object obj, string funcname)
{
removeFiredEventOut(obj, eventname, funcname);
return deregister(events_out, eventname, obj, funcname);
}
public static bool deregisterIn(string eventname, object obj, string funcname)
{
removeFiredEventIn(obj, eventname, funcname);
return deregister(events_in, eventname, obj, funcname);
}
private static bool deregister(Dictionary<string, List<Pair>> events, string eventname, object obj, string funcname)
{
monitor_Enter(events);
List<Pair> lst = null;
if(!events.TryGetValue(eventname, out lst))
{
monitor_Exit(events);
return false;
}
for(int i=0; i<lst.Count; i++)
{
if(obj == lst[i].obj && lst[i].funcname == funcname)
{
//Dbg.DEBUG_MSG("Event::deregister: event(" + eventname + ":" + funcname + ")!");
lst.RemoveAt(i);
monitor_Exit(events);
return true;
}
}
monitor_Exit(events);
return false;
}
public static bool deregisterOut(object obj)
{
removeAllFiredEventOut(obj);
return deregister(events_out, obj);
}
public static bool deregisterIn(object obj)
{
removeAllFiredEventIn(obj);
return deregister(events_in, obj);
}
private static bool deregister(Dictionary<string, List<Pair>> events, object obj)
{
monitor_Enter(events);
var iter = events.GetEnumerator();
while (iter.MoveNext())
{
List<Pair> lst = iter.Current.Value;
// 从后往前遍历,以避免中途删除的问题
for (int i = lst.Count - 1; i >= 0; i--)
{
if (obj == lst[i].obj)
{
//Dbg.DEBUG_MSG("Event::deregister: event(" + e.Key + ":" + lst[i].funcname + ")!");
lst.RemoveAt(i);
}
}
}
monitor_Exit(events);
return true;
}
/*
kbe插件触发事件(out = kbe->render)
通常由渲染表现层来注册, 例如:监听角色血量属性的变化, 如果UI层注册这个事件,
事件触发后就可以根据事件所附带的当前血量值来改变角色头顶的血条值。
*/
public static void fireOut(string eventname, params object[] args)
{
fire_(events_out, firedEvents_out, eventname, args);
}
/*
渲染表现层抛出事件(in = render->kbe)
通常由kbe插件层来注册, 例如:UI层点击登录, 此时需要触发一个事件给kbe插件层进行与服务端交互的处理。
*/
public static void fireIn(string eventname, params object[] args)
{
fire_(events_in, firedEvents_in, eventname, args);
}
/*
触发kbe插件和渲染表现层都能够收到的事件
*/
public static void fireAll(string eventname, params object[] args)
{
fire_(events_in, firedEvents_in, eventname, args);
fire_(events_out, firedEvents_out, eventname, args);
}
private static void fire_(Dictionary<string, List<Pair>> events, LinkedList<EventObj> firedEvents, string eventname, object[] args)
{
monitor_Enter(events);
List<Pair> lst = null;
if(!events.TryGetValue(eventname, out lst))
{
if(events == events_in)
Dbg.WARNING_MSG("Event::fireIn: event(" + eventname + ") not found!");
else
Dbg.WARNING_MSG("Event::fireOut: event(" + eventname + ") not found!");
monitor_Exit(events);
return;
}
for(int i=0; i<lst.Count; i++)
{
EventObj eobj = new EventObj();
eobj.info = lst[i];
eobj.eventname = eventname;
eobj.args = args;
firedEvents.AddLast(eobj);
}
monitor_Exit(events);
}
public static void processOutEvents()
{
monitor_Enter(events_out);
if(firedEvents_out.Count > 0)
{
var iter = firedEvents_out.GetEnumerator();
while (iter.MoveNext())
{
doingEvents_out.AddLast(iter.Current);
}
firedEvents_out.Clear();
}
monitor_Exit(events_out);
while (doingEvents_out.Count > 0 && !_isPauseOut)
{
EventObj eobj = doingEvents_out.First.Value;
//Debug.Log("processOutEvents:" + eobj.info.funcname + "(" + eobj.info + ")");
//foreach(object v in eobj.args)
//{
// Debug.Log("processOutEvents:args=" + v);
//}
try
{
eobj.info.method.Invoke (eobj.info.obj, eobj.args);
}
catch (Exception e)
{
Dbg.ERROR_MSG("Event::processOutEvents: event=" + eobj.info.funcname + "\n" + e.ToString());
}
if(doingEvents_out.Count > 0)
doingEvents_out.RemoveFirst();
}
}
public static void processInEvents()
{
monitor_Enter(events_in);
if(firedEvents_in.Count > 0)
{
var iter = firedEvents_in.GetEnumerator();
while (iter.MoveNext())
{
doingEvents_in.AddLast(iter.Current);
}
firedEvents_in.Clear();
}
monitor_Exit(events_in);
while (doingEvents_in.Count > 0)
{
EventObj eobj = doingEvents_in.First.Value;
//Debug.Log("processInEvents:" + eobj.info.funcname + "(" + eobj.info + ")");
//foreach(object v in eobj.args)
//{
// Debug.Log("processInEvents:args=" + v);
//}
try
{
eobj.info.method.Invoke (eobj.info.obj, eobj.args);
}
catch (Exception e)
{
Dbg.ERROR_MSG("Event::processInEvents: event=" + eobj.info.funcname + "\n" + e.ToString());
}
if(doingEvents_in.Count > 0)
doingEvents_in.RemoveFirst();
}
}
public static void removeAllFiredEventIn(object obj)
{
removeFiredEvent(firedEvents_in, obj);
}
public static void removeAllFiredEventOut(object obj)
{
removeFiredEvent(firedEvents_out, obj);
}
public static void removeFiredEventIn(object obj, string eventname, string funcname)
{
removeFiredEvent(firedEvents_in, obj, eventname, funcname);
}
public static void removeFiredEventOut(object obj, string eventname, string funcname)
{
removeFiredEvent(firedEvents_out, obj, eventname, funcname);
}
public static void removeFiredEvent(LinkedList<EventObj> firedEvents, object obj, string eventname="", string funcname="")
{
monitor_Enter(firedEvents);
while(true)
{
bool found = false;
foreach(EventObj eobj in firedEvents)
{
if( ((eventname == "" && funcname == "") || (eventname == eobj.eventname && funcname == eobj.info.funcname))
&& eobj.info.obj == obj)
{
firedEvents.Remove(eobj);
found = true;
break;
}
}
if (!found)
break;
}
monitor_Exit(firedEvents);
}
}
}