acdream/src/AcDream.Core.Net/Messages/HearSpeech.cs
Erik ff5ed9ec0b 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>
2026-04-25 19:06:01 +02:00

88 lines
3.1 KiB
C#

using System;
using System.Buffers.Binary;
using System.Text;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Inbound <c>0x02BB HearSpeech</c> + <c>0x02BC HearRangedSpeech</c>
/// GameMessages. Local-area / shout chat heard by the player. These
/// do NOT ride the 0xF7B0 GameEvent envelope — they're standalone
/// GameMessages dispatched the same way as CreateObject / UpdateMotion.
///
/// <para>
/// Wire layout:
/// <code>
/// u32 opcode // 0x02BB or 0x02BC
/// string16L text
/// string16L senderName
/// u32 senderGuid
/// u32 chatType
/// </code>
/// </para>
///
/// <para>
/// ChatType (from ACE):
/// <list type="bullet">
/// <item><description>0x01 = Broadcast</description></item>
/// <item><description>0x02 = Combat</description></item>
/// <item><description>0x0B = Speech</description></item>
/// <item><description>0x0F = Emote</description></item>
/// <item><description>0x10 = Tell</description></item>
/// <item><description>0x11 = Syllables (spell casting)</description></item>
/// <item><description>other values in ACE ChatMessageType.cs</description></item>
/// </list>
/// </para>
/// </summary>
public static class HearSpeech
{
public const uint LocalOpcode = 0x02BBu;
public const uint RangedOpcode = 0x02BCu;
public readonly record struct Parsed(
string Text,
string SenderName,
uint SenderGuid,
uint ChatType,
bool IsRanged);
public static Parsed? TryParse(byte[] body)
{
if (body is null || body.Length < 16) return null;
uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body);
bool isRanged;
if (opcode == LocalOpcode) isRanged = false;
else if (opcode == RangedOpcode) isRanged = true;
else return null;
int pos = 4;
try
{
string text = ReadString16L(body, ref pos);
string sender = ReadString16L(body, ref pos);
if (body.Length - pos < 8) return null;
uint senderGuid = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(pos)); pos += 4;
uint chatType = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(pos)); pos += 4;
return new Parsed(text, sender, senderGuid, chatType, isRanged);
}
catch { return null; }
}
private 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");
// 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;
pos += padding;
return result;
}
}