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:
Kamron Batman 2021-01-09 14:10:29 -08:00 • committed by GitHub
parent 0b9fb9c071
commit 8763be8a21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 417 additions and 529 deletions

View file

@ -72,20 +72,14 @@ namespace Server
if (map != null) if (map != null)
{ {
Span<byte> buffer = stackalloc byte[OutgoingEffectPackets.SoundPacketLength]; Span<byte> buffer = stackalloc byte[OutgoingEffectPackets.SoundPacketLength].InitializePacket();
buffer.InitializePacket();
var eable = map.GetClientsInRange(new Point3D(p)); var eable = map.GetClientsInRange(new Point3D(p));
foreach (var state in eable) foreach (var state in eable)
{ {
state.Mobile.ProcessDelta(); state.Mobile.ProcessDelta();
OutgoingEffectPackets.CreateSoundEffect(buffer, soundID, p);
if (buffer[0] == 0)
{
OutgoingEffectPackets.CreateSoundEffect(buffer, soundID, p);
}
state.Send(buffer); state.Send(buffer);
} }
@ -104,16 +98,9 @@ namespace Server
e.ProcessDelta(); e.ProcessDelta();
Span<byte> preEffect = stackalloc byte[OutgoingEffectPackets.ParticleEffectLength]; Span<byte> preEffect = stackalloc byte[OutgoingEffectPackets.ParticleEffectLength].InitializePacket();
preEffect.InitializePacket(); Span<byte> boltEffect = stackalloc byte[OutgoingEffectPackets.BoltEffectLength].InitializePacket();
Span<byte> boltEffect = stackalloc byte[OutgoingEffectPackets.BoltEffectLength]; Span<byte> soundEffect = sound ? stackalloc byte[OutgoingEffectPackets.SoundPacketLength].InitializePacket() : null;
boltEffect.InitializePacket();
Span<byte> soundEffect = sound ? stackalloc byte[OutgoingEffectPackets.SoundPacketLength] : null;
if (sound)
{
soundEffect.InitializePacket();
}
var eable = map.GetClientsInRange(e.Location); var eable = map.GetClientsInRange(e.Location);
@ -123,31 +110,19 @@ namespace Server
{ {
if (SendParticlesTo(state)) if (SendParticlesTo(state))
{ {
if (preEffect[0] == 0) OutgoingEffectPackets.CreateTargetParticleEffect(
{ preEffect,
OutgoingEffectPackets.CreateTargetParticleEffect( e, 0, 10, 5, 0, 0, 5031, 3, 0
preEffect, );
e, 0, 10, 5, 0, 0, 5031, 3, 0
);
}
state.Send(preEffect); state.Send(preEffect);
} }
if (boltEffect[0] == 0) OutgoingEffectPackets.CreateBoltEffect(boltEffect, e, hue);
{
OutgoingEffectPackets.CreateBoltEffect(boltEffect, e, hue);
}
state.Send(boltEffect); state.Send(boltEffect);
if (sound) if (sound)
{ {
if (soundEffect[0] == 0) OutgoingEffectPackets.CreateSoundEffect(soundEffect, 0x29, e);
{
OutgoingEffectPackets.CreateSoundEffect(soundEffect, 0x29, e);
}
state.Send(soundEffect); state.Send(soundEffect);
} }
} }
@ -164,7 +139,7 @@ namespace Server
Point3D p, Map map, int itemID, int duration, int speed = 10, int hue = 0, int renderMode = 0 Point3D p, Map map, int itemID, int duration, int speed = 10, int hue = 0, int renderMode = 0
) )
{ {
Span<byte> effect = stackalloc byte[OutgoingEffectPackets.HuedEffectLength]; Span<byte> effect = stackalloc byte[OutgoingEffectPackets.HuedEffectLength].InitializePacket();
OutgoingEffectPackets.CreateLocationHuedEffect( OutgoingEffectPackets.CreateLocationHuedEffect(
effect, effect,
p, itemID, speed, duration, hue, renderMode p, itemID, speed, duration, hue, renderMode
@ -194,14 +169,9 @@ namespace Server
return; return;
} }
Span<byte> particles = stackalloc byte[OutgoingEffectPackets.ParticleEffectLength]; Span<byte> particles = stackalloc byte[OutgoingEffectPackets.ParticleEffectLength].InitializePacket();
particles.InitializePacket();
Span<byte> regular = itemID != 0 ? stackalloc byte[OutgoingEffectPackets.HuedEffectLength] : null; Span<byte> regular = itemID != 0 ? stackalloc byte[OutgoingEffectPackets.HuedEffectLength].InitializePacket() : null;
if (itemID != 0)
{
regular.InitializePacket();
}
var eable = map.GetClientsInRange(e.Location); var eable = map.GetClientsInRange(e.Location);
@ -211,26 +181,18 @@ namespace Server
if (SendParticlesTo(state)) if (SendParticlesTo(state))
{ {
if (particles[0] == 0) OutgoingEffectPackets.CreateLocationParticleEffect(
{ particles,
OutgoingEffectPackets.CreateLocationParticleEffect( e, itemID, speed, duration, hue, renderMode, effect, unknown
particles, );
e, itemID, speed, duration, hue, renderMode, effect, unknown
);
}
state.Send(particles); state.Send(particles);
} }
else if (itemID != 0) else if (itemID != 0)
{ {
if (regular[0] == 0) OutgoingEffectPackets.CreateLocationHuedEffect(
{ regular,
OutgoingEffectPackets.CreateLocationHuedEffect( e.Location, itemID, speed, duration, hue, renderMode
regular, );
e.Location, itemID, speed, duration, hue, renderMode
);
}
state.Send(regular); state.Send(regular);
} }
} }
@ -242,7 +204,7 @@ namespace Server
{ {
(target as Mobile)?.ProcessDelta(); (target as Mobile)?.ProcessDelta();
Span<byte> effect = stackalloc byte[OutgoingEffectPackets.HuedEffectLength]; Span<byte> effect = stackalloc byte[OutgoingEffectPackets.HuedEffectLength].InitializePacket();
OutgoingEffectPackets.CreateTargetHuedEffect( OutgoingEffectPackets.CreateTargetHuedEffect(
effect, effect,
target, itemID, speed, duration, hue, renderMode target, itemID, speed, duration, hue, renderMode
@ -273,14 +235,8 @@ namespace Server
return; return;
} }
Span<byte> particles = stackalloc byte[OutgoingEffectPackets.ParticleEffectLength]; Span<byte> particles = stackalloc byte[OutgoingEffectPackets.ParticleEffectLength].InitializePacket();
particles.InitializePacket(); Span<byte> regular = itemID != 0 ? stackalloc byte[OutgoingEffectPackets.HuedEffectLength].InitializePacket() : null;
Span<byte> regular = itemID != 0 ? stackalloc byte[OutgoingEffectPackets.HuedEffectLength] : null;
if (itemID != 0)
{
regular.InitializePacket();
}
var eable = map.GetClientsInRange(target.Location); var eable = map.GetClientsInRange(target.Location);
@ -290,23 +246,15 @@ namespace Server
if (SendParticlesTo(state)) if (SendParticlesTo(state))
{ {
if (particles[0] == 0) OutgoingEffectPackets.CreateTargetParticleEffect(
{ particles,
OutgoingEffectPackets.CreateTargetParticleEffect( target, itemID, speed, duration, hue, renderMode, effect, (int)layer, unknown
particles, );
target, itemID, speed, duration, hue, renderMode, effect, (int)layer, unknown
);
}
state.Send(particles); state.Send(particles);
} }
else if (itemID != 0) else if (itemID != 0)
{ {
if (regular[0] == 0) OutgoingEffectPackets.CreateTargetHuedEffect(regular, target, itemID, speed, duration, hue, renderMode);
{
OutgoingEffectPackets.CreateTargetHuedEffect(regular, target, itemID, speed, duration, hue, renderMode);
}
state.Send(regular); state.Send(regular);
} }
} }
@ -503,14 +451,8 @@ namespace Server
return; return;
} }
Span<byte> particles = stackalloc byte[OutgoingEffectPackets.ParticleEffectLength]; Span<byte> particles = stackalloc byte[OutgoingEffectPackets.ParticleEffectLength].InitializePacket();
particles.InitializePacket(); Span<byte> regular = itemID != 0 ? stackalloc byte[OutgoingEffectPackets.HuedEffectLength].InitializePacket() : null;
Span<byte> regular = itemID != 0 ? stackalloc byte[OutgoingEffectPackets.HuedEffectLength] : null;
if (itemID != 0)
{
regular.InitializePacket();
}
var eable = map.GetClientsInRange(from.Location); var eable = map.GetClientsInRange(from.Location);
@ -520,27 +462,19 @@ namespace Server
if (SendParticlesTo(state)) if (SendParticlesTo(state))
{ {
if (particles[0] == 0) OutgoingEffectPackets.CreateMovingParticleEffect(
{ particles,
OutgoingEffectPackets.CreateMovingParticleEffect( from, to, itemID, speed, duration, fixedDirection, explodes, hue, renderMode, effect,
particles, explodeEffect, explodeSound, layer, unknown
from, to, itemID, speed, duration, fixedDirection, explodes, hue, renderMode, effect, );
explodeEffect, explodeSound, layer, unknown
);
}
state.Send(particles); state.Send(particles);
} }
else if (itemID > 1) else if (itemID > 1)
{ {
if (regular[0] == 0) OutgoingEffectPackets.CreateMovingHuedEffect(
{ regular,
OutgoingEffectPackets.CreateMovingHuedEffect( from, to, itemID, speed, duration, fixedDirection, explodes, hue, renderMode
regular, );
from, to, itemID, speed, duration, fixedDirection, explodes, hue, renderMode
);
}
state.Send(regular); state.Send(regular);
} }
} }

