fixed the generic issue

This commit is contained in:
Aleksander Waage 2020-08-09 08:58:54 +02:00
parent a3e3dff725
commit a7a6b9b86d
18 changed files with 22 additions and 45 deletions

View File

@ -5,8 +5,6 @@ namespace QSB.Animation
{ {
public class AnimTriggerMessage : QSBMessage public class AnimTriggerMessage : QSBMessage
{ {
public override MessageType MessageType => MessageType.AnimTrigger;
public short TriggerId; public short TriggerId;
public uint SenderId; public uint SenderId;
public float Value; public float Value;

View File

@ -62,7 +62,7 @@ namespace QSB.Animation
{ {
InitCommon(body); InitCommon(body);
_triggerHandler = new MessageHandler<AnimTriggerMessage>(); _triggerHandler = new MessageHandler<AnimTriggerMessage>(MessageType.AnimTrigger);
_triggerHandler.OnServerReceiveMessage += OnServerReceiveMessage; _triggerHandler.OnServerReceiveMessage += OnServerReceiveMessage;
_triggerHandler.OnClientReceiveMessage += OnClientReceiveMessage; _triggerHandler.OnClientReceiveMessage += OnClientReceiveMessage;

View File

@ -1,12 +1,9 @@
using QSB.Messaging; using UnityEngine.Networking;
using UnityEngine.Networking;
namespace QSB.Events namespace QSB.Events
{ {
public class DeathMessage : PlayerMessage public class DeathMessage : PlayerMessage
{ {
public override MessageType MessageType => MessageType.Death;
public DeathType DeathType { get; set; } public DeathType DeathType { get; set; }
public override void Deserialize(NetworkReader reader) public override void Deserialize(NetworkReader reader)

View File

@ -2,7 +2,6 @@
using System.Linq; using System.Linq;
using QSB.Messaging; using QSB.Messaging;
using QSB.TransformSync; using QSB.TransformSync;
using QSB.Utility;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
@ -18,7 +17,7 @@ namespace QSB.Events
{ {
LocalInstance = this; LocalInstance = this;
_eventHandler = new MessageHandler<EventMessage>(); _eventHandler = new MessageHandler<EventMessage>(MessageType.Event);
_eventHandler.OnClientReceiveMessage += OnClientReceiveMessage; _eventHandler.OnClientReceiveMessage += OnClientReceiveMessage;
_eventHandler.OnServerReceiveMessage += OnServerReceiveMessage; _eventHandler.OnServerReceiveMessage += OnServerReceiveMessage;
} }

View File

@ -1,6 +1,4 @@
using QSB.Messaging; using System;
using QSB.Utility;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
@ -10,8 +8,6 @@ namespace QSB.Events
{ {
public class EventMessage : PlayerMessage public class EventMessage : PlayerMessage
{ {
public override MessageType MessageType => MessageType.Event;
public int EventType { get; set; } public int EventType { get; set; }
public object[] Data { get; set; } public object[] Data { get; set; }

View File

@ -1,12 +1,9 @@
using QSB.Messaging; using UnityEngine.Networking;
using UnityEngine.Networking;
namespace QSB.Events namespace QSB.Events
{ {
public class FullStateMessage : PlayerMessage public class FullStateMessage : PlayerMessage
{ {
public override MessageType MessageType => MessageType.FullState;
public string PlayerName { get; set; } public string PlayerName { get; set; }
public bool IsReady { get; set; } public bool IsReady { get; set; }

View File

@ -16,7 +16,7 @@ namespace QSB.Events
{ {
Instance = this; Instance = this;
_stateRequestHandler = new MessageHandler<StateRequestMessage>(); _stateRequestHandler = new MessageHandler<StateRequestMessage>(MessageType.FullStateRequest);
_stateRequestHandler.OnServerReceiveMessage += OnServerReceiveMessage; _stateRequestHandler.OnServerReceiveMessage += OnServerReceiveMessage;
} }

View File

@ -12,7 +12,7 @@ namespace QSB.Events
private void Awake() private void Awake()
{ {
_messageHandler = new MessageHandler<FullStateMessage>(); _messageHandler = new MessageHandler<FullStateMessage>(MessageType.FullState);
_messageHandler.OnClientReceiveMessage += OnClientReceiveMessage; _messageHandler.OnClientReceiveMessage += OnClientReceiveMessage;
LocalInstance = this; LocalInstance = this;

View File

@ -1,14 +1,11 @@
using System; using System;
using System.Linq; using System.Linq;
using QSB.Messaging;
using UnityEngine.Networking; using UnityEngine.Networking;
namespace QSB.Events namespace QSB.Events
{ {
public class LeaveMessage : PlayerMessage public class LeaveMessage : PlayerMessage
{ {
public override MessageType MessageType => MessageType.Leave;
public uint[] ObjectIds { get; set; } public uint[] ObjectIds { get; set; }
public override void Deserialize(NetworkReader reader) public override void Deserialize(NetworkReader reader)

View File

@ -1,7 +1,6 @@
using System.Linq; using System.Linq;
using QSB.Messaging; using QSB.Messaging;
using QSB.Utility; using QSB.Utility;
using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
namespace QSB.Events namespace QSB.Events
@ -15,7 +14,7 @@ namespace QSB.Events
private void Awake() private void Awake()
{ {
_leaveHandler = new MessageHandler<LeaveMessage>(); _leaveHandler = new MessageHandler<LeaveMessage>(MessageType.Leave);
_leaveHandler.OnClientReceiveMessage += OnClientReceiveMessage; _leaveHandler.OnClientReceiveMessage += OnClientReceiveMessage;
} }
@ -42,7 +41,7 @@ namespace QSB.Events
private void DestroyObject(uint objectId) private void DestroyObject(uint objectId)
{ {
var component = GameObject.FindObjectsOfType<NetworkBehaviour>() var component = FindObjectsOfType<NetworkBehaviour>()
.FirstOrDefault(x => x.netId.Value == objectId); .FirstOrDefault(x => x.netId.Value == objectId);
if (component == null) if (component == null)
{ {

View File

@ -1,9 +1,6 @@
using QSB.Messaging; namespace QSB.Events
namespace QSB.Events
{ {
public class StateRequestMessage : PlayerMessage public class StateRequestMessage : PlayerMessage
{ {
public override MessageType MessageType => MessageType.FullStateRequest;
} }
} }

View File

@ -9,8 +9,11 @@ namespace QSB.Messaging
public event Action<T> OnClientReceiveMessage; public event Action<T> OnClientReceiveMessage;
public event Action<T> OnServerReceiveMessage; public event Action<T> OnServerReceiveMessage;
public MessageHandler() private readonly MessageType _messageType;
public MessageHandler(MessageType messageType)
{ {
_messageType = messageType;
if (QSBNetworkManager.IsReady) if (QSBNetworkManager.IsReady)
{ {
Init(); Init();
@ -23,9 +26,8 @@ namespace QSB.Messaging
private void Init() private void Init()
{ {
var message = (T)Activator.CreateInstance(typeof(T)); NetworkServer.RegisterHandler((short)_messageType, OnServerReceiveMessageHandler);
NetworkServer.RegisterHandler((short)message.MessageType, OnServerReceiveMessageHandler); NetworkManager.singleton.client.RegisterHandler((short)_messageType, OnClientReceiveMessageHandler);
NetworkManager.singleton.client.RegisterHandler((short)message.MessageType, OnClientReceiveMessageHandler);
} }
public void SendToAll(T message) public void SendToAll(T message)
@ -34,7 +36,7 @@ namespace QSB.Messaging
{ {
return; return;
} }
NetworkServer.SendToAll((short)message.MessageType, message); NetworkServer.SendToAll((short)_messageType, message);
} }
public void SendToServer(T message) public void SendToServer(T message)
@ -43,7 +45,7 @@ namespace QSB.Messaging
{ {
return; return;
} }
NetworkManager.singleton.client.Send((short)message.MessageType, message); NetworkManager.singleton.client.Send((short)_messageType, message);
} }
private void OnClientReceiveMessageHandler(NetworkMessage netMsg) private void OnClientReceiveMessageHandler(NetworkMessage netMsg)

View File

@ -2,8 +2,7 @@
namespace QSB.Messaging namespace QSB.Messaging
{ {
public abstract class QSBMessage : MessageBase public class QSBMessage : MessageBase
{ {
public abstract MessageType MessageType { get; }
} }
} }

View File

@ -75,7 +75,7 @@ namespace QSB.TimeSync
_shipSpawnPoint.transform.rotation = shipTransform.rotation; _shipSpawnPoint.transform.rotation = shipTransform.rotation;
} }
_deathHandler = new MessageHandler<DeathMessage>(); _deathHandler = new MessageHandler<DeathMessage>(MessageType.Death);
_deathHandler.OnServerReceiveMessage += OnServerReceiveMessage; _deathHandler.OnServerReceiveMessage += OnServerReceiveMessage;
_deathHandler.OnClientReceiveMessage += OnClientReceiveMessage; _deathHandler.OnClientReceiveMessage += OnClientReceiveMessage;
} }

View File

@ -5,8 +5,6 @@ namespace QSB.TimeSync
{ {
public class WakeUpMessage : QSBMessage public class WakeUpMessage : QSBMessage
{ {
public override MessageType MessageType => MessageType.WakeUp;
public float ServerTime { get; set; } public float ServerTime { get; set; }
public int LoopCount { get; set; } public int LoopCount { get; set; }

View File

@ -31,7 +31,7 @@ namespace QSB.TimeSync
return; return;
} }
_wakeUpHandler = new MessageHandler<WakeUpMessage>(); _wakeUpHandler = new MessageHandler<WakeUpMessage>(MessageType.WakeUp);
_wakeUpHandler.OnClientReceiveMessage += OnClientReceiveMessage; _wakeUpHandler.OnClientReceiveMessage += OnClientReceiveMessage;
var sceneName = SceneManager.GetActiveScene().name; var sceneName = SceneManager.GetActiveScene().name;

View File

@ -5,8 +5,6 @@ namespace QSB.TransformSync
{ {
public class SectorMessage : QSBMessage public class SectorMessage : QSBMessage
{ {
public override MessageType MessageType => MessageType.Sector;
public int SectorId; public int SectorId;
public uint SenderId; public uint SenderId;

View File

@ -24,7 +24,7 @@ namespace QSB.TransformSync
private void Start() private void Start()
{ {
_sectorHandler = new MessageHandler<SectorMessage>(); _sectorHandler = new MessageHandler<SectorMessage>(MessageType.Sector);
_sectorHandler.OnClientReceiveMessage += OnClientReceiveMessage; _sectorHandler.OnClientReceiveMessage += OnClientReceiveMessage;
_sectorHandler.OnServerReceiveMessage += OnServerReceiveMessage; _sectorHandler.OnServerReceiveMessage += OnServerReceiveMessage;
} }