forked from NullTale/UnityEventBus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalBus.cs
More file actions
310 lines (260 loc) · 10.7 KB
/
GlobalBus.cs
File metadata and controls
310 lines (260 loc) · 10.7 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
using System;
using System.Linq;
using System.Reflection;
using UnityEventBus.Utils;
using UnityEngine;
namespace UnityEventBus
{
/// <summary>
/// EventBus singleton
/// </summary>
[DefaultExecutionOrder(-100)]
public sealed partial class GlobalBus : MonoBehaviour, IEventBus
{
private static GlobalBus s_Instance;
public static GlobalBus Instance
{
get
{
#if !UNITY_EVENT_BUS_DISABLE_AUTO_INITIALIZATION
if (s_Instance.IsNull())
Create(false, false);
#endif
return s_Instance;
}
private set
{
if (s_Instance == value)
return;
s_Instance = value;
// instance discarded
if (s_Instance.IsNull())
return;
}
}
public const object k_DefaultEventData = null;
public const int k_DefaultPriority = 0;
public const string k_DefaultName = "";
private IEventBusImpl m_Impl;
internal IEventBusImpl Impl => m_Impl;
public bool InitOnAwake;
public bool CollectClasses;
public bool CollectFunctions;
// =======================================================================
private abstract class ListenerActionBase<T> : IListener<T>, ISubscriberOptions
{
protected string m_Name;
protected int m_Proprity;
public string Name => m_Name;
public int Priority => m_Proprity;
// =======================================================================
public abstract void React(in T e);
protected ListenerActionBase(string name, int proprity)
{
m_Name = string.IsNullOrEmpty(name) ? Guid.NewGuid().ToString() : name;
m_Proprity = proprity;
}
}
private class ListenerStaticFunction<T> : ListenerActionBase<T>
{
private ProcessDelagate m_Action;
// =======================================================================
private delegate void ProcessDelagate(T e);
// =======================================================================
public override void React(in T e)
{
// if key matches invoke action
m_Action(e);
}
public ListenerStaticFunction(string name, MethodInfo method, int proprity)
: base(name, proprity)
{
// proceed call
m_Action = (ProcessDelagate)Delegate.CreateDelegate(typeof(ProcessDelagate), method);
// set defaults from method info
if (string.IsNullOrEmpty(name))
m_Name = method.Name;
}
}
// =======================================================================
private void Awake()
{
if (InitOnAwake)
{
Init(new EventBusImpl());
DontDestroyOnLoad(gameObject);
}
}
private void Init(IEventBusImpl impl)
{
if (s_Instance != null && s_Instance != this)
throw new NotSupportedException("Can't initialize EventSystem singleton twice.");
if (impl == null)
throw new ArgumentNullException(nameof(impl));
// set implementation
m_Impl = impl;
// set instance
Instance = this;
// parse assembly for listeners
_collectAssemblyListeners();
}
private void OnDestroy()
{
if (s_Instance == this)
Instance = null;
if (m_Impl != null)
{
m_Impl.Dispose();
m_Impl = null;
}
}
// =======================================================================
void IEventBus.Send<TEvent, TInvoker>(in TEvent e, in TInvoker invoker)
{
Send(in e, in invoker);
}
void IEventBus.Subscribe(ISubscriber sub)
{
Subscribe(sub);
}
void IEventBus.UnSubscribe(ISubscriber sub)
{
UnSubscribe(sub);
}
// =======================================================================
/// <summary> Create and initialize EventSystem singleton game object, if singleton already created nothing will happen </summary>
public static void Create(bool collectClasses, bool collectFunctions)
{
if (s_Instance != null)
return;
var go = new GameObject(nameof(GlobalBus));
DontDestroyOnLoad(go);
var es = go.AddComponent<GlobalBus>();
es.CollectClasses = collectClasses;
es.CollectFunctions = collectFunctions;
es.Init(new EventBusImpl());
}
public static void Send<TEvent, TInvoker>(in TEvent e, in TInvoker invoker) where TInvoker : IEventInvoker
{
Instance.m_Impl.Send(in e, in invoker);
}
public static void Send<TEvent>(in TEvent e, in Func<ISubscriber, bool> check)
{
Instance.m_Impl.Send(in e, new Extensions.DefaultInvokerConditional() { m_Filter = check });
}
public static void Send<TEvent>(in TEvent e)
{
Instance.Send(in e);
}
public static void Subscribe(ISubscriber sub)
{
Instance.m_Impl.Subscribe(sub);
}
public static void UnSubscribe(ISubscriber sub)
{
#if UNITY_EDITOR
if (s_Instance == null)
return;
#endif
Instance.m_Impl.UnSubscribe(sub);
}
// =======================================================================
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void _domainReloadCapability()
{
s_Instance = null;
}
private void _collectAssemblyListeners()
{
// not tested with AOT, questionable usefulness, confusional behavior
if (CollectClasses || CollectFunctions)
{
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(n => n.GetTypes()).ToArray();
// create listener instances
if (CollectClasses)
{
foreach (var type in types)
{
var attribure = type.GetCustomAttribute<ListenerAttribute>();
// not null & active
if (attribure != null && attribure.Active)
{
// must be creatable class
if (type.IsAbstract || type.IsClass == false || type.IsGenericType)
continue;
// must implement event listener interface
if (typeof(ISubscriber).IsAssignableFrom(type) == false)
continue;
// create & register listener
try
{
if (typeof(MonoBehaviour).IsAssignableFrom(type))
{
// listener is monobehaviour type
var el = new GameObject(attribure.Name, type).GetComponent(type) as MonoBehaviour;
el.transform.SetParent(transform);
Subscribe(el as ISubscriber);
}
else
{
// listener is class
var el = (ISubscriber)Activator.CreateInstance(type);
Subscribe(el);
}
}
catch (Exception e)
{
Debug.LogWarning(e);
}
}
}
}
// create static function listeners
if (CollectFunctions)
{
foreach (var type in types)
{
// check all static methods
foreach (var methodInfo in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))
{
try
{
// must be static
if (methodInfo.IsStatic == false)
continue;
// not generic
if (methodInfo.IsGenericMethod)
continue;
var attribure = methodInfo.GetCustomAttribute<ListenerAttribute>();
// not null & active attribute
if (attribure == null || attribure.Active == false)
continue;
var args = methodInfo.GetParameters();
// must have input parameter
if (args.Length != 1)
continue;
// create & register listener
var keyType = args[0].ParameterType;
var el = Activator.CreateInstance(
typeof(ListenerStaticFunction<>).MakeGenericType(keyType),
attribure.Name, methodInfo, attribure.Order) as ISubscriber;
Subscribe(el);
}
catch (Exception e)
{
Debug.LogWarning(e);
}
}
}
}
}
}
// =======================================================================
[ContextMenu("Log subscribers")]
public void LogSubscribers()
{
foreach (var subscriber in m_Impl.GetSubscribers())
Debug.Log(subscriber, subscriber.Target as MonoBehaviour);
}
}
}