From 958f07ed08c6246d9ef71761b16be6a44b697999 Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidoS@users.noreply.github.com> Date: Tue, 10 Sep 2024 20:22:14 -0500 Subject: [PATCH] Stickers: added subscription check on outbound mod network calls --- .../Stickers/Networking/ModNetwork.Main.cs | 29 +++++++++++++++++ .../Networking/ModNetwork.Outbound.cs | 24 ++++++++++++++ Stickers/Stickers/StickerSystem.Main.cs | 31 ++++++++++++++----- 3 files changed, 76 insertions(+), 8 deletions(-) diff --git a/Stickers/Stickers/Networking/ModNetwork.Main.cs b/Stickers/Stickers/Networking/ModNetwork.Main.cs index 64f21b9..d2cdeea 100644 --- a/Stickers/Stickers/Networking/ModNetwork.Main.cs +++ b/Stickers/Stickers/Networking/ModNetwork.Main.cs @@ -6,6 +6,25 @@ public static partial class ModNetwork { #region Mod Network Internals + // private static bool _isEnabled = true; + // + // public static bool IsEnabled + // { + // get => _isEnabled; + // set + // { + // if (_isEnabled == value) + // return; + // + // _isEnabled = value; + // + // if (_isEnabled) Subscribe(); + // else Unsubscribe(); + // + // Reset(); // reset buffers and metadata + // } + // } + public static bool IsSendingTexture { get; private set; } private static bool _isSubscribedToModNetwork; @@ -15,6 +34,16 @@ public static partial class ModNetwork _isSubscribedToModNetwork = ModNetworkManager.IsSubscribed(ModId); if (!_isSubscribedToModNetwork) StickerMod.Logger.Error("Failed to subscribe to Mod Network! This should not happen."); + else StickerMod.Logger.Msg("Subscribed to Mod Network."); + } + + private static void Unsubscribe() + { + ModNetworkManager.Unsubscribe(ModId); + + _isSubscribedToModNetwork = ModNetworkManager.IsSubscribed(ModId); + if (_isSubscribedToModNetwork) StickerMod.Logger.Error("Failed to unsubscribe from Mod Network! This should not happen."); + else StickerMod.Logger.Msg("Unsubscribed from Mod Network."); } #endregion Mod Network Internals diff --git a/Stickers/Stickers/Networking/ModNetwork.Outbound.cs b/Stickers/Stickers/Networking/ModNetwork.Outbound.cs index d3f20f3..241f566 100644 --- a/Stickers/Stickers/Networking/ModNetwork.Outbound.cs +++ b/Stickers/Stickers/Networking/ModNetwork.Outbound.cs @@ -38,6 +38,9 @@ public static partial class ModNetwork public static void SendPlaceSticker(int stickerSlot, Vector3 position, Vector3 forward, Vector3 up) { + if (!_isSubscribedToModNetwork) + return; + if (!IsConnectedToGameNetwork()) return; @@ -55,6 +58,9 @@ public static partial class ModNetwork public static void SendClearSticker(int stickerSlot) { + if (!_isSubscribedToModNetwork) + return; + if (!IsConnectedToGameNetwork()) return; @@ -68,6 +74,9 @@ public static partial class ModNetwork public static void SendClearAllStickers() { + if (!_isSubscribedToModNetwork) + return; + if (!IsConnectedToGameNetwork()) return; @@ -80,6 +89,9 @@ public static partial class ModNetwork private static void SendStartTexture(int stickerSlot, Guid textureHash, int chunkCount, int width, int height) { + if (!_isSubscribedToModNetwork) + return; + if (!IsConnectedToGameNetwork()) return; @@ -97,6 +109,9 @@ public static partial class ModNetwork public static void SendTextureChunk(int chunkIdx, byte[] chunkData) { + if (!_isSubscribedToModNetwork) + return; + if (!IsConnectedToGameNetwork()) return; @@ -111,6 +126,9 @@ public static partial class ModNetwork public static void SendEndTexture() { + if (!_isSubscribedToModNetwork) + return; + if (!IsConnectedToGameNetwork()) return; @@ -123,6 +141,9 @@ public static partial class ModNetwork public static void SendRequestTexture(int stickerSlot, Guid textureHash) { + if (!_isSubscribedToModNetwork) + return; + if (!IsConnectedToGameNetwork()) return; @@ -137,6 +158,9 @@ public static partial class ModNetwork public static void SendTexture(int stickerSlot) { + if (!_isSubscribedToModNetwork) + return; + if (!IsConnectedToGameNetwork() || IsSendingTexture) return; diff --git a/Stickers/Stickers/StickerSystem.Main.cs b/Stickers/Stickers/StickerSystem.Main.cs index 8bdc480..ab95877 100644 --- a/Stickers/Stickers/StickerSystem.Main.cs +++ b/Stickers/Stickers/StickerSystem.Main.cs @@ -4,7 +4,6 @@ using ABI_RC.Core.UI; using ABI_RC.Systems.GameEventSystem; using NAK.Stickers.Networking; using NAK.Stickers.Utilities; -using UnityEngine; namespace NAK.Stickers; @@ -64,6 +63,29 @@ public partial class StickerSystem #endregion Game Events #region Data + + // private bool _isEnabled = true; + // + // public bool IsEnabled + // { + // get => _isEnabled; + // set + // { + // if (_isEnabled == value) + // return; + // + // _isEnabled = value; + // if (!_isEnabled) ClearAllStickers(); + // ModNetwork.IsEnabled = _isEnabled; + // } + // } + + private string SelectedStickerName => ModSettings.Hidden_SelectedStickerNames.Value[_selectedStickerSlot]; + + private const float StickerKillTime = 30f; + private const float StickerCooldown = 0.2f; + private readonly Dictionary _playerStickers = new(); + internal const string PlayerLocalId = "_PLAYERLOCAL"; private int _selectedStickerSlot; public int SelectedStickerSlot @@ -97,13 +119,6 @@ public partial class StickerSystem } } } - - private string SelectedStickerName => ModSettings.Hidden_SelectedStickerNames.Value[_selectedStickerSlot]; - - private const float StickerKillTime = 30f; - private const float StickerCooldown = 0.2f; - private readonly Dictionary _playerStickers = new(); - internal const string PlayerLocalId = "_PLAYERLOCAL"; #endregion Data } \ No newline at end of file