feat(net): #18 holtburger inbound chat parity - EmoteText, SoulEmote, ServerMessage, PlayerKilled, WeenieError + Windows-1252 codec

Five sub-changes:

1. Windows-1252 codec switch (global). Every Encoding.ASCII call site
   in src/AcDream.Core.Net/Messages/ -> Encoding.GetEncoding(1252).
   Touched HearSpeech, ChatRequests, GameEvents, AppraiseInfoParser,
   CharacterList, CreateObject, PlayerDescriptionParser, SocialActions.
   New Encodings.cs module-init registers CodePagesEncodingProvider
   (System.Text.Encoding.CodePages ships with .NET 10 SDK but isn't
   auto-registered). Matches retail + holtburger; accented names
   no longer round-trip-broken.

2. New parsers (opcodes confirmed against holtburger opcodes.rs):
   - EmoteText (0x01E0)     { u32 senderGuid, string16 senderName, string16 text }
   - SoulEmote (0x01E2)     same wire layout as EmoteText
   - ServerMessage (0xF7E0) { string16 message, u32 chatType }
   - PlayerKilled (0x019E)  { string16 deathMessage, u32 victimGuid, u32 killerGuid }
   Shared StringReader.cs has the CP1252 String16L primitive.

3. WorldSession dispatch. ProcessDatagram adds branches for the four
   new top-level opcodes + fires session-level events (EmoteHeard,
   SoulEmoteHeard, ServerMessageReceived, PlayerKilledReceived).
   0x0295 SetTurbineChatChannels stubbed with TODO for parallel I.6.

4. GameEventWiring routes WeenieError + WeenieErrorWithString
   (parsers existed but were unrouted) -> chat.OnWeenieError.

5. ChatLog adapters: Emote / SoulEmote ChatKind values, OnEmote,
   OnSoulEmote, OnPlayerKilled, OnWeenieError. OnLocalSpeech now
   substitutes empty sender -> "You" per holtburger client/messages.rs.
   ChatVM.FormatEntry handles new kinds (asterisk + sender + text).

22 new tests covering parser round-trips + reject-bad-opcode +
ChatLog adapter coverage + Win-1252 round-trip with non-ASCII chars.
Solution total: 881 green (210->225 in Core.Net.Tests, 606->613 in Core.Tests).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-25 19:06:01 +02:00
parent b131514d51
commit ff5ed9ec0b
25 changed files with 899 additions and 10 deletions

View file

@ -429,7 +429,8 @@ public static class AppraiseInfoParser
ushort len = BinaryPrimitives.ReadUInt16LittleEndian(src.Slice(pos));
pos += 2;
if (src.Length - pos < len) throw new FormatException("truncated string body");
string v = Encoding.ASCII.GetString(src.Slice(pos, len));
// Windows-1252 matches retail (and holtburger's encoding_rs::WINDOWS_1252).
string v = Encoding.GetEncoding(1252).GetString(src.Slice(pos, len));
pos += len;
int record = 2 + len;
int pad = (4 - (record & 3)) & 3;

View file

@ -92,7 +92,8 @@ public static class CharacterList
ushort length = BinaryPrimitives.ReadUInt16LittleEndian(source.Slice(pos));
pos += 2;
if (source.Length - pos < length) throw new FormatException("truncated String16L body");
string result = Encoding.ASCII.GetString(source.Slice(pos, length));
// Windows-1252 matches retail (and holtburger's encoding_rs::WINDOWS_1252).
string result = Encoding.GetEncoding(1252).GetString(source.Slice(pos, length));
pos += length;
int recordSize = 2 + length;
int padding = (4 - (recordSize & 3)) & 3;

View file

@ -84,7 +84,8 @@ public static class ChatRequests
private static byte[] PackString16L(string s)
{
byte[] data = Encoding.ASCII.GetBytes(s);
// Windows-1252 matches retail (and holtburger's encoding_rs::WINDOWS_1252).
byte[] data = Encoding.GetEncoding(1252).GetBytes(s);
if (data.Length > ushort.MaxValue)
throw new ArgumentException("String too long for 16-bit length prefix.", nameof(s));

View file

@ -433,7 +433,8 @@ public static class CreateObject
pos += 2;
if (length > 1024) throw new FormatException($"String16L length {length} exceeds sanity limit");
if (source.Length - pos < length) throw new FormatException("truncated String16L body");
string result = System.Text.Encoding.ASCII.GetString(source.Slice(pos, length));
// Windows-1252 matches retail (and holtburger's encoding_rs::WINDOWS_1252).
string result = System.Text.Encoding.GetEncoding(1252).GetString(source.Slice(pos, length));
pos += length;
int recordSize = 2 + length;
int padding = (4 - (recordSize & 3)) & 3;

View file

@ -0,0 +1,55 @@
using System;
using System.Buffers.Binary;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Inbound <c>0x01E0 EmoteText</c> top-level GameMessage.
/// Server-driven third-person emote announcement (e.g.
/// "The Olthoi growls at you.").
///
/// <para>
/// This is a standalone GameMessage — NOT wrapped in the 0xF7B0
/// GameEvent envelope. Dispatched directly from the opcode switch in
/// <see cref="WorldSession.ProcessDatagram"/>.
/// </para>
///
/// <para>
/// Wire layout (port from holtburger
/// <c>references/holtburger/.../messages/chat/types.rs::EmoteTextData</c>,
/// see also opcodes.rs:155):
/// <code>
/// u32 opcode // 0x01E0
/// u32 senderGuid
/// string16L senderName
/// string16L text
/// </code>
/// </para>
/// </summary>
public static class EmoteText
{
public const uint Opcode = 0x01E0u;
public readonly record struct Parsed(
uint SenderGuid,
string SenderName,
string Text);
public static Parsed? TryParse(byte[] body)
{
if (body is null || body.Length < 8) return null;
uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body);
if (opcode != Opcode) return null;
try
{
int pos = 4;
uint senderGuid = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(pos));
pos += 4;
string senderName = StringReader.ReadString16L(body, ref pos);
string text = StringReader.ReadString16L(body, ref pos);
return new Parsed(senderGuid, senderName, text);
}
catch { return null; }
}
}

