From a7a6b9b86d0ff40ae05aac1aa53a7ef2a26d6533 Mon Sep 17 00:00:00 2001 From: Aleksander Waage Date: Sun, 9 Aug 2020 08:58:54 +0200 Subject: [PATCH] fixed the generic issue --- QSB/Animation/AnimTriggerMessage.cs | 2 -- QSB/Animation/AnimationSync.cs | 2 +- QSB/Events/DeathMessage.cs | 5 +---- QSB/Events/EventHandler.cs | 3 +-- QSB/Events/EventMessage.cs | 6 +----- QSB/Events/FullStateMessage.cs | 5 +---- QSB/Events/FullStateRequest.cs | 2 +- QSB/Events/GameState.cs | 2 +- QSB/Events/LeaveMessage.cs | 3 --- QSB/Events/PlayerLeave.cs | 5 ++--- QSB/Events/StateRequestMessage.cs | 5 +---- QSB/Messaging/MessageHandler.cs | 14 ++++++++------ QSB/Messaging/QSBMessage.cs | 3 +-- QSB/TimeSync/RespawnOnDeath.cs | 2 +- QSB/TimeSync/WakeUpMessage.cs | 2 -- QSB/TimeSync/WakeUpSync.cs | 2 +- QSB/TransformSync/SectorMessage.cs | 2 -- QSB/TransformSync/SectorSync.cs | 2 +- 18 files changed, 22 insertions(+), 45 deletions(-) diff --git a/QSB/Animation/AnimTriggerMessage.cs b/QSB/Animation/AnimTriggerMessage.cs index da0741c0..c28bd7b5 100644 --- a/QSB/Animation/AnimTriggerMessage.cs +++ b/QSB/Animation/AnimTriggerMessage.cs @@ -5,8 +5,6 @@ namespace QSB.Animation { public class AnimTriggerMessage : QSBMessage { - public override MessageType MessageType => MessageType.AnimTrigger; - public short TriggerId; public uint SenderId; public float Value; diff --git a/QSB/Animation/AnimationSync.cs b/QSB/Animation/AnimationSync.cs index 1e5d3e00..62f80d9c 100644 --- a/QSB/Animation/AnimationSync.cs +++ b/QSB/Animation/AnimationSync.cs @@ -62,7 +62,7 @@ namespace QSB.Animation { InitCommon(body); - _triggerHandler = new MessageHandler(); + _triggerHandler = new MessageHandler(MessageType.AnimTrigger); _triggerHandler.OnServerReceiveMessage += OnServerReceiveMessage; _triggerHandler.OnClientReceiveMessage += OnClientReceiveMessage; diff --git a/QSB/Events/DeathMessage.cs b/QSB/Events/DeathMessage.cs index b6c5ced6..cbd8b362 100644 --- a/QSB/Events/DeathMessage.cs +++ b/QSB/Events/DeathMessage.cs @@ -1,12 +1,9 @@ -using QSB.Messaging; -using UnityEngine.Networking; +using UnityEngine.Networking; namespace QSB.Events { public class DeathMessage : PlayerMessage { - public override MessageType MessageType => MessageType.Death; - public DeathType DeathType { get; set; } public override void Deserialize(NetworkReader reader) diff --git a/QSB/Events/EventHandler.cs b/QSB/Events/EventHandler.cs index bc99cdb9..c5e7de26 100644 --- a/QSB/Events/EventHandler.cs +++ b/QSB/Events/EventHandler.cs @@ -2,7 +2,6 @@ using System.Linq; using QSB.Messaging; using QSB.TransformSync; -using QSB.Utility; using UnityEngine; using UnityEngine.Networking; @@ -18,7 +17,7 @@ namespace QSB.Events { LocalInstance = this; - _eventHandler = new MessageHandler(); + _eventHandler = new MessageHandler(MessageType.Event); _eventHandler.OnClientReceiveMessage += OnClientReceiveMessage; _eventHandler.OnServerReceiveMessage += OnServerReceiveMessage; } diff --git a/QSB/Events/EventMessage.cs b/QSB/Events/EventMessage.cs index 55600e65..8f3b2676 100644 --- a/QSB/Events/EventMessage.cs +++ b/QSB/Events/EventMessage.cs @@ -1,6 +1,4 @@ -using QSB.Messaging; -using QSB.Utility; -using System; +using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; @@ -10,8 +8,6 @@ namespace QSB.Events { public class EventMessage : PlayerMessage { - public override MessageType MessageType => MessageType.Event; - public int EventType { get; set; } public object[] Data { get; set; } diff --git a/QSB/Events/FullStateMessage.cs b/QSB/Events/FullStateMessage.cs index d6601fb7..72db0bf1 100644 --- a/QSB/Events/FullStateMessage.cs +++ b/QSB/Events/FullStateMessage.cs @@ -1,12 +1,9 @@ -using QSB.Messaging; -using UnityEngine.Networking; +using UnityEngine.Networking; namespace QSB.Events { public class FullStateMessage : PlayerMessage { - public override MessageType MessageType => MessageType.FullState; - public string PlayerName { get; set; } public bool IsReady { get; set; } diff --git a/QSB/Events/FullStateRequest.cs b/QSB/Events/FullStateRequest.cs index c664166b..2c5bda60 100644 --- a/QSB/Events/FullStateRequest.cs +++ b/QSB/Events/FullStateRequest.cs @@ -16,7 +16,7 @@ namespace QSB.Events { Instance = this; - _stateRequestHandler = new MessageHandler(); + _stateRequestHandler = new MessageHandler(MessageType.FullStateRequest); _stateRequestHandler.OnServerReceiveMessage += OnServerReceiveMessage; } diff --git a/QSB/Events/GameState.cs b/QSB/Events/GameState.cs index acec9361..bc3ae9ce 100644 --- a/QSB/Events/GameState.cs +++ b/QSB/Events/GameState.cs @@ -12,7 +12,7 @@ namespace QSB.Events private void Awake() { - _messageHandler = new MessageHandler(); + _messageHandler = new MessageHandler(MessageType.FullState); _messageHandler.OnClientReceiveMessage += OnClientReceiveMessage; LocalInstance = this; diff --git a/QSB/Events/LeaveMessage.cs b/QSB/Events/LeaveMessage.cs index 8c9423ff..933817df 100644 --- a/QSB/Events/LeaveMessage.cs +++ b/QSB/Events/LeaveMessage.cs @@ -1,14 +1,11 @@ using System; using System.Linq; -using QSB.Messaging; using UnityEngine.Networking; namespace QSB.Events { public class LeaveMessage : PlayerMessage { - public override MessageType MessageType => MessageType.Leave; - public uint[] ObjectIds { get; set; } public override void Deserialize(NetworkReader reader) diff --git a/QSB/Events/PlayerLeave.cs b/QSB/Events/PlayerLeave.cs index 3d88315f..edc33d00 100644 --- a/QSB/Events/PlayerLeave.cs +++ b/QSB/Events/PlayerLeave.cs @@ -1,7 +1,6 @@ using System.Linq; using QSB.Messaging; using QSB.Utility; -using UnityEngine; using UnityEngine.Networking; namespace QSB.Events @@ -15,7 +14,7 @@ namespace QSB.Events private void Awake() { - _leaveHandler = new MessageHandler(); + _leaveHandler = new MessageHandler(MessageType.Leave); _leaveHandler.OnClientReceiveMessage += OnClientReceiveMessage; } @@ -42,7 +41,7 @@ namespace QSB.Events private void DestroyObject(uint objectId) { - var component = GameObject.FindObjectsOfType() + var component = FindObjectsOfType() .FirstOrDefault(x => x.netId.Value == objectId); if (component == null) { diff --git a/QSB/Events/StateRequestMessage.cs b/QSB/Events/StateRequestMessage.cs index fd4138bc..c193b876 100644 --- a/QSB/Events/StateRequestMessage.cs +++ b/QSB/Events/StateRequestMessage.cs @@ -1,9 +1,6 @@ -using QSB.Messaging; - -namespace QSB.Events +namespace QSB.Events { public class StateRequestMessage : PlayerMessage { - public override MessageType MessageType => MessageType.FullStateRequest; } } diff --git a/QSB/Messaging/MessageHandler.cs b/QSB/Messaging/MessageHandler.cs index 1bdd6d26..78a480fb 100644 --- a/QSB/Messaging/MessageHandler.cs +++ b/QSB/Messaging/MessageHandler.cs @@ -9,8 +9,11 @@ namespace QSB.Messaging public event Action OnClientReceiveMessage; public event Action OnServerReceiveMessage; - public MessageHandler() + private readonly MessageType _messageType; + + public MessageHandler(MessageType messageType) { + _messageType = messageType; if (QSBNetworkManager.IsReady) { Init(); @@ -23,9 +26,8 @@ namespace QSB.Messaging private void Init() { - var message = (T)Activator.CreateInstance(typeof(T)); - NetworkServer.RegisterHandler((short)message.MessageType, OnServerReceiveMessageHandler); - NetworkManager.singleton.client.RegisterHandler((short)message.MessageType, OnClientReceiveMessageHandler); + NetworkServer.RegisterHandler((short)_messageType, OnServerReceiveMessageHandler); + NetworkManager.singleton.client.RegisterHandler((short)_messageType, OnClientReceiveMessageHandler); } public void SendToAll(T message) @@ -34,7 +36,7 @@ namespace QSB.Messaging { return; } - NetworkServer.SendToAll((short)message.MessageType, message); + NetworkServer.SendToAll((short)_messageType, message); } public void SendToServer(T message) @@ -43,7 +45,7 @@ namespace QSB.Messaging { return; } - NetworkManager.singleton.client.Send((short)message.MessageType, message); + NetworkManager.singleton.client.Send((short)_messageType, message); } private void OnClientReceiveMessageHandler(NetworkMessage netMsg) diff --git a/QSB/Messaging/QSBMessage.cs b/QSB/Messaging/QSBMessage.cs index 63e9a24f..b6bb3fdc 100644 --- a/QSB/Messaging/QSBMessage.cs +++ b/QSB/Messaging/QSBMessage.cs @@ -2,8 +2,7 @@ namespace QSB.Messaging { - public abstract class QSBMessage : MessageBase + public class QSBMessage : MessageBase { - public abstract MessageType MessageType { get; } } } diff --git a/QSB/TimeSync/RespawnOnDeath.cs b/QSB/TimeSync/RespawnOnDeath.cs index 11c024ec..e11a9804 100644 --- a/QSB/TimeSync/RespawnOnDeath.cs +++ b/QSB/TimeSync/RespawnOnDeath.cs @@ -75,7 +75,7 @@ namespace QSB.TimeSync _shipSpawnPoint.transform.rotation = shipTransform.rotation; } - _deathHandler = new MessageHandler(); + _deathHandler = new MessageHandler(MessageType.Death); _deathHandler.OnServerReceiveMessage += OnServerReceiveMessage; _deathHandler.OnClientReceiveMessage += OnClientReceiveMessage; } diff --git a/QSB/TimeSync/WakeUpMessage.cs b/QSB/TimeSync/WakeUpMessage.cs index e83caed0..2312b348 100644 --- a/QSB/TimeSync/WakeUpMessage.cs +++ b/QSB/TimeSync/WakeUpMessage.cs @@ -5,8 +5,6 @@ namespace QSB.TimeSync { public class WakeUpMessage : QSBMessage { - public override MessageType MessageType => MessageType.WakeUp; - public float ServerTime { get; set; } public int LoopCount { get; set; } diff --git a/QSB/TimeSync/WakeUpSync.cs b/QSB/TimeSync/WakeUpSync.cs index 3ba79e54..df53406d 100644 --- a/QSB/TimeSync/WakeUpSync.cs +++ b/QSB/TimeSync/WakeUpSync.cs @@ -31,7 +31,7 @@ namespace QSB.TimeSync return; } - _wakeUpHandler = new MessageHandler(); + _wakeUpHandler = new MessageHandler(MessageType.WakeUp); _wakeUpHandler.OnClientReceiveMessage += OnClientReceiveMessage; var sceneName = SceneManager.GetActiveScene().name; diff --git a/QSB/TransformSync/SectorMessage.cs b/QSB/TransformSync/SectorMessage.cs index 481ccaf8..60d4635b 100644 --- a/QSB/TransformSync/SectorMessage.cs +++ b/QSB/TransformSync/SectorMessage.cs @@ -5,8 +5,6 @@ namespace QSB.TransformSync { public class SectorMessage : QSBMessage { - public override MessageType MessageType => MessageType.Sector; - public int SectorId; public uint SenderId; diff --git a/QSB/TransformSync/SectorSync.cs b/QSB/TransformSync/SectorSync.cs index 74f52008..b4d7c529 100644 --- a/QSB/TransformSync/SectorSync.cs +++ b/QSB/TransformSync/SectorSync.cs @@ -24,7 +24,7 @@ namespace QSB.TransformSync private void Start() { - _sectorHandler = new MessageHandler(); + _sectorHandler = new MessageHandler(MessageType.Sector); _sectorHandler.OnClientReceiveMessage += OnClientReceiveMessage; _sectorHandler.OnServerReceiveMessage += OnServerReceiveMessage; }