namespace DxMessaging.Core.Extensions
{
using Core;
using MessageBus;
using Messages;
///
/// Extensions to smartly go about emitting messages :^)
///
public static class MessageExtensions
{
#if UNITY_2017_1_OR_NEWER
///
/// Emits a TargetedMessage of the given type.
///
/// TargetedMessage to emit.
/// Target that this message is intended for.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitGameObjectTargeted(
this TMessage message,
UnityEngine.GameObject target,
IMessageBus messageBus = null
)
where TMessage : class, ITargetedMessage
{
InstanceId targetId = target;
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(ITargetedMessage))
{
messageBus.UntypedTargetedBroadcast(targetId, message);
return;
}
messageBus.TargetedBroadcast(ref targetId, ref message);
}
///
/// Emits a TargetedMessage of the given type.
///
/// TargetedMessage to emit.
/// Target that this message is intended for.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitGameObjectTargeted(
this ref TMessage message,
UnityEngine.GameObject target,
IMessageBus messageBus = null
)
where TMessage : struct, ITargetedMessage
{
InstanceId targetId = target;
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(ITargetedMessage))
{
messageBus.UntypedTargetedBroadcast(targetId, message);
return;
}
messageBus.TargetedBroadcast(ref targetId, ref message);
}
///
/// Emits a TargetedMessage of the given type.
///
/// TargetedMessage to emit.
/// Target that this message is intended for.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitComponentTargeted(
this TMessage message,
UnityEngine.Component target,
IMessageBus messageBus = null
)
where TMessage : class, ITargetedMessage
{
InstanceId targetId = target;
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(ITargetedMessage))
{
messageBus.UntypedTargetedBroadcast(targetId, message);
return;
}
messageBus.TargetedBroadcast(ref targetId, ref message);
}
///
/// Emits a TargetedMessage of the given type.
///
/// TargetedMessage to emit.
/// Target that this message is intended for.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitComponentTargeted(
this ref TMessage message,
UnityEngine.Component target,
IMessageBus messageBus = null
)
where TMessage : struct, ITargetedMessage
{
InstanceId targetId = target;
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(ITargetedMessage))
{
messageBus.UntypedTargetedBroadcast(targetId, message);
return;
}
messageBus.TargetedBroadcast(ref targetId, ref message);
}
#endif
///
/// Emits a TargetedMessage of the given type.
///
/// TargetedMessage to emit.
/// Target that this message is intended for.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitTargeted(
this TMessage message,
InstanceId target,
IMessageBus messageBus = null
)
where TMessage : class, ITargetedMessage
{
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(ITargetedMessage))
{
messageBus.UntypedTargetedBroadcast(target, message);
return;
}
messageBus.TargetedBroadcast(ref target, ref message);
}
///
/// Emits a TargetedMessage of the given type.
///
/// TargetedMessage to emit.
/// Target that this message is intended for.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitTargeted(
this ref TMessage message,
InstanceId target,
IMessageBus messageBus = null
)
where TMessage : struct, ITargetedMessage
{
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(ITargetedMessage))
{
messageBus.UntypedTargetedBroadcast(target, message);
return;
}
messageBus.TargetedBroadcast(ref target, ref message);
}
///
/// Emits an UntargetedMessage of the given type.
///
/// UntargetedMessage to emit.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitUntargeted(
this TMessage message,
IMessageBus messageBus = null
)
where TMessage : class, IUntargetedMessage
{
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(IUntargetedMessage))
{
messageBus.UntypedUntargetedBroadcast(message);
return;
}
messageBus.UntargetedBroadcast(ref message);
}
///
/// Emits an UntargetedMessage of the given type.
///
/// UntargetedMessage to emit.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitUntargeted(
this ref TMessage message,
IMessageBus messageBus = null
)
where TMessage : struct, IUntargetedMessage
{
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(IUntargetedMessage))
{
messageBus.UntypedUntargetedBroadcast(message);
return;
}
messageBus.UntargetedBroadcast(ref message);
}
#if UNITY_2017_1_OR_NEWER
///
/// Emits a BroadcastMessage of the given type.
///
/// BroadcastMessage to emit.
/// Source of this message.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitGameObjectBroadcast(
this TMessage message,
UnityEngine.GameObject source,
IMessageBus messageBus = null
)
where TMessage : class, IBroadcastMessage
{
InstanceId sourceId = source;
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(IBroadcastMessage))
{
messageBus.UntypedSourcedBroadcast(sourceId, message);
return;
}
messageBus.SourcedBroadcast(ref sourceId, ref message);
}
///
/// Emits a BroadcastMessage of the given type.
///
/// BroadcastMessage to emit.
/// Source of this message.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitGameObjectBroadcast(
this ref TMessage message,
UnityEngine.GameObject source,
IMessageBus messageBus = null
)
where TMessage : struct, IBroadcastMessage
{
InstanceId sourceId = source;
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(IBroadcastMessage))
{
messageBus.UntypedSourcedBroadcast(sourceId, message);
return;
}
messageBus.SourcedBroadcast(ref sourceId, ref message);
}
///
/// Emits a BroadcastMessage of the given type from the specified component.
///
/// BroadcastMessage to emit.
/// Source of this message.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitComponentBroadcast(
this TMessage message,
UnityEngine.Component source,
IMessageBus messageBus = null
)
where TMessage : class, IBroadcastMessage
{
InstanceId sourceId = source;
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(IBroadcastMessage))
{
messageBus.UntypedSourcedBroadcast(sourceId, message);
return;
}
messageBus.SourcedBroadcast(ref sourceId, ref message);
}
///
/// Emits a BroadcastMessage of the given type from the specified component.
///
/// BroadcastMessage to emit.
/// Source of this message.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitComponentBroadcast(
this ref TMessage message,
UnityEngine.Component source,
IMessageBus messageBus = null
)
where TMessage : struct, IBroadcastMessage
{
InstanceId sourceId = source;
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(IBroadcastMessage))
{
messageBus.UntypedSourcedBroadcast(sourceId, message);
return;
}
messageBus.SourcedBroadcast(ref sourceId, ref message);
}
#endif
///
/// Emits a BroadcastMessage of the given type from the specified component.
///
/// BroadcastMessage to emit.
/// Source of this message.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitBroadcast(
this TMessage message,
InstanceId source,
IMessageBus messageBus = null
)
where TMessage : class, IBroadcastMessage
{
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(IBroadcastMessage))
{
messageBus.UntypedSourcedBroadcast(source, message);
return;
}
messageBus.SourcedBroadcast(ref source, ref message);
}
///
/// Emits a BroadcastMessage of the given type from the specified component.
///
/// BroadcastMessage to emit.
/// Source of this message.
/// MessageBus to emit to. If null, uses the GlobalMessageBus.
public static void EmitBroadcast(
this ref TMessage message,
InstanceId source,
IMessageBus messageBus = null
)
where TMessage : struct, IBroadcastMessage
{
messageBus ??= MessageHandler.MessageBus;
if (typeof(TMessage) == typeof(IBroadcastMessage))
{
messageBus.UntypedSourcedBroadcast(source, message);
return;
}
messageBus.SourcedBroadcast(ref source, ref message);
}
}
}