View file

@ -0,0 +1,51 @@
using System.Runtime.CompilerServices;
using System.Text;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Shared text encodings for the AC wire protocol.
///
/// <para>
/// Retail and holtburger both use Windows-1252 (CP1252) for every
/// String16L on the wire — chat, character names, item names,
/// system messages, popup text. Before Phase I.5 we used ASCII,
/// which was wrong: any non-ASCII byte (e.g. é = 0xE9, name accents
/// or chat punctuation from Latin-1 locales) decoded to '?'. Switching
/// to CP1252 matches what the server actually sends.
/// </para>
///
/// <para>
/// .NET 6+ ships only ASCII / Latin-1 / UTF8/16/32 in the base library;
/// Windows-1252 lives in <c>System.Text.Encoding.CodePages</c>. The
/// module initializer below registers that provider on first load of
/// any type in <c>AcDream.Core.Net.Messages</c>, so call sites can use
/// <see cref="Encoding.GetEncoding(int)"/> with id <c>1252</c> directly.
/// </para>
/// </summary>
public static class Encodings
{
/// <summary>
/// Module initializer — guaranteed by ECMA-335 to run before any
/// other code in this module (in particular, before any static
/// field initializer in user code that follows). Registers
/// <c>CodePagesEncodingProvider</c> so
/// <see cref="Encoding.GetEncoding(int)"/> with id 1252 returns a
/// real encoding instance instead of throwing
/// <see cref="System.NotSupportedException"/>.
/// </summary>
#pragma warning disable CA2255 // Library-internal: registers CP1252 once
// before any wire-string parser is invoked.
[ModuleInitializer]
internal static void Register()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
#pragma warning restore CA2255
/// <summary>
/// CP1252 (Windows-1252). Cached so callers don't look it up each frame.
/// Initialized after <see cref="Register"/> via field-init ordering.
/// </summary>
public static readonly Encoding Windows1252 = Encoding.GetEncoding(1252);
}

View file

@ -471,7 +471,8 @@ public static class GameEvents
ushort length = BinaryPrimitives.ReadUInt16LittleEndian(source.Slice(pos));
pos += 2;
if (source.Length - pos < length) throw new FormatException("truncated String16L body");
string result = Encoding.ASCII.GetString(source.Slice(pos, length));
// Windows-1252 matches retail (and holtburger's encoding_rs::WINDOWS_1252).
string result = Encoding.GetEncoding(1252).GetString(source.Slice(pos, length));
pos += length;
int recordSize = 2 + length;
int padding = (4 - (recordSize & 3)) & 3;

View file

@ -75,7 +75,10 @@ public static class HearSpeech
ushort length = BinaryPrimitives.ReadUInt16LittleEndian(source.Slice(pos));
pos += 2;
if (source.Length - pos < length) throw new FormatException("truncated String16L body");
string result = Encoding.ASCII.GetString(source.Slice(pos, length));
// Windows-1252 matches retail (and holtburger's encoding_rs::WINDOWS_1252).
// ASCII would munge any non-ASCII byte into a '?' which corrupts player
// names and chat with accented characters (e.g. "Café").
string result = Encoding.GetEncoding(1252).GetString(source.Slice(pos, length));
pos += length;
int recordSize = 2 + length;
int padding = (4 - (recordSize & 3)) & 3;

View file

@ -590,7 +590,8 @@ public static class PlayerDescriptionParser
ushort len = BinaryPrimitives.ReadUInt16LittleEndian(src.Slice(pos));
pos += 2;
if (src.Length - pos < len) throw new FormatException("truncated string body");
string v = Encoding.ASCII.GetString(src.Slice(pos, len));
// Windows-1252 matches retail (and holtburger's encoding_rs::WINDOWS_1252).
string v = Encoding.GetEncoding(1252).GetString(src.Slice(pos, len));
pos += len;
// String16L records pad to 4-byte alignment per AppraiseInfoParser convention.
int record = 2 + len;

View file

