91 lines
2.6 KiB
C#
Raw Normal View History

2020-12-19 12:51:15 +00:00
using OWML.Common;
using QSB.Messaging;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2021-04-11 17:05:02 +01:00
using QSB.Player.TransformSync;
2020-12-19 12:51:15 +00:00
using QSB.Utility;
using QuantumUNET.Components;
2020-12-14 16:24:52 +00:00
namespace QSB.Events
{
2020-12-02 21:29:53 +00:00
public abstract class QSBEvent<T> : IQSBEvent where T : PlayerMessage, new()
{
public abstract EventType Type { get; }
2020-12-14 21:20:53 +00:00
public uint LocalPlayerId => QSBPlayerManager.LocalPlayerId;
2020-12-14 21:41:56 +01:00
2020-12-02 21:29:53 +00:00
private readonly MessageHandler<T> _eventHandler;
2020-08-10 14:48:40 +01:00
2020-12-02 21:29:53 +00:00
protected QSBEvent()
{
2021-02-12 13:29:01 +00:00
if (UnitTestDetector.IsInUnitTest)
{
return;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
_eventHandler = new MessageHandler<T>(Type);
2020-12-14 21:41:56 +01:00
_eventHandler.OnClientReceiveMessage += message => OnReceive(false, message);
_eventHandler.OnServerReceiveMessage += message => OnReceive(true, message);
2020-12-02 21:29:53 +00:00
}
2020-08-10 14:40:06 +01:00
2020-12-02 21:29:53 +00:00
public abstract void SetupListener();
public abstract void CloseListener();
2020-08-15 20:32:58 +01:00
2021-08-08 19:53:55 +01:00
public virtual void OnReceiveRemote(bool isHost, T message) { }
public virtual void OnReceiveLocal(bool isHost, T message) { }
2020-08-09 09:37:32 +02:00
2020-12-02 21:29:53 +00:00
public void SendEvent(T message)
{
message.FromId = QSBPlayerManager.LocalPlayerId;
2021-03-13 10:17:52 +00:00
QSBCore.UnityEvents.RunWhen(
2020-12-19 10:56:25 +00:00
() => PlayerTransformSync.LocalInstance != null,
() => _eventHandler.SendToServer(message));
2020-12-02 21:29:53 +00:00
}
2020-08-15 20:32:58 +01:00
2021-01-30 10:09:27 +00:00
/// <summary>
/// Checks whether the message should be processed by the executing client/server.
/// </summary>
/// <returns>True if the message should be processed.</returns>
public virtual bool CheckMessage(bool isServer, T message) => true;
2020-12-19 10:56:25 +00:00
private void OnReceive(bool isServer, T message)
2020-12-02 21:29:53 +00:00
{
2020-12-19 10:56:25 +00:00
/* Explanation :
* if <isServer> is true, this message has been received on the server *server*.
* Therefore, we don't want to do any event handling code - that should be dealt
* with on the server *client* and any other client. So just forward the message
2021-07-31 09:45:07 +01:00
* onto all clients. This way, the server *server* just acts as the distribution
2020-12-19 10:56:25 +00:00
* hub for all events.
*/
2021-01-30 10:09:27 +00:00
if (!CheckMessage(isServer, message))
{
return;
}
2020-12-19 10:56:25 +00:00
if (isServer)
2020-12-02 21:29:53 +00:00
{
_eventHandler.SendToAll(message);
2020-12-19 10:56:25 +00:00
return;
2020-12-02 21:29:53 +00:00
}
2020-08-09 09:17:00 +02:00
2021-08-08 19:53:55 +01:00
if (message.OnlySendToHost && !QSBCore.IsHost)
2020-12-13 22:25:23 +00:00
{
2020-12-19 10:56:25 +00:00
return;
2020-12-13 22:25:23 +00:00
}
2020-12-19 10:56:25 +00:00
2020-12-23 12:58:45 +00:00
if (PlayerTransformSync.LocalInstance == null || PlayerTransformSync.LocalInstance.GetComponent<QNetworkIdentity>() == null)
2020-12-19 12:51:15 +00:00
{
DebugLog.ToConsole($"Warning - Tried to handle message of type <{message.GetType().Name}> before localplayer was established.", MessageType.Warning);
return;
}
2020-12-02 21:29:53 +00:00
if (message.FromId == QSBPlayerManager.LocalPlayerId ||
2021-03-09 16:43:41 +00:00
QSBPlayerManager.IsBelongingToLocalPlayer(message.FromId))
2020-12-02 21:29:53 +00:00
{
2021-08-08 19:53:55 +01:00
OnReceiveLocal(QSBCore.IsHost, message);
2020-12-02 21:29:53 +00:00
return;
}
2020-08-09 09:37:32 +02:00
2021-08-08 19:53:55 +01:00
OnReceiveRemote(QSBCore.IsHost, message);
2020-12-02 21:29:53 +00:00
}
}
2020-12-03 08:28:05 +00:00
}