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 where T : QSBMessageBase, new() { public event Action OnClientReceiveMessage; public event Action 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(); OnClientReceiveMessage?.Invoke(message); } private void OnServerReceiveMessageHandler(QSBNetworkMessage netMsg) { var message = netMsg.ReadMessage(); OnServerReceiveMessage?.Invoke(message); } } }