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.
This commit is contained in:
Kamron Batman 2022-02-27 03:10:35 -08:00 • committed by GitHub
parent e873d2ed7e
commit 651cffa872
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 47 deletions

View file

@ -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<byte> facialhairPacket = stackalloc byte[facialHairLength].InitializePacket();
Span<byte> 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)
{

View file

@ -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<byte> InitializePackets(this Span2D<byte> buffer)
public static Span<byte> InitializePackets(this Span<byte> 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;

View file

@ -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<byte> buffer, Serial serial, bool bonded)
@ -144,12 +146,12 @@ public static class OutgoingMobilePackets
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendMobileMovingUsingCache(this NetState ns, Span2D<byte> cache, Mobile source, Mobile target) =>
public static void SendMobileMovingUsingCache(this NetState ns, Span<byte> 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<byte> 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<byte> 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<byte> 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<byte> 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<byte> buffer = stackalloc byte[length];
CreateMobileStatus(buffer, beheld, version, beheld.CanBeRenamedBy(beholder));
ns.Send(buffer);
}
public static int CreateMobileStatus(
Span<byte> buffer, Mobile beholder, Mobile beheld, int version, bool canBeRenamed
)
public static void CreateMobileStatus(Span<byte> 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)