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

@ -279,4 +279,49 @@ public sealed class GameEventWiringTests
Assert.Equal(0, book.ActiveCount);
}
[Fact]
public void WireAll_WeenieError_RoutesToChatLog()
{
// Phase I.5: 0x028A previously had a parser
// (GameEvents.ParseWeenieError) but no dispatcher registration. The
// server fires this for plain game-logic failures (e.g. "you can't
// pick that up"). Now wired → ChatLog.OnWeenieError.
var (d, _, _, _, chat) = MakeAll();
byte[] payload = new byte[4];
BinaryPrimitives.WriteUInt32LittleEndian(payload, 0x9C); // arbitrary error code
var env = GameEventEnvelope.TryParse(WrapEnvelope(GameEventType.WeenieError, payload));
d.Dispatch(env!.Value);
Assert.Equal(1, chat.Count);
var e = chat.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Equal(0x9Cu, e.ChannelId);
Assert.Contains("0x009C", e.Text);
}
[Fact]
public void WireAll_WeenieErrorWithString_RoutesToChatLogWithInterpolation()
{
// Phase I.5: 0x028B carries an interpolated substring (e.g. the
// target's name in "you can't pick up the {Mana Stone}"). Now
// wired → ChatLog.OnWeenieError with the param.
var (d, _, _, _, chat) = MakeAll();
byte[] interpBytes = MakeString16L("Mana Stone");
byte[] payload = new byte[4 + interpBytes.Length];
BinaryPrimitives.WriteUInt32LittleEndian(payload, 0x42u);
Array.Copy(interpBytes, 0, payload, 4, interpBytes.Length);
var env = GameEventEnvelope.TryParse(WrapEnvelope(GameEventType.WeenieErrorWithString, payload));
d.Dispatch(env!.Value);
Assert.Equal(1, chat.Count);
var e = chat.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Equal(0x42u, e.ChannelId);
Assert.Contains("Mana Stone", e.Text);
}
}

View file

