quantum-space-buddies/QSB/MessagesCore/MessageHandler.cs
Mister_Nebula 43f6034800 cleanup
2020-12-04 21:04:18 +00:00

75 lines
1.9 KiB
C#

using OWML.Common;
using QSB.EventsCore;
using QSB.QuantumUNET;
using QSB.Utility;
using System;
using System.Linq;
using UnityEngine.Networking;
namespace QSB.Messaging
{
// Extend this to create new message handlers.
public class MessageHandler<T> where T : QSBMessageBase, new()
{
public event Action<T> OnClientReceiveMessage;
public event Action<T> OnServerReceiveMessage;
private readonly EventType _eventType;
public MessageHandler(EventType eventType)
{
_eventType = eventType + MsgType.Highest + 1;
if (QSBNetworkManager.Instance.IsReady)
{
Init();
}
else
{
QSBNetworkManager.Instance.OnNetworkManagerReady += Init;
}
}
private void Init()
{
var eventName = Enum.GetName(typeof(EventType), _eventType - 1 - MsgType.Highest).ToUpper();
if (QSBNetworkServer.handlers.Keys.Contains((short)_eventType))
{
DebugLog.ToConsole($"Warning - NetworkServer already contains a handler for EventType {_eventType}", MessageType.Warning);
QSBNetworkServer.handlers.Remove((short)_eventType);
}
QSBNetworkServer.RegisterHandler((short)_eventType, OnServerReceiveMessageHandler);
QSBNetworkManager.singleton.client.RegisterHandler((short)_eventType, OnClientReceiveMessageHandler);
}
public void SendToAll(T message)
{
if (!QSBNetworkManager.Instance.IsReady)
{
return;
}
QSBNetworkServer.SendToAll((short)_eventType, message);
}
public void SendToServer(T message)
{
if (!QSBNetworkManager.Instance.IsReady)
{
return;
}
QSBNetworkManager.singleton.client.Send((short)_eventType, message);
}
private void OnClientReceiveMessageHandler(QSBNetworkMessage netMsg)
{
var message = netMsg.ReadMessage<T>();
OnClientReceiveMessage?.Invoke(message);
}
private void OnServerReceiveMessageHandler(QSBNetworkMessage netMsg)
{
var message = netMsg.ReadMessage<T>();
OnServerReceiveMessage?.Invoke(message);
}
}
}