-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsg.cs
More file actions
474 lines (451 loc) · 16.4 KB
/
Msg.cs
File metadata and controls
474 lines (451 loc) · 16.4 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#if DEBUG
#define MSG_LOG_ENABLE
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Assertions;
namespace Emptybraces
{
public enum MsgId
{
OnInitedLongTime,
OnCreated,
OnRemoved,
}
public static class Msg
{
#if MSG_LOG_ENABLE
static readonly MsgId[] _ignoreLogMsgId = new MsgId[]
{
MsgId.OnCreated,
MsgId.OnRemoved,
};
public static bool IsOutputError = true;
#endif
public static Dictionary<(int id, int instanceId), int> RegisteredTime = new Dictionary<(int, int), int>(32);
static Dictionary<(int id, int instanceId), HashSet<Delegate>> _idToDeligates = new Dictionary<(int, int), HashSet<Delegate>>(32);
static Dictionary<int, int> _idToInvokingCount = new Dictionary<int, int>(32);
static Stack<List<Delegate>> _deligateListCaches = new();
const int RECURSION_LIMIT = 10; // 再帰コール制限
static Msg()
{
for (int i = 0; i < 2; ++i)
_deligateListCaches.Push(new List<Delegate>());
}
public static void Invoke(MsgId id)
{
// invoke中に同じIDでInvokeしたら無限ループになるので阻止
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, 0, out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action a)
a.Invoke();
else
_LogE($"Invoke Failed Error: Not specified arguments. but delegate sigunature is: {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T>(MsgId id, T t)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, 0, out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T> a)
a.Invoke(t);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T)} but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T0, T1>(MsgId id, T0 t0, T1 t1)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, 0, out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T0, T1> a)
a.Invoke(t0, t1);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T0)}, {typeof(T1)} but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T0, T1, T2>(MsgId id, T0 t0, T1 t1, T2 t2)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, 0, out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T0, T1, T2> a)
a.Invoke(t0, t1, t2);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T0)}, {typeof(T1)}, {typeof(T2)} but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T0, T1, T2, T3>(MsgId id, T0 t0, T1 t1, T2 t2, T3 t3)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, 0, out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T0, T1, T2, T3> a)
a.Invoke(t0, t1, t2, t3);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T0)}, {typeof(T1)}, {typeof(T2)}, {typeof(T3)} but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T0, T1, T2, T3, T4>(MsgId id, T0 t0, T1 t1, T2 t2, T3 t3, T4 t4)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, 0, out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T0, T1, T2, T3, T4> a)
a.Invoke(t0, t1, t2, t3, t4);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T0)}, {typeof(T1)}, {typeof(T2)}, {typeof(T3)}, {typeof(T4)} but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke(GameObject g, MsgId id)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, g.GetInstanceID(), out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action a)
a.Invoke();
else
_LogE($"Invoke Failed Error: Not specified arguments. but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T>(GameObject g, MsgId id, T t)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, g.GetInstanceID(), out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T> a)
a.Invoke(t);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T)}, but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T0, T1>(GameObject g, MsgId id, T0 t0, T1 t1)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, g.GetInstanceID(), out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T0, T1> a)
a.Invoke(t0, t1);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T0)}, {typeof(T1)}, but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T0, T1, T2>(GameObject g, MsgId id, T0 t0, T1 t1, T2 t2)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, g.GetInstanceID(), out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T0, T1, T2> a)
a.Invoke(t0, t1, t2);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T0)}, {typeof(T1)}, {typeof(T2)}, but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T0, T1, T2, T3>(GameObject g, MsgId id, T0 t0, T1 t1, T2 t2, T3 t3)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, g.GetInstanceID(), out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T0, T1, T2, T3> a)
a.Invoke(t0, t1, t2, t3);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T0)}, {typeof(T1)}, {typeof(T2)}, {typeof(T3)}, but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
public static void Invoke<T0, T1, T2, T3, T4>(GameObject g, MsgId id, T0 t0, T1 t1, T2 t2, T3 t3, T4 t4)
{
if (_CheckInfiniteLoop(id))
return;
if (_InvokePreprocess(id, g.GetInstanceID(), out var deligate_list))
{
for (int i = 0; i < deligate_list.Count; ++i)
{
var d = deligate_list[i];
if (d is Action<T0, T1, T2, T3, T4> a)
a.Invoke(t0, t1, t2, t3, t4);
else
_LogE($"Invoke Failed Error: Specified arguments are {typeof(T0)}, {typeof(T1)}, {typeof(T2)}, {typeof(T3)}, {typeof(T4)}, but registered sigunature is {d.Method}");
}
_InvokePostprocess(id, deligate_list);
}
else
_LogInvokeFailed(id);
}
static bool _CheckInfiniteLoop(MsgId id)
{
if (_idToInvokingCount.TryGetValue((int)id, out var count) && RECURSION_LIMIT < count)
{
_LogW($"Invoke Warning: Invoke with ID '{id}' was triggered '{count}' times during its own execution. Preventing infinite loop.");
return true;
}
return false;
}
static bool _InvokePreprocess(MsgId id, int instanceId, out List<Delegate> delegateList)
{
if (_idToDeligates.TryGetValue(((int)id, instanceId), out var deligates))
{
_LogInvoke(id);
// リストからバッファから取得する。Invokeする1フレームだけ必要なdelegateリストは使いまわすため、バッファを用意。
if (!_deligateListCaches.TryPop(out delegateList))
delegateList = new();
// 覚えておく
_idToInvokingCount.TryAdd((int)id, 0);
++_idToInvokingCount[(int)id];
delegateList.AddRange(deligates);
return true;
}
delegateList = null;
return false;
}
static void _InvokePostprocess(MsgId id, List<Delegate> delegateList)
{
delegateList.Clear();
_deligateListCaches.Push(delegateList);
--_idToInvokingCount[(int)id];
}
public static bool Set(MsgId id, Action action) => _Set(null, id, (Delegate)action);
public static bool Unset(MsgId id, Action action) => _Unset(null, id, (Delegate)action);
public static bool Set<T>(MsgId id, Action<T> action) => _Set(null, id, (Delegate)action);
public static bool Unset<T>(MsgId id, Action<T> action) => _Unset(null, id, (Delegate)action);
public static bool Set<T0, T1>(MsgId id, Action<T0, T1> action) => _Set(null, id, (Delegate)action);
public static bool Unset<T0, T1>(MsgId id, Action<T0, T1> action) => _Unset(null, id, (Delegate)action);
public static bool Set<T0, T1, T2>(MsgId id, Action<T0, T1, T2> action) => _Set(null, id, (Delegate)action);
public static bool Unset<T0, T1, T2>(MsgId id, Action<T0, T1, T2> action) => _Unset(null, id, (Delegate)action);
public static bool Set<T0, T1, T2, T3>(MsgId id, Action<T0, T1, T2, T3> action) => _Set(null, id, (Delegate)action);
public static bool Unset<T0, T1, T2, T3>(MsgId id, Action<T0, T1, T2, T3> action) => _Unset(null, id, (Delegate)action);
public static bool Set<T0, T1, T2, T3, T4>(MsgId id, Action<T0, T1, T2, T3, T4> action) => _Set(null, id, (Delegate)action);
public static bool Unset<T0, T1, T2, T3, T4>(MsgId id, Action<T0, T1, T2, T3, T4> action) => _Unset(null, id, (Delegate)action);
public static bool Set(GameObject g, MsgId id, Action action) { Assert.IsNotNull(g); return _Set(g, id, (Delegate)action); }
public static bool Unset(GameObject g, MsgId id, Action action) { Assert.IsNotNull(g); return _Unset(g, id, (Delegate)action); }
public static bool Set<T>(GameObject g, MsgId id, Action<T> action) { Assert.IsNotNull(g); return _Set(g, id, (Delegate)action); }
public static bool Unset<T>(GameObject g, MsgId id, Action<T> action) { Assert.IsNotNull(g); return _Unset(g, id, (Delegate)action); }
public static bool Set<T0, T1>(GameObject g, MsgId id, Action<T0, T1> action) { Assert.IsNotNull(g); return _Set(g, id, (Delegate)action); }
public static bool Unset<T0, T1>(GameObject g, MsgId id, Action<T0, T1> action) { Assert.IsNotNull(g); return _Unset(g, id, (Delegate)action); }
public static bool Set<T0, T1, T2>(GameObject g, MsgId id, Action<T0, T1, T2> action) { Assert.IsNotNull(g); return _Set(g, id, (Delegate)action); }
public static bool Unset<T0, T1, T2>(GameObject g, MsgId id, Action<T0, T1, T2> action) { Assert.IsNotNull(g); return _Unset(g, id, (Delegate)action); }
public static bool Set<T0, T1, T2, T3>(GameObject g, MsgId id, Action<T0, T1, T2, T3> action) { Assert.IsNotNull(g); return _Set(g, id, (Delegate)action); }
public static bool Unset<T0, T1, T2, T3>(GameObject g, MsgId id, Action<T0, T1, T2, T3> action) { Assert.IsNotNull(g); return _Unset(g, id, (Delegate)action); }
public static bool Set<T0, T1, T2, T3, T4>(GameObject g, MsgId id, Action<T0, T1, T2, T3, T4> action) { Assert.IsNotNull(g); return _Set(g, id, (Delegate)action); }
public static bool Unset<T0, T1, T2, T3, T4>(GameObject g, MsgId id, Action<T0, T1, T2, T3, T4> action) { Assert.IsNotNull(g); return _Unset(g, id, (Delegate)action); }
static bool _Set(GameObject g, MsgId id, Delegate action)
{
// // Invoke中に呼び出された場合、呼び出し中リストに加える
// if (_idToInvokingDelicates.TryGetValue(id, out var invoking_list) && !invoking_list.Contains(action))
// {
// }
var gid = g != null ? g.GetInstanceID() : 0;
var key = ((int)id, gid);
if (!_idToDeligates.TryGetValue(key, out var set))
_idToDeligates.Add(key, set = new HashSet<Delegate>());
var r = set.Add(action);
_LogSet(r, id, action);
if (r)
RegisteredTime[key] = Time.frameCount;
return r;
}
static bool _Unset(GameObject g, MsgId id, Delegate action)
{
var gid = g != null ? g.GetInstanceID() : 0;
bool r = false;
var key = ((int)id, gid);
if (_idToDeligates.TryGetValue(key, out var set))
{
r = set.Remove(action);
// 登録がゼロになったら削除する
if (set.Count() == 0)
{
_idToDeligates.Remove(key);
RegisteredTime.Remove(key);
}
}
_LogUnset(r, id, action);
return r;
}
public static bool Unset(GameObject g)
{
var gid = g.GetInstanceID();
bool r = false;
foreach (var key in _idToDeligates.Keys.ToArray())
{
if (key.Item2 == gid)
{
r = true;
_idToDeligates.Remove(key);
}
}
_LogUnset(r, g);
return r;
}
static StringBuilder _sb;
public static string GetState()
{
#if DEBUG
_sb ??= new StringBuilder();
_sb.Clear();
_sb.Append("Key Total: ").AppendLine(_idToDeligates.Count.ToString());
_sb.Append("Value Total: ").AppendLine(_idToDeligates.Sum(e => e.Value.Count()).ToString());
foreach (var i in _idToDeligates.OrderBy(e => e.Key.Item1))
{
_sb.AppendLine($"\tno=({(MsgId)i.Key.Item1}, objid={i.Key.Item2,8}), cnt={i.Value.Count,2}, reg={Time.frameCount - RegisteredTime[i.Key],5}");
// _sb.AppendLine($"\tno=({i.Key.Item1,3}, objid={i.Key.Item2,8}), cnt={i.Value.Count,2}, reg={Time.frameCount - _registeredTime[i.Key],5}");
}
return _sb.ToString();
#else
return "";
#endif
}
[System.Diagnostics.Conditional("MSG_LOG_ENABLE")]
static void _LogSet(bool result, MsgId id, Delegate action)
{
if (result) Debug.Log($"<color=lightblue>Msg: Register({id}({(int)id})) Success</color>");
else if (IsOutputError) Debug.Log($"<color=yellow>Msg: Register({id}({(int)id})) Failed</color>");
}
[System.Diagnostics.Conditional("MSG_LOG_ENABLE")]
static void _LogUnset(bool result, MsgId id, Delegate action)
{
if (result) Debug.Log($"<color=lightblue>Msg: Unregister({id}({(int)id})) Success</color>");
else if (IsOutputError) Debug.Log($"<color=yellow>Msg: Unregister({id}({(int)id})) Failed</color>");
}
[System.Diagnostics.Conditional("MSG_LOG_ENABLE")]
static void _LogUnset(bool result, GameObject g)
{
if (result) Debug.Log($"<color=lightblue>Msg: Batch Unregister({g.name}) Success</color>");
else if (IsOutputError) Debug.Log($"<color=yellow>Msg: Batch Unregister({g.name}) Failed</color>");
}
[System.Diagnostics.Conditional("MSG_LOG_ENABLE")]
static void _LogInvoke(MsgId id)
{
if (!_ignoreLogMsgId.Contains(id))
Debug.Log($"<color=lightblue>Msg: Invoke({id}({(int)id}))</color>");
}
[System.Diagnostics.Conditional("MSG_LOG_ENABLE")]
static void _LogInvokeFailed(MsgId id, GameObject g = null)
{
if (IsOutputError && !_ignoreLogMsgId.Contains(id))
Debug.Log($"<color=yellow>Msg: Invoke Failed({id}({(int)id}), {g})</color>");
}
[System.Diagnostics.Conditional("DEBUG")] static void _Log(string s) => Debug.Log(s);
[System.Diagnostics.Conditional("DEBUG")] static void _LogE(string s) => Debug.LogError(s);
[System.Diagnostics.Conditional("DEBUG")] static void _LogW(string s) => Debug.LogWarning(s);
#if !DEBUG
[System.Diagnostics.Conditional("NEVER_CALLED")]
#endif
public static void Dump()
{
var sb = new StringBuilder();
foreach (var kvp in _idToDeligates)
{
sb.Clear();
sb.Append($"{(MsgId)kvp.Key.id}, {kvp.Key.instanceId}, {kvp.Value.ElementAt(0)}");
foreach (var d in kvp.Value)
{
sb.Append($"\n - {d.Method}/{d.Target}");
}
Debug.Log(sb.ToString());
}
}
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void _OnDomainReload()
{
RegisteredTime = new Dictionary<(int, int), int>(32);
_idToDeligates = new Dictionary<(int, int), HashSet<Delegate>>(32);
_idToInvokingCount = new();
_deligateListCaches = new();
}
#endif
}
}