From 651cffa872a214a9a15abdf782ae30bd3324534d Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sun, 27 Feb 2022 03:10:35 -0800 Subject: [PATCH] fix: Cleans up mobile status packets (#835) * Removes Span2D for mobile moving. Instead uses pure math and simplifies the calculation. * Cleans up the extended mobile status packet. --- Projects/Server/Mobiles/Mobile.cs | 23 +++---- Projects/Server/Network/PacketUtilities.cs | 7 +- .../Network/Packets/OutgoingMobilePackets.cs | 66 ++++++++++--------- 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/Projects/Server/Mobiles/Mobile.cs b/Projects/Server/Mobiles/Mobile.cs index 84e12e759..b348f1041 100644 --- a/Projects/Server/Mobiles/Mobile.cs +++ b/Projects/Server/Mobiles/Mobile.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; -using Microsoft.Toolkit.HighPerformance; using Server.Accounting; using Server.Buffers; using Server.ContextMenus; @@ -2961,13 +2960,12 @@ namespace Server ? OutgoingVirtualHairPackets.RemovePacketLength : OutgoingVirtualHairPackets.EquipUpdatePacketLength; - Span facialhairPacket = stackalloc byte[facialHairLength].InitializePacket(); + Span facialHairPacket = stackalloc byte[facialHairLength].InitializePacket(); const int cacheLength = OutgoingMobilePackets.MobileMovingPacketCacheByteLength; const int width = OutgoingMobilePackets.MobileMovingPacketLength; - const int height = OutgoingMobilePackets.MobileMovingPacketCacheHeight; - var mobileMovingCache = stackalloc byte[cacheLength].AsSpan2D(height, width).InitializePackets(); + var mobileMovingCache = stackalloc byte[cacheLength].InitializePackets(width); var ourState = m_NetState; @@ -3069,12 +3067,12 @@ namespace Server { if (removeFacialHair) { - OutgoingVirtualHairPackets.CreateRemoveHairPacket(facialhairPacket, facialHairSerial); + OutgoingVirtualHairPackets.CreateRemoveHairPacket(facialHairPacket, facialHairSerial); } else { OutgoingVirtualHairPackets.CreateHairEquipUpdatePacket( - facialhairPacket, + facialHairPacket, this, facialHairSerial, FacialHairItemID, @@ -3082,7 +3080,7 @@ namespace Server Layer.FacialHair ); } - ourState.Send(facialhairPacket); + ourState.Send(facialHairPacket); } if (sendOPLUpdate) @@ -3208,12 +3206,12 @@ namespace Server { if (removeFacialHair) { - OutgoingVirtualHairPackets.CreateRemoveHairPacket(facialhairPacket, facialHairSerial); + OutgoingVirtualHairPackets.CreateRemoveHairPacket(facialHairPacket, facialHairSerial); } else { OutgoingVirtualHairPackets.CreateHairEquipUpdatePacket( - facialhairPacket, + facialHairPacket, this, facialHairSerial, FacialHairItemID, @@ -3221,7 +3219,7 @@ namespace Server Layer.FacialHair ); } - state.Send(facialhairPacket); + state.Send(facialHairPacket); } SendOPLPacketTo(state); @@ -4472,10 +4470,9 @@ namespace Server eable.Free(); const int cacheLength = OutgoingMobilePackets.MobileMovingPacketCacheByteLength; - var width = OutgoingMobilePackets.MobileMovingPacketLength; - var height = OutgoingMobilePackets.MobileMovingPacketCacheHeight; + const int width = OutgoingMobilePackets.MobileMovingPacketLength; - var mobileMovingCache = stackalloc byte[cacheLength].AsSpan2D(height, width).InitializePackets(); + var mobileMovingCache = stackalloc byte[cacheLength].InitializePackets(width); foreach (var m in m_MoveClientList) { diff --git a/Projects/Server/Network/PacketUtilities.cs b/Projects/Server/Network/PacketUtilities.cs index f6fd7df14..320cda166 100644 --- a/Projects/Server/Network/PacketUtilities.cs +++ b/Projects/Server/Network/PacketUtilities.cs @@ -17,7 +17,6 @@ using System; using System.Buffers; using System.IO; using System.Runtime.CompilerServices; -using Microsoft.Toolkit.HighPerformance; namespace Server.Network { @@ -55,12 +54,12 @@ namespace Server.Network } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Span2D InitializePackets(this Span2D buffer) + public static Span InitializePackets(this Span buffer, int width) { #if NO_LOCAL_INIT - for (var i = 0; i < buffer.Height; i++) + for (var i = 0; i < buffer.Length; i += width) { - buffer.GetRowSpan(i)[0] = 0; + buffer[i] = 0; } #endif return buffer; diff --git a/Projects/Server/Network/Packets/OutgoingMobilePackets.cs b/Projects/Server/Network/Packets/OutgoingMobilePackets.cs index 751737fec..271af7d97 100644 --- a/Projects/Server/Network/Packets/OutgoingMobilePackets.cs +++ b/Projects/Server/Network/Packets/OutgoingMobilePackets.cs @@ -17,7 +17,6 @@ using System; using System.Buffers; using System.IO; using System.Runtime.CompilerServices; -using Microsoft.Toolkit.HighPerformance; namespace Server.Network; @@ -26,7 +25,7 @@ public static class OutgoingMobilePackets public const int BondedStatusPacketLength = 11; public const int DeathAnimationPacketLength = 13; public const int MobileMovingPacketLength = 17; - public const int MobileMovingPacketCacheHeight = 16; // 8 notoriety, 2 client versions + public const int MobileMovingPacketCacheHeight = 7 * 2; // 7 notoriety, 2 client versions public const int MobileMovingPacketCacheByteLength = MobileMovingPacketLength * MobileMovingPacketCacheHeight; public const int AttributeMaximum = 100; public const int MobileAttributePacketLength = 9; @@ -35,13 +34,16 @@ public static class OutgoingMobilePackets public const int NewMobileAnimationPacketLength = 10; public const int MobileHealthbarPacketLength = 12; public const int MobileStatusCompactLength = 43; - public const int MobileStatusMaxLength = 121; + public const int MobileStatusLength = 70; + public const int MobileStatusAOSLength = 88; + public const int MobileStatusMLLength = 91; + public const int MobileStatusHSLength = 121; - public static bool ExtendedStatus { get; set; } + public static bool ExtendedStatus { get; private set; } = true; - public static void Initialize() + public static void Configure() { - ExtendedStatus = ServerConfiguration.GetOrUpdateSetting("extendedStatus", false); + ExtendedStatus = ServerConfiguration.GetSetting("client.showExtendedStatus", true); } public static void CreateBondedStatus(Span buffer, Serial serial, bool bonded) @@ -144,12 +146,12 @@ public static class OutgoingMobilePackets } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void SendMobileMovingUsingCache(this NetState ns, Span2D cache, Mobile source, Mobile target) => + public static void SendMobileMovingUsingCache(this NetState ns, Span cache, Mobile source, Mobile target) => ns.SendMobileMovingUsingCache(cache, target, Notoriety.Compute(source, target)); - // Requires a buffer of 16 packets, 17bytes per packet (272 bytes). - // Requires cache to have the first byte of each packet zeroed. - public static void SendMobileMovingUsingCache(this NetState ns, Span2D cache, Mobile target, int noto) + // Requires a buffer of 14 packets, 17 bytes per packet (238 bytes). + // Requires cache to have the first byte of each packet initially zeroed. + public static void SendMobileMovingUsingCache(this NetState ns, Span cache, Mobile target, int noto) { if (ns.CannotSendPackets()) { @@ -157,8 +159,9 @@ public static class OutgoingMobilePackets } var stygianAbyss = ns.StygianAbyss; - var row = noto * 2 + (stygianAbyss ? 1 : 0); - var buffer = cache.GetRowSpan(row); + // Indexes 0-6 for pre-SA, and 7-13 for SA + var row = noto + (stygianAbyss ? 6 : -1); + var buffer = cache.Slice(row * MobileMovingPacketLength, MobileMovingPacketLength); CreateMobileMoving(buffer, target, noto, stygianAbyss); ns.Send(buffer); @@ -431,7 +434,7 @@ public static class OutgoingMobilePackets [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void CreateMobileStatusCompact(Span buffer, Mobile m, bool canBeRenamed) => - CreateMobileStatus(buffer, null, m, 0, canBeRenamed); + CreateMobileStatus(buffer, m, 0, canBeRenamed); public static void SendMobileStatusCompact(this NetState ns, Mobile m, bool canBeRenamed) { @@ -451,46 +454,50 @@ public static class OutgoingMobilePackets public static void SendMobileStatus(this NetState ns, Mobile beholder, Mobile beheld) { - if (ns == null || beheld == null) + if (ns.CannotSendPackets() || beheld == null) { return; } - Span buffer = stackalloc byte[MobileStatusMaxLength]; int version; + int length; if (beholder != beheld) { version = 0; + length = MobileStatusCompactLength; } - else if (Core.HS && ns.ExtendedStatus) + else if (ExtendedStatus && ns.ExtendedStatus) { version = 6; + length = MobileStatusHSLength; } else if (Core.ML && ns.SupportsExpansion(Expansion.ML)) { - /* - * For the ML era, the version value must be 5 if the original UO distribution - * is used and the client is not lower than version 5 - */ - version = ExtendedStatus ? 6 : 5; + version = 5; + length = MobileStatusMLLength; + } + else if (Core.AOS) + { + version = 4; + length = MobileStatusAOSLength; } else { - version = Core.AOS ? 4 : 3; + version = 3; + length = MobileStatusLength; } - var length = CreateMobileStatus(buffer, beholder, beheld, version, beheld.CanBeRenamedBy(beholder)); - ns.Send(buffer[..length]); + Span buffer = stackalloc byte[length]; + CreateMobileStatus(buffer, beheld, version, beheld.CanBeRenamedBy(beholder)); + ns.Send(buffer); } - public static int CreateMobileStatus( - Span buffer, Mobile beholder, Mobile beheld, int version, bool canBeRenamed - ) + public static void CreateMobileStatus(Span buffer, Mobile beheld, int version, bool canBeRenamed) { if (buffer[0] != 0) { - return buffer.Length; + return; } var name = beheld.Name ?? ""; @@ -507,7 +514,7 @@ public static class OutgoingMobilePackets if (version <= 0) { writer.WritePacketLength(); - return writer.Position; + return; } writer.Write(beheld.Female); @@ -563,7 +570,6 @@ public static class OutgoingMobilePackets } writer.WritePacketLength(); - return writer.Position; } public static void SendMobileUpdate(this NetState ns, Mobile m)