View file

@ -369,8 +369,7 @@ namespace Server
var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange()); var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange());
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength]; Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
removeEntity.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
@ -378,11 +377,7 @@ namespace Server
if (!m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m))) if (!m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m)))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
state.Send(removeEntity); state.Send(removeEntity);
} }
} }
@ -1140,20 +1135,10 @@ namespace Server
if (m_Map != null) if (m_Map != null)
{ {
Span<byte> oldWorldItem = stackalloc byte[OutgoingItemPackets.MaxWorldItemPacketLength]; Span<byte> oldWorldItem = stackalloc byte[OutgoingItemPackets.MaxWorldItemPacketLength].InitializePacket();
oldWorldItem.InitializePacket(); Span<byte> saWorldItem = stackalloc byte[OutgoingItemPackets.MaxWorldItemPacketLength].InitializePacket();
Span<byte> hsWorldItem = stackalloc byte[OutgoingItemPackets.MaxWorldItemPacketLength].InitializePacket();
Span<byte> saWorldItem = stackalloc byte[OutgoingItemPackets.MaxWorldItemPacketLength]; Span<byte> opl = ObjectPropertyList.Enabled ? stackalloc byte[OutgoingEntityPackets.OPLPacketLength].InitializePacket() : null;
saWorldItem.InitializePacket();
Span<byte> hsWorldItem = stackalloc byte[OutgoingItemPackets.MaxWorldItemPacketLength];
hsWorldItem.InitializePacket();
Span<byte> opl = ObjectPropertyList.Enabled ? stackalloc byte[OutgoingEntityPackets.OPLPacketLength] : null;
if (opl != null)
{
opl.InitializePacket();
}
var eable = m_Map.GetClientsInRange(m_Location, GetMaxUpdateRange()); var eable = m_Map.GetClientsInRange(m_Location, GetMaxUpdateRange());
@ -1214,8 +1199,7 @@ namespace Server
{ {
eable = m_Map.GetClientsInRange(oldLocation, GetMaxUpdateRange()); eable = m_Map.GetClientsInRange(oldLocation, GetMaxUpdateRange());
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength]; Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
removeEntity.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
@ -1223,10 +1207,7 @@ namespace Server
if (!m.InRange(location, GetUpdateRange(m))) if (!m.InRange(location, GetUpdateRange(m)))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
state.Send(removeEntity); state.Send(removeEntity);
} }
} }
@ -1559,8 +1540,7 @@ namespace Server
{ {
eable = m_Map.GetClientsInRange(oldLocation, GetMaxUpdateRange()); eable = m_Map.GetClientsInRange(oldLocation, GetMaxUpdateRange());
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength]; Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
removeEntity.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
@ -1568,11 +1548,7 @@ namespace Server
if (!m.InRange(value, GetUpdateRange(m))) if (!m.InRange(value, GetUpdateRange(m)))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
state.Send(removeEntity); state.Send(removeEntity);
} }
} }
@ -3357,8 +3333,7 @@ namespace Server
var worldLoc = GetWorldLocation(); var worldLoc = GetWorldLocation();
var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange()); var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange());
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
buffer.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
@ -3366,12 +3341,12 @@ namespace Server
if (m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m))) if (m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m)))
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
{ buffer, Serial, m_ItemID, type, hue, 3, ascii, "ENU", Name, text
var length = OutgoingMessagePackets.CreateMessage( );
buffer, Serial, m_ItemID, type, hue, 3, ascii, "ENU", Name, text
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }
@ -3392,8 +3367,7 @@ namespace Server
var worldLoc = GetWorldLocation(); var worldLoc = GetWorldLocation();
var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange()); var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange());
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)].InitializePacket();
buffer.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
@ -3401,12 +3375,12 @@ namespace Server
if (m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m))) if (m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m)))
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessageLocalized(
{ buffer, Serial, m_ItemID, type, hue, 3, number, Name, args
var length = OutgoingMessagePackets.CreateMessageLocalized( );
buffer, Serial, m_ItemID, type, hue, 3, number, Name, args
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }
@ -3859,8 +3833,7 @@ namespace Server
var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange()); var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange());
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength]; Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
removeEntity.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
@ -3868,11 +3841,7 @@ namespace Server
if (m.InRange(worldLoc, GetUpdateRange(m))) if (m.InRange(worldLoc, GetUpdateRange(m)))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
state.Send(removeEntity); state.Send(removeEntity);
} }
} }

View file

@ -5,6 +5,8 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Microsoft.Toolkit.HighPerformance.Extensions;
using Microsoft.Toolkit.HighPerformance.Memory;
using Server.Accounting; using Server.Accounting;
using Server.Buffers; using Server.Buffers;
using Server.ContextMenus; using Server.ContextMenus;
@ -3019,8 +3021,11 @@ namespace Server
sendFacialHair = true; sendFacialHair = true;
} }
Span<byte> mobileMovingPackets = stackalloc byte[OutgoingMobilePackets.MobileMovingPacketCacheLength]; const int cacheLength = OutgoingMobilePackets.MobileMovingPacketCacheByteLength;
mobileMovingPackets.InitializePackets(OutgoingMobilePackets.MobileMovingPacketLength); var width = OutgoingMobilePackets.MobileMovingPacketLength;
var height = OutgoingMobilePackets.MobileMovingPacketCacheHeight;
var mobileMovingCache = stackalloc byte[cacheLength].AsSpan2D(height, width).InitializePackets();
var ourState = m_NetState; var ourState = m_NetState;
@ -3039,7 +3044,7 @@ namespace Server
if (sendMoving || !ourState.StygianAbyss && (sendHealthbarPoison || sendHealthbarYellow)) if (sendMoving || !ourState.StygianAbyss && (sendHealthbarPoison || sendHealthbarYellow))
{ {
ourState.SendMobileMovingUsingCache(mobileMovingPackets, this, this); ourState.SendMobileMovingUsingCache(mobileMovingCache, this, this);
} }
if (ourState.StygianAbyss) if (ourState.StygianAbyss)
@ -3146,40 +3151,25 @@ namespace Server
var eable = Map.GetClientsInRange(m_Location); var eable = Map.GetClientsInRange(m_Location);
Span<byte> statBufferTrue = stackalloc byte[OutgoingMobilePackets.MobileStatusCompactLength]; Span<byte> statBufferTrue = stackalloc byte[OutgoingMobilePackets.MobileStatusCompactLength].InitializePacket();
statBufferTrue.InitializePacket(); Span<byte> statBufferFalse = stackalloc byte[OutgoingMobilePackets.MobileStatusCompactLength].InitializePacket();
Span<byte> hbpBuffer = stackalloc byte[OutgoingMobilePackets.MobileHealthbarPacketLength].InitializePacket();
Span<byte> statBufferFalse = stackalloc byte[OutgoingMobilePackets.MobileStatusCompactLength]; Span<byte> hbyBuffer = stackalloc byte[OutgoingMobilePackets.MobileHealthbarPacketLength].InitializePacket();
statBufferFalse.InitializePacket(); Span<byte> deadBuffer = stackalloc byte[OutgoingMobilePackets.BondedStatusPacketLength].InitializePacket();
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
Span<byte> hbpBuffer = stackalloc byte[OutgoingMobilePackets.MobileHealthbarPacketLength]; Span<byte> hitsPacket = stackalloc byte[OutgoingMobilePackets.MobileAttributePacketLength].InitializePacket();
hbpBuffer.InitializePacket();
Span<byte> hbyBuffer = stackalloc byte[OutgoingMobilePackets.MobileHealthbarPacketLength];
hbyBuffer.InitializePacket();
Span<byte> deadBuffer = stackalloc byte[OutgoingMobilePackets.BondedStatusPacketLength];
deadBuffer.InitializePacket();
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength];
removeEntity.InitializePacket();
Span<byte> hitsPacket = stackalloc byte[OutgoingMobilePackets.MobileAttributePacketLength];
hitsPacket.InitializePacket();
var hairLength = removeHair var hairLength = removeHair
? OutgoingVirtualHairPackets.RemovePacketLength ? OutgoingVirtualHairPackets.RemovePacketLength
: OutgoingVirtualHairPackets.EquipUpdatePacketLength; : OutgoingVirtualHairPackets.EquipUpdatePacketLength;
Span<byte> hairPacket = stackalloc byte[hairLength]; Span<byte> hairPacket = stackalloc byte[hairLength].InitializePacket();
hairPacket.InitializePacket();
var facialHairLength = removeFacialHair var facialHairLength = removeFacialHair
? OutgoingVirtualHairPackets.RemovePacketLength ? OutgoingVirtualHairPackets.RemovePacketLength
: OutgoingVirtualHairPackets.EquipUpdatePacketLength; : OutgoingVirtualHairPackets.EquipUpdatePacketLength;
Span<byte> facialhairPacket = stackalloc byte[facialHairLength]; Span<byte> facialhairPacket = stackalloc byte[facialHairLength].InitializePacket();
facialhairPacket.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
@ -3192,11 +3182,7 @@ namespace Server
if (sendRemove) if (sendRemove)
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
state.Send(removeEntity); state.Send(removeEntity);
} }
@ -3217,7 +3203,7 @@ namespace Server
if (sendMoving || !state.StygianAbyss && (sendHealthbarPoison || sendHealthbarYellow)) if (sendMoving || !state.StygianAbyss && (sendHealthbarPoison || sendHealthbarYellow))
{ {
state.SendMobileMovingUsingCache(mobileMovingPackets, beholder, this); state.SendMobileMovingUsingCache(mobileMovingCache, beholder, this);
} }
if (state.StygianAbyss) if (state.StygianAbyss)
@ -4574,8 +4560,11 @@ namespace Server
eable.Free(); eable.Free();
Span<byte> mobileMovingPackets = stackalloc byte[OutgoingMobilePackets.MobileMovingPacketCacheLength]; const int cacheLength = OutgoingMobilePackets.MobileMovingPacketCacheByteLength;
mobileMovingPackets.InitializePackets(OutgoingMobilePackets.MobileMovingPacketLength); var width = OutgoingMobilePackets.MobileMovingPacketLength;
var height = OutgoingMobilePackets.MobileMovingPacketCacheHeight;
var mobileMovingCache = stackalloc byte[cacheLength].AsSpan2D(height, width).InitializePackets();
foreach (var m in m_MoveClientList) foreach (var m in m_MoveClientList)
{ {
@ -4583,7 +4572,7 @@ namespace Server
if (ns != null && Utility.InUpdateRange(m_Location, m.m_Location) && m.CanSee(this)) if (ns != null && Utility.InUpdateRange(m_Location, m.m_Location) && m.CanSee(this))
{ {
ns.SendMobileMovingUsingCache(mobileMovingPackets, m, this); ns.SendMobileMovingUsingCache(mobileMovingCache, m, this);
} }
} }
@ -5029,11 +5018,8 @@ namespace Server
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
var corpseSerial = c?.Serial ?? Serial.Zero; var corpseSerial = c?.Serial ?? Serial.Zero;
Span<byte> deathAnimation = stackalloc byte[OutgoingMobilePackets.DeathAnimationPacketLength]; Span<byte> deathAnimation = stackalloc byte[OutgoingMobilePackets.DeathAnimationPacketLength].InitializePacket();
deathAnimation.InitializePacket(); Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength];
removeEntity.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
@ -5048,11 +5034,7 @@ namespace Server
if (!state.Mobile.CanSee(this)) if (!state.Mobile.CanSee(this))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
state.Send(removeEntity); state.Send(removeEntity);
} }
} }
@ -5326,27 +5308,23 @@ namespace Server
var eable = map.GetClientsInRange(from.Location); var eable = map.GetClientsInRange(from.Location);
var rootItem = root as Item; var rootItem = root as Item;
Span<byte> buffer = stackalloc byte[OutgoingPlayerPackets.DragEffectPacketLength]; Span<byte> buffer = stackalloc byte[OutgoingPlayerPackets.DragEffectPacketLength].InitializePacket();
buffer.InitializePacket();
foreach (var ns in eable) foreach (var ns in eable)
{ {
if (ns.Mobile != from && ns.Mobile.CanSee(from) && ns.Mobile.InLOS(from) && if (ns.Mobile != from && ns.Mobile.CanSee(from) && ns.Mobile.InLOS(from) &&
ns.Mobile.CanSee(root)) ns.Mobile.CanSee(root))
{ {
if (buffer[0] == 0) OutgoingPlayerPackets.CreateDragEffect(
{ buffer,
OutgoingPlayerPackets.CreateDragEffect( rootItem?.Serial ?? Serial.Zero,
buffer, rootItem?.Location ?? item.Location,
rootItem?.Serial ?? Serial.Zero, from.Serial,
rootItem?.Location ?? item.Location, from.Location,
from.Serial, item.ItemID,
from.Location, item.Hue,
item.ItemID, amount
item.Hue, );
amount
);
}
ns.Send(buffer); ns.Send(buffer);
} }
@ -5485,34 +5463,28 @@ namespace Server
var eable = map.GetClientsInRange(m_Location); var eable = map.GetClientsInRange(m_Location);
Span<byte> buffer = stackalloc byte[OutgoingPlayerPackets.DragEffectPacketLength]; Span<byte> buffer = stackalloc byte[OutgoingPlayerPackets.DragEffectPacketLength].InitializePacket();
buffer.InitializePacket();
foreach (var ns in eable) foreach (var ns in eable)
{ {
if (ns.StygianAbyss) if (ns.StygianAbyss || ns.Mobile == this ||
!ns.Mobile.CanSee(this) || !ns.Mobile.InLOS(this) || !ns.Mobile.CanSee(root))
{ {
continue; continue;
} }
if (ns.Mobile != this && ns.Mobile.CanSee(this) && ns.Mobile.InLOS(this) && ns.Mobile.CanSee(root)) OutgoingPlayerPackets.CreateDragEffect(
{ buffer,
if (buffer[0] == 0) Serial,
{ Location,
OutgoingPlayerPackets.CreateDragEffect( rootItem?.Serial ?? Serial.Zero,
buffer, rootItem?.Location ?? item.Location,
Serial, item.ItemID,
Location, item.Hue,
rootItem?.Serial ?? Serial.Zero, item.Amount
rootItem?.Location ?? item.Location, );
item.ItemID,
item.Hue,
item.Amount
);
}
ns.Send(buffer); ns.Send(buffer);
}
} }
eable.Free(); eable.Free();
@ -5844,10 +5816,8 @@ namespace Server
ProcessDelta(); ProcessDelta();
Span<byte> regBuffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)]; Span<byte> regBuffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
regBuffer.InitializePacket(); Span<byte> mutBuffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(mutatedText)].InitializePacket();
Span<byte> mutBuffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(mutatedText)];
mutBuffer.InitializePacket();
// TODO: Should this be sorted like onSpeech is below? // TODO: Should this be sorted like onSpeech is below?
for (var i = 0; i < hears.Count; ++i) for (var i = 0; i < hears.Count; ++i)
@ -5856,11 +5826,12 @@ namespace Server
if (mutatedArgs == null || !CheckHearsMutatedSpeech(heard, mutateContext)) if (mutatedArgs == null || !CheckHearsMutatedSpeech(heard, mutateContext))
{ {
if (regBuffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
regBuffer, Serial, Body, type, hue, 3, false, m_Language, Name, text
);
if (length != regBuffer.Length)
{ {
var length = OutgoingMessagePackets.CreateMessage(
regBuffer, Serial, Body, type, hue, 3, false, m_Language, Name, text
);
regBuffer = regBuffer.Slice(0, length); // Adjust to the actual size regBuffer = regBuffer.Slice(0, length); // Adjust to the actual size
} }
@ -5869,11 +5840,12 @@ namespace Server
} }
else else
{ {
if (mutBuffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
mutBuffer, Serial, Body, type, hue, 3, false, m_Language, Name, mutatedText
);
if (length != mutBuffer.Length)
{ {
var length = OutgoingMessagePackets.CreateMessage(
mutBuffer, Serial, Body, type, hue, 3, false, m_Language, Name, mutatedText
);
mutBuffer = mutBuffer.Slice(0, length); // Adjust to the actual size mutBuffer = mutBuffer.Slice(0, length); // Adjust to the actual size
} }
@ -6917,60 +6889,58 @@ namespace Server
var eable = map.GetClientsInRange(m_Location); var eable = map.GetClientsInRange(m_Location);
Span<byte> buffer = stackalloc byte[OutgoingMobilePackets.MobileAnimationPacketLength]; Span<byte> buffer = stackalloc byte[OutgoingMobilePackets.MobileAnimationPacketLength].InitializePacket();
buffer.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
if (state.Mobile.CanSee(this)) if (!state.Mobile.CanSee(this))
{ {
state.Mobile.ProcessDelta(); continue;
if (Body.IsGargoyle)
{
frameCount = 10;
if (Flying)
{
action = action switch
{
>= 9 and <= 11 => 71,
>= 12 and <= 14 => 72,
20 => 77,
31 => 71,
34 => 78,
>= 200 and <= 259 => 75,
>= 260 and <= 270 => 75,
_ => action
};
}
else
{
action = action switch
{
>= 200 and <= 259 => 17,
>= 260 and <= 270 => 16,
_ => action
};
}
}
if (buffer[0] == 0)
{
OutgoingMobilePackets.CreateMobileAnimation(
buffer,
Serial,
action,
frameCount,
repeatCount,
forward,
repeat,
delay
);
}
state.Send(buffer);
} }
state.Mobile.ProcessDelta();
if (Body.IsGargoyle)
{
frameCount = 10;
if (Flying)
{
action = action switch
{
>= 9 and <= 11 => 71,
>= 12 and <= 14 => 72,
20 => 77,
31 => 71,
34 => 78,
>= 200 and <= 259 => 75,
>= 260 and <= 270 => 75,
_ => action
};
}
else
{
action = action switch
{
>= 200 and <= 259 => 17,
>= 260 and <= 270 => 16,
_ => action
};
}
}
OutgoingMobilePackets.CreateMobileAnimation(
buffer,
Serial,
action,
frameCount,
repeatCount,
forward,
repeat,
delay
);
state.Send(buffer);
} }
eable.Free(); eable.Free();
@ -6998,8 +6968,7 @@ namespace Server
return; return;
} }
Span<byte> buffer = stackalloc byte[OutgoingEffectPackets.SoundPacketLength]; Span<byte> buffer = stackalloc byte[OutgoingEffectPackets.SoundPacketLength].InitializePacket();
buffer.InitializePacket();
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
@ -7007,11 +6976,7 @@ namespace Server
{ {
if (state.Mobile.CanSee(this)) if (state.Mobile.CanSee(this))
{ {
if (buffer[0] == 0) OutgoingEffectPackets.CreateSoundEffect(buffer, soundID, this);
{
OutgoingEffectPackets.CreateSoundEffect(buffer, soundID, this);
}
state.Send(buffer); state.Send(buffer);
} }
} }
@ -7066,18 +7031,13 @@ namespace Server
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength]; Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
removeEntity.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
if (state != m_NetState && (everyone || !state.Mobile.CanSee(this))) if (state != m_NetState && (everyone || !state.Mobile.CanSee(this)))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
state.Send(removeEntity); state.Send(removeEntity);
} }
} }
@ -7320,19 +7280,14 @@ namespace Server
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength]; Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
removeEntity.InitializePacket();
foreach (var state in eable) foreach (var state in eable)
{ {
var m = state.Mobile; var m = state.Mobile;
if (!m.CanSee(this)) if (!m.CanSee(this))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
state.Send(removeEntity); state.Send(removeEntity);
} }
else else
@ -7556,18 +7511,13 @@ namespace Server
var eable = map.GetClientsInRange(oldLocation); var eable = map.GetClientsInRange(oldLocation);
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength]; Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
removeEntity.InitializePacket();
foreach (var ns in eable) foreach (var ns in eable)
{ {
if (ns != m_NetState && !Utility.InUpdateRange(newLocation, ns.Mobile.Location)) if (ns != m_NetState && !Utility.InUpdateRange(newLocation, ns.Mobile.Location))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, Serial);
}
ns.Send(removeEntity); ns.Send(removeEntity);
} }
} }
@ -9234,8 +9184,7 @@ namespace Server
return; return;
} }
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
buffer.InitializePacket();
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
@ -9243,12 +9192,12 @@ namespace Server
{ {
if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this))) if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this)))
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
{ buffer, Serial, Body, type, hue, 3, ascii, Language, Name, text
var length = OutgoingMessagePackets.CreateMessage( );
buffer, Serial, Body, type, hue, 3, ascii, Language, Name, text
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }
@ -9266,8 +9215,7 @@ namespace Server
return; return;
} }
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)].InitializePacket();
buffer.InitializePacket();
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
@ -9275,11 +9223,12 @@ namespace Server
{ {
if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this))) if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this)))
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessageLocalized(
buffer, Serial, Body, type, hue, 3, number, Name, args
);
if (length != buffer.Length)
{ {
var length = OutgoingMessagePackets.CreateMessageLocalized(
buffer, Serial, Body, type, hue, 3, number, Name, args
);
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }
@ -9300,8 +9249,7 @@ namespace Server
return; return;
} }
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedAffixLength(affix, args)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedAffixLength(affix, args)].InitializePacket();
buffer.InitializePacket();
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
@ -9309,11 +9257,12 @@ namespace Server
{ {
if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this))) if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this)))
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessageLocalizedAffix(
buffer, Serial, Body, type, hue, 3, number, Name, affixType, affix, args
);
if (length != buffer.Length)
{ {
var length = OutgoingMessagePackets.CreateMessageLocalizedAffix(
buffer, Serial, Body, type, hue, 3, number, Name, affixType, affix, args
);
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }
@ -9348,8 +9297,7 @@ namespace Server
return; return;
} }
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)].InitializePacket();
buffer.InitializePacket();
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
@ -9357,11 +9305,12 @@ namespace Server
{ {
if (state != m_NetState && state.Mobile.CanSee(this)) if (state != m_NetState && state.Mobile.CanSee(this))
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessageLocalized(
buffer, Serial, Body, type, hue, 3, number, Name, args
);
if (length != buffer.Length)
{ {
var length = OutgoingMessagePackets.CreateMessageLocalized(
buffer, Serial, Body, type, hue, 3, number, Name, args
);
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }
@ -9379,8 +9328,7 @@ namespace Server
return; return;
} }
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
buffer.InitializePacket();
var eable = m_Map.GetClientsInRange(m_Location); var eable = m_Map.GetClientsInRange(m_Location);
@ -9388,12 +9336,12 @@ namespace Server
{ {
if (state != m_NetState && state.Mobile.CanSee(this)) if (state != m_NetState && state.Mobile.CanSee(this))
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
{ buffer, Serial, Body, type, hue, 3, ascii, Language, Name, text
var length = OutgoingMessagePackets.CreateMessage( );
buffer, Serial, Body, type, hue, 3, ascii, Language, Name, text
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }

View file

@ -17,6 +17,7 @@ using System;
using System.Buffers; using System.Buffers;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Memory;
namespace Server.Network namespace Server.Network
{ {
@ -42,25 +43,27 @@ namespace Server.Network
// If LOCAL INIT is off, then stack/heap allocations have garbage data // If LOCAL INIT is off, then stack/heap allocations have garbage data
// Initializes the first byte (Packet ID) so it can be used as a flag. // Initializes the first byte (Packet ID) so it can be used as a flag.
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InitializePacket(this Span<byte> buffer) public static Span<byte> InitializePacket(this Span<byte> buffer)
{ {
#if NO_LOCAL_INIT #if NO_LOCAL_INIT
buffer[0] = 0; if (buffer != null)
{
buffer[0] = 0;
}
#endif #endif
return buffer;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InitializePackets(this Span<byte> buffer, int chunkLength) public static Span2D<byte> InitializePackets(this Span2D<byte> buffer)
{ {
#if NO_LOCAL_INIT #if NO_LOCAL_INIT
var index = 0; for (var i = 0; i < buffer.Height; i++)
while (index < buffer.Length)
{ {
buffer[index] = 0; buffer.GetRowSpan(i)[0] = 0;
index += chunkLength;
} }
#endif #endif
return buffer;
} }
} }
} }

View file

@ -32,7 +32,7 @@ namespace Server.Network
return; return;
} }
Span<byte> buffer = stackalloc byte[SoundPacketLength]; Span<byte> buffer = stackalloc byte[SoundPacketLength].InitializePacket();
CreateSoundEffect(buffer, soundID, target); CreateSoundEffect(buffer, soundID, target);
ns.Send(buffer); ns.Send(buffer);
@ -40,6 +40,11 @@ namespace Server.Network
public static void CreateSoundEffect(Span<byte> buffer, int soundID, IPoint3D target) public static void CreateSoundEffect(Span<byte> buffer, int soundID, IPoint3D target)
{ {
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer); var writer = new SpanWriter(buffer);
writer.Write((byte)0x54); // Packet ID writer.Write((byte)0x54); // Packet ID
writer.Write((byte)1); // flags writer.Write((byte)1); // flags
@ -57,6 +62,11 @@ namespace Server.Network
int explodeEffect, int explodeSound, Serial serial, int layer, int unknown int explodeEffect, int explodeSound, Serial serial, int layer, int unknown
) )
{ {
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer); var writer = new SpanWriter(buffer);
writer.Write((byte)0xC7); // Packet ID writer.Write((byte)0xC7); // Packet ID
writer.Write((byte)type); writer.Write((byte)type);
@ -168,6 +178,11 @@ namespace Server.Network
int speed, int duration, bool fixedDirection, bool explode, int hue, int renderMode int speed, int duration, bool fixedDirection, bool explode, int hue, int renderMode
) )
{ {
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer); var writer = new SpanWriter(buffer);
writer.Write((byte)0xC0); // Packet ID writer.Write((byte)0xC0); // Packet ID
writer.Write((byte)type); writer.Write((byte)type);

View file

@ -52,6 +52,11 @@ namespace Server.Network
public static void CreateRemoveEntity(Span<byte> buffer, Serial serial) public static void CreateRemoveEntity(Span<byte> buffer, Serial serial)
{ {
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer); var writer = new SpanWriter(buffer);
writer.Write((byte)0x1D); // Packet ID writer.Write((byte)0x1D); // Packet ID
writer.Write(serial); writer.Write(serial);
@ -64,7 +69,7 @@ namespace Server.Network
return; return;
} }
Span<byte> buffer = stackalloc byte[RemoveEntityLength]; Span<byte> buffer = stackalloc byte[RemoveEntityLength].InitializePacket();
CreateRemoveEntity(buffer, serial); CreateRemoveEntity(buffer, serial);
ns.Send(buffer); ns.Send(buffer);

View file

@ -56,6 +56,11 @@ namespace Server.Network
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name = "", string args = "" Serial serial, int graphic, MessageType type, int hue, int font, int number, string name = "", string args = ""
) )
{ {
if (buffer[0] != 0)
{
return buffer.Length;
}
name ??= ""; name ??= "";
args ??= ""; args ??= "";
@ -91,7 +96,7 @@ namespace Server.Network
return; return;
} }
Span<byte> buffer = stackalloc byte[GetMaxMessageLocalizedAffixLength(affix, args)]; Span<byte> buffer = stackalloc byte[GetMaxMessageLocalizedAffixLength(affix, args)].InitializePacket();
var length = CreateMessageLocalizedAffix( var length = CreateMessageLocalizedAffix(
buffer, serial, graphic, type, hue, font, number, name, affixType, affix, args buffer, serial, graphic, type, hue, font, number, name, affixType, affix, args
); );
@ -108,6 +113,11 @@ namespace Server.Network
AffixType affixType, string affix = "", string args = "" AffixType affixType, string affix = "", string args = ""
) )
{ {
if (buffer[0] != 0)
{
return buffer.Length;
}
name ??= ""; name ??= "";
affix ??= ""; affix ??= "";
args ??= ""; args ??= "";
@ -146,7 +156,7 @@ namespace Server.Network
return; return;
} }
Span<byte> buffer = stackalloc byte[GetMaxMessageLength(text)]; Span<byte> buffer = stackalloc byte[GetMaxMessageLength(text)].InitializePacket();
var length = CreateMessage( var length = CreateMessage(
buffer, buffer,
serial, serial,
@ -179,6 +189,11 @@ namespace Server.Network
string text string text
) )
{ {
if (buffer[0] != 0)
{
return buffer.Length;
}
name ??= ""; name ??= "";
text ??= ""; text ??= "";
lang ??= "ENU"; lang ??= "ENU";

View file

@ -17,6 +17,7 @@ using System;
using System.Buffers; using System.Buffers;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Memory;
namespace Server.Network namespace Server.Network
{ {
@ -25,7 +26,8 @@ namespace Server.Network
public const int BondedStatusPacketLength = 11; public const int BondedStatusPacketLength = 11;
public const int DeathAnimationPacketLength = 13; public const int DeathAnimationPacketLength = 13;
public const int MobileMovingPacketLength = 17; 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 AttributeMaximum = 100;
public const int MobileAttributePacketLength = 9; public const int MobileAttributePacketLength = 9;
public const int MobileAttributesPacketLength = 17; 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) public static void CreateMobileMoving(Span<byte> buffer, Mobile m, int noto, bool stygianAbyss)
{ {
if (buffer[0] != 0)
{
return;
}
var loc = m.Location; var loc = m.Location;
var hue = m.SolidHueOverride >= 0 ? m.SolidHueOverride : m.Hue; var hue = m.SolidHueOverride >= 0 ? m.SolidHueOverride : m.Hue;
@ -114,18 +121,18 @@ namespace Server.Network
return; return;
} }
Span<byte> span = stackalloc byte[MobileMovingPacketLength]; Span<byte> buffer = stackalloc byte[MobileMovingPacketLength].InitializePacket();
CreateMobileMoving(span, target, noto, ns.StygianAbyss); CreateMobileMoving(buffer, target, noto, ns.StygianAbyss);
ns.Send(span); ns.Send(buffer);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)); ns.SendMobileMovingUsingCache(cache, target, Notoriety.Compute(source, target));
// Requires a buffer of 16 packets, 17bytes per packet (272 bytes). // Requires a buffer of 16 packets, 17bytes per packet (272 bytes).
// Requires cache to have the first byte of each packet zeroed. // 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) if (ns == null)
{ {
@ -133,14 +140,9 @@ namespace Server.Network
} }
var stygianAbyss = ns.StygianAbyss; var stygianAbyss = ns.StygianAbyss;
var startIndex = (noto * 2 + (stygianAbyss ? 1 : 0)) * MobileMovingPacketLength; var row = noto * 2 + (stygianAbyss ? 1 : 0);
var buffer = cache.Slice(startIndex, MobileMovingPacketLength); var buffer = cache.GetRowSpan(row);
CreateMobileMoving(buffer, target, noto, stygianAbyss);
// Packet not created yet
if (buffer[0] == 0)
{
CreateMobileMoving(buffer, target, noto, stygianAbyss);
}
ns.Send(buffer); ns.Send(buffer);
} }
@ -283,6 +285,11 @@ namespace Server.Network
Serial mobile, int action, int frameCount, int repeatCount, bool forward, bool repeat, int delay Serial mobile, int action, int frameCount, int repeatCount, bool forward, bool repeat, int delay
) )
{ {
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer); var writer = new SpanWriter(buffer);
writer.Write((byte)0x6E); // Packet ID writer.Write((byte)0x6E); // Packet ID
writer.Write(mobile); writer.Write(mobile);

View file

@ -219,6 +219,11 @@ namespace Server.Network
int itemID, int hue, int amount int itemID, int hue, int amount
) )
{ {
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer); var writer = new SpanWriter(buffer);
writer.Write((byte)0x23); // Packet ID writer.Write((byte)0x23); // Packet ID
writer.Write((short)itemID); writer.Write((short)itemID);
@ -247,9 +252,9 @@ namespace Server.Network
return; return;
} }
Span<byte> span = stackalloc byte[DragEffectPacketLength]; Span<byte> buffer = stackalloc byte[DragEffectPacketLength].InitializePacket();
CreateDragEffect(span, srcSerial, srcLocation, trgSerial, trgLocation, itemID, hue, amount); CreateDragEffect(buffer, srcSerial, srcLocation, trgSerial, trgLocation, itemID, hue, amount);
ns.Send(span); ns.Send(buffer);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View file

@ -152,19 +152,25 @@ namespace Server
{ {
var length = OutgoingMessagePackets.GetMaxMessageLength(text); var length = OutgoingMessagePackets.GetMaxMessageLength(text);
Span<byte> buffer = stackalloc byte[length]; Span<byte> buffer = stackalloc byte[length].InitializePacket();
length = OutgoingMessagePackets.CreateMessage(
buffer, Serial.MinusOne, -1, MessageType.Regular, hue, 3, ascii, "ENU", "System", text
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
foreach (var ns in TcpServer.Instances) foreach (var ns in TcpServer.Instances)
{ {
if (ns.Mobile != null) if (ns.Mobile == null)
{ {
ns.Send(buffer); continue;
} }
length = OutgoingMessagePackets.CreateMessage(
buffer, Serial.MinusOne, -1, MessageType.Regular, hue, 3, ascii, "ENU", "System", text
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
}
ns.Send(buffer);
} }
} }

View file

@ -416,18 +416,13 @@ namespace Server.Commands
toAll toAll
); );
Span<byte> buffer = stackalloc byte[OutgoingEffectPackets.SoundPacketLength]; Span<byte> buffer = stackalloc byte[OutgoingEffectPackets.SoundPacketLength].InitializePacket();
buffer.InitializePacket();
foreach (var state in m.GetClientsInRange(12)) foreach (var state in m.GetClientsInRange(12))
{ {
if (toAll || state.Mobile.CanSee(m)) if (toAll || state.Mobile.CanSee(m))
{ {
if (buffer[0] == 0) OutgoingEffectPackets.CreateSoundEffect(buffer, index, m);
{
OutgoingEffectPackets.CreateSoundEffect(buffer, index, m);
}
state.Send(buffer); state.Send(buffer);
} }
} }

View file

@ -69,8 +69,7 @@ namespace Server.Commands
if (list.Count > 0) if (list.Count > 0)
{ {
Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength]; Span<byte> removeEntity = stackalloc byte[OutgoingEntityPackets.RemoveEntityLength].InitializePacket();
removeEntity.InitializePacket();
for (var i = 0; i < list.Count; ++i) for (var i = 0; i < list.Count; ++i)
{ {
@ -78,11 +77,7 @@ namespace Server.Commands
if (!m.CanSee(pm) && Utility.InUpdateRange(m, pm)) if (!m.CanSee(pm) && Utility.InUpdateRange(m, pm))
{ {
if (removeEntity[0] == 0) OutgoingEntityPackets.CreateRemoveEntity(removeEntity, pm.Serial);
{
OutgoingEntityPackets.CreateRemoveEntity(removeEntity, pm.Serial);
}
m.NetState?.Send(removeEntity); m.NetState?.Send(removeEntity);
} }
} }

View file

@ -503,28 +503,27 @@ namespace Server.Engines.Doom
/* I cant find any better way to send "speech" using fonts other than default */ /* I cant find any better way to send "speech" using fonts other than default */
public static void POHMessage(Mobile from, int index) public static void POHMessage(Mobile from, int index)
{ {
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(Msgs[index])]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(Msgs[index])].InitializePacket();
buffer.InitializePacket();
var eable = from.Map.GetClientsInRange(from.Location); var eable = from.Map.GetClientsInRange(from.Location);
foreach (var state in eable) foreach (var state in eable)
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
{ buffer,
var length = OutgoingMessagePackets.CreateMessage( from.Serial,
buffer, from.Body,
from.Serial, MessageType.Regular,
from.Body, MsgParams[index][0],
MessageType.Regular, MsgParams[index][1],
MsgParams[index][0], true,
MsgParams[index][1], null,
true, from.Name,
null, Msgs[index]
from.Name, );
Msgs[index]
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }

View file

@ -95,8 +95,7 @@ namespace Server.Engines.MLQuests.Mobiles
return; return;
} }
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength("")]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength("")].InitializePacket();
buffer.InitializePacket();
foreach (var state in GetClientsInRange(12)) foreach (var state in GetClientsInRange(12))
{ {
@ -104,13 +103,13 @@ namespace Server.Engines.MLQuests.Mobiles
if (m.CanSee(this) && m.InLOS(this) && m.CanBeginAction(this)) if (m.CanSee(this) && m.InLOS(this) && m.CanBeginAction(this))
{ {
if (buffer[0] == 0) // Double Click On Me For Help!
{ var length = OutgoingMessagePackets.CreateMessageLocalized(
// Double Click On Me For Help! buffer, Serial, Body, MessageType.Regular, 946, 3, 1078099, Name
var length = OutgoingMessagePackets.CreateMessageLocalized( );
buffer, Serial, Body, MessageType.Regular, 946, 3, 1078099, Name
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); buffer = buffer.Slice(0, length);
} }

View file

@ -370,7 +370,8 @@ namespace Server.Engines.PartySystem
public void SendToAll(int number, string args = "", int hue = 0x3B2) public void SendToAll(int number, string args = "", int hue = 0x3B2)
{ {
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)].InitializePacket();
var length = OutgoingMessagePackets.CreateMessageLocalized( var length = OutgoingMessagePackets.CreateMessageLocalized(
buffer, buffer,
Serial.MinusOne, -1, MessageType.Regular, hue, 3, number, "System", args Serial.MinusOne, -1, MessageType.Regular, hue, 3, number, "System", args
@ -415,8 +416,7 @@ namespace Server.Engines.PartySystem
private void SendToStaffMessage(Mobile from, string text) private void SendToStaffMessage(Mobile from, string text)
{ {
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
buffer.InitializePacket();
foreach (var ns in from.GetClientsInRange(8)) foreach (var ns in from.GetClientsInRange(8))
{ {
@ -425,21 +425,21 @@ namespace Server.Engines.PartySystem
if (mob?.AccessLevel >= AccessLevel.GameMaster && mob.AccessLevel > from.AccessLevel && if (mob?.AccessLevel >= AccessLevel.GameMaster && mob.AccessLevel > from.AccessLevel &&
mob.Party != this && !m_Listeners.Contains(mob)) mob.Party != this && !m_Listeners.Contains(mob))
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
{ buffer,
var length = OutgoingMessagePackets.CreateMessage( from.Serial,
buffer, from.Body,
from.Serial, MessageType.Regular,
from.Body, from.SpeechHue,
MessageType.Regular, 3,
from.SpeechHue, false,
3, from.Language,
false, from.Name,
from.Language, text
from.Name, );
text
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }
@ -504,8 +504,7 @@ namespace Server.Engines.PartySystem
m_Mobile.SendLocalizedMessage(1005437); // You have rejoined the party. m_Mobile.SendLocalizedMessage(1005437); // You have rejoined the party.
m_Mobile.Send(new PartyMemberList(p)); m_Mobile.Send(new PartyMemberList(p));
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedAffixLength(m_Mobile.Name, "")]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedAffixLength(m_Mobile.Name, "")].InitializePacket();
buffer.InitializePacket();
Span<byte> attrsPacket = stackalloc byte[OutgoingMobilePackets.MobileAttributesPacketLength]; Span<byte> attrsPacket = stackalloc byte[OutgoingMobilePackets.MobileAttributesPacketLength];
OutgoingMobilePackets.CreateMobileAttributes(attrsPacket, m_Mobile, true); OutgoingMobilePackets.CreateMobileAttributes(attrsPacket, m_Mobile, true);
@ -516,32 +515,34 @@ namespace Server.Engines.PartySystem
{ {
var m = mi.Mobile; var m = mi.Mobile;
if (m != m_Mobile) if (m == m_Mobile)
{ {
if (buffer[0] == 0) continue;
{
var length = OutgoingMessagePackets.CreateMessageLocalizedAffix(
buffer,
Serial.MinusOne,
-1,
MessageType.Label,
0x3B2,
3,
1008087,
"",
AffixType.Prepend | AffixType.System,
m_Mobile.Name
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
}
m.NetState?.Send(buffer);
m.NetState.SendMobileStatusCompact(m_Mobile, m_Mobile.CanBeRenamedBy(m));
m.NetState?.Send(attrsPacket);
ns.SendMobileStatusCompact(m, m.CanBeRenamedBy(m_Mobile));
ns.SendMobileAttributes(m, true);
} }
var length = OutgoingMessagePackets.CreateMessageLocalizedAffix(
buffer,
Serial.MinusOne,
-1,
MessageType.Label,
0x3B2,
3,
1008087,
"",
AffixType.Prepend | AffixType.System,
m_Mobile.Name
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
}
m.NetState?.Send(buffer);
m.NetState.SendMobileStatusCompact(m_Mobile, m_Mobile.CanBeRenamedBy(m));
m.NetState?.Send(attrsPacket);
ns.SendMobileStatusCompact(m, m.CanBeRenamedBy(m_Mobile));
ns.SendMobileAttributes(m, true);
} }
} }
} }

View file

@ -87,16 +87,11 @@ namespace Server.Items
} }
else else
{ {
Span<byte> buffer = stackalloc byte[OutgoingEffectPackets.SoundPacketLength]; Span<byte> buffer = stackalloc byte[OutgoingEffectPackets.SoundPacketLength].InitializePacket();
buffer.InitializePacket();
foreach (var state in GetClientsInRange(2)) foreach (var state in GetClientsInRange(2))
{ {
if (buffer[0] == 0) OutgoingEffectPackets.CreateSoundEffect(buffer, 0x127, GetWorldLocation());
{
OutgoingEffectPackets.CreateSoundEffect(buffer, 0x127, GetWorldLocation());
}
state.Send(buffer); state.Send(buffer);
} }
} }

View file

@ -330,8 +330,7 @@ namespace Server.Guilds
public void AllianceChat(Mobile from, int hue, string text) public void AllianceChat(Mobile from, int hue, string text)
{ {
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
buffer.InitializePacket();
for (var i = 0; i < m_Members.Count; i++) for (var i = 0; i < m_Members.Count; i++)
{ {
@ -339,21 +338,21 @@ namespace Server.Guilds
for (var j = 0; j < g.Members.Count; j++) for (var j = 0; j < g.Members.Count; j++)
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
{ buffer,
var length = OutgoingMessagePackets.CreateMessage( from.Serial,
buffer, from.Body,
from.Serial, MessageType.Alliance,
from.Body, hue,
MessageType.Alliance, 3,
hue, false,
3, from.Language,
false, from.Name,
from.Language, text
from.Name, );
text
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }
@ -1520,18 +1519,17 @@ namespace Server.Guilds
public void GuildChat(Mobile from, int hue, string text) public void GuildChat(Mobile from, int hue, string text)
{ {
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
buffer.InitializePacket();
for (var i = 0; i < Members.Count; i++) for (var i = 0; i < Members.Count; i++)
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
{ buffer, from.Serial, from.Body, MessageType.Guild, hue, 3, false, from.Language,
var length = OutgoingMessagePackets.CreateMessage( from.Name, text
buffer, from.Serial, from.Body, MessageType.Guild, hue, 3, false, from.Language, );
from.Name, text
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }

View file

@ -2818,8 +2818,7 @@ namespace Server.Mobiles
private static void SendToStaffMessage(Mobile from, string text) private static void SendToStaffMessage(Mobile from, string text)
{ {
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)]; Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
buffer.InitializePacket();
foreach (var ns in from.GetClientsInRange(8)) foreach (var ns in from.GetClientsInRange(8))
{ {
@ -2827,21 +2826,21 @@ namespace Server.Mobiles
if (mob?.AccessLevel >= AccessLevel.GameMaster && mob.AccessLevel > from.AccessLevel) if (mob?.AccessLevel >= AccessLevel.GameMaster && mob.AccessLevel > from.AccessLevel)
{ {
if (buffer[0] == 0) var length = OutgoingMessagePackets.CreateMessage(
{ buffer,
var length = OutgoingMessagePackets.CreateMessage( from.Serial,
buffer, from.Body,
from.Serial, MessageType.Regular,
from.Body, from.SpeechHue,
MessageType.Regular, 3,
from.SpeechHue, false,
3, from.Language,
false, from.Name,
from.Language, text
from.Name, );
text
);
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size buffer = buffer.Slice(0, length); // Adjust to the actual size
} }