2020-12-11 22:42:21 +00:00
|
|
|
|
using OWML.ModHelper.Events;
|
|
|
|
|
using QSB.Messaging;
|
2020-11-03 21:33:48 +00:00
|
|
|
|
using QSB.Player;
|
2020-08-09 07:17:00 +00:00
|
|
|
|
using QSB.TransformSync;
|
2020-12-11 22:42:21 +00:00
|
|
|
|
using QSB.Utility;
|
2020-12-04 22:15:41 +00:00
|
|
|
|
using QuantumUNET;
|
2020-12-11 22:42:21 +00:00
|
|
|
|
using System;
|
2020-08-07 23:08:44 +00:00
|
|
|
|
|
2020-11-03 21:42:14 +00:00
|
|
|
|
namespace QSB.EventsCore
|
2020-08-07 23:08:44 +00:00
|
|
|
|
{
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public abstract class QSBEvent<T> : IQSBEvent where T : PlayerMessage, new()
|
|
|
|
|
{
|
|
|
|
|
public abstract EventType Type { get; }
|
|
|
|
|
public uint LocalPlayerId => QSBPlayerManager.LocalPlayerId;
|
|
|
|
|
private readonly MessageHandler<T> _eventHandler;
|
2020-08-10 13:48:40 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
protected QSBEvent()
|
|
|
|
|
{
|
|
|
|
|
_eventHandler = new MessageHandler<T>(Type);
|
2020-12-11 22:42:21 +00:00
|
|
|
|
_eventHandler.OnClientReceiveMessage += OnReceive;
|
|
|
|
|
_eventHandler.OnServerReceiveMessage += OnReceive;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-08-10 13:40:06 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public abstract void SetupListener();
|
2020-08-10 13:48:40 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public abstract void CloseListener();
|
2020-08-15 19:32:58 +00:00
|
|
|
|
|
2020-12-11 22:42:21 +00:00
|
|
|
|
public virtual void OnReceiveRemote(bool server, T message) { }
|
|
|
|
|
public virtual void OnReceiveLocal(bool server, T message) { }
|
2020-08-09 07:37:32 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public void SendEvent(T message)
|
|
|
|
|
{
|
|
|
|
|
message.FromId = QSBPlayerManager.LocalPlayerId;
|
|
|
|
|
QSB.Helper.Events.Unity.RunWhen(() => PlayerTransformSync.LocalInstance != null, () => Send(message));
|
|
|
|
|
}
|
2020-08-15 19:32:58 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private void Send(T message)
|
|
|
|
|
{
|
|
|
|
|
if (QSBNetworkServer.active)
|
|
|
|
|
{
|
|
|
|
|
_eventHandler.SendToAll(message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_eventHandler.SendToServer(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-09 07:17:00 +00:00
|
|
|
|
|
2020-12-11 22:42:21 +00:00
|
|
|
|
private void OnReceive(T message)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2020-12-11 22:42:21 +00:00
|
|
|
|
/* Uncomment for event debugging - very noisey and annoying (keeping as I don't want to have to rewrite this each time)
|
|
|
|
|
if (_eventHandler.BaseEventType != EventType.ServerTime && _eventHandler.BaseEventType != EventType.Geyser)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"message type {_eventHandler.BaseEventType} from {message.FromId}, about {message.AboutId}. isserver? {QSB.IsServer}");
|
|
|
|
|
}
|
|
|
|
|
*/
|
2020-12-02 21:29:53 +00:00
|
|
|
|
if (message.FromId == QSBPlayerManager.LocalPlayerId ||
|
|
|
|
|
QSBPlayerManager.IsBelongingToLocalPlayer(message.AboutId))
|
|
|
|
|
{
|
2020-12-11 22:42:21 +00:00
|
|
|
|
OnReceiveLocal(QSB.IsServer, message);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-09 07:37:32 +00:00
|
|
|
|
|
2020-12-11 22:42:21 +00:00
|
|
|
|
OnReceiveRemote(QSB.IsServer, message);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|