@ -120,9 +120,32 @@ public sealed class ChatTests
Assert.Null(HearSpeech.TryParse(body));
}
[Fact]
public void HearSpeech_TryParse_PreservesWindows1252_RoundTrip()
{
// Phase I.5: ASCII would munge non-ASCII bytes into '?'. CP1252
// round-trips them. The high-byte 0xE9 = 'é' in Latin-1/CP1252.
byte[] msg = PackString16L("Café");
byte[] sender = PackString16L("Élise");
byte[] inbound = new byte[4 + msg.Length + sender.Length + 8];
int pos = 0;
BinaryPrimitives.WriteUInt32LittleEndian(inbound, HearSpeech.LocalOpcode);
pos += 4;
Array.Copy(msg, 0, inbound, pos, msg.Length); pos += msg.Length;
Array.Copy(sender, 0, inbound, pos, sender.Length); pos += sender.Length;
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0u); pos += 4;
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0u);
var parsed = HearSpeech.TryParse(inbound);
Assert.NotNull(parsed);
Assert.Equal("Café", parsed!.Value.Text);
Assert.Equal("Élise", parsed.Value.SenderName);
}
private static byte[] PackString16L(string s)
{
byte[] data = Encoding.ASCII.GetBytes(s);
// Test helper now uses CP1252 to match the production codec.
byte[] data = Encoding.GetEncoding(1252).GetBytes(s);
int recordSize = 2 + data.Length;
int padding = (4 - (recordSize & 3)) & 3;
byte[] result = new byte[recordSize + padding];

View file

@ -0,0 +1,80 @@
using System;
using System.Buffers.Binary;
using System.Text;
using AcDream.Core.Net.Messages;
using Xunit;
namespace AcDream.Core.Net.Tests.Messages;
/// <summary>
/// Phase I.5: 0x01E0 EmoteText parser. Wire layout matches holtburger's
/// EmoteTextData: u32 senderGuid + string16L senderName + string16L text.
/// </summary>
public sealed class EmoteTextTests
{
[Fact]
public void TryParse_RoundTrips_GuidAndStrings()
{
byte[] sender = PackString16L("Caith");
byte[] text = PackString16L("waves at you");
byte[] body = new byte[4 + 4 + sender.Length + text.Length];
int pos = 0;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), EmoteText.Opcode);
pos += 4;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), 0xDEADBEEFu);
pos += 4;
Array.Copy(sender, 0, body, pos, sender.Length); pos += sender.Length;
Array.Copy(text, 0, body, pos, text.Length);
var parsed = EmoteText.TryParse(body);
Assert.NotNull(parsed);
Assert.Equal(0xDEADBEEFu, parsed!.Value.SenderGuid);
Assert.Equal("Caith", parsed.Value.SenderName);
Assert.Equal("waves at you", parsed.Value.Text);
}
[Fact]
public void TryParse_WrongOpcode_ReturnsNull()
{
byte[] body = new byte[8];
BinaryPrimitives.WriteUInt32LittleEndian(body, 0x12345678u);
Assert.Null(EmoteText.TryParse(body));
}
[Fact]
public void TryParse_TruncatedHeader_ReturnsNull()
{
byte[] body = new byte[6];
BinaryPrimitives.WriteUInt32LittleEndian(body, EmoteText.Opcode);
Assert.Null(EmoteText.TryParse(body));
}
[Fact]
public void TryParse_PreservesWindows1252_RoundTrip()
{
byte[] sender = PackString16L("Élise");
byte[] text = PackString16L("waves at you, Café");
byte[] body = new byte[4 + 4 + sender.Length + text.Length];
int pos = 0;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), EmoteText.Opcode); pos += 4;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), 1u); pos += 4;
Array.Copy(sender, 0, body, pos, sender.Length); pos += sender.Length;
Array.Copy(text, 0, body, pos, text.Length);
var parsed = EmoteText.TryParse(body);
Assert.NotNull(parsed);
Assert.Equal("Élise", parsed!.Value.SenderName);
Assert.Equal("waves at you, Café", parsed.Value.Text);
}
private static byte[] PackString16L(string s)
{
byte[] data = Encoding.GetEncoding(1252).GetBytes(s);
int recordSize = 2 + data.Length;
int padding = (4 - (recordSize & 3)) & 3;
byte[] result = new byte[recordSize + padding];
BinaryPrimitives.WriteUInt16LittleEndian(result, (ushort)data.Length);
Array.Copy(data, 0, result, 2, data.Length);
return result;
}
}

View file

@ -0,0 +1,65 @@
using System;
using System.Buffers.Binary;
using System.Text;
using AcDream.Core.Net.Messages;
using Xunit;
namespace AcDream.Core.Net.Tests.Messages;
/// <summary>
/// Phase I.5: 0x019E PlayerKilled parser. Wire layout per holtburger
/// PlayerKilledData: string16L deathMessage + u32 victimGuid + u32
/// killerGuid.
/// </summary>
public sealed class PlayerKilledTests
{
[Fact]
public void TryParse_RoundTrips_AllThreeFields()
{
// Synthetic payload mirroring holtburger's test_player_killed_fixture
// (combat/types.rs lines 100-115). Death message = "Test", victim =
// 0x12345678, killer = 0x90ABCDEF.
byte[] msg = PackString16L("Test");
byte[] body = new byte[4 + msg.Length + 8];
int pos = 0;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), PlayerKilled.Opcode); pos += 4;
Array.Copy(msg, 0, body, pos, msg.Length); pos += msg.Length;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), 0x12345678u); pos += 4;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), 0x90ABCDEFu);
var parsed = PlayerKilled.TryParse(body);
Assert.NotNull(parsed);
Assert.Equal("Test", parsed!.Value.DeathMessage);
Assert.Equal(0x12345678u, parsed.Value.VictimGuid);
Assert.Equal(0x90ABCDEFu, parsed.Value.KillerGuid);
}
[Fact]
public void TryParse_WrongOpcode_ReturnsNull()
{
byte[] body = new byte[16];
BinaryPrimitives.WriteUInt32LittleEndian(body, 0xDEADBEEFu);
Assert.Null(PlayerKilled.TryParse(body));
}
[Fact]
public void TryParse_TruncatedAfterMessage_ReturnsNull()
{
byte[] msg = PackString16L("died");
byte[] body = new byte[4 + msg.Length + 4]; // only one guid present
BinaryPrimitives.WriteUInt32LittleEndian(body, PlayerKilled.Opcode);
Array.Copy(msg, 0, body, 4, msg.Length);
Assert.Null(PlayerKilled.TryParse(body));
}
private static byte[] PackString16L(string s)
{
byte[] data = Encoding.GetEncoding(1252).GetBytes(s);
int recordSize = 2 + data.Length;
int padding = (4 - (recordSize & 3)) & 3;
byte[] result = new byte[recordSize + padding];
BinaryPrimitives.WriteUInt16LittleEndian(result, (ushort)data.Length);
Array.Copy(data, 0, result, 2, data.Length);
return result;
}
}

View file

@ -0,0 +1,58 @@
using System;
using System.Buffers.Binary;
using System.Text;
using AcDream.Core.Net.Messages;
using Xunit;
namespace AcDream.Core.Net.Tests.Messages;
/// <summary>
/// Phase I.5: 0xF7E0 ServerMessage parser. Wire layout per holtburger
/// ServerMessageData: string16L message + u32 chatType.
/// </summary>
public sealed class ServerMessageTests
{
[Fact]
public void TryParse_RoundTrips_MessageAndChatType()
{
byte[] msg = PackString16L("The server will reset in 5 minutes.");
byte[] body = new byte[4 + msg.Length + 4];
BinaryPrimitives.WriteUInt32LittleEndian(body, ServerMessage.Opcode);
Array.Copy(msg, 0, body, 4, msg.Length);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(4 + msg.Length), 5u); // System
var parsed = ServerMessage.TryParse(body);
Assert.NotNull(parsed);
Assert.Equal("The server will reset in 5 minutes.", parsed!.Value.Message);
Assert.Equal(5u, parsed.Value.ChatType);
}
[Fact]
public void TryParse_WrongOpcode_ReturnsNull()
{
byte[] body = new byte[8];
BinaryPrimitives.WriteUInt32LittleEndian(body, 0xDEADBEEFu);
Assert.Null(ServerMessage.TryParse(body));
}
[Fact]
public void TryParse_TruncatedChatType_ReturnsNull()
{
byte[] msg = PackString16L("hi");
byte[] body = new byte[4 + msg.Length + 2]; // 2 bytes short
BinaryPrimitives.WriteUInt32LittleEndian(body, ServerMessage.Opcode);
Array.Copy(msg, 0, body, 4, msg.Length);
Assert.Null(ServerMessage.TryParse(body));
}
private static byte[] PackString16L(string s)
{
byte[] data = Encoding.GetEncoding(1252).GetBytes(s);
int recordSize = 2 + data.Length;
int padding = (4 - (recordSize & 3)) & 3;
byte[] result = new byte[recordSize + padding];
BinaryPrimitives.WriteUInt16LittleEndian(result, (ushort)data.Length);
Array.Copy(data, 0, result, 2, data.Length);
return result;
}
}

View file

