com.wallstop-studios.dxmessaging 2.0.0-rc11 → 2.0.0-rc13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/.gitattributes +63 -63
  2. package/.github/workflows/npm-publish.yml +66 -0
  3. package/CHANGELOG.md.meta +7 -7
  4. package/Editor/Analyzers/Microsoft.CodeAnalysis.CSharp.dll.meta +33 -33
  5. package/Editor/Analyzers/Microsoft.CodeAnalysis.dll.meta +33 -33
  6. package/Editor/Analyzers/System.Reflection.Metadata.dll.meta +33 -33
  7. package/Editor/Analyzers/WallstopStudios.DxMessaging.SourceGenerators.dll.meta +71 -71
  8. package/Editor/Analyzers.meta +8 -8
  9. package/Editor/SetupCscRsp.cs +162 -146
  10. package/Editor/SetupCscRsp.cs.meta +2 -2
  11. package/Editor/WallstopStudios.DxMessaging.Editor.asmdef +17 -17
  12. package/Editor/WallstopStudios.DxMessaging.Editor.asmdef.meta +7 -7
  13. package/Editor.meta +8 -8
  14. package/LICENSE.md +6 -6
  15. package/LICENSE.md.meta +7 -7
  16. package/README.md +387 -363
  17. package/README.md.meta +7 -7
  18. package/Runtime/Core/Attributes/DxAutoMessageTypeAttribute.cs +7 -9
  19. package/Runtime/Core/Attributes/DxAutoMessageTypeAttribute.cs.meta +2 -2
  20. package/Runtime/Core/Attributes/DxBroadcastMessageAttribute.cs +7 -10
  21. package/Runtime/Core/Attributes/DxBroadcastMessageAttribute.cs.meta +2 -2
  22. package/Runtime/Core/Attributes/DxTargetedMessageAttribute.cs +7 -10
  23. package/Runtime/Core/Attributes/DxTargetedMessageAttribute.cs.meta +2 -2
  24. package/Runtime/Core/Attributes/DxUntargetedMessageAttribute.cs +7 -10
  25. package/Runtime/Core/Attributes/DxUntargetedMessageAttribute.cs.meta +2 -2
  26. package/Runtime/Core/Attributes.meta +2 -2
  27. package/Runtime/Core/Extensions/MessageExtensions.cs +342 -275
  28. package/Runtime/Core/Extensions/MessageExtensions.cs.meta +11 -11
  29. package/Runtime/Core/Extensions.meta +8 -8
  30. package/Runtime/Core/IMessage.cs +12 -12
  31. package/Runtime/Core/IMessage.cs.meta +11 -11
  32. package/Runtime/Core/InstanceId.cs +149 -149
  33. package/Runtime/Core/InstanceId.cs.meta +11 -11
  34. package/Runtime/Core/MessageBus/IMessageBus.cs +309 -309
  35. package/Runtime/Core/MessageBus/IMessageBus.cs.meta +11 -11
  36. package/Runtime/Core/MessageBus/MessageBus.cs +2243 -2084
  37. package/Runtime/Core/MessageBus/MessageBus.cs.meta +11 -11
  38. package/Runtime/Core/MessageBus/MessagingRegistration.cs +105 -100
  39. package/Runtime/Core/MessageBus/MessagingRegistration.cs.meta +11 -11
  40. package/Runtime/Core/MessageBus/RegistrationLog.cs +111 -111
  41. package/Runtime/Core/MessageBus/RegistrationLog.cs.meta +11 -11
  42. package/Runtime/Core/MessageBus.meta +8 -8
  43. package/Runtime/Core/MessageHandler.cs +2778 -2759
  44. package/Runtime/Core/MessageHandler.cs.meta +11 -11
  45. package/Runtime/Core/MessageRegistrationHandle.cs +110 -110
  46. package/Runtime/Core/MessageRegistrationHandle.cs.meta +11 -11
  47. package/Runtime/Core/MessageRegistrationToken.cs +1234 -1234
  48. package/Runtime/Core/MessageRegistrationToken.cs.meta +11 -11
  49. package/Runtime/Core/Messages/IBroadcastMessage.cs +27 -27
  50. package/Runtime/Core/Messages/IBroadcastMessage.cs.meta +11 -11
  51. package/Runtime/Core/Messages/ITargetedMessage.cs +27 -26
  52. package/Runtime/Core/Messages/ITargetedMessage.cs.meta +11 -11
  53. package/Runtime/Core/Messages/IUntargetedMessage.cs +27 -26
  54. package/Runtime/Core/Messages/IUntargetedMessage.cs.meta +11 -11
  55. package/Runtime/Core/Messages.meta +8 -8
  56. package/Runtime/Core/MessagingDebug.cs +70 -71
  57. package/Runtime/Core/MessagingDebug.cs.meta +11 -11
  58. package/Runtime/Core.meta +8 -8
  59. package/Runtime/Unity/MessageAwareComponent.cs +77 -77
  60. package/Runtime/Unity/MessageAwareComponent.cs.meta +11 -11
  61. package/Runtime/Unity/MessagingComponent.cs +99 -99
  62. package/Runtime/Unity/MessagingComponent.cs.meta +11 -11
  63. package/Runtime/Unity.meta +8 -8
  64. package/Runtime/WallstopStudios.DxMessaging.asmdef +15 -15
  65. package/Runtime.meta +8 -8
  66. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxAutoMessageTypeGenerator.cs +92 -75
  67. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxAutoMessageTypeGenerator.cs.meta +11 -11
  68. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxBroadcastMessageGenerator.cs +94 -77
  69. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxBroadcastMessageGenerator.cs.meta +2 -2
  70. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxTargetedMessageGenerator.cs +94 -77
  71. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxTargetedMessageGenerator.cs.meta +2 -2
  72. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxUntargetedMessageGenerator.cs +94 -78
  73. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxUntargetedMessageGenerator.cs.meta +2 -2
  74. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators.csproj +21 -21
  75. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators.csproj.meta +7 -7
  76. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators.meta +8 -8
  77. package/SourceGenerators.meta +8 -8
  78. package/Tests/Runtime/Benchmarks/PerformanceTests.cs +373 -310
  79. package/Tests/Runtime/Benchmarks/PerformanceTests.cs.meta +11 -11
  80. package/Tests/Runtime/Benchmarks.meta +8 -8
  81. package/Tests/Runtime/Core/BroadcastTests.cs +899 -899
  82. package/Tests/Runtime/Core/GlobalAcceptAllTests.cs +190 -169
  83. package/Tests/Runtime/Core/GlobalAcceptAllTests.cs.meta +11 -11
  84. package/Tests/Runtime/Core/MessagingTestBase.cs +185 -164
  85. package/Tests/Runtime/Core/MessagingTestBase.cs.meta +11 -11
  86. package/Tests/Runtime/Core/NominalTests.cs +1740 -1518
  87. package/Tests/Runtime/Core/PostProcessorTests.cs +1236 -849
  88. package/Tests/Runtime/Core/PostProcessorTests.cs.meta +11 -11
  89. package/Tests/Runtime/Core/RegistrationTests.cs +1266 -995
  90. package/Tests/Runtime/Core/RegistrationTests.cs.meta +11 -11
  91. package/Tests/Runtime/Core/TargetedTests.cs +888 -888
  92. package/Tests/Runtime/Core/TargetedTests.cs.meta +11 -11
  93. package/Tests/Runtime/Core/UntargetedTests.cs +272 -272
  94. package/Tests/Runtime/Core/UntargetedTests.cs.meta +11 -11
  95. package/Tests/Runtime/Scripts/Components/EmptyMessageAwareComponent.cs +9 -9
  96. package/Tests/Runtime/Scripts/Components/EmptyMessageAwareComponent.cs.meta +11 -11
  97. package/Tests/Runtime/Scripts/Components/SimpleMessageAwareComponent.cs +186 -140
  98. package/Tests/Runtime/Scripts/Messages/ComplexTargetedMessage.cs +30 -30
  99. package/Tests/Runtime/Scripts/Messages/SimpleBroadcastMessage.cs +8 -8
  100. package/Tests/Runtime/Scripts/Messages/SimpleTargetedMessage.cs +8 -8
  101. package/Tests/Runtime/Scripts/Messages/SimpleUntargetedMessage.cs +8 -8
  102. package/Tests/Runtime/WallstopStudios.DxMessaging.Tests.Runtime.asmdef +22 -22
  103. package/Tests/Runtime/WallstopStudios.DxMessaging.Tests.Runtime.csproj.meta +7 -7
  104. package/Tests.meta +8 -8
  105. package/Third Party Notices.md.meta +7 -7
  106. package/package.json +36 -36
  107. package/package.json.meta +7 -7
