acdream/src/AcDream.Core.Net/Messages/PlayerKilled.cs
Erik e928c5dd02 perf(net): borrow inbound packet storage
Decode headers, optional fields, fragments, and single-fragment messages directly over pooled datagrams. Copy only fragment state that crosses a datagram lifetime, preserve synchronous dispatch and ACK ordering, and lock the path to the owned decoder with differential and zero-allocation tests.
2026-07-25 05:58:55 +02:00

51 lines
1.5 KiB
C#

using System;
using System.Buffers.Binary;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Inbound <c>0x019E PlayerKilled</c> top-level GameMessage.
/// Server announcement that a player was killed in combat — used for
/// the death-message scroll and chat-log lines like
/// "Caith was killed by an Olthoi Soldier."
///
/// <para>
/// Wire layout (port from holtburger
/// <c>references/holtburger/.../messages/combat/types.rs::PlayerKilledData</c>,
/// see also opcodes.rs:150):
/// <code>
/// u32 opcode // 0x019E
/// string16L deathMessage
/// u32 victimGuid
/// u32 killerGuid
/// </code>
/// </para>
/// </summary>
public static class PlayerKilled
{
public const uint Opcode = 0x019Eu;
public readonly record struct Parsed(
string DeathMessage,
uint VictimGuid,
uint KillerGuid);
public static Parsed? TryParse(ReadOnlySpan<byte> body)
{
if (body.Length < 4) return null;
uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body);
if (opcode != Opcode) return null;
try
{
int pos = 4;
string deathMessage = StringReader.ReadString16L(body, ref pos);
if (body.Length - pos < 8) return null;
uint victimGuid = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(pos));
pos += 4;
uint killerGuid = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(pos));
return new Parsed(deathMessage, victimGuid, killerGuid);
}
catch { return null; }
}
}