2020-08-08 13:27:49 +01:00
|
|
|
|
using QSB.Messaging;
|
2020-08-09 09:17:00 +02:00
|
|
|
|
using QSB.TransformSync;
|
2020-08-21 22:31:42 +02:00
|
|
|
|
using UnityEngine.Networking;
|
2020-08-08 00:08:44 +01:00
|
|
|
|
|
|
|
|
|
namespace QSB.Events
|
|
|
|
|
{
|
2020-08-10 14:48:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Abstract class that handles all event code.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The message type to use.</typeparam>
|
2020-08-18 20:47:17 +02:00
|
|
|
|
public abstract class QSBEvent<T> : IQSBEvent where T : PlayerMessage, new()
|
2020-08-08 00:08:44 +01:00
|
|
|
|
{
|
2020-08-22 18:08:25 +01:00
|
|
|
|
public abstract EventType Type { get; }
|
2020-09-04 20:09:25 +01:00
|
|
|
|
public uint LocalPlayerId => PlayerRegistry.LocalPlayerId;
|
2020-08-10 14:48:40 +01:00
|
|
|
|
private readonly MessageHandler<T> _eventHandler;
|
|
|
|
|
|
|
|
|
|
protected QSBEvent()
|
|
|
|
|
{
|
|
|
|
|
_eventHandler = new MessageHandler<T>(Type);
|
|
|
|
|
_eventHandler.OnClientReceiveMessage += OnClientReceive;
|
|
|
|
|
_eventHandler.OnServerReceiveMessage += OnServerReceive;
|
2020-08-10 20:14:19 +02:00
|
|
|
|
}
|
2020-08-10 14:40:06 +01:00
|
|
|
|
|
2020-08-10 14:48:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called to set up the activators for the event.
|
|
|
|
|
/// </summary>
|
2020-08-08 12:23:23 +01:00
|
|
|
|
public abstract void SetupListener();
|
2020-08-10 14:48:40 +01:00
|
|
|
|
|
2020-08-15 20:32:58 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called to remove all set up activators.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract void CloseListener();
|
|
|
|
|
|
2020-08-10 14:48:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called on every client that didn't send the event.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
2020-08-10 14:40:06 +01:00
|
|
|
|
public virtual void OnReceiveRemote(T message)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-08-09 09:17:00 +02:00
|
|
|
|
|
2020-08-10 14:48:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called on the client that sent the event.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
2020-08-09 09:37:32 +02:00
|
|
|
|
public virtual void OnReceiveLocal(T message)
|
2020-08-09 09:17:00 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 14:48:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called on the server.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
public virtual void OnServerReceive(T message)
|
2020-08-09 09:37:32 +02:00
|
|
|
|
{
|
2020-08-10 14:48:40 +01:00
|
|
|
|
_eventHandler.SendToAll(message);
|
2020-08-09 09:37:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 14:48:40 +01:00
|
|
|
|
public void SendEvent(T message)
|
2020-08-09 09:17:00 +02:00
|
|
|
|
{
|
2020-08-17 19:19:41 +02:00
|
|
|
|
message.FromId = PlayerRegistry.LocalPlayerId;
|
2020-08-20 14:10:37 +01:00
|
|
|
|
QSB.Helper.Events.Unity.RunWhen(() => PlayerTransformSync.LocalInstance != null, () => Send(message));
|
2020-08-09 09:17:00 +02:00
|
|
|
|
}
|
2020-08-15 20:32:58 +01:00
|
|
|
|
|
2020-08-09 09:37:32 +02:00
|
|
|
|
private void Send(T message)
|
2020-08-09 09:17:00 +02:00
|
|
|
|
{
|
2020-08-21 22:31:42 +02:00
|
|
|
|
if (NetworkServer.active)
|
|
|
|
|
{
|
|
|
|
|
_eventHandler.SendToAll(message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_eventHandler.SendToServer(message);
|
|
|
|
|
}
|
2020-08-09 09:17:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 11:45:24 +01:00
|
|
|
|
private void OnClientReceive(T message)
|
2020-08-09 09:17:00 +02:00
|
|
|
|
{
|
2020-09-02 11:17:04 +01:00
|
|
|
|
if (message.FromId == PlayerRegistry.LocalPlayerId || PlayerRegistry.IsBelongingToLocalPlayer(message.AboutId))
|
2020-08-09 09:17:00 +02:00
|
|
|
|
{
|
2020-08-09 12:19:51 +01:00
|
|
|
|
OnReceiveLocal(message);
|
2020-08-09 09:17:00 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-09 09:37:32 +02:00
|
|
|
|
|
2020-08-10 14:40:06 +01:00
|
|
|
|
OnReceiveRemote(message);
|
2020-08-09 09:17:00 +02:00
|
|
|
|
}
|
2020-08-08 00:08:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|