2020-08-22 18:08:25 +01:00
|
|
|
|
using OWML.Common;
|
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2020-02-21 23:36:07 +01:00
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace QSB.Messaging
|
|
|
|
|
{
|
|
|
|
|
// Extend this to create new message handlers.
|
2020-08-10 14:48:40 +01:00
|
|
|
|
public class MessageHandler<T> where T : MessageBase, new()
|
2020-02-21 23:36:07 +01:00
|
|
|
|
{
|
|
|
|
|
public event Action<T> OnClientReceiveMessage;
|
|
|
|
|
public event Action<T> OnServerReceiveMessage;
|
|
|
|
|
|
2020-08-22 18:08:25 +01:00
|
|
|
|
private readonly EventType _eventType;
|
2020-08-09 11:11:45 +02:00
|
|
|
|
|
2020-08-22 18:08:25 +01:00
|
|
|
|
public MessageHandler(EventType eventType)
|
2020-03-13 20:44:32 +01:00
|
|
|
|
{
|
2020-08-22 18:08:25 +01:00
|
|
|
|
_eventType = eventType + MsgType.Highest + 1;
|
2020-08-18 22:37:27 +02:00
|
|
|
|
if (QSBNetworkManager.Instance.IsReady)
|
2020-03-13 20:44:32 +01:00
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-18 22:37:27 +02:00
|
|
|
|
QSBNetworkManager.Instance.OnNetworkManagerReady += Init;
|
2020-03-13 20:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Init()
|
2020-02-21 23:36:07 +01:00
|
|
|
|
{
|
2020-08-22 18:08:25 +01:00
|
|
|
|
if (NetworkServer.handlers.Keys.Contains((short)_eventType))
|
|
|
|
|
{
|
|
|
|
|
DebugLog.LogState($"{_eventType} HANDLER", false);
|
2020-08-23 13:56:58 +02:00
|
|
|
|
DebugLog.ToConsole($"Warning - NetworkServer already contains a handler for EventType {_eventType}", MessageType.Warning);
|
2020-08-23 13:11:17 +02:00
|
|
|
|
NetworkServer.handlers.Remove((short)_eventType);
|
2020-08-22 18:08:25 +01:00
|
|
|
|
}
|
|
|
|
|
NetworkServer.RegisterHandler((short)_eventType, OnServerReceiveMessageHandler);
|
|
|
|
|
NetworkManager.singleton.client.RegisterHandler((short)_eventType, OnClientReceiveMessageHandler);
|
|
|
|
|
DebugLog.LogState($"{_eventType} HANDLER", true);
|
2020-02-21 23:36:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendToAll(T message)
|
|
|
|
|
{
|
2020-08-18 22:37:27 +02:00
|
|
|
|
if (!QSBNetworkManager.Instance.IsReady)
|
2020-03-13 20:44:32 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-22 18:08:25 +01:00
|
|
|
|
NetworkServer.SendToAll((short)_eventType, message);
|
2020-02-21 23:36:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendToServer(T message)
|
|
|
|
|
{
|
2020-08-18 22:37:27 +02:00
|
|
|
|
if (!QSBNetworkManager.Instance.IsReady)
|
2020-03-13 20:44:32 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-22 18:08:25 +01:00
|
|
|
|
NetworkManager.singleton.client.Send((short)_eventType, message);
|
2020-02-21 23:36:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnClientReceiveMessageHandler(NetworkMessage netMsg)
|
|
|
|
|
{
|
|
|
|
|
var message = netMsg.ReadMessage<T>();
|
|
|
|
|
OnClientReceiveMessage?.Invoke(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnServerReceiveMessageHandler(NetworkMessage netMsg)
|
|
|
|
|
{
|
|
|
|
|
var message = netMsg.ReadMessage<T>();
|
|
|
|
|
OnServerReceiveMessage?.Invoke(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|