@ -0,0 +1,53 @@
using System;
using System.Buffers.Binary;
using System.Text;
using AcDream.Core.Net.Messages;
using Xunit;
namespace AcDream.Core.Net.Tests.Messages;
/// <summary>
/// Phase I.5: 0x01E2 SoulEmote parser. Wire layout is identical to
/// EmoteText (u32 senderGuid + string16L senderName + string16L text)
/// per holtburger SoulEmoteData.
/// </summary>
public sealed class SoulEmoteTests
{
[Fact]
public void TryParse_RoundTrips_GuidAndStrings()
{
byte[] sender = PackString16L("Caith");
byte[] text = PackString16L("dances");
byte[] body = new byte[4 + 4 + sender.Length + text.Length];
int pos = 0;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), SoulEmote.Opcode); pos += 4;
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(pos), 0xCAFEF00Du); pos += 4;
Array.Copy(sender, 0, body, pos, sender.Length); pos += sender.Length;
Array.Copy(text, 0, body, pos, text.Length);
var parsed = SoulEmote.TryParse(body);
Assert.NotNull(parsed);
Assert.Equal(0xCAFEF00Du, parsed!.Value.SenderGuid);
Assert.Equal("Caith", parsed.Value.SenderName);
Assert.Equal("dances", parsed.Value.Text);
}
[Fact]
public void TryParse_WrongOpcode_ReturnsNull()
{
byte[] body = new byte[8];
BinaryPrimitives.WriteUInt32LittleEndian(body, 0xBADBEEFu);
Assert.Null(SoulEmote.TryParse(body));
}
private static byte[] PackString16L(string s)
{
byte[] data = Encoding.GetEncoding(1252).GetBytes(s);
int recordSize = 2 + data.Length;
int padding = (4 - (recordSize & 3)) & 3;
byte[] result = new byte[recordSize + padding];
BinaryPrimitives.WriteUInt16LittleEndian(result, (ushort)data.Length);
Array.Copy(data, 0, result, 2, data.Length);
return result;
}
}

View file

@ -90,4 +90,86 @@ public sealed class ChatLogTests
log.Clear();
Assert.Equal(0, log.Count);
}
// ── Phase I.5: emote / soul-emote / killed / weenie-error adapters ──
[Fact]
public void OnEmote_AppendsEmoteEntry()
{
var log = new ChatLog();
log.OnEmote("Caith", "waves at you", 0xCAFE);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.Emote, e.Kind);
Assert.Equal("Caith", e.Sender);
Assert.Equal("waves at you", e.Text);
Assert.Equal(0xCAFEu, e.SenderGuid);
}
[Fact]
public void OnSoulEmote_AppendsSoulEmoteEntry()
{
var log = new ChatLog();
log.OnSoulEmote("Bob", "dances", 0xBEEF);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.SoulEmote, e.Kind);
Assert.Equal("Bob", e.Sender);
Assert.Equal("dances", e.Text);
Assert.Equal(0xBEEFu, e.SenderGuid);
}
[Fact]
public void OnPlayerKilled_AppendsSystemEntry_StoresGuids()
{
var log = new ChatLog();
log.OnPlayerKilled("Caith was killed by a Drudge.",
victimGuid: 0x12345678u, killerGuid: 0x90ABCDEFu);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Equal("Caith was killed by a Drudge.", e.Text);
Assert.Equal(0x12345678u, e.SenderGuid);
Assert.Equal(0x90ABCDEFu, e.ChannelId); // killer guid stashed here
}
[Fact]
public void OnWeenieError_PlainCode_AppendsSystemEntry()
{
var log = new ChatLog();
log.OnWeenieError(errorId: 0x1234, param: null);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Contains("0x1234", e.Text);
Assert.Equal(0x1234u, e.ChannelId);
}
[Fact]
public void OnWeenieError_WithString_AppendsInterpolation()
{
var log = new ChatLog();
log.OnWeenieError(errorId: 0x5678, param: "Mana Stone");
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Contains("Mana Stone", e.Text);
}
[Fact]
public void OnLocalSpeech_EmptySender_SubstitutesYou()
{
// Holtburger client/messages.rs lines 476-487 — empty sender
// means the player is the speaker (echo back of their own
// ranged shout). Substitute "You" so the chat line reads
// "You: hello" instead of ": hello".
var log = new ChatLog();
log.OnLocalSpeech(sender: "", text: "hello", senderGuid: 0, isRanged: false);
var e = log.Snapshot()[0];
Assert.Equal("You", e.Sender);
Assert.Equal("hello", e.Text);
}
[Fact]
public void OnLocalSpeech_NonEmptySender_KeepsAsIs()
{
var log = new ChatLog();
log.OnLocalSpeech(sender: "Alice", text: "hi", senderGuid: 0xAA, isRanged: false);
Assert.Equal("Alice", log.Snapshot()[0].Sender);
}
}