@ -0,0 +1,51 @@
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(byte[] body)
{
if (body is null || 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.AsSpan(pos));
pos += 4;
uint killerGuid = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(pos));
return new Parsed(deathMessage, victimGuid, killerGuid);
}
catch { return null; }
}
}

View file

@ -0,0 +1,51 @@
using System;
using System.Buffers.Binary;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Inbound <c>0xF7E0 ServerMessage</c> top-level GameMessage.
/// General-purpose server-broadcast text — admin announcements,
/// combat logs, and routine error messages routed by the server
/// instead of via WeenieError.
///
/// <para>
/// This is a standalone GameMessage — NOT wrapped in 0xF7B0
/// GameEvent envelope. Dispatched directly from
/// <see cref="WorldSession.ProcessDatagram"/>.
/// </para>
///
/// <para>
/// Wire layout (port from holtburger
/// <c>references/holtburger/.../messages/chat/types.rs::ServerMessageData</c>,
/// see also opcodes.rs:167):
/// <code>
/// u32 opcode // 0xF7E0
/// string16L message
/// u32 chatType // ChatMessageType (Broadcast / System / etc)
/// </code>
/// </para>
/// </summary>
public static class ServerMessage
{
public const uint Opcode = 0xF7E0u;
public readonly record struct Parsed(string Message, uint ChatType);
public static Parsed? TryParse(byte[] body)
{
if (body is null || body.Length < 8) return null;
uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body);
if (opcode != Opcode) return null;
try
{
int pos = 4;
string message = StringReader.ReadString16L(body, ref pos);
if (body.Length - pos < 4) return null;
uint chatType = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(pos));
return new Parsed(message, chatType);
}
catch { return null; }
}
}

View file

@ -160,7 +160,8 @@ public static class SocialActions
private static byte[] PackString16L(string s)
{
ArgumentNullException.ThrowIfNull(s);
byte[] data = Encoding.ASCII.GetBytes(s);
// Windows-1252 matches retail (and holtburger's encoding_rs::WINDOWS_1252).
byte[] data = Encoding.GetEncoding(1252).GetBytes(s);
if (data.Length > ushort.MaxValue)
throw new ArgumentException("String too long for 16-bit length prefix.", nameof(s));

View file

@ -0,0 +1,51 @@
using System;
using System.Buffers.Binary;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Inbound <c>0x01E2 SoulEmote</c> top-level GameMessage.
/// Server-driven complex emote with optional animation pairing.
/// Wire layout is identical to <see cref="EmoteText"/>; the difference
/// is only how the client renders it (chat-only vs paired with a
/// <c>PlayScript</c> on the same target).
///
/// <para>
/// Wire layout (port from holtburger
/// <c>references/holtburger/.../messages/chat/types.rs::SoulEmoteData</c>,
/// see also opcodes.rs:158):
/// <code>
/// u32 opcode // 0x01E2
/// u32 senderGuid
/// string16L senderName
/// string16L text
/// </code>
/// </para>
/// </summary>
public static class SoulEmote
{
public const uint Opcode = 0x01E2u;
public readonly record struct Parsed(
uint SenderGuid,
string SenderName,
string Text);
public static Parsed? TryParse(byte[] body)
{
if (body is null || body.Length < 8) return null;
uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body);
if (opcode != Opcode) return null;
try
{
int pos = 4;
uint senderGuid = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(pos));
pos += 4;
string senderName = StringReader.ReadString16L(body, ref pos);
string text = StringReader.ReadString16L(body, ref pos);
return new Parsed(senderGuid, senderName, text);
}
catch { return null; }
}
}

View file

@ -0,0 +1,46 @@
using System;
using System.Buffers.Binary;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Shared reader for the AC <c>String16L</c> wire format used by every
/// inbound and outbound chat / name / message body.
///
/// <para>
/// Wire shape:
/// <code>
/// u16 length // byte count, NOT char count
/// byte[] text // CP1252-encoded bytes, no terminator
/// pad to 4-byte boundary
/// </code>
/// </para>
///
/// <para>
/// Codec is Windows-1252 (CP1252), matching retail and holtburger's
/// <c>encoding_rs::WINDOWS_1252</c>. Registration of the
/// <c>CodePagesEncodingProvider</c> is handled by
/// <see cref="Encodings"/>.
/// </para>
/// </summary>
internal static class StringReader
{
/// <summary>
/// Read a String16L from <paramref name="source"/> at <paramref name="pos"/>,
/// advancing <paramref name="pos"/> past the length, body, and 4-byte
/// alignment padding. Throws <see cref="FormatException"/> on truncation.
/// </summary>
public static string ReadString16L(ReadOnlySpan<byte> source, ref int pos)
{
if (source.Length - pos < 2) throw new FormatException("truncated String16L length");
ushort length = BinaryPrimitives.ReadUInt16LittleEndian(source.Slice(pos));
pos += 2;
if (source.Length - pos < length) throw new FormatException("truncated String16L body");
string result = Encodings.Windows1252.GetString(source.Slice(pos, length));
pos += length;
int recordSize = 2 + length;
int padding = (4 - (recordSize & 3)) & 3;
pos += padding;
return result;
}
}