@@ -1,1234 +1,1234 @@
1
- namespace DxMessaging.Core
2
- {
3
- using System;
4
- using System.Collections.Generic;
5
- using MessageBus;
6
- using Messages;
7
-
8
- /// <summary>
9
- /// Maintains all the [de]registration logic for MessagingComponents. Wraps registrations up for lazy registration, which are executed on Enable() call.
10
- /// </summary>
11
- /// <note>
12
- /// General usage should be to create one of these on awake or start (probably start), and bind all messaging functions there.
13
- /// Then, on OnEnable(), call .Enable(), OnDisable(), call .Disable()
14
- /// </note>
15
- public sealed class MessageRegistrationToken
16
- {
17
- public bool Enabled => _enabled;
18
-
19
- private readonly MessageHandler _messageHandler;
20
-
21
- private readonly Dictionary<MessageRegistrationHandle, Action> _registrations = new();
22
- private readonly Dictionary<MessageRegistrationHandle, Action> _deregistrations = new();
23
-
24
- private readonly IMessageBus _messageBus;
25
- private bool _enabled;
26
-
27
- private MessageRegistrationToken(MessageHandler messageHandler, IMessageBus messageBus)
28
- {
29
- _enabled = false;
30
- _messageHandler =
31
- messageHandler ?? throw new ArgumentNullException(nameof(messageHandler));
32
- _messageBus = messageBus;
33
- }
34
-
35
- private MessageRegistrationHandle RegisterTargetedInternal<T>(
36
- InstanceId target,
37
- Action<T> targetedHandler,
38
- int priority = 0
39
- )
40
- where T : ITargetedMessage
41
- {
42
- if (_messageHandler == null) // Unity has a bug
43
- {
44
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
45
- }
46
- return InternalRegister(
47
- () =>
48
- _messageHandler.RegisterTargetedMessageHandler(
49
- target,
50
- targetedHandler,
51
- priority: priority,
52
- messageBus: _messageBus
53
- )
54
- );
55
- }
56
-
57
- private MessageRegistrationHandle RegisterTargetedInternal<T>(
58
- InstanceId target,
59
- MessageHandler.FastHandler<T> targetedHandler,
60
- int priority = 0
61
- )
62
- where T : ITargetedMessage
63
- {
64
- if (_messageHandler == null) // Unity has a bug
65
- {
66
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
67
- }
68
- return InternalRegister(
69
- () =>
70
- _messageHandler.RegisterTargetedMessageHandler(
71
- target,
72
- targetedHandler,
73
- priority: priority,
74
- messageBus: _messageBus
75
- )
76
- );
77
- }
78
-
79
- #if UNITY_2017_1_OR_NEWER
80
- /// <summary>
81
- /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
82
- /// </summary>
83
- /// <note>
84
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
85
- /// </note>
86
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
87
- /// <param name="target">Target of the TargetedMessages to consume.</param>
88
- /// <param name="targetedHandler">Actual handler functionality.</param>
89
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
90
- /// <returns>A handle that allows for registration and de-registration.</returns>
91
- public MessageRegistrationHandle RegisterGameObjectTargeted<T>(
92
- UnityEngine.GameObject target,
93
- Action<T> targetedHandler,
94
- int priority = 0
95
- )
96
- where T : ITargetedMessage
97
- {
98
- return RegisterTargetedInternal(target, targetedHandler, priority: priority);
99
- }
100
-
101
- /// <summary>
102
- /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
103
- /// </summary>
104
- /// <note>
105
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
106
- /// </note>
107
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
108
- /// <param name="target">Target of the TargetedMessages to consume.</param>
109
- /// <param name="targetedHandler">Actual handler functionality.</param>
110
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
111
- /// <returns>A handle that allows for registration and de-registration.</returns>
112
- public MessageRegistrationHandle RegisterGameObjectTargeted<T>(
113
- UnityEngine.GameObject target,
114
- MessageHandler.FastHandler<T> targetedHandler,
115
- int priority = 0
116
- )
117
- where T : ITargetedMessage
118
- {
119
- return RegisterTargetedInternal(target, targetedHandler, priority: priority);
120
- }
121
-
122
- /// <summary>
123
- /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
124
- /// </summary>
125
- /// <note>
126
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
127
- /// </note>
128
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
129
- /// <param name="target">Target of the TargetedMessages to consume.</param>
130
- /// <param name="targetedHandler">Actual handler functionality.</param>
131
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
132
- /// <returns>A handle that allows for registration and de-registration.</returns>
133
- public MessageRegistrationHandle RegisterComponentTargeted<T>(
134
- UnityEngine.Component target,
135
- Action<T> targetedHandler,
136
- int priority = 0
137
- )
138
- where T : ITargetedMessage
139
- {
140
- return RegisterTargetedInternal(target, targetedHandler, priority: priority);
141
- }
142
-
143
- /// <summary>
144
- /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
145
- /// </summary>
146
- /// <note>
147
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
148
- /// </note>
149
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
150
- /// <param name="target">Target of the TargetedMessages to consume.</param>
151
- /// <param name="targetedHandler">Actual handler functionality.</param>
152
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
153
- /// <returns>A handle that allows for registration and de-registration.</returns>
154
- public MessageRegistrationHandle RegisterComponentTargeted<T>(
155
- UnityEngine.Component target,
156
- MessageHandler.FastHandler<T> targetedHandler,
157
- int priority = 0
158
- )
159
- where T : ITargetedMessage
160
- {
161
- return RegisterTargetedInternal(target, targetedHandler, priority: priority);
162
- }
163
-
164
- /// <summary>
165
- /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
166
- /// </summary>
167
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
168
- /// <param name="target">Target to post process messages for.</param>
169
- /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
170
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
171
- /// <returns>A handle that allows for registration and de-registration.</returns>
172
- public MessageRegistrationHandle RegisterGameObjectTargetedPostProcessor<T>(
173
- UnityEngine.GameObject target,
174
- MessageHandler.FastHandler<T> targetedPostProcessor,
175
- int priority = 0
176
- )
177
- where T : ITargetedMessage
178
- {
179
- return InternalRegister(
180
- () =>
181
- _messageHandler.RegisterTargetedPostProcessor(
182
- target,
183
- targetedPostProcessor,
184
- priority,
185
- _messageBus
186
- )
187
- );
188
- }
189
-
190
- /// <summary>
191
- /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
192
- /// </summary>
193
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
194
- /// <param name="target">Target to post process messages for.</param>
195
- /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
196
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
197
- /// <returns>A handle that allows for registration and de-registration.</returns>
198
- public MessageRegistrationHandle RegisterComponentTargetedPostProcessor<T>(
199
- UnityEngine.Component target,
200
- MessageHandler.FastHandler<T> targetedPostProcessor,
201
- int priority = 0
202
- )
203
- where T : ITargetedMessage
204
- {
205
- return InternalRegister(
206
- () =>
207
- _messageHandler.RegisterTargetedPostProcessor(
208
- target,
209
- targetedPostProcessor,
210
- priority,
211
- _messageBus
212
- )
213
- );
214
- }
215
- #else
216
-
217
- /// <summary>
218
- /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
219
- /// </summary>
220
- /// <note>
221
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
222
- /// </note>
223
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
224
- /// <param name="target">Target of the TargetedMessages to consume.</param>
225
- /// <param name="targetedHandler">Actual handler functionality.</param>
226
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
227
- /// <returns>A handle that allows for registration and de-registration.</returns>
228
- public MessageRegistrationHandle RegisterTargeted<T>(
229
- InstanceId target,
230
- Action<T> targetedHandler,
231
- int priority = 0
232
- )
233
- where T : ITargetedMessage
234
- {
235
- return RegisterTargetedInternal(target, targetedHandler, priority: priority);
236
- }
237
-
238
- /// <summary>
239
- /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
240
- /// </summary>
241
- /// <note>
242
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
243
- /// </note>
244
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
245
- /// <param name="target">Target of the TargetedMessages to consume.</param>
246
- /// <param name="targetedHandler">Actual handler functionality.</param>
247
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
248
- /// <returns>A handle that allows for registration and de-registration.</returns>
249
- public MessageRegistrationHandle RegisterTargeted<T>(
250
- InstanceId target,
251
- MessageHandler.FastHandler<T> targetedHandler,
252
- int priority = 0
253
- )
254
- where T : ITargetedMessage
255
- {
256
- return RegisterTargetedInternal(target, targetedHandler, priority: priority);
257
- }
258
-
259
- /// <summary>
260
- /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
261
- /// </summary>
262
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
263
- /// <param name="target">Target to post process messages for.</param>
264
- /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
265
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
266
- /// <returns>A handle that allows for registration and de-registration.</returns>
267
- public MessageRegistrationHandle RegisterTargetedPostProcessor<T>(
268
- InstanceId target,
269
- MessageHandler.FastHandler<T> targetedPostProcessor,
270
- int priority = 0
271
- )
272
- where T : ITargetedMessage
273
- {
274
- return InternalRegister(
275
- () =>
276
- _messageHandler.RegisterTargetedPostProcessor(
277
- target,
278
- targetedPostProcessor,
279
- priority,
280
- _messageBus
281
- )
282
- );
283
- }
284
-
285
- /// <summary>
286
- /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
287
- /// </summary>
288
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
289
- /// <param name="target">Target to post process messages for.</param>
290
- /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
291
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
292
- /// <returns>A handle that allows for registration and de-registration.</returns>
293
- public MessageRegistrationHandle RegisterTargetedPostProcessor<T>(
294
- InstanceId target,
295
- Action<T> targetedPostProcessor,
296
- int priority = 0
297
- )
298
- where T : ITargetedMessage
299
- {
300
- return InternalRegister(
301
- () =>
302
- _messageHandler.RegisterTargetedPostProcessor(
303
- target,
304
- targetedPostProcessor,
305
- priority,
306
- _messageBus
307
- )
308
- );
309
- }
310
- #endif
311
-
312
- /// <summary>
313
- /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards anything (including itself).
314
- /// </summary>
315
- /// <note>
316
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
317
- /// </note>
318
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
319
- /// <param name="messageHandler">Actual handler functionality.</param>
320
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
321
- /// <returns>A handle that allows for registration and de-registration.</returns>
322
- public MessageRegistrationHandle RegisterTargetedWithoutTargeting<T>(
323
- Action<InstanceId, T> messageHandler,
324
- int priority = 0
325
- )
326
- where T : ITargetedMessage
327
- {
328
- if (_messageHandler == null) // Unity has a bug
329
- {
330
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
331
- }
332
- return InternalRegister(
333
- () =>
334
- _messageHandler.RegisterTargetedWithoutTargeting(
335
- messageHandler,
336
- priority: priority,
337
- messageBus: _messageBus
338
- )
339
- );
340
- }
341
-
342
- /// <summary>
343
- /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards anything (including itself).
344
- /// </summary>
345
- /// <note>
346
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
347
- /// </note>
348
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
349
- /// <param name="messageHandler">Actual handler functionality.</param>
350
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
351
- /// <returns>A handle that allows for registration and de-registration.</returns>
352
- public MessageRegistrationHandle RegisterTargetedWithoutTargeting<T>(
353
- MessageHandler.FastHandlerWithContext<T> messageHandler,
354
- int priority = 0
355
- )
356
- where T : ITargetedMessage
357
- {
358
- if (_messageHandler == null) // Unity has a bug
359
- {
360
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
361
- }
362
- return InternalRegister(
363
- () =>
364
- _messageHandler.RegisterTargetedWithoutTargeting(
365
- messageHandler,
366
- priority: priority,
367
- messageBus: _messageBus
368
- )
369
- );
370
- }
371
-
372
- /// <summary>
373
- /// Stages a registration of the provided MessageHandler to post process TargetedMessages of the given type targeted towards anything (including itself).
374
- /// </summary>
375
- /// <note>
376
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
377
- /// </note>
378
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
379
- /// <param name="postProcessor">Actual handler functionality.</param>
380
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
381
- /// <returns>A handle that allows for registration and de-registration.</returns>
382
- public MessageRegistrationHandle RegisterTargetedWithoutTargetingPostProcessor<T>(
383
- Action<InstanceId, T> postProcessor,
384
- int priority = 0
385
- )
386
- where T : ITargetedMessage
387
- {
388
- if (_messageHandler == null) // Unity has a bug
389
- {
390
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
391
- }
392
- return InternalRegister(
393
- () =>
394
- _messageHandler.RegisterTargetedWithoutTargetingPostProcessor(
395
- postProcessor,
396
- priority,
397
- _messageBus
398
- )
399
- );
400
- }
401
-
402
- /// <summary>
403
- /// Stages a registration of the provided MessageHandler to post process TargetedMessages of the given type targeted towards anything (including itself).
404
- /// </summary>
405
- /// <note>
406
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
407
- /// </note>
408
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
409
- /// <param name="postProcessor">Actual post processor functionality.</param>
410
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
411
- /// <returns>A handle that allows for registration and de-registration.</returns>
412
- public MessageRegistrationHandle RegisterTargetedWithoutTargetingPostProcessor<T>(
413
- MessageHandler.FastHandlerWithContext<T> postProcessor,
414
- int priority = 0
415
- )
416
- where T : ITargetedMessage
417
- {
418
- if (_messageHandler == null) // Unity has a bug
419
- {
420
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
421
- }
422
- return InternalRegister(
423
- () =>
424
- _messageHandler.RegisterTargetedWithoutTargetingPostProcessor(
425
- postProcessor,
426
- priority,
427
- _messageBus
428
- )
429
- );
430
- }
431
-
432
- /// <summary>
433
- /// Stages a registration of the provided MessageHandler to accept UntargetedMessages of the given type.
434
- /// </summary>
435
- /// <note>
436
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
437
- /// </note>
438
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
439
- /// <param name="untargetedHandler">Actual handler functionality.</param>
440
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
441
- /// <returns>A handle that allows for registration and de-registration.</returns>
442
- public MessageRegistrationHandle RegisterUntargeted<T>(
443
- Action<T> untargetedHandler,
444
- int priority = 0
445
- )
446
- where T : IUntargetedMessage
447
- {
448
- if (_messageHandler == null) // Unity has a bug
449
- {
450
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
451
- }
452
- return InternalRegister(
453
- () =>
454
- _messageHandler.RegisterUntargetedMessageHandler(
455
- untargetedHandler,
456
- priority: priority,
457
- messageBus: _messageBus
458
- )
459
- );
460
- }
461
-
462
- /// <summary>
463
- /// Stages a registration of the provided MessageHandler to accept UntargetedMessages of the given type.
464
- /// </summary>
465
- /// <note>
466
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
467
- /// </note>
468
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
469
- /// <param name="untargetedHandler">Actual handler functionality.</param>
470
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
471
- /// <returns>A handle that allows for registration and de-registration.</returns>
472
- public MessageRegistrationHandle RegisterUntargeted<T>(
473
- MessageHandler.FastHandler<T> untargetedHandler,
474
- int priority = 0
475
- )
476
- where T : IUntargetedMessage
477
- {
478
- if (_messageHandler == null) // Unity has a bug
479
- {
480
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
481
- }
482
- return InternalRegister(
483
- () =>
484
- _messageHandler.RegisterUntargetedMessageHandler(
485
- untargetedHandler,
486
- priority: priority,
487
- messageBus: _messageBus
488
- )
489
- );
490
- }
491
-
492
- /// <summary>
493
- /// Stages a registration of the provided PostProcessor to post process UntargetedMessages of the given type.
494
- /// </summary>
495
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
496
- /// <param name="untargetedPostProcessor">Actual post processor functionality.</param>
497
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
498
- /// <returns>A handle that allows for registration and de-registration.</returns>
499
- public MessageRegistrationHandle RegisterUntargetedPostProcessor<T>(
500
- MessageHandler.FastHandler<T> untargetedPostProcessor,
501
- int priority = 0
502
- )
503
- where T : IUntargetedMessage
504
- {
505
- if (_messageHandler == null)
506
- {
507
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
508
- }
509
- return InternalRegister(
510
- () =>
511
- _messageHandler.RegisterUntargetedPostProcessor(
512
- untargetedPostProcessor,
513
- priority,
514
- _messageBus
515
- )
516
- );
517
- }
518
-
519
- private MessageRegistrationHandle RegisterBroadcastInternal<T>(
520
- InstanceId source,
521
- Action<T> broadcastHandler,
522
- int priority = 0
523
- )
524
- where T : IBroadcastMessage
525
- {
526
- if (_messageHandler == null) // Unity has a bug
527
- {
528
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
529
- }
530
- return InternalRegister(
531
- () =>
532
- _messageHandler.RegisterSourcedBroadcastMessageHandler(
533
- source,
534
- broadcastHandler,
535
- priority: priority,
536
- messageBus: _messageBus
537
- )
538
- );
539
- }
540
-
541
- private MessageRegistrationHandle RegisterBroadcastInternal<T>(
542
- InstanceId source,
543
- MessageHandler.FastHandler<T> broadcastHandler,
544
- int priority = 0
545
- )
546
- where T : IBroadcastMessage
547
- {
548
- if (_messageHandler == null) // Unity has a bug
549
- {
550
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
551
- }
552
- return InternalRegister(
553
- () =>
554
- _messageHandler.RegisterSourcedBroadcastMessageHandler(
555
- source,
556
- broadcastHandler,
557
- priority: priority,
558
- messageBus: _messageBus
559
- )
560
- );
561
- }
562
-
563
- private MessageRegistrationHandle RegisterBroadcastPostProcessorInternal<T>(
564
- InstanceId source,
565
- Action<T> broadcastPostProcessor,
566
- int priority
567
- )
568
- where T : IBroadcastMessage
569
- {
570
- if (_messageHandler == null)
571
- {
572
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
573
- }
574
- return InternalRegister(
575
- () =>
576
- _messageHandler.RegisterSourcedBroadcastPostProcessor(
577
- source,
578
- broadcastPostProcessor,
579
- priority,
580
- _messageBus
581
- )
582
- );
583
- }
584
-
585
- private MessageRegistrationHandle RegisterBroadcastPostProcessorInternal<T>(
586
- InstanceId source,
587
- MessageHandler.FastHandler<T> broadcastPostProcessor,
588
- int priority
589
- )
590
- where T : IBroadcastMessage
591
- {
592
- if (_messageHandler == null)
593
- {
594
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
595
- }
596
- return InternalRegister(
597
- () =>
598
- _messageHandler.RegisterSourcedBroadcastPostProcessor(
599
- source,
600
- broadcastPostProcessor,
601
- priority,
602
- _messageBus
603
- )
604
- );
605
- }
606
-
607
- #if UNITY_2017_1_OR_NEWER
608
- /// <summary>
609
- /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
610
- /// </summary>
611
- /// <note>
612
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
613
- /// </note>
614
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
615
- /// <param name="source">Id of the source for BroadcastMessages to listen for.</param>
616
- /// <param name="broadcastHandler">Actual handler functionality.</param>
617
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
618
- /// <returns>A handle that allows for registration and de-registration.</returns>
619
- public MessageRegistrationHandle RegisterGameObjectBroadcast<T>(
620
- UnityEngine.GameObject source,
621
- Action<T> broadcastHandler,
622
- int priority = 0
623
- )
624
- where T : IBroadcastMessage
625
- {
626
- return RegisterBroadcastInternal(source, broadcastHandler, priority: priority);
627
- }
628
-
629
- /// <summary>
630
- /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
631
- /// </summary>
632
- /// <note>
633
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
634
- /// </note>
635
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
636
- /// <param name="source">Id of the source for BroadcastMessages to listen for.</param>
637
- /// <param name="broadcastHandler">Actual handler functionality.</param>
638
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
639
- /// <returns>A handle that allows for registration and de-registration.</returns>
640
- public MessageRegistrationHandle RegisterGameObjectBroadcast<T>(
641
- UnityEngine.GameObject source,
642
- MessageHandler.FastHandler<T> broadcastHandler,
643
- int priority = 0
644
- )
645
- where T : IBroadcastMessage
646
- {
647
- return RegisterBroadcastInternal(source, broadcastHandler, priority: priority);
648
- }
649
-
650
- /// <summary>
651
- /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given GameObject.
652
- /// </summary>
653
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
654
- /// <param name="source">Source of the messages.</param>
655
- /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
656
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
657
- /// <returns>A handle that allows for registration and de-registration.</returns>
658
- public MessageRegistrationHandle RegisterGameObjectBroadcastPostProcessor<T>(
659
- UnityEngine.GameObject source,
660
- Action<T> broadcastPostProcessor,
661
- int priority = 0
662
- )
663
- where T : IBroadcastMessage
664
- {
665
- return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcessor, priority);
666
- }
667
-
668
- /// <summary>
669
- /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given GameObject.
670
- /// </summary>
671
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
672
- /// <param name="source">Source of the messages.</param>
673
- /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
674
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
675
- /// <returns>A handle that allows for registration and de-registration.</returns>
676
- public MessageRegistrationHandle RegisterGameObjectBroadcastPostProcessor<T>(
677
- UnityEngine.GameObject source,
678
- MessageHandler.FastHandler<T> broadcastPostProcessor,
679
- int priority = 0
680
- )
681
- where T : IBroadcastMessage
682
- {
683
- return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcessor, priority);
684
- }
685
-
686
- /// <summary>
687
- /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
688
- /// </summary>
689
- /// <note>
690
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
691
- /// </note>
692
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
693
- /// <param name="source">The component source for BroadcastMessages to listen for.</param>
694
- /// <param name="broadcastHandler">Actual handler functionality.</param>
695
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
696
- /// <returns>A handle that allows for registration and de-registration.</returns>
697
- public MessageRegistrationHandle RegisterComponentBroadcast<T>(
698
- UnityEngine.Component source,
699
- Action<T> broadcastHandler,
700
- int priority = 0
701
- )
702
- where T : IBroadcastMessage
703
- {
704
- return RegisterBroadcastInternal(source, broadcastHandler, priority);
705
- }
706
-
707
- /// <summary>
708
- /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
709
- /// </summary>
710
- /// <note>
711
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
712
- /// </note>
713
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
714
- /// <param name="source">The component source for BroadcastMessages to listen for.</param>
715
- /// <param name="broadcastHandler">Actual handler functionality.</param>
716
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
717
- /// <returns>A handle that allows for registration and de-registration.</returns>
718
- public MessageRegistrationHandle RegisterComponentBroadcast<T>(
719
- UnityEngine.Component source,
720
- MessageHandler.FastHandler<T> broadcastHandler,
721
- int priority = 0
722
- )
723
- where T : IBroadcastMessage
724
- {
725
- return RegisterBroadcastInternal(source, broadcastHandler, priority);
726
- }
727
-
728
- /// <summary>
729
- /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given component.
730
- /// </summary>
731
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
732
- /// <param name="source">Source of the messages.</param>
733
- /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
734
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
735
- /// <returns>A handle that allows for registration and de-registration.</returns>
736
- public MessageRegistrationHandle RegisterComponentBroadcastPostProcessor<T>(
737
- UnityEngine.Component source,
738
- Action<T> broadcastPostProcessor,
739
- int priority = 0
740
- )
741
- where T : IBroadcastMessage
742
- {
743
- if (_messageHandler == null)
744
- {
745
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
746
- }
747
- return InternalRegister(
748
- () =>
749
- _messageHandler.RegisterSourcedBroadcastPostProcessor(
750
- source,
751
- broadcastPostProcessor,
752
- priority: priority,
753
- _messageBus
754
- )
755
- );
756
- }
757
-
758
- /// <summary>
759
- /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given component.
760
- /// </summary>
761
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
762
- /// <param name="source">Source of the messages.</param>
763
- /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
764
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
765
- /// <returns>A handle that allows for registration and de-registration.</returns>
766
- public MessageRegistrationHandle RegisterComponentBroadcastPostProcessor<T>(
767
- UnityEngine.Component source,
768
- MessageHandler.FastHandler<T> broadcastPostProcessor,
769
- int priority = 0
770
- )
771
- where T : IBroadcastMessage
772
- {
773
- if (_messageHandler == null)
774
- {
775
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
776
- }
777
- return InternalRegister(
778
- () =>
779
- _messageHandler.RegisterSourcedBroadcastPostProcessor(
780
- source,
781
- broadcastPostProcessor,
782
- priority: priority,
783
- _messageBus
784
- )
785
- );
786
- }
787
- #else
788
-
789
- /// <summary>
790
- /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
791
- /// </summary>
792
- /// <note>
793
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
794
- /// </note>
795
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
796
- /// <param name="source">Source of the messages.</param>
797
- /// <param name="broadcastHandler">Actual handler functionality.</param>
798
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
799
- /// <returns>A handle that allows for registration and de-registration.</returns>
800
- public MessageRegistrationHandle RegisterBroadcast<T>(
801
- InstanceId source,
802
- Action<T> broadcastHandler,
803
- int priority = 0
804
- )
805
- where T : IBroadcastMessage
806
- {
807
- return RegisterBroadcastInternal(source, broadcastHandler, priority: priority);
808
- }
809
-
810
- /// <summary>
811
- /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
812
- /// </summary>
813
- /// <note>
814
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
815
- /// </note>
816
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
817
- /// <param name="source">Source of the messages.</param>
818
- /// <param name="broadcastHandler">Actual handler functionality.</param>
819
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
820
- /// <returns>A handle that allows for registration and de-registration.</returns>
821
- public MessageRegistrationHandle RegisterBroadcast<T>(
822
- InstanceId source,
823
- MessageHandler.FastHandler<T> broadcastHandler,
824
- int priority = 0
825
- )
826
- where T : IBroadcastMessage
827
- {
828
- return RegisterBroadcastInternal(source, broadcastHandler, priority: priority);
829
- }
830
-
831
- /// <summary>
832
- /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given source.
833
- /// </summary>
834
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
835
- /// <param name="source">Source of the messages.</param>
836
- /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
837
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
838
- /// <returns>A handle that allows for registration and de-registration.</returns>
839
- public MessageRegistrationHandle RegisterBroadcastPostProcessor<T>(
840
- InstanceId source,
841
- Action<T> broadcastPostProcessor,
842
- int priority = 0
843
- )
844
- where T : IBroadcastMessage
845
- {
846
- return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcessor, priority);
847
- }
848
-
849
- /// <summary>
850
- /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given source.
851
- /// </summary>
852
- /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
853
- /// <param name="source">Source of the messages.</param>
854
- /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
855
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
856
- /// <returns>A handle that allows for registration and de-registration.</returns>
857
- public MessageRegistrationHandle RegisterBroadcastPostProcessor<T>(
858
- InstanceId source,
859
- MessageHandler.FastHandler<T> broadcastPostProcessor,
860
- int priority = 0
861
- )
862
- where T : IBroadcastMessage
863
- {
864
- return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcessor, priority);
865
- }
866
- #endif
867
-
868
- /// <summary>
869
- /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
870
- /// </summary>
871
- /// <note>
872
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
873
- /// </note>
874
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
875
- /// <param name="broadcastHandler">Action handler functionality.</param>
876
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
877
- /// <returns>A handle that allows for registration and de-registration.</returns>
878
- public MessageRegistrationHandle RegisterBroadcastWithoutSource<T>(
879
- Action<InstanceId, T> broadcastHandler,
880
- int priority = 0
881
- )
882
- where T : IBroadcastMessage
883
- {
884
- if (_messageHandler == null) // Unity has a bug
885
- {
886
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
887
- }
888
-
889
- return InternalRegister(
890
- () =>
891
- _messageHandler.RegisterSourcedBroadcastWithoutSource(
892
- broadcastHandler,
893
- priority: priority,
894
- messageBus: _messageBus
895
- )
896
- );
897
- }
898
-
899
- /// <summary>
900
- /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
901
- /// </summary>
902
- /// <note>
903
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
904
- /// </note>
905
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
906
- /// <param name="broadcastHandler">Action handler functionality.</param>
907
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
908
- /// <returns>A handle that allows for registration and de-registration.</returns>
909
- public MessageRegistrationHandle RegisterBroadcastWithoutSource<T>(
910
- MessageHandler.FastHandlerWithContext<T> broadcastHandler,
911
- int priority = 0
912
- )
913
- where T : IBroadcastMessage
914
- {
915
- if (_messageHandler == null) // Unity has a bug
916
- {
917
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
918
- }
919
-
920
- return InternalRegister(
921
- () =>
922
- _messageHandler.RegisterSourcedBroadcastWithoutSource(
923
- broadcastHandler,
924
- priority: priority,
925
- messageBus: _messageBus
926
- )
927
- );
928
- }
929
-
930
- /// <summary>
931
- /// Stages a registration of the provided MessageHandler to post process BroadcastMessages of the given type.
932
- /// </summary>
933
- /// <note>
934
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
935
- /// </note>
936
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
937
- /// <param name="broadcastHandler">Actual post process functionality.</param>
938
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
939
- /// <returns>A handle that allows for registration and de-registration.</returns>
940
- public MessageRegistrationHandle RegisterBroadcastWithoutSourcePostProcessor<T>(
941
- Action<InstanceId, T> broadcastHandler,
942
- int priority = 0
943
- )
944
- where T : IBroadcastMessage
945
- {
946
- if (_messageHandler == null) // Unity has a bug
947
- {
948
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
949
- }
950
-
951
- return InternalRegister(
952
- () =>
953
- _messageHandler.RegisterSourcedBroadcastWithoutSourcePostProcessor(
954
- broadcastHandler,
955
- priority: priority,
956
- _messageBus
957
- )
958
- );
959
- }
960
-
961
- /// <summary>
962
- /// Stages a registration of the provided MessageHandler to post post process BroadcastMessages of the given type.
963
- /// </summary>
964
- /// <note>
965
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
966
- /// </note>
967
- /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
968
- /// <param name="broadcastHandler">Actual post process functionality.</param>
969
- /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
970
- /// <returns>A handle that allows for registration and de-registration.</returns>
971
- public MessageRegistrationHandle RegisterBroadcastWithoutSourcePostProcessor<T>(
972
- MessageHandler.FastHandlerWithContext<T> broadcastHandler,
973
- int priority = 0
974
- )
975
- where T : IBroadcastMessage
976
- {
977
- if (_messageHandler == null) // Unity has a bug
978
- {
979
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
980
- }
981
-
982
- return InternalRegister(
983
- () =>
984
- _messageHandler.RegisterSourcedBroadcastWithoutSourcePostProcessor(
985
- broadcastHandler,
986
- priority: priority,
987
- _messageBus
988
- )
989
- );
990
- }
991
-
992
- /// <summary>
993
- /// Stages a registration of the provided MessageHandler to accept every message that is broadcast.
994
- /// </summary>
995
- /// <note>
996
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
997
- /// </note>
998
- /// <param name="acceptAllUntargeted">Action handler functionality for UntargetedMessages.</param>
999
- /// <param name="acceptAllTargeted">Action handler functionality for TargetedMessages.</param>
1000
- /// <param name="acceptAllBroadcast">Action handler functionality for BroadcastMessages.</param>
1001
- /// <returns>A handle that allows for registration and de-registration.</returns>
1002
- public MessageRegistrationHandle RegisterGlobalAcceptAll(
1003
- Action<IUntargetedMessage> acceptAllUntargeted,
1004
- Action<InstanceId, ITargetedMessage> acceptAllTargeted,
1005
- Action<InstanceId, IBroadcastMessage> acceptAllBroadcast
1006
- )
1007
- {
1008
- if (_messageHandler == null) // Unity has a bug
1009
- {
1010
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1011
- }
1012
- return InternalRegister(
1013
- () =>
1014
- _messageHandler.RegisterGlobalAcceptAll(
1015
- acceptAllUntargeted,
1016
- acceptAllTargeted,
1017
- acceptAllBroadcast,
1018
- _messageBus
1019
- )
1020
- );
1021
- }
1022
-
1023
- /// <summary>
1024
- /// Stages a registration of the provided MessageHandler to accept every message that is broadcast.
1025
- /// </summary>
1026
- /// <note>
1027
- /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
1028
- /// </note>
1029
- /// <param name="acceptAllUntargeted">Action handler functionality for UntargetedMessages.</param>
1030
- /// <param name="acceptAllTargeted">Action handler functionality for TargetedMessages.</param>
1031
- /// <param name="acceptAllBroadcast">Action handler functionality for BroadcastMessages.</param>
1032
- /// <returns>A handle that allows for registration and de-registration.</returns>
1033
- public MessageRegistrationHandle RegisterGlobalAcceptAll(
1034
- MessageHandler.FastHandler<IUntargetedMessage> acceptAllUntargeted,
1035
- MessageHandler.FastHandlerWithContext<ITargetedMessage> acceptAllTargeted,
1036
- MessageHandler.FastHandlerWithContext<IBroadcastMessage> acceptAllBroadcast
1037
- )
1038
- {
1039
- if (_messageHandler == null) // Unity has a bug
1040
- {
1041
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1042
- }
1043
- return InternalRegister(
1044
- () =>
1045
- _messageHandler.RegisterGlobalAcceptAll(
1046
- acceptAllUntargeted,
1047
- acceptAllTargeted,
1048
- acceptAllBroadcast,
1049
- _messageBus
1050
- )
1051
- );
1052
- }
1053
-
1054
- public MessageRegistrationHandle RegisterUntargetedInterceptor<T>(
1055
- IMessageBus.UntargetedInterceptor<T> interceptor,
1056
- int priority = 0
1057
- )
1058
- where T : IUntargetedMessage
1059
- {
1060
- if (_messageHandler == null)
1061
- {
1062
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1063
- }
1064
-
1065
- return InternalRegister(
1066
- () => _messageHandler.RegisterUntargetedInterceptor(interceptor, priority)
1067
- );
1068
- }
1069
-
1070
- public MessageRegistrationHandle RegisterBroadcastInterceptor<T>(
1071
- IMessageBus.BroadcastInterceptor<T> interceptor,
1072
- int priority = 0
1073
- )
1074
- where T : IBroadcastMessage
1075
- {
1076
- if (_messageHandler == null)
1077
- {
1078
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1079
- }
1080
-
1081
- return InternalRegister(
1082
- () => _messageHandler.RegisterBroadcastInterceptor(interceptor, priority)
1083
- );
1084
- }
1085
-
1086
- public MessageRegistrationHandle RegisterTargetedInterceptor<T>(
1087
- IMessageBus.TargetedInterceptor<T> interceptor,
1088
- int priority = 0
1089
- )
1090
- where T : ITargetedMessage
1091
- {
1092
- if (_messageHandler == null)
1093
- {
1094
- return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1095
- }
1096
-
1097
- return InternalRegister(
1098
- () => _messageHandler.RegisterTargetedInterceptor(interceptor, priority)
1099
- );
1100
- }
1101
-
1102
- /// <summary>
1103
- /// Handles the actual [de]registration wrapping and (potential) lazy execution.
1104
- /// </summary>
1105
- /// <param name="registerAndGetDeregistration">Proxied registration function that returns a de-registration function.</param>
1106
- /// <returns>A handle that allows for registration and de-registration.</returns>
1107
- private MessageRegistrationHandle InternalRegister(
1108
- Func<Action> registerAndGetDeregistration
1109
- )
1110
- {
1111
- MessageRegistrationHandle handle =
1112
- MessageRegistrationHandle.CreateMessageRegistrationHandle();
1113
-
1114
- _registrations[handle] = Registration;
1115
-
1116
- // Generally, registrations should take place before all calls to enable. Just in case, though...
1117
- if (_enabled)
1118
- {
1119
- Registration();
1120
- }
1121
-
1122
- return handle;
1123
-
1124
- // We don't want to actually register at this time (might not be awake/enabled) - so we wrap that shit up, to lazy register when we're enabled.
1125
- void Registration()
1126
- {
1127
- Action actualDeregistration = registerAndGetDeregistration();
1128
- _deregistrations[handle] = actualDeregistration;
1129
- }
1130
- }
1131
-
1132
- /// <summary>
1133
- /// Enables the token if not already enabled. Executes all staged registrations.
1134
- /// </summary>
1135
- /// <note>
1136
- /// Idempotent.
1137
- /// </note>
1138
- public void Enable()
1139
- {
1140
- if (_enabled)
1141
- {
1142
- return;
1143
- }
1144
-
1145
- if (_registrations is { Count: > 0 })
1146
- {
1147
- foreach (Action registrationAction in _registrations.Values)
1148
- {
1149
- registrationAction();
1150
- }
1151
- }
1152
-
1153
- _enabled = true;
1154
- }
1155
-
1156
- /// <summary>
1157
- /// Disables the token if not already disabled. Executes all staged de-registrations.
1158
- /// </summary>
1159
- /// <note>
1160
- /// Idempotent.
1161
- /// </note>
1162
- public void Disable()
1163
- {
1164
- if (!_enabled)
1165
- {
1166
- return;
1167
- }
1168
-
1169
- if (_deregistrations is { Count: > 0 })
1170
- {
1171
- foreach (Action deregistrationAction in _deregistrations.Values)
1172
- {
1173
- deregistrationAction();
1174
- }
1175
- }
1176
-
1177
- // ReSharper disable once ForCanBeConvertedToForeach
1178
-
1179
-
1180
- _enabled = false;
1181
- }
1182
-
1183
- /// <summary>
1184
- /// Disables the token and clears all registrations + de-registrations
1185
- /// </summary>
1186
- public void UnregisterAll()
1187
- {
1188
- if (_enabled && _deregistrations is { Count: > 0 })
1189
- {
1190
- foreach (Action deregistrationAction in _deregistrations.Values)
1191
- {
1192
- deregistrationAction();
1193
- }
1194
- }
1195
-
1196
- _enabled = false;
1197
- _registrations?.Clear();
1198
- _deregistrations?.Clear();
1199
- }
1200
-
1201
- public void RemoveRegistration(MessageRegistrationHandle handle)
1202
- {
1203
- if (
1204
- _deregistrations != null
1205
- && _deregistrations.TryGetValue(handle, out Action deregistrationAction)
1206
- )
1207
- {
1208
- deregistrationAction();
1209
- _ = _deregistrations.Remove(handle);
1210
- }
1211
-
1212
- _ = _registrations?.Remove(handle);
1213
- }
1214
-
1215
- /// <summary>
1216
- /// Creates a MessagingRegistrationToken that operates on the given handler.
1217
- /// </summary>
1218
- /// <param name="messageHandler">Message handler to register handlers to.</param>
1219
- /// <param name="messageBus">MessageBus to use for this MessageRegistrationToken. Uses the GlobalMessageBus if left null.</param>
1220
- /// <returns>MessagingRegistrationToken bound to the MessageHandler.</returns>
1221
- public static MessageRegistrationToken Create(
1222
- MessageHandler messageHandler,
1223
- IMessageBus messageBus = null
1224
- )
1225
- {
1226
- if (messageHandler == null)
1227
- {
1228
- throw new ArgumentNullException(nameof(messageHandler));
1229
- }
1230
-
1231
- return new MessageRegistrationToken(messageHandler, messageBus);
1232
- }
1233
- }
1234
- }
1
+ namespace DxMessaging.Core
2
+ {
3
+ using System;
4
+ using System.Collections.Generic;
5
+ using MessageBus;
6
+ using Messages;
7
+
8
+ /// <summary>
9
+ /// Maintains all the [de]registration logic for MessagingComponents. Wraps registrations up for lazy registration, which are executed on Enable() call.
10
+ /// </summary>
11
+ /// <note>
12
+ /// General usage should be to create one of these on awake or start (probably start), and bind all messaging functions there.
13
+ /// Then, on OnEnable(), call .Enable(), OnDisable(), call .Disable()
14
+ /// </note>
15
+ public sealed class MessageRegistrationToken
16
+ {
17
+ public bool Enabled => _enabled;
18
+
19
+ private readonly MessageHandler _messageHandler;
20
+
21
+ private readonly Dictionary<MessageRegistrationHandle, Action> _registrations = new();
22
+ private readonly Dictionary<MessageRegistrationHandle, Action> _deregistrations = new();
23
+
24
+ private readonly IMessageBus _messageBus;
25
+ private bool _enabled;
26
+
27
+ private MessageRegistrationToken(MessageHandler messageHandler, IMessageBus messageBus)
28
+ {
29
+ _enabled = false;
30
+ _messageHandler =
31
+ messageHandler ?? throw new ArgumentNullException(nameof(messageHandler));
32
+ _messageBus = messageBus;
33
+ }
34
+
35
+ private MessageRegistrationHandle RegisterTargetedInternal<T>(
36
+ InstanceId target,
37
+ Action<T> targetedHandler,
38
+ int priority = 0
39
+ )
40
+ where T : ITargetedMessage
41
+ {
42
+ if (_messageHandler == null) // Unity has a bug
43
+ {
44
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
45
+ }
46
+ return InternalRegister(
47
+ () =>
48
+ _messageHandler.RegisterTargetedMessageHandler(
49
+ target,
50
+ targetedHandler,
51
+ priority: priority,
52
+ messageBus: _messageBus
53
+ )
54
+ );
55
+ }
56
+
57
+ private MessageRegistrationHandle RegisterTargetedInternal<T>(
58
+ InstanceId target,
59
+ MessageHandler.FastHandler<T> targetedHandler,
60
+ int priority = 0
61
+ )
62
+ where T : ITargetedMessage
63
+ {
64
+ if (_messageHandler == null) // Unity has a bug
65
+ {
66
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
67
+ }
68
+ return InternalRegister(
69
+ () =>
70
+ _messageHandler.RegisterTargetedMessageHandler(
71
+ target,
72
+ targetedHandler,
73
+ priority: priority,
74
+ messageBus: _messageBus
75
+ )
76
+ );
77
+ }
78
+
79
+ #if UNITY_2017_1_OR_NEWER
80
+ /// <summary>
81
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
82
+ /// </summary>
83
+ /// <note>
84
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
85
+ /// </note>
86
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
87
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
88
+ /// <param name="targetedHandler">Actual handler functionality.</param>
89
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
90
+ /// <returns>A handle that allows for registration and de-registration.</returns>
91
+ public MessageRegistrationHandle RegisterGameObjectTargeted<T>(
92
+ UnityEngine.GameObject target,
93
+ Action<T> targetedHandler,
94
+ int priority = 0
95
+ )
96
+ where T : ITargetedMessage
97
+ {
98
+ return RegisterTargetedInternal(target, targetedHandler, priority: priority);
99
+ }
100
+
101
+ /// <summary>
102
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
103
+ /// </summary>
104
+ /// <note>
105
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
106
+ /// </note>
107
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
108
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
109
+ /// <param name="targetedHandler">Actual handler functionality.</param>
110
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
111
+ /// <returns>A handle that allows for registration and de-registration.</returns>
112
+ public MessageRegistrationHandle RegisterGameObjectTargeted<T>(
113
+ UnityEngine.GameObject target,
114
+ MessageHandler.FastHandler<T> targetedHandler,
115
+ int priority = 0
116
+ )
117
+ where T : ITargetedMessage
118
+ {
119
+ return RegisterTargetedInternal(target, targetedHandler, priority: priority);
120
+ }
121
+
122
+ /// <summary>
123
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
124
+ /// </summary>
125
+ /// <note>
126
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
127
+ /// </note>
128
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
129
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
130
+ /// <param name="targetedHandler">Actual handler functionality.</param>
131
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
132
+ /// <returns>A handle that allows for registration and de-registration.</returns>
133
+ public MessageRegistrationHandle RegisterComponentTargeted<T>(
134
+ UnityEngine.Component target,
135
+ Action<T> targetedHandler,
136
+ int priority = 0
137
+ )
138
+ where T : ITargetedMessage
139
+ {
140
+ return RegisterTargetedInternal(target, targetedHandler, priority: priority);
141
+ }
142
+
143
+ /// <summary>
144
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
145
+ /// </summary>
146
+ /// <note>
147
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
148
+ /// </note>
149
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
150
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
151
+ /// <param name="targetedHandler">Actual handler functionality.</param>
152
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
153
+ /// <returns>A handle that allows for registration and de-registration.</returns>
154
+ public MessageRegistrationHandle RegisterComponentTargeted<T>(
155
+ UnityEngine.Component target,
156
+ MessageHandler.FastHandler<T> targetedHandler,
157
+ int priority = 0
158
+ )
159
+ where T : ITargetedMessage
160
+ {
161
+ return RegisterTargetedInternal(target, targetedHandler, priority: priority);
162
+ }
163
+
164
+ /// <summary>
165
+ /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
166
+ /// </summary>
167
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
168
+ /// <param name="target">Target to post process messages for.</param>
169
+ /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
170
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
171
+ /// <returns>A handle that allows for registration and de-registration.</returns>
172
+ public MessageRegistrationHandle RegisterGameObjectTargetedPostProcessor<T>(
173
+ UnityEngine.GameObject target,
174
+ MessageHandler.FastHandler<T> targetedPostProcessor,
175
+ int priority = 0
176
+ )
177
+ where T : ITargetedMessage
178
+ {
179
+ return InternalRegister(
180
+ () =>
181
+ _messageHandler.RegisterTargetedPostProcessor(
182
+ target,
183
+ targetedPostProcessor,
184
+ priority,
185
+ _messageBus
186
+ )
187
+ );
188
+ }
189
+
190
+ /// <summary>
191
+ /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
192
+ /// </summary>
193
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
194
+ /// <param name="target">Target to post process messages for.</param>
195
+ /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
196
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
197
+ /// <returns>A handle that allows for registration and de-registration.</returns>
198
+ public MessageRegistrationHandle RegisterComponentTargetedPostProcessor<T>(
199
+ UnityEngine.Component target,
200
+ MessageHandler.FastHandler<T> targetedPostProcessor,
201
+ int priority = 0
202
+ )
203
+ where T : ITargetedMessage
204
+ {
205
+ return InternalRegister(
206
+ () =>
207
+ _messageHandler.RegisterTargetedPostProcessor(
208
+ target,
209
+ targetedPostProcessor,
210
+ priority,
211
+ _messageBus
212
+ )
213
+ );
214
+ }
215
+ #else
216
+
217
+ /// <summary>
218
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
219
+ /// </summary>
220
+ /// <note>
221
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
222
+ /// </note>
223
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
224
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
225
+ /// <param name="targetedHandler">Actual handler functionality.</param>
226
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
227
+ /// <returns>A handle that allows for registration and de-registration.</returns>
228
+ public MessageRegistrationHandle RegisterTargeted<T>(
229
+ InstanceId target,
230
+ Action<T> targetedHandler,
231
+ int priority = 0
232
+ )
233
+ where T : ITargetedMessage
234
+ {
235
+ return RegisterTargetedInternal(target, targetedHandler, priority: priority);
236
+ }
237
+
238
+ /// <summary>
239
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
240
+ /// </summary>
241
+ /// <note>
242
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
243
+ /// </note>
244
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
245
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
246
+ /// <param name="targetedHandler">Actual handler functionality.</param>
247
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
248
+ /// <returns>A handle that allows for registration and de-registration.</returns>
249
+ public MessageRegistrationHandle RegisterTargeted<T>(
250
+ InstanceId target,
251
+ MessageHandler.FastHandler<T> targetedHandler,
252
+ int priority = 0
253
+ )
254
+ where T : ITargetedMessage
255
+ {
256
+ return RegisterTargetedInternal(target, targetedHandler, priority: priority);
257
+ }
258
+
259
+ /// <summary>
260
+ /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
261
+ /// </summary>
262
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
263
+ /// <param name="target">Target to post process messages for.</param>
264
+ /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
265
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
266
+ /// <returns>A handle that allows for registration and de-registration.</returns>
267
+ public MessageRegistrationHandle RegisterTargetedPostProcessor<T>(
268
+ InstanceId target,
269
+ MessageHandler.FastHandler<T> targetedPostProcessor,
270
+ int priority = 0
271
+ )
272
+ where T : ITargetedMessage
273
+ {
274
+ return InternalRegister(
275
+ () =>
276
+ _messageHandler.RegisterTargetedPostProcessor(
277
+ target,
278
+ targetedPostProcessor,
279
+ priority,
280
+ _messageBus
281
+ )
282
+ );
283
+ }
284
+
285
+ /// <summary>
286
+ /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
287
+ /// </summary>
288
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
289
+ /// <param name="target">Target to post process messages for.</param>
290
+ /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
291
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
292
+ /// <returns>A handle that allows for registration and de-registration.</returns>
293
+ public MessageRegistrationHandle RegisterTargetedPostProcessor<T>(
294
+ InstanceId target,
295
+ Action<T> targetedPostProcessor,
296
+ int priority = 0
297
+ )
298
+ where T : ITargetedMessage
299
+ {
300
+ return InternalRegister(
301
+ () =>
302
+ _messageHandler.RegisterTargetedPostProcessor(
303
+ target,
304
+ targetedPostProcessor,
305
+ priority,
306
+ _messageBus
307
+ )
308
+ );
309
+ }
310
+ #endif
311
+
312
+ /// <summary>
313
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards anything (including itself).
314
+ /// </summary>
315
+ /// <note>
316
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
317
+ /// </note>
318
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
319
+ /// <param name="messageHandler">Actual handler functionality.</param>
320
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
321
+ /// <returns>A handle that allows for registration and de-registration.</returns>
322
+ public MessageRegistrationHandle RegisterTargetedWithoutTargeting<T>(
323
+ Action<InstanceId, T> messageHandler,
324
+ int priority = 0
325
+ )
326
+ where T : ITargetedMessage
327
+ {
328
+ if (_messageHandler == null) // Unity has a bug
329
+ {
330
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
331
+ }
332
+ return InternalRegister(
333
+ () =>
334
+ _messageHandler.RegisterTargetedWithoutTargeting(
335
+ messageHandler,
336
+ priority: priority,
337
+ messageBus: _messageBus
338
+ )
339
+ );
340
+ }
341
+
342
+ /// <summary>
343
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards anything (including itself).
344
+ /// </summary>
345
+ /// <note>
346
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
347
+ /// </note>
348
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
349
+ /// <param name="messageHandler">Actual handler functionality.</param>
350
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
351
+ /// <returns>A handle that allows for registration and de-registration.</returns>
352
+ public MessageRegistrationHandle RegisterTargetedWithoutTargeting<T>(
353
+ MessageHandler.FastHandlerWithContext<T> messageHandler,
354
+ int priority = 0
355
+ )
356
+ where T : ITargetedMessage
357
+ {
358
+ if (_messageHandler == null) // Unity has a bug
359
+ {
360
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
361
+ }
362
+ return InternalRegister(
363
+ () =>
364
+ _messageHandler.RegisterTargetedWithoutTargeting(
365
+ messageHandler,
366
+ priority: priority,
367
+ messageBus: _messageBus
368
+ )
369
+ );
370
+ }
371
+
372
+ /// <summary>
373
+ /// Stages a registration of the provided MessageHandler to post process TargetedMessages of the given type targeted towards anything (including itself).
374
+ /// </summary>
375
+ /// <note>
376
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
377
+ /// </note>
378
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
379
+ /// <param name="postProcessor">Actual handler functionality.</param>
380
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
381
+ /// <returns>A handle that allows for registration and de-registration.</returns>
382
+ public MessageRegistrationHandle RegisterTargetedWithoutTargetingPostProcessor<T>(
383
+ Action<InstanceId, T> postProcessor,
384
+ int priority = 0
385
+ )
386
+ where T : ITargetedMessage
387
+ {
388
+ if (_messageHandler == null) // Unity has a bug
389
+ {
390
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
391
+ }
392
+ return InternalRegister(
393
+ () =>
394
+ _messageHandler.RegisterTargetedWithoutTargetingPostProcessor(
395
+ postProcessor,
396
+ priority,
397
+ _messageBus
398
+ )
399
+ );
400
+ }
401
+
402
+ /// <summary>
403
+ /// Stages a registration of the provided MessageHandler to post process TargetedMessages of the given type targeted towards anything (including itself).
404
+ /// </summary>
405
+ /// <note>
406
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
407
+ /// </note>
408
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
409
+ /// <param name="postProcessor">Actual post processor functionality.</param>
410
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
411
+ /// <returns>A handle that allows for registration and de-registration.</returns>
412
+ public MessageRegistrationHandle RegisterTargetedWithoutTargetingPostProcessor<T>(
413
+ MessageHandler.FastHandlerWithContext<T> postProcessor,
414
+ int priority = 0
415
+ )
416
+ where T : ITargetedMessage
417
+ {
418
+ if (_messageHandler == null) // Unity has a bug
419
+ {
420
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
421
+ }
422
+ return InternalRegister(
423
+ () =>
424
+ _messageHandler.RegisterTargetedWithoutTargetingPostProcessor(
425
+ postProcessor,
426
+ priority,
427
+ _messageBus
428
+ )
429
+ );
430
+ }
431
+
432
+ /// <summary>
433
+ /// Stages a registration of the provided MessageHandler to accept UntargetedMessages of the given type.
434
+ /// </summary>
435
+ /// <note>
436
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
437
+ /// </note>
438
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
439
+ /// <param name="untargetedHandler">Actual handler functionality.</param>
440
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
441
+ /// <returns>A handle that allows for registration and de-registration.</returns>
442
+ public MessageRegistrationHandle RegisterUntargeted<T>(
443
+ Action<T> untargetedHandler,
444
+ int priority = 0
445
+ )
446
+ where T : IUntargetedMessage
447
+ {
448
+ if (_messageHandler == null) // Unity has a bug
449
+ {
450
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
451
+ }
452
+ return InternalRegister(
453
+ () =>
454
+ _messageHandler.RegisterUntargetedMessageHandler(
455
+ untargetedHandler,
456
+ priority: priority,
457
+ messageBus: _messageBus
458
+ )
459
+ );
460
+ }
461
+
462
+ /// <summary>
463
+ /// Stages a registration of the provided MessageHandler to accept UntargetedMessages of the given type.
464
+ /// </summary>
465
+ /// <note>
466
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
467
+ /// </note>
468
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
469
+ /// <param name="untargetedHandler">Actual handler functionality.</param>
470
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
471
+ /// <returns>A handle that allows for registration and de-registration.</returns>
472
+ public MessageRegistrationHandle RegisterUntargeted<T>(
473
+ MessageHandler.FastHandler<T> untargetedHandler,
474
+ int priority = 0
475
+ )
476
+ where T : IUntargetedMessage
477
+ {
478
+ if (_messageHandler == null) // Unity has a bug
479
+ {
480
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
481
+ }
482
+ return InternalRegister(
483
+ () =>
484
+ _messageHandler.RegisterUntargetedMessageHandler(
485
+ untargetedHandler,
486
+ priority: priority,
487
+ messageBus: _messageBus
488
+ )
489
+ );
490
+ }
491
+
492
+ /// <summary>
493
+ /// Stages a registration of the provided PostProcessor to post process UntargetedMessages of the given type.
494
+ /// </summary>
495
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
496
+ /// <param name="untargetedPostProcessor">Actual post processor functionality.</param>
497
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
498
+ /// <returns>A handle that allows for registration and de-registration.</returns>
499
+ public MessageRegistrationHandle RegisterUntargetedPostProcessor<T>(
500
+ MessageHandler.FastHandler<T> untargetedPostProcessor,
501
+ int priority = 0
502
+ )
503
+ where T : IUntargetedMessage
504
+ {
505
+ if (_messageHandler == null)
506
+ {
507
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
508
+ }
509
+ return InternalRegister(
510
+ () =>
511
+ _messageHandler.RegisterUntargetedPostProcessor(
512
+ untargetedPostProcessor,
513
+ priority,
514
+ _messageBus
515
+ )
516
+ );
517
+ }
518
+
519
+ private MessageRegistrationHandle RegisterBroadcastInternal<T>(
520
+ InstanceId source,
521
+ Action<T> broadcastHandler,
522
+ int priority = 0
523
+ )
524
+ where T : IBroadcastMessage
525
+ {
526
+ if (_messageHandler == null) // Unity has a bug
527
+ {
528
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
529
+ }
530
+ return InternalRegister(
531
+ () =>
532
+ _messageHandler.RegisterSourcedBroadcastMessageHandler(
533
+ source,
534
+ broadcastHandler,
535
+ priority: priority,
536
+ messageBus: _messageBus
537
+ )
538
+ );
539
+ }
540
+
541
+ private MessageRegistrationHandle RegisterBroadcastInternal<T>(
542
+ InstanceId source,
543
+ MessageHandler.FastHandler<T> broadcastHandler,
544
+ int priority = 0
545
+ )
546
+ where T : IBroadcastMessage
547
+ {
548
+ if (_messageHandler == null) // Unity has a bug
549
+ {
550
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
551
+ }
552
+ return InternalRegister(
553
+ () =>
554
+ _messageHandler.RegisterSourcedBroadcastMessageHandler(
555
+ source,
556
+ broadcastHandler,
557
+ priority: priority,
558
+ messageBus: _messageBus
559
+ )
560
+ );
561
+ }
562
+
563
+ private MessageRegistrationHandle RegisterBroadcastPostProcessorInternal<T>(
564
+ InstanceId source,
565
+ Action<T> broadcastPostProcessor,
566
+ int priority
567
+ )
568
+ where T : IBroadcastMessage
569
+ {
570
+ if (_messageHandler == null)
571
+ {
572
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
573
+ }
574
+ return InternalRegister(
575
+ () =>
576
+ _messageHandler.RegisterSourcedBroadcastPostProcessor(
577
+ source,
578
+ broadcastPostProcessor,
579
+ priority,
580
+ _messageBus
581
+ )
582
+ );
583
+ }
584
+
585
+ private MessageRegistrationHandle RegisterBroadcastPostProcessorInternal<T>(
586
+ InstanceId source,
587
+ MessageHandler.FastHandler<T> broadcastPostProcessor,
588
+ int priority
589
+ )
590
+ where T : IBroadcastMessage
591
+ {
592
+ if (_messageHandler == null)
593
+ {
594
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
595
+ }
596
+ return InternalRegister(
597
+ () =>
598
+ _messageHandler.RegisterSourcedBroadcastPostProcessor(
599
+ source,
600
+ broadcastPostProcessor,
601
+ priority,
602
+ _messageBus
603
+ )
604
+ );
605
+ }
606
+
607
+ #if UNITY_2017_1_OR_NEWER
608
+ /// <summary>
609
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
610
+ /// </summary>
611
+ /// <note>
612
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
613
+ /// </note>
614
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
615
+ /// <param name="source">Id of the source for BroadcastMessages to listen for.</param>
616
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
617
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
618
+ /// <returns>A handle that allows for registration and de-registration.</returns>
619
+ public MessageRegistrationHandle RegisterGameObjectBroadcast<T>(
620
+ UnityEngine.GameObject source,
621
+ Action<T> broadcastHandler,
622
+ int priority = 0
623
+ )
624
+ where T : IBroadcastMessage
625
+ {
626
+ return RegisterBroadcastInternal(source, broadcastHandler, priority: priority);
627
+ }
628
+
629
+ /// <summary>
630
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
631
+ /// </summary>
632
+ /// <note>
633
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
634
+ /// </note>
635
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
636
+ /// <param name="source">Id of the source for BroadcastMessages to listen for.</param>
637
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
638
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
639
+ /// <returns>A handle that allows for registration and de-registration.</returns>
640
+ public MessageRegistrationHandle RegisterGameObjectBroadcast<T>(
641
+ UnityEngine.GameObject source,
642
+ MessageHandler.FastHandler<T> broadcastHandler,
643
+ int priority = 0
644
+ )
645
+ where T : IBroadcastMessage
646
+ {
647
+ return RegisterBroadcastInternal(source, broadcastHandler, priority: priority);
648
+ }
649
+
650
+ /// <summary>
651
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given GameObject.
652
+ /// </summary>
653
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
654
+ /// <param name="source">Source of the messages.</param>
655
+ /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
656
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
657
+ /// <returns>A handle that allows for registration and de-registration.</returns>
658
+ public MessageRegistrationHandle RegisterGameObjectBroadcastPostProcessor<T>(
659
+ UnityEngine.GameObject source,
660
+ Action<T> broadcastPostProcessor,
661
+ int priority = 0
662
+ )
663
+ where T : IBroadcastMessage
664
+ {
665
+ return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcessor, priority);
666
+ }
667
+
668
+ /// <summary>
669
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given GameObject.
670
+ /// </summary>
671
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
672
+ /// <param name="source">Source of the messages.</param>
673
+ /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
674
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
675
+ /// <returns>A handle that allows for registration and de-registration.</returns>
676
+ public MessageRegistrationHandle RegisterGameObjectBroadcastPostProcessor<T>(
677
+ UnityEngine.GameObject source,
678
+ MessageHandler.FastHandler<T> broadcastPostProcessor,
679
+ int priority = 0
680
+ )
681
+ where T : IBroadcastMessage
682
+ {
683
+ return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcessor, priority);
684
+ }
685
+
686
+ /// <summary>
687
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
688
+ /// </summary>
689
+ /// <note>
690
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
691
+ /// </note>
692
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
693
+ /// <param name="source">The component source for BroadcastMessages to listen for.</param>
694
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
695
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
696
+ /// <returns>A handle that allows for registration and de-registration.</returns>
697
+ public MessageRegistrationHandle RegisterComponentBroadcast<T>(
698
+ UnityEngine.Component source,
699
+ Action<T> broadcastHandler,
700
+ int priority = 0
701
+ )
702
+ where T : IBroadcastMessage
703
+ {
704
+ return RegisterBroadcastInternal(source, broadcastHandler, priority);
705
+ }
706
+
707
+ /// <summary>
708
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
709
+ /// </summary>
710
+ /// <note>
711
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
712
+ /// </note>
713
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
714
+ /// <param name="source">The component source for BroadcastMessages to listen for.</param>
715
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
716
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
717
+ /// <returns>A handle that allows for registration and de-registration.</returns>
718
+ public MessageRegistrationHandle RegisterComponentBroadcast<T>(
719
+ UnityEngine.Component source,
720
+ MessageHandler.FastHandler<T> broadcastHandler,
721
+ int priority = 0
722
+ )
723
+ where T : IBroadcastMessage
724
+ {
725
+ return RegisterBroadcastInternal(source, broadcastHandler, priority);
726
+ }
727
+
728
+ /// <summary>
729
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given component.
730
+ /// </summary>
731
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
732
+ /// <param name="source">Source of the messages.</param>
733
+ /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
734
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
735
+ /// <returns>A handle that allows for registration and de-registration.</returns>
736
+ public MessageRegistrationHandle RegisterComponentBroadcastPostProcessor<T>(
737
+ UnityEngine.Component source,
738
+ Action<T> broadcastPostProcessor,
739
+ int priority = 0
740
+ )
741
+ where T : IBroadcastMessage
742
+ {
743
+ if (_messageHandler == null)
744
+ {
745
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
746
+ }
747
+ return InternalRegister(
748
+ () =>
749
+ _messageHandler.RegisterSourcedBroadcastPostProcessor(
750
+ source,
751
+ broadcastPostProcessor,
752
+ priority: priority,
753
+ _messageBus
754
+ )
755
+ );
756
+ }
757
+
758
+ /// <summary>
759
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given component.
760
+ /// </summary>
761
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
762
+ /// <param name="source">Source of the messages.</param>
763
+ /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
764
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
765
+ /// <returns>A handle that allows for registration and de-registration.</returns>
766
+ public MessageRegistrationHandle RegisterComponentBroadcastPostProcessor<T>(
767
+ UnityEngine.Component source,
768
+ MessageHandler.FastHandler<T> broadcastPostProcessor,
769
+ int priority = 0
770
+ )
771
+ where T : IBroadcastMessage
772
+ {
773
+ if (_messageHandler == null)
774
+ {
775
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
776
+ }
777
+ return InternalRegister(
778
+ () =>
779
+ _messageHandler.RegisterSourcedBroadcastPostProcessor(
780
+ source,
781
+ broadcastPostProcessor,
782
+ priority: priority,
783
+ _messageBus
784
+ )
785
+ );
786
+ }
787
+ #else
788
+
789
+ /// <summary>
790
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
791
+ /// </summary>
792
+ /// <note>
793
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
794
+ /// </note>
795
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
796
+ /// <param name="source">Source of the messages.</param>
797
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
798
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
799
+ /// <returns>A handle that allows for registration and de-registration.</returns>
800
+ public MessageRegistrationHandle RegisterBroadcast<T>(
801
+ InstanceId source,
802
+ Action<T> broadcastHandler,
803
+ int priority = 0
804
+ )
805
+ where T : IBroadcastMessage
806
+ {
807
+ return RegisterBroadcastInternal(source, broadcastHandler, priority: priority);
808
+ }
809
+
810
+ /// <summary>
811
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
812
+ /// </summary>
813
+ /// <note>
814
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
815
+ /// </note>
816
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
817
+ /// <param name="source">Source of the messages.</param>
818
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
819
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
820
+ /// <returns>A handle that allows for registration and de-registration.</returns>
821
+ public MessageRegistrationHandle RegisterBroadcast<T>(
822
+ InstanceId source,
823
+ MessageHandler.FastHandler<T> broadcastHandler,
824
+ int priority = 0
825
+ )
826
+ where T : IBroadcastMessage
827
+ {
828
+ return RegisterBroadcastInternal(source, broadcastHandler, priority: priority);
829
+ }
830
+
831
+ /// <summary>
832
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given source.
833
+ /// </summary>
834
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
835
+ /// <param name="source">Source of the messages.</param>
836
+ /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
837
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
838
+ /// <returns>A handle that allows for registration and de-registration.</returns>
839
+ public MessageRegistrationHandle RegisterBroadcastPostProcessor<T>(
840
+ InstanceId source,
841
+ Action<T> broadcastPostProcessor,
842
+ int priority = 0
843
+ )
844
+ where T : IBroadcastMessage
845
+ {
846
+ return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcessor, priority);
847
+ }
848
+
849
+ /// <summary>
850
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given source.
851
+ /// </summary>
852
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
853
+ /// <param name="source">Source of the messages.</param>
854
+ /// <param name="broadcastPostProcessor">Actual post processor logic.</param>
855
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
856
+ /// <returns>A handle that allows for registration and de-registration.</returns>
857
+ public MessageRegistrationHandle RegisterBroadcastPostProcessor<T>(
858
+ InstanceId source,
859
+ MessageHandler.FastHandler<T> broadcastPostProcessor,
860
+ int priority = 0
861
+ )
862
+ where T : IBroadcastMessage
863
+ {
864
+ return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcessor, priority);
865
+ }
866
+ #endif
867
+
868
+ /// <summary>
869
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
870
+ /// </summary>
871
+ /// <note>
872
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
873
+ /// </note>
874
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
875
+ /// <param name="broadcastHandler">Action handler functionality.</param>
876
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
877
+ /// <returns>A handle that allows for registration and de-registration.</returns>
878
+ public MessageRegistrationHandle RegisterBroadcastWithoutSource<T>(
879
+ Action<InstanceId, T> broadcastHandler,
880
+ int priority = 0
881
+ )
882
+ where T : IBroadcastMessage
883
+ {
884
+ if (_messageHandler == null) // Unity has a bug
885
+ {
886
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
887
+ }
888
+
889
+ return InternalRegister(
890
+ () =>
891
+ _messageHandler.RegisterSourcedBroadcastWithoutSource(
892
+ broadcastHandler,
893
+ priority: priority,
894
+ messageBus: _messageBus
895
+ )
896
+ );
897
+ }
898
+
899
+ /// <summary>
900
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
901
+ /// </summary>
902
+ /// <note>
903
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
904
+ /// </note>
905
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
906
+ /// <param name="broadcastHandler">Action handler functionality.</param>
907
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
908
+ /// <returns>A handle that allows for registration and de-registration.</returns>
909
+ public MessageRegistrationHandle RegisterBroadcastWithoutSource<T>(
910
+ MessageHandler.FastHandlerWithContext<T> broadcastHandler,
911
+ int priority = 0
912
+ )
913
+ where T : IBroadcastMessage
914
+ {
915
+ if (_messageHandler == null) // Unity has a bug
916
+ {
917
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
918
+ }
919
+
920
+ return InternalRegister(
921
+ () =>
922
+ _messageHandler.RegisterSourcedBroadcastWithoutSource(
923
+ broadcastHandler,
924
+ priority: priority,
925
+ messageBus: _messageBus
926
+ )
927
+ );
928
+ }
929
+
930
+ /// <summary>
931
+ /// Stages a registration of the provided MessageHandler to post process BroadcastMessages of the given type.
932
+ /// </summary>
933
+ /// <note>
934
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
935
+ /// </note>
936
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
937
+ /// <param name="broadcastHandler">Actual post process functionality.</param>
938
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
939
+ /// <returns>A handle that allows for registration and de-registration.</returns>
940
+ public MessageRegistrationHandle RegisterBroadcastWithoutSourcePostProcessor<T>(
941
+ Action<InstanceId, T> broadcastHandler,
942
+ int priority = 0
943
+ )
944
+ where T : IBroadcastMessage
945
+ {
946
+ if (_messageHandler == null) // Unity has a bug
947
+ {
948
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
949
+ }
950
+
951
+ return InternalRegister(
952
+ () =>
953
+ _messageHandler.RegisterSourcedBroadcastWithoutSourcePostProcessor(
954
+ broadcastHandler,
955
+ priority: priority,
956
+ _messageBus
957
+ )
958
+ );
959
+ }
960
+
961
+ /// <summary>
962
+ /// Stages a registration of the provided MessageHandler to post post process BroadcastMessages of the given type.
963
+ /// </summary>
964
+ /// <note>
965
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
966
+ /// </note>
967
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
968
+ /// <param name="broadcastHandler">Actual post process functionality.</param>
969
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
970
+ /// <returns>A handle that allows for registration and de-registration.</returns>
971
+ public MessageRegistrationHandle RegisterBroadcastWithoutSourcePostProcessor<T>(
972
+ MessageHandler.FastHandlerWithContext<T> broadcastHandler,
973
+ int priority = 0
974
+ )
975
+ where T : IBroadcastMessage
976
+ {
977
+ if (_messageHandler == null) // Unity has a bug
978
+ {
979
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
980
+ }
981
+
982
+ return InternalRegister(
983
+ () =>
984
+ _messageHandler.RegisterSourcedBroadcastWithoutSourcePostProcessor(
985
+ broadcastHandler,
986
+ priority: priority,
987
+ _messageBus
988
+ )
989
+ );
990
+ }
991
+
992
+ /// <summary>
993
+ /// Stages a registration of the provided MessageHandler to accept every message that is broadcast.
994
+ /// </summary>
995
+ /// <note>
996
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
997
+ /// </note>
998
+ /// <param name="acceptAllUntargeted">Action handler functionality for UntargetedMessages.</param>
999
+ /// <param name="acceptAllTargeted">Action handler functionality for TargetedMessages.</param>
1000
+ /// <param name="acceptAllBroadcast">Action handler functionality for BroadcastMessages.</param>
1001
+ /// <returns>A handle that allows for registration and de-registration.</returns>
1002
+ public MessageRegistrationHandle RegisterGlobalAcceptAll(
1003
+ Action<IUntargetedMessage> acceptAllUntargeted,
1004
+ Action<InstanceId, ITargetedMessage> acceptAllTargeted,
1005
+ Action<InstanceId, IBroadcastMessage> acceptAllBroadcast
1006
+ )
1007
+ {
1008
+ if (_messageHandler == null) // Unity has a bug
1009
+ {
1010
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1011
+ }
1012
+ return InternalRegister(
1013
+ () =>
1014
+ _messageHandler.RegisterGlobalAcceptAll(
1015
+ acceptAllUntargeted,
1016
+ acceptAllTargeted,
1017
+ acceptAllBroadcast,
1018
+ _messageBus
1019
+ )
1020
+ );
1021
+ }
1022
+
1023
+ /// <summary>
1024
+ /// Stages a registration of the provided MessageHandler to accept every message that is broadcast.
1025
+ /// </summary>
1026
+ /// <note>
1027
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
1028
+ /// </note>
1029
+ /// <param name="acceptAllUntargeted">Action handler functionality for UntargetedMessages.</param>
1030
+ /// <param name="acceptAllTargeted">Action handler functionality for TargetedMessages.</param>
1031
+ /// <param name="acceptAllBroadcast">Action handler functionality for BroadcastMessages.</param>
1032
+ /// <returns>A handle that allows for registration and de-registration.</returns>
1033
+ public MessageRegistrationHandle RegisterGlobalAcceptAll(
1034
+ MessageHandler.FastHandler<IUntargetedMessage> acceptAllUntargeted,
1035
+ MessageHandler.FastHandlerWithContext<ITargetedMessage> acceptAllTargeted,
1036
+ MessageHandler.FastHandlerWithContext<IBroadcastMessage> acceptAllBroadcast
1037
+ )
1038
+ {
1039
+ if (_messageHandler == null) // Unity has a bug
1040
+ {
1041
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1042
+ }
1043
+ return InternalRegister(
1044
+ () =>
1045
+ _messageHandler.RegisterGlobalAcceptAll(
1046
+ acceptAllUntargeted,
1047
+ acceptAllTargeted,
1048
+ acceptAllBroadcast,
1049
+ _messageBus
1050
+ )
1051
+ );
1052
+ }
1053
+
1054
+ public MessageRegistrationHandle RegisterUntargetedInterceptor<T>(
1055
+ IMessageBus.UntargetedInterceptor<T> interceptor,
1056
+ int priority = 0
1057
+ )
1058
+ where T : IUntargetedMessage
1059
+ {
1060
+ if (_messageHandler == null)
1061
+ {
1062
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1063
+ }
1064
+
1065
+ return InternalRegister(
1066
+ () => _messageHandler.RegisterUntargetedInterceptor(interceptor, priority)
1067
+ );
1068
+ }
1069
+
1070
+ public MessageRegistrationHandle RegisterBroadcastInterceptor<T>(
1071
+ IMessageBus.BroadcastInterceptor<T> interceptor,
1072
+ int priority = 0
1073
+ )
1074
+ where T : IBroadcastMessage
1075
+ {
1076
+ if (_messageHandler == null)
1077
+ {
1078
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1079
+ }
1080
+
1081
+ return InternalRegister(
1082
+ () => _messageHandler.RegisterBroadcastInterceptor(interceptor, priority)
1083
+ );
1084
+ }
1085
+
1086
+ public MessageRegistrationHandle RegisterTargetedInterceptor<T>(
1087
+ IMessageBus.TargetedInterceptor<T> interceptor,
1088
+ int priority = 0
1089
+ )
1090
+ where T : ITargetedMessage
1091
+ {
1092
+ if (_messageHandler == null)
1093
+ {
1094
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
1095
+ }
1096
+
1097
+ return InternalRegister(
1098
+ () => _messageHandler.RegisterTargetedInterceptor(interceptor, priority)
1099
+ );
1100
+ }
1101
+
1102
+ /// <summary>
1103
+ /// Handles the actual [de]registration wrapping and (potential) lazy execution.
1104
+ /// </summary>
1105
+ /// <param name="registerAndGetDeregistration">Proxied registration function that returns a de-registration function.</param>
1106
+ /// <returns>A handle that allows for registration and de-registration.</returns>
1107
+ private MessageRegistrationHandle InternalRegister(
1108
+ Func<Action> registerAndGetDeregistration
1109
+ )
1110
+ {
1111
+ MessageRegistrationHandle handle =
1112
+ MessageRegistrationHandle.CreateMessageRegistrationHandle();
1113
+
1114
+ _registrations[handle] = Registration;
1115
+
1116
+ // Generally, registrations should take place before all calls to enable. Just in case, though...
1117
+ if (_enabled)
1118
+ {
1119
+ Registration();
1120
+ }
1121
+
1122
+ return handle;
1123
+
1124
+ // We don't want to actually register at this time (might not be awake/enabled) - so we wrap that shit up, to lazy register when we're enabled.
1125
+ void Registration()
1126
+ {
1127
+ Action actualDeregistration = registerAndGetDeregistration();
1128
+ _deregistrations[handle] = actualDeregistration;
1129
+ }
1130
+ }
1131
+
1132
+ /// <summary>
1133
+ /// Enables the token if not already enabled. Executes all staged registrations.
1134
+ /// </summary>
1135
+ /// <note>
1136
+ /// Idempotent.
1137
+ /// </note>
1138
+ public void Enable()
1139
+ {
1140
+ if (_enabled)
1141
+ {
1142
+ return;
1143
+ }
1144
+
1145
+ if (_registrations is { Count: > 0 })
1146
+ {
1147
+ foreach (Action registrationAction in _registrations.Values)
1148
+ {
1149
+ registrationAction();
1150
+ }
1151
+ }
1152
+
1153
+ _enabled = true;
1154
+ }
1155
+
1156
+ /// <summary>
1157
+ /// Disables the token if not already disabled. Executes all staged de-registrations.
1158
+ /// </summary>
1159
+ /// <note>
1160
+ /// Idempotent.
1161
+ /// </note>
1162
+ public void Disable()
1163
+ {
1164
+ if (!_enabled)
1165
+ {
1166
+ return;
1167
+ }
1168
+
1169
+ if (_deregistrations is { Count: > 0 })
1170
+ {
1171
+ foreach (Action deregistrationAction in _deregistrations.Values)
1172
+ {
1173
+ deregistrationAction();
1174
+ }
1175
+ }
1176
+
1177
+ // ReSharper disable once ForCanBeConvertedToForeach
1178
+
1179
+
1180
+ _enabled = false;
1181
+ }
1182
+
1183
+ /// <summary>
1184
+ /// Disables the token and clears all registrations + de-registrations
1185
+ /// </summary>
1186
+ public void UnregisterAll()
1187
+ {
1188
+ if (_enabled && _deregistrations is { Count: > 0 })
1189
+ {
1190
+ foreach (Action deregistrationAction in _deregistrations.Values)
1191
+ {
1192
+ deregistrationAction();
1193
+ }
1194
+ }
1195
+
1196
+ _enabled = false;
1197
+ _registrations?.Clear();
1198
+ _deregistrations?.Clear();
1199
+ }
1200
+
1201
+ public void RemoveRegistration(MessageRegistrationHandle handle)
1202
+ {
1203
+ if (
1204
+ _deregistrations != null
1205
+ && _deregistrations.TryGetValue(handle, out Action deregistrationAction)
1206
+ )
1207
+ {
1208
+ deregistrationAction();
1209
+ _ = _deregistrations.Remove(handle);
1210
+ }
1211
+
1212
+ _ = _registrations?.Remove(handle);
1213
+ }
1214
+
1215
+ /// <summary>
1216
+ /// Creates a MessagingRegistrationToken that operates on the given handler.
1217
+ /// </summary>
1218
+ /// <param name="messageHandler">Message handler to register handlers to.</param>
1219
+ /// <param name="messageBus">MessageBus to use for this MessageRegistrationToken. Uses the GlobalMessageBus if left null.</param>
1220
+ /// <returns>MessagingRegistrationToken bound to the MessageHandler.</returns>
1221
+ public static MessageRegistrationToken Create(
1222
+ MessageHandler messageHandler,
1223
+ IMessageBus messageBus = null
1224
+ )
1225
+ {
1226
+ if (messageHandler == null)
1227
+ {
1228
+ throw new ArgumentNullException(nameof(messageHandler));
1229
+ }
1230
+
1231
+ return new MessageRegistrationToken(messageHandler, messageBus);
1232
+ }
1233
+ }
1234
+ }