fix(core): Cleans up uninitialized packets (#397)
- [X] Encapsulates/abstracts buffer cache checking - [X] Changes mobile moving cache to use Span2D
This commit is contained in:
parent
0b9fb9c071
commit
8763be8a21
18 changed files with 417 additions and 529 deletions
|
|
@ -32,7 +32,7 @@ namespace Server.Network
|
|||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[SoundPacketLength];
|
||||
Span<byte> buffer = stackalloc byte[SoundPacketLength].InitializePacket();
|
||||
CreateSoundEffect(buffer, soundID, target);
|
||||
|
||||
ns.Send(buffer);
|
||||
|
|
@ -40,6 +40,11 @@ namespace Server.Network
|
|||
|
||||
public static void CreateSoundEffect(Span<byte> buffer, int soundID, IPoint3D target)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0x54); // Packet ID
|
||||
writer.Write((byte)1); // flags
|
||||
|
|
@ -57,6 +62,11 @@ namespace Server.Network
|
|||
int explodeEffect, int explodeSound, Serial serial, int layer, int unknown
|
||||
)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0xC7); // Packet ID
|
||||
writer.Write((byte)type);
|
||||
|
|
@ -168,6 +178,11 @@ namespace Server.Network
|
|||
int speed, int duration, bool fixedDirection, bool explode, int hue, int renderMode
|
||||
)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0xC0); // Packet ID
|
||||
writer.Write((byte)type);
|
||||
|
|
|
|||
|
|
@ -52,6 +52,11 @@ namespace Server.Network
|
|||
|
||||
public static void CreateRemoveEntity(Span<byte> buffer, Serial serial)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0x1D); // Packet ID
|
||||
writer.Write(serial);
|
||||
|
|
@ -64,7 +69,7 @@ namespace Server.Network
|
|||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[RemoveEntityLength];
|
||||
Span<byte> buffer = stackalloc byte[RemoveEntityLength].InitializePacket();
|
||||
CreateRemoveEntity(buffer, serial);
|
||||
|
||||
ns.Send(buffer);
|
||||
|
|
|
|||
|
|
@ -56,6 +56,11 @@ namespace Server.Network
|
|||
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name = "", string args = ""
|
||||
)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return buffer.Length;
|
||||
}
|
||||
|
||||
name ??= "";
|
||||
args ??= "";
|
||||
|
||||
|
|
@ -91,7 +96,7 @@ namespace Server.Network
|
|||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[GetMaxMessageLocalizedAffixLength(affix, args)];
|
||||
Span<byte> buffer = stackalloc byte[GetMaxMessageLocalizedAffixLength(affix, args)].InitializePacket();
|
||||
var length = CreateMessageLocalizedAffix(
|
||||
buffer, serial, graphic, type, hue, font, number, name, affixType, affix, args
|
||||
);
|
||||
|
|
@ -108,6 +113,11 @@ namespace Server.Network
|
|||
AffixType affixType, string affix = "", string args = ""
|
||||
)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return buffer.Length;
|
||||
}
|
||||
|
||||
name ??= "";
|
||||
affix ??= "";
|
||||
args ??= "";
|
||||
|
|
@ -146,7 +156,7 @@ namespace Server.Network
|
|||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[GetMaxMessageLength(text)];
|
||||
Span<byte> buffer = stackalloc byte[GetMaxMessageLength(text)].InitializePacket();
|
||||
var length = CreateMessage(
|
||||
buffer,
|
||||
serial,
|
||||
|
|
@ -179,6 +189,11 @@ namespace Server.Network
|
|||
string text
|
||||
)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return buffer.Length;
|
||||
}
|
||||
|
||||
name ??= "";
|
||||
text ??= "";
|
||||
lang ??= "ENU";
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ using System;
|
|||
using System.Buffers;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Toolkit.HighPerformance.Memory;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
|
|
@ -25,7 +26,8 @@ namespace Server.Network
|
|||
public const int BondedStatusPacketLength = 11;
|
||||
public const int DeathAnimationPacketLength = 13;
|
||||
public const int MobileMovingPacketLength = 17;
|
||||
public const int MobileMovingPacketCacheLength = MobileMovingPacketLength * 8 * 2; // 8 notoriety, 2 client versions
|
||||
public const int MobileMovingPacketCacheHeight = 16; // 8 notoriety, 2 client versions
|
||||
public const int MobileMovingPacketCacheByteLength = MobileMovingPacketLength * MobileMovingPacketCacheHeight;
|
||||
public const int AttributeMaximum = 100;
|
||||
public const int MobileAttributePacketLength = 9;
|
||||
public const int MobileAttributesPacketLength = 17;
|
||||
|
|
@ -87,6 +89,11 @@ namespace Server.Network
|
|||
|
||||
public static void CreateMobileMoving(Span<byte> buffer, Mobile m, int noto, bool stygianAbyss)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var loc = m.Location;
|
||||
var hue = m.SolidHueOverride >= 0 ? m.SolidHueOverride : m.Hue;
|
||||
|
||||
|
|
@ -114,18 +121,18 @@ namespace Server.Network
|
|||
return;
|
||||
}
|
||||
|
||||
Span<byte> span = stackalloc byte[MobileMovingPacketLength];
|
||||
CreateMobileMoving(span, target, noto, ns.StygianAbyss);
|
||||
ns.Send(span);
|
||||
Span<byte> buffer = stackalloc byte[MobileMovingPacketLength].InitializePacket();
|
||||
CreateMobileMoving(buffer, target, noto, ns.StygianAbyss);
|
||||
ns.Send(buffer);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void SendMobileMovingUsingCache(this NetState ns, Span<byte> cache, Mobile source, Mobile target) =>
|
||||
public static void SendMobileMovingUsingCache(this NetState ns, Span2D<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, Span<byte> cache, Mobile target, int noto)
|
||||
public static void SendMobileMovingUsingCache(this NetState ns, Span2D<byte> cache, Mobile target, int noto)
|
||||
{
|
||||
if (ns == null)
|
||||
{
|
||||
|
|
@ -133,14 +140,9 @@ namespace Server.Network
|
|||
}
|
||||
|
||||
var stygianAbyss = ns.StygianAbyss;
|
||||
var startIndex = (noto * 2 + (stygianAbyss ? 1 : 0)) * MobileMovingPacketLength;
|
||||
var buffer = cache.Slice(startIndex, MobileMovingPacketLength);
|
||||
|
||||
// Packet not created yet
|
||||
if (buffer[0] == 0)
|
||||
{
|
||||
CreateMobileMoving(buffer, target, noto, stygianAbyss);
|
||||
}
|
||||
var row = noto * 2 + (stygianAbyss ? 1 : 0);
|
||||
var buffer = cache.GetRowSpan(row);
|
||||
CreateMobileMoving(buffer, target, noto, stygianAbyss);
|
||||
|
||||
ns.Send(buffer);
|
||||
}
|
||||
|
|
@ -283,6 +285,11 @@ namespace Server.Network
|
|||
Serial mobile, int action, int frameCount, int repeatCount, bool forward, bool repeat, int delay
|
||||
)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0x6E); // Packet ID
|
||||
writer.Write(mobile);
|
||||
|
|
|
|||
|
|
@ -219,6 +219,11 @@ namespace Server.Network
|
|||
int itemID, int hue, int amount
|
||||
)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0x23); // Packet ID
|
||||
writer.Write((short)itemID);
|
||||
|
|
@ -247,9 +252,9 @@ namespace Server.Network
|
|||
return;
|
||||
}
|
||||
|
||||
Span<byte> span = stackalloc byte[DragEffectPacketLength];
|
||||
CreateDragEffect(span, srcSerial, srcLocation, trgSerial, trgLocation, itemID, hue, amount);
|
||||
ns.Send(span);
|
||||
Span<byte> buffer = stackalloc byte[DragEffectPacketLength].InitializePacket();
|
||||
CreateDragEffect(buffer, srcSerial, srcLocation, trgSerial, trgLocation, itemID, hue, amount);
|
||||
ns.Send(buffer);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue