ModernUO/Projects/UOContent/Engines/Party/PartyPackets.cs
Kamron Batman 03f850fe03
fix: Fixes sending packets and sidesteps a major issue with stackalloc and PGO in .NET 8 (#1607)
### Summary
- Works around a sneaky edge case bug in the JIT with stackalloc where sometimes the buffer is not zero'd.
- Fixes SendDisplayBoatHS
- Fixes sending health bars in the `SendEverything()` logic.
- Fixes a bug in sizing for some string helper functions.

### Developer Note
We are enabled `SkipLocalsInit` - do not rely on `stackalloc` to be zero'd. To zero the buffer, use `span.Clear();`

Closes #1606
2023-11-21 12:18:20 -08:00

153 lines
5.3 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: PartyPackets.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using Server.Network;
namespace Server.Engines.PartySystem
{
public static class PartyPackets
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPartyMemberListPacketLength(int partyCount) => 7 + partyCount * 4;
public static void CreatePartyMemberList(Span<byte> buffer, Party p)
{
if (buffer[0] != 0)
{
return;
}
var count = p.Count;
var length = GetPartyMemberListPacketLength(count);
var writer = new SpanWriter(buffer);
writer.Write((byte)0xBF); // Packet ID
writer.Write((ushort)length);
writer.Write((ushort)0x06); // Sub-packet
writer.Write((byte)0x01); // Command
writer.Write((byte)count);
for (var i = 0; i < count; ++i)
{
writer.Write(p[i].Mobile.Serial);
}
}
public static void SendPartyMemberList(this NetState ns, Party p)
{
if (ns.CannotSendPackets())
{
return;
}
Span<byte> buffer = stackalloc byte[GetPartyMemberListPacketLength(p.Count)].InitializePacket();
CreatePartyMemberList(buffer, p);
ns.Send(buffer);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPartyRemoveMemberPacketLength(int partyCount) => 11 + partyCount * 4;
public static void SendPartyRemoveMember(this NetState ns, Serial m, Party p = null)
{
if (ns.CannotSendPackets())
{
return;
}
Span<byte> buffer = stackalloc byte[GetPartyRemoveMemberPacketLength(p?.Count ?? 0)].InitializePacket();
CreatePartyRemoveMember(buffer, m, p);
ns.Send(buffer);
}
public static void CreatePartyRemoveMember(Span<byte> buffer, Serial removed, Party p = null)
{
if (buffer[0] != 0)
{
return;
}
var count = p?.Count ?? 0;
var length = GetPartyRemoveMemberPacketLength(count);
var writer = new SpanWriter(buffer);
writer.Write((byte)0xBF); // Packet ID
writer.Write((ushort)length);
writer.Write((ushort)0x06); // Sub-packet
writer.Write((byte)0x02); // Command
writer.Write((byte)count);
writer.Write(removed);
for (var i = 0; i < count; ++i)
{
writer.Write(p![i].Mobile.Serial);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPartyTextMessagePacketLength(string text) => 12 + (text?.Length * 2 ?? 0);
public static void SendPartyTextMessage(this NetState ns, Serial m, string text, bool toAll)
{
if (ns.CannotSendPackets())
{
return;
}
Span<byte> buffer = stackalloc byte[GetPartyTextMessagePacketLength(text)].InitializePacket();
CreatePartyTextMessage(buffer, m, text, toAll);
ns.Send(buffer);
}
public static void CreatePartyTextMessage(Span<byte> buffer, Serial m, string text, bool toAll)
{
if (buffer[0] != 0)
{
return;
}
var length = GetPartyTextMessagePacketLength(text);
var writer = new SpanWriter(buffer);
writer.Write((byte)0xBF); // Packet ID
writer.Write((ushort)length);
writer.Write((ushort)0x06); // Sub-packet
writer.Write((byte)(toAll ? 0x04 : 0x03));
writer.Write(m);
writer.WriteBigUniNull(text);
}
public static void SendPartyInvitation(this NetState ns, Serial leader)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[10]);
writer.Write((byte)0xBF); // Packet ID
writer.Write((ushort)10);
writer.Write((ushort)0x06); // Sub-packet
writer.Write((byte)0x07); // command
writer.Write(leader);
ns.Send(writer.Span);
}
}
}