2020-08-22 18:08:25 +01:00
|
|
|
|
using OWML.Common;
|
2020-11-03 22:13:12 +00:00
|
|
|
|
using QSB.EventsCore;
|
2020-08-22 18:08:25 +01:00
|
|
|
|
using QSB.Utility;
|
2020-12-04 22:15:41 +00:00
|
|
|
|
using QuantumUNET;
|
2020-12-07 21:19:16 +00:00
|
|
|
|
using QuantumUNET.Messages;
|
2020-08-22 18:08:25 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2020-02-21 23:36:07 +01:00
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace QSB.Messaging
|
|
|
|
|
{
|
2020-12-02 21:29:53 +00:00
|
|
|
|
// Extend this to create new message handlers.
|
2020-12-04 09:23:27 +00:00
|
|
|
|
public class MessageHandler<T> where T : QSBMessageBase, new()
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
public event Action<T> OnClientReceiveMessage;
|
2020-12-03 08:28:05 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public event Action<T> OnServerReceiveMessage;
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private readonly EventType _eventType;
|
2020-08-09 11:11:45 +02:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public MessageHandler(EventType eventType)
|
|
|
|
|
{
|
|
|
|
|
_eventType = eventType + MsgType.Highest + 1;
|
|
|
|
|
if (QSBNetworkManager.Instance.IsReady)
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QSBNetworkManager.Instance.OnNetworkManagerReady += Init;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 20:44:32 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
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);
|
2020-12-11 13:14:58 +00:00
|
|
|
|
QuantumUNET.Components.QSBNetworkManagerUNET.singleton.client.RegisterHandler((short)_eventType, OnClientReceiveMessageHandler);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public void SendToAll(T message)
|
|
|
|
|
{
|
|
|
|
|
if (!QSBNetworkManager.Instance.IsReady)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QSBNetworkServer.SendToAll((short)_eventType, message);
|
|
|
|
|
}
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public void SendToServer(T message)
|
|
|
|
|
{
|
|
|
|
|
if (!QSBNetworkManager.Instance.IsReady)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-12-11 13:14:58 +00:00
|
|
|
|
QuantumUNET.Components.QSBNetworkManagerUNET.singleton.client.Send((short)_eventType, message);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private void OnClientReceiveMessageHandler(QSBNetworkMessage netMsg)
|
|
|
|
|
{
|
|
|
|
|
var message = netMsg.ReadMessage<T>();
|
|
|
|
|
OnClientReceiveMessage?.Invoke(message);
|
|
|
|
|
}
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private void OnServerReceiveMessageHandler(QSBNetworkMessage netMsg)
|
|
|
|
|
{
|
|
|
|
|
var message = netMsg.ReadMessage<T>();
|
|
|
|
|
OnServerReceiveMessage?.Invoke(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|