2020-08-08 12:27:49 +00:00
|
|
|
|
using QSB.Messaging;
|
2020-08-09 07:17:00 +00:00
|
|
|
|
using QSB.TransformSync;
|
|
|
|
|
using QSB.Utility;
|
2020-08-07 23:08:44 +00:00
|
|
|
|
|
|
|
|
|
namespace QSB.Events
|
|
|
|
|
{
|
2020-08-09 07:37:32 +00:00
|
|
|
|
public abstract class QSBEvent<T> where T : PlayerMessage, new()
|
|
|
|
|
|
2020-08-07 23:08:44 +00:00
|
|
|
|
{
|
2020-08-09 07:17:00 +00:00
|
|
|
|
public abstract MessageType Type { get; }
|
2020-08-07 23:08:44 +00:00
|
|
|
|
|
2020-08-08 11:23:23 +00:00
|
|
|
|
public abstract void SetupListener();
|
2020-08-09 11:19:51 +00:00
|
|
|
|
public abstract void OnReceive(T message);
|
2020-08-09 07:17:00 +00:00
|
|
|
|
|
2020-08-09 07:37:32 +00:00
|
|
|
|
public virtual void OnReceiveLocal(T message)
|
2020-08-09 07:17:00 +00:00
|
|
|
|
{
|
2020-08-09 12:58:13 +00:00
|
|
|
|
OnReceive(message);
|
2020-08-09 07:17:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 07:37:32 +00:00
|
|
|
|
public void SendEvent(T message)
|
|
|
|
|
{
|
|
|
|
|
UnityHelper.Instance.RunWhen(() => PlayerTransformSync.LocalInstance != null, () => Send(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly MessageHandler<T> _eventHandler;
|
2020-08-09 07:17:00 +00:00
|
|
|
|
|
|
|
|
|
protected QSBEvent()
|
|
|
|
|
{
|
2020-08-09 07:37:32 +00:00
|
|
|
|
_eventHandler = new MessageHandler<T>(Type);
|
2020-08-09 07:17:00 +00:00
|
|
|
|
_eventHandler.OnClientReceiveMessage += OnClientReceiveMessage;
|
|
|
|
|
_eventHandler.OnServerReceiveMessage += OnServerReceiveMessage;
|
|
|
|
|
|
|
|
|
|
SetupListener();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 07:37:32 +00:00
|
|
|
|
private void Send(T message)
|
2020-08-09 07:17:00 +00:00
|
|
|
|
{
|
2020-08-09 07:37:32 +00:00
|
|
|
|
_eventHandler.SendToServer(message);
|
2020-08-09 07:17:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 07:37:32 +00:00
|
|
|
|
private void OnServerReceiveMessage(T message)
|
2020-08-09 07:17:00 +00:00
|
|
|
|
{
|
|
|
|
|
_eventHandler.SendToAll(message);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 07:37:32 +00:00
|
|
|
|
private void OnClientReceiveMessage(T message)
|
2020-08-09 07:17:00 +00:00
|
|
|
|
{
|
2020-08-09 13:26:33 +00:00
|
|
|
|
if (message.SenderId == PlayerTransformSync.LocalInstance?.netId.Value)
|
2020-08-09 07:17:00 +00:00
|
|
|
|
{
|
2020-08-09 11:19:51 +00:00
|
|
|
|
OnReceiveLocal(message);
|
2020-08-09 07:17:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-09 07:37:32 +00:00
|
|
|
|
|
2020-08-09 11:19:51 +00:00
|
|
|
|
OnReceive(message);
|
2020-08-09 07:17:00 +00:00
|
|
|
|
}
|
2020-08-07 23:08:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|