From aa68d968078550f32d0ad6ce93aafa6566d15b13 Mon Sep 17 00:00:00 2001 From: Stefano Merotta <97297186+stefanomerotta@users.noreply.github.com> Date: Sat, 10 Aug 2024 19:20:34 +0200 Subject: [PATCH] fix: Removes redundant code in GumpSystem (#1921) --- Projects/UOContent/Gumps/Base/GumpSystem.cs | 44 +++++++++---------- .../UOContent/Gumps/Base/NetStateGumps.cs | 3 +- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/Projects/UOContent/Gumps/Base/GumpSystem.cs b/Projects/UOContent/Gumps/Base/GumpSystem.cs index 957f31852..c0243a08d 100644 --- a/Projects/UOContent/Gumps/Base/GumpSystem.cs +++ b/Projects/UOContent/Gumps/Base/GumpSystem.cs @@ -24,8 +24,6 @@ namespace Server.Gumps; public static partial class GumpSystem { - public const int GumpCap = 512; - private static readonly Dictionary> _gumps = []; public static unsafe void Configure() @@ -43,8 +41,6 @@ public static partial class GumpSystem } } - private static T Find(NetState ns) where T : BaseGump => ns != null ? Get(ns).Find() : null; - private static void Remove(NetState ns, BaseGump gump) { if (_gumps.TryGetValue(ns, out var gumps)) @@ -60,18 +56,6 @@ public static partial class GumpSystem } } - private static void Send(NetState ns, BaseGump gump, bool singleton) - { - if (ns.CannotSendPackets()) // Handles ns null check too - { - return; - } - - Get(ns).Send(gump, singleton); - } - - private static bool Close(NetState ns) where T : BaseGump => ns != null && Get(ns).Close(); - private static NetStateGumps Get(NetState ns) { if (ns == null) @@ -93,28 +77,40 @@ public static partial class GumpSystem public static bool HasGump([DisallowNull] this Mobile m) where T : BaseGump { ArgumentNullException.ThrowIfNull(m); - return Find(m.NetState) != null; + + var state = m.NetState; + return state != null && Get(state).Find() != null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T FindGump([DisallowNull] this Mobile m) where T : BaseGump { ArgumentNullException.ThrowIfNull(m); - return Find(m.NetState); + + var state = m.NetState; + return state != null ? Get(state).Find() : null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool CloseGump([DisallowNull] this Mobile m) where T : BaseGump { ArgumentNullException.ThrowIfNull(m); - return Close(m.NetState); + + var state = m.NetState; + return state != null && Get(state).Close(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void SendGump([DisallowNull] this Mobile m, BaseGump g, bool singleton = false) { ArgumentNullException.ThrowIfNull(m); - Send(m.NetState, g, singleton); + + var state = m.NetState; + + if (state != null) + { + Get(state).Send(g, singleton); + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -128,28 +124,28 @@ public static partial class GumpSystem public static bool HasGump([DisallowNull] this NetState ns) where T : BaseGump { ArgumentNullException.ThrowIfNull(ns); - return Find(ns) != null; + return Get(ns).Find() != null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T FindGump([DisallowNull] this NetState ns) where T : BaseGump { ArgumentNullException.ThrowIfNull(ns); - return Find(ns); + return Get(ns).Find(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool CloseGump([DisallowNull] this NetState ns) where T : BaseGump { ArgumentNullException.ThrowIfNull(ns); - return Close(ns); + return Get(ns).Close(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void SendGump([DisallowNull] this NetState ns, BaseGump g, bool singleton = false) { ArgumentNullException.ThrowIfNull(ns); - Send(ns, g, singleton); + Get(ns).Send(g, singleton); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Projects/UOContent/Gumps/Base/NetStateGumps.cs b/Projects/UOContent/Gumps/Base/NetStateGumps.cs index 61f47b3a0..ba21804d0 100644 --- a/Projects/UOContent/Gumps/Base/NetStateGumps.cs +++ b/Projects/UOContent/Gumps/Base/NetStateGumps.cs @@ -24,6 +24,7 @@ namespace Server.Gumps; public readonly ref struct NetStateGumps { + private const int GumpCap = 512; private static readonly ILogger _logger = LogFactory.GetLogger(typeof(NetStateGumps)); private readonly List _gumps; @@ -102,7 +103,7 @@ public readonly ref struct NetStateGumps } } - if (_gumps.Count >= GumpSystem.GumpCap) + if (_gumps.Count >= GumpCap) { _logger.Information("Exceeded gump cap, disconnecting..."); _state.Disconnect("Exceeded gump cap.");