com.wallstop-studios.dxmessaging 1.0.2 → 1.0.4
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.
package/README.md
CHANGED
|
@@ -10,16 +10,19 @@ Game engine agnostic robust, synchronous pub/sub C# messaging solution, mostly g
|
|
|
10
10
|
- Scope(s): `com.wallstop-studios.dxmessaging` *and* `com.wallstop-studios.unity-helpers`
|
|
11
11
|
4. Resolve the latest `DxMessaging`
|
|
12
12
|
|
|
13
|
+
# Dependencies
|
|
14
|
+
This project has a dependency on my [`Unity Helpers`](https://github.com/wallstop/unity-helpers) project, which contains the System.Runtime.CompilerServices.Unsafe.dll, which is used for some speed hacks in DxMessaging directly. Unity Helpers bundles a few (small) dependencies, including protobuf. If you don't want these dependencies, or if they conflict in some way, you can either include a copy of the System.Runtime.CompilerServices.Unsafe.dll yourself without relying on UnityHelpers, or manually download and include the Unity Helpers project and delete anything that conflicts with your project.
|
|
15
|
+
|
|
13
16
|
# Benchmarks
|
|
14
17
|
DxMessaging is currently a bit slower (2-4x) than Unity's built in messaging solution (when running in Unity). [Source](./Tests/Runtime/Core/PerformanceTests.cs).
|
|
15
18
|
| Message Tech | Operations / Second |
|
|
16
19
|
| ------------ | ------------------- |
|
|
17
|
-
| Unity | 1,
|
|
18
|
-
| DxMessaging (GameObject) - Normal |
|
|
19
|
-
| DxMessaging (Component) - Normal |
|
|
20
|
-
| DxMessaging (GameObject) - No-Copy |
|
|
21
|
-
| DxMessaging (Component) - No-Copy |
|
|
22
|
-
| DxMessaging (Untargeted) - No-Copy | 1,
|
|
20
|
+
| Unity | 1,908,539 |
|
|
21
|
+
| DxMessaging (GameObject) - Normal | 729,221 |
|
|
22
|
+
| DxMessaging (Component) - Normal | 740,140 |
|
|
23
|
+
| DxMessaging (GameObject) - No-Copy | 720,678 |
|
|
24
|
+
| DxMessaging (Component) - No-Copy | 754,243 |
|
|
25
|
+
| DxMessaging (Untargeted) - No-Copy | 1,176,809 |
|
|
23
26
|
|
|
24
27
|
# Functionality
|
|
25
28
|
While not as fast, DxMessaging offers *additional functionality* as compared to Unity's messaging solution.
|
|
@@ -55,6 +58,17 @@ Receivers can subscribe to any number of message types.
|
|
|
55
58
|
|
|
56
59
|
For Unity, we have an easy-to-integrate [MessageAwareComponent](./Runtime/Unity/MessageAwareComponent.cs) - simply extend any component you want off of this base class. This will handle message registration lifetimes automatically for you.
|
|
57
60
|
If you have your own base classes or aren't using Unity, then you'll need to add lifetimes yourself. Please use the MessageAwareComponent as reference.
|
|
61
|
+
|
|
62
|
+
### Implementation Notes (Unity)
|
|
63
|
+
Please note, if you want to receive messages and inherit off of the `MessageAwareComponent`, the component implements several Unity-specific methods to manage lifetimes. Specifically,
|
|
64
|
+
* `protected virtual void Awake()`
|
|
65
|
+
* `protected virtual void OnEnable()`
|
|
66
|
+
* `protected virtual void OnDisable()`
|
|
67
|
+
* `protected virtual void OnDestroy()`
|
|
68
|
+
* `protected virtual void OnApplicationQuit()`
|
|
69
|
+
|
|
70
|
+
If you wish to use any of these methods in components that inherit from `MessageAwareComponent`, please make sure to have the overrides call the base methods, otherwise messaging *may break* or not work as expected.
|
|
71
|
+
|
|
58
72
|
## Integration
|
|
59
73
|
See the [tests](./Tests/Runtime/Scripts/) directory for examples about how to integrate with the MessageAwareComponent. But, for some starters:
|
|
60
74
|
```csharp
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
{
|
|
22
22
|
InstanceId targetId = target;
|
|
23
23
|
messageBus ??= MessageHandler.MessageBus;
|
|
24
|
-
if (typeof(TMessage)
|
|
24
|
+
if (typeof(TMessage) == typeof(ITargetedMessage))
|
|
25
25
|
{
|
|
26
26
|
messageBus.UntypedTargetedBroadcast(targetId, message);
|
|
27
27
|
return;
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
{
|
|
41
41
|
InstanceId targetId = target;
|
|
42
42
|
messageBus ??= MessageHandler.MessageBus;
|
|
43
|
-
if (typeof(TMessage)
|
|
43
|
+
if (typeof(TMessage) == typeof(ITargetedMessage))
|
|
44
44
|
{
|
|
45
45
|
messageBus.UntypedTargetedBroadcast(targetId, message);
|
|
46
46
|
return;
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
{
|
|
60
60
|
InstanceId targetId = target;
|
|
61
61
|
messageBus ??= MessageHandler.MessageBus;
|
|
62
|
-
if (typeof(TMessage)
|
|
62
|
+
if (typeof(TMessage) == typeof(ITargetedMessage))
|
|
63
63
|
{
|
|
64
64
|
messageBus.UntypedTargetedBroadcast(targetId, message);
|
|
65
65
|
return;
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
{
|
|
79
79
|
InstanceId targetId = target;
|
|
80
80
|
messageBus ??= MessageHandler.MessageBus;
|
|
81
|
-
if (typeof(TMessage)
|
|
81
|
+
if (typeof(TMessage) == typeof(ITargetedMessage))
|
|
82
82
|
{
|
|
83
83
|
messageBus.UntypedTargetedBroadcast(targetId, message);
|
|
84
84
|
return;
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
public static void EmitTargeted<TMessage>(this TMessage message, InstanceId target, IMessageBus messageBus = null) where TMessage : class, ITargetedMessage
|
|
99
99
|
{
|
|
100
100
|
messageBus ??= MessageHandler.MessageBus;
|
|
101
|
-
if (typeof(TMessage)
|
|
101
|
+
if (typeof(TMessage) == typeof(ITargetedMessage))
|
|
102
102
|
{
|
|
103
103
|
messageBus.UntypedTargetedBroadcast(target, message);
|
|
104
104
|
return;
|
|
@@ -116,7 +116,7 @@
|
|
|
116
116
|
public static void EmitTargeted<TMessage>(this ref TMessage message, InstanceId target, IMessageBus messageBus = null) where TMessage : struct, ITargetedMessage
|
|
117
117
|
{
|
|
118
118
|
messageBus ??= MessageHandler.MessageBus;
|
|
119
|
-
if (typeof(TMessage)
|
|
119
|
+
if (typeof(TMessage) == typeof(ITargetedMessage))
|
|
120
120
|
{
|
|
121
121
|
messageBus.UntypedTargetedBroadcast(target, message);
|
|
122
122
|
return;
|
|
@@ -133,7 +133,7 @@
|
|
|
133
133
|
public static void EmitUntargeted<TMessage>(this TMessage message, IMessageBus messageBus = null) where TMessage : class, IUntargetedMessage
|
|
134
134
|
{
|
|
135
135
|
messageBus ??= MessageHandler.MessageBus;
|
|
136
|
-
if (typeof(TMessage)
|
|
136
|
+
if (typeof(TMessage) == typeof(IUntargetedMessage))
|
|
137
137
|
{
|
|
138
138
|
messageBus.UntypedUntargetedBroadcast(message);
|
|
139
139
|
return;
|
|
@@ -150,7 +150,7 @@
|
|
|
150
150
|
public static void EmitUntargeted<TMessage>(this ref TMessage message, IMessageBus messageBus = null) where TMessage : struct, IUntargetedMessage
|
|
151
151
|
{
|
|
152
152
|
messageBus ??= MessageHandler.MessageBus;
|
|
153
|
-
if (typeof(TMessage)
|
|
153
|
+
if (typeof(TMessage) == typeof(IUntargetedMessage))
|
|
154
154
|
{
|
|
155
155
|
messageBus.UntypedUntargetedBroadcast(message);
|
|
156
156
|
return;
|
|
@@ -170,7 +170,7 @@
|
|
|
170
170
|
{
|
|
171
171
|
InstanceId sourceId = source;
|
|
172
172
|
messageBus ??= MessageHandler.MessageBus;
|
|
173
|
-
if (typeof(TMessage)
|
|
173
|
+
if (typeof(TMessage) == typeof(IBroadcastMessage))
|
|
174
174
|
{
|
|
175
175
|
messageBus.UntypedSourcedBroadcast(sourceId, message);
|
|
176
176
|
return;
|
|
@@ -189,7 +189,7 @@
|
|
|
189
189
|
{
|
|
190
190
|
InstanceId sourceId = source;
|
|
191
191
|
messageBus ??= MessageHandler.MessageBus;
|
|
192
|
-
if (typeof(TMessage)
|
|
192
|
+
if (typeof(TMessage) == typeof(IBroadcastMessage))
|
|
193
193
|
{
|
|
194
194
|
messageBus.UntypedSourcedBroadcast(sourceId, message);
|
|
195
195
|
return;
|
|
@@ -208,7 +208,7 @@
|
|
|
208
208
|
{
|
|
209
209
|
InstanceId sourceId = source;
|
|
210
210
|
messageBus ??= MessageHandler.MessageBus;
|
|
211
|
-
if (typeof(TMessage)
|
|
211
|
+
if (typeof(TMessage) == typeof(IBroadcastMessage))
|
|
212
212
|
{
|
|
213
213
|
messageBus.UntypedSourcedBroadcast(sourceId, message);
|
|
214
214
|
return;
|
|
@@ -227,7 +227,7 @@
|
|
|
227
227
|
{
|
|
228
228
|
InstanceId sourceId = source;
|
|
229
229
|
messageBus ??= MessageHandler.MessageBus;
|
|
230
|
-
if (typeof(TMessage)
|
|
230
|
+
if (typeof(TMessage) == typeof(IBroadcastMessage))
|
|
231
231
|
{
|
|
232
232
|
messageBus.UntypedSourcedBroadcast(sourceId, message);
|
|
233
233
|
return;
|
|
@@ -245,7 +245,7 @@
|
|
|
245
245
|
public static void EmitBroadcast<TMessage>(this TMessage message, InstanceId source, IMessageBus messageBus = null) where TMessage : class, IBroadcastMessage
|
|
246
246
|
{
|
|
247
247
|
messageBus ??= MessageHandler.MessageBus;
|
|
248
|
-
if (typeof(TMessage)
|
|
248
|
+
if (typeof(TMessage) == typeof(IBroadcastMessage))
|
|
249
249
|
{
|
|
250
250
|
messageBus.UntypedSourcedBroadcast(source, message);
|
|
251
251
|
return;
|
|
@@ -263,7 +263,7 @@
|
|
|
263
263
|
public static void EmitBroadcast<TMessage>(this ref TMessage message, InstanceId source, IMessageBus messageBus = null) where TMessage : struct, IBroadcastMessage
|
|
264
264
|
{
|
|
265
265
|
messageBus ??= MessageHandler.MessageBus;
|
|
266
|
-
if (typeof(TMessage)
|
|
266
|
+
if (typeof(TMessage) == typeof(IBroadcastMessage))
|
|
267
267
|
{
|
|
268
268
|
messageBus.UntypedSourcedBroadcast(source, message);
|
|
269
269
|
return;
|
|
@@ -229,7 +229,6 @@
|
|
|
229
229
|
}
|
|
230
230
|
};
|
|
231
231
|
}
|
|
232
|
-
|
|
233
232
|
|
|
234
233
|
public void UntypedUntargetedBroadcast(IUntargetedMessage typedMessage)
|
|
235
234
|
{
|
|
@@ -250,11 +249,10 @@
|
|
|
250
249
|
Action<IUntargetedMessage> broadcast = (Action<IUntargetedMessage>)untargetedMethod;
|
|
251
250
|
broadcast.Invoke(typedMessage);
|
|
252
251
|
}
|
|
253
|
-
|
|
254
252
|
|
|
255
253
|
public void UntargetedBroadcast<TMessage>(ref TMessage typedMessage) where TMessage : IUntargetedMessage
|
|
256
254
|
{
|
|
257
|
-
Type type =
|
|
255
|
+
Type type = typeof(TMessage);
|
|
258
256
|
if (!RunUntargetedInterceptors(type, ref typedMessage))
|
|
259
257
|
{
|
|
260
258
|
return;
|
|
@@ -310,11 +308,10 @@
|
|
|
310
308
|
Action<InstanceId, ITargetedMessage> broadcast = (Action<InstanceId, ITargetedMessage>)targetedMethod;
|
|
311
309
|
broadcast.Invoke(target, typedMessage);
|
|
312
310
|
}
|
|
313
|
-
|
|
314
311
|
|
|
315
312
|
public void TargetedBroadcast<TMessage>(ref InstanceId target, ref TMessage typedMessage) where TMessage : ITargetedMessage
|
|
316
313
|
{
|
|
317
|
-
Type type =
|
|
314
|
+
Type type = typeof(TMessage);
|
|
318
315
|
if (!RunTargetedInterceptors(type, ref typedMessage, ref target))
|
|
319
316
|
{
|
|
320
317
|
return;
|
|
@@ -411,7 +408,7 @@
|
|
|
411
408
|
|
|
412
409
|
public void SourcedBroadcast<TMessage>(ref InstanceId source, ref TMessage typedMessage) where TMessage : IBroadcastMessage
|
|
413
410
|
{
|
|
414
|
-
Type type =
|
|
411
|
+
Type type = typeof(TMessage);
|
|
415
412
|
if (!RunBroadcastInterceptors(type, ref typedMessage, ref source))
|
|
416
413
|
{
|
|
417
414
|
return;
|