diff --git a/QSB/Events/QSBEvent.cs b/QSB/Events/QSBEvent.cs index 3414e8ec..03f668fe 100644 --- a/QSB/Events/QSBEvent.cs +++ b/QSB/Events/QSBEvent.cs @@ -1,4 +1,4 @@ -using JetBrains.Annotations; +using JetBrains.Annotations; using OWML.Utils; using QSB.Messaging; using QSB.Player; @@ -90,9 +90,9 @@ namespace QSB.Events Message.Deserialize(reader); } - public override bool ShouldReceive => Event.Invoke("CheckMessage", Message); - public override void OnReceiveRemote() => Event.Invoke("OnReceiveRemote", QSBCore.IsHost, Message); - public override void OnReceiveLocal() => Event.Invoke("OnReceiveLocal", QSBCore.IsHost, Message); + protected override bool ShouldReceive => Event.Invoke("CheckMessage", Message); + protected override void OnReceiveRemote() => Event.Invoke("OnReceiveRemote", QSBCore.IsHost, Message); + protected override void OnReceiveLocal() => Event.Invoke("OnReceiveLocal", QSBCore.IsHost, Message); public override string ToString() => Event.GetType().Name; } diff --git a/QSB/Messaging/QSBMessage.cs b/QSB/Messaging/QSBMessage.cs index 1495e10f..86a7f73c 100644 --- a/QSB/Messaging/QSBMessage.cs +++ b/QSB/Messaging/QSBMessage.cs @@ -1,10 +1,10 @@ using System; -using QuantumUNET.Messages; +using QSB.Player; using QuantumUNET.Transport; namespace QSB.Messaging { - public abstract class QSBMessage : QMessageBase + public abstract class QSBMessage : QSBMessageRaw { /// set automatically when sending a message public uint From; @@ -28,12 +28,25 @@ namespace QSB.Messaging To = reader.ReadUInt32(); } - /// checked before calling either OnReceive - public virtual bool ShouldReceive => true; - public virtual void OnReceiveRemote() { } - public virtual void OnReceiveLocal() { } + public sealed override void OnReceive() + { + if (ShouldReceive) + { + if (From != QSBPlayerManager.LocalPlayerId) + { + OnReceiveRemote(); + } + else + { + OnReceiveLocal(); + } + } + } - public override string ToString() => GetType().Name; + /// checked before calling either OnReceive + protected virtual bool ShouldReceive => true; + protected virtual void OnReceiveRemote() { } + protected virtual void OnReceiveLocal() { } } diff --git a/QSB/Messaging/QSBMessageManager.cs b/QSB/Messaging/QSBMessageManager.cs index c55257ed..0d02ee0d 100644 --- a/QSB/Messaging/QSBMessageManager.cs +++ b/QSB/Messaging/QSBMessageManager.cs @@ -25,7 +25,7 @@ namespace QSB.Messaging static QSBMessageManager() { - var types = typeof(QSBMessage).GetDerivedTypes().ToArray(); + var types = typeof(QSBMessageRaw).GetDerivedTypes().ToArray(); for (var i = 0; i < types.Length; i++) { var msgType = (short)(QMsgType.Highest + 1 + i); @@ -53,75 +53,73 @@ namespace QSB.Messaging private static void OnServerReceive(QNetworkMessage netMsg) { var msgType = netMsg.MsgType; - var msg = (QSBMessage)Activator.CreateInstance(_msgTypeToType[msgType]); + var msg = (QSBMessageRaw)Activator.CreateInstance(_msgTypeToType[msgType]); netMsg.ReadMessage(msg); - if (msg.To == uint.MaxValue) + if (msg is QSBMessage m) { - QNetworkServer.SendToAll(msgType, msg); - } - else if (msg.To == 0) - { - OnReceive(msg); + if (m.To == uint.MaxValue) + { + QNetworkServer.SendToAll(msgType, m); + } + else if (m.To == 0) + { + OnReceive(m); + } + else + { + var conn = QNetworkServer.connections.FirstOrDefault(x => m.To == x.GetPlayerId()); + if (conn == null) + { + DebugLog.ToConsole($"SendTo unknown player! id: {m.To}, message: {m}", MessageType.Error); + return; + } + conn.Send(msgType, m); + } } else { - var conn = QNetworkServer.connections.FirstOrDefault(x => msg.To == x.GetPlayerId()); - if (conn == null) - { - DebugLog.ToConsole($"SendTo unknown player! id: {msg.To}, message: {msg}", MessageType.Error); - return; - } - conn.Send(msgType, msg); + QNetworkServer.SendToAll(msgType, msg); } } private static void OnClientReceive(QNetworkMessage netMsg) { var msgType = netMsg.MsgType; - var msg = (QSBMessage)Activator.CreateInstance(_msgTypeToType[msgType]); + var msg = (QSBMessageRaw)Activator.CreateInstance(_msgTypeToType[msgType]); netMsg.ReadMessage(msg); OnReceive(msg); } - private static void OnReceive(QSBMessage msg) + private static void OnReceive(QSBMessageRaw msg) { if (PlayerTransformSync.LocalInstance == null) { - DebugLog.ToConsole($"Warning - Tried to handle message {msg} before localplayer was established.", MessageType.Warning); + DebugLog.ToConsole($"Warning - Tried to handle message {msg} before local player was established.", MessageType.Warning); return; } - if (QSBPlayerManager.PlayerExists(msg.From)) + if (msg is QSBMessage m) { - var player = QSBPlayerManager.GetPlayer(msg.From); - - if (!player.IsReady - && player.PlayerId != QSBPlayerManager.LocalPlayerId - && player.State is ClientState.AliveInSolarSystem or ClientState.AliveInEye or ClientState.DeadInSolarSystem - && msg is not QSBEventRelay { Event: PlayerInformationEvent or PlayerReadyEvent or RequestStateResyncEvent or ServerStateEvent }) + if (QSBPlayerManager.PlayerExists(m.From)) { - DebugLog.ToConsole($"Warning - Got message {msg} from player {msg.From}, but they were not ready. Asking for state resync, just in case.", MessageType.Warning); - QSBEventManager.FireEvent(EventNames.QSBRequestStateResync); + var player = QSBPlayerManager.GetPlayer(m.From); + + if (!player.IsReady + && player.PlayerId != QSBPlayerManager.LocalPlayerId + && player.State is ClientState.AliveInSolarSystem or ClientState.AliveInEye or ClientState.DeadInSolarSystem + && m is not QSBEventRelay { Event: PlayerInformationEvent or PlayerReadyEvent or RequestStateResyncEvent or ServerStateEvent }) + { + DebugLog.ToConsole($"Warning - Got message {m} from player {m.From}, but they were not ready. Asking for state resync, just in case.", MessageType.Warning); + QSBEventManager.FireEvent(EventNames.QSBRequestStateResync); + } } } try { - if (!msg.ShouldReceive) - { - return; - } - - if (msg.From != QSBPlayerManager.LocalPlayerId) - { - msg.OnReceiveRemote(); - } - else - { - msg.OnReceiveLocal(); - } + msg.OnReceive(); } catch (Exception ex) { @@ -133,15 +131,19 @@ namespace QSB.Messaging public static void Send(this M msg) - where M : QSBMessage, new() + where M : QSBMessageRaw, new() { - if (!QSBCore.IsInMultiplayer) + if (PlayerTransformSync.LocalInstance == null) { - DebugLog.ToConsole($"Warning - Tried to send message {msg} while not connected to/hosting server.", MessageType.Warning); + DebugLog.ToConsole($"Warning - Tried to send message {msg} before local player was established.", MessageType.Warning); return; } - msg.From = QSBPlayerManager.LocalPlayerId; + if (msg is QSBMessage m) + { + m.From = QSBPlayerManager.LocalPlayerId; + } + var msgType = _typeToMsgType[typeof(M)]; QNetworkManager.singleton.client.Send(msgType, msg); } @@ -154,4 +156,13 @@ namespace QSB.Messaging Send(msg); } } + + /// + /// message that will be sent to every client + /// + public abstract class QSBMessageRaw : QMessageBase + { + public abstract void OnReceive(); + public override string ToString() => GetType().Name; + } } diff --git a/QSB/Messaging/QSBWorldObjectMessage.cs b/QSB/Messaging/QSBWorldObjectMessage.cs index a2bc9958..4dcf024f 100644 --- a/QSB/Messaging/QSBWorldObjectMessage.cs +++ b/QSB/Messaging/QSBWorldObjectMessage.cs @@ -21,7 +21,7 @@ namespace QSB.Messaging ObjectId = reader.ReadInt32(); } - public override bool ShouldReceive + protected override bool ShouldReceive { get {