feat(chat): Phase H.1 Talk/Tell/ChatChannel + HearSpeech + ChatLog

Completes the chat-wire layer end-to-end: outbound Talk (/say), Tell
(/tell), ChatChannel, + inbound HearSpeech (0x02BB) / HearRangedSpeech
(0x02BC) routed into a unified ChatLog that also consumes the already-
parsed GameEvent ChannelBroadcast / Tell / TransientMessage / Popup.

Wire layer (AcDream.Core.Net/Messages):
- ChatRequests.BuildTalk (0x0015, inside 0xF7B1): gameActionSequence
  + string16L message. PackString16L helper with 4-byte pad.
- ChatRequests.BuildTell (0x005D): targetName + message, each
  string16L with its own padding.
- ChatRequests.BuildChatChannel (0x0147): channelId + message.
- HearSpeech.TryParse handles BOTH 0x02BB local AND 0x02BC ranged —
  single parser with IsRanged flag in the returned record. Standalone
  GameMessage (NOT wrapped in 0xF7B0).

WorldSession integration:
- ProcessDatagram branch for HearSpeech.LocalOpcode /
  HearSpeech.RangedOpcode; fires new SpeechHeard event.
- Places the new branch before the 0xF7B0 GameEvent branch so ordering
  stays stable.

Core layer (AcDream.Core/Chat):
- ChatEntry record: (Kind, Sender, Text, SenderGuid, ChannelId, Received).
- ChatKind enum: LocalSpeech, RangedSpeech, Channel, Tell, System, Popup.
- ChatLog: ring-buffer (default 500) of entries; adapters for every
  inbound source (OnLocalSpeech, OnChannelBroadcast, OnTellReceived,
  OnSystemMessage, OnPopup) plus OnSelfSent for echoing outbound.
  Fires EntryAppended so UI panel can scroll / highlight.

Tests (15 new):
- ChatRequests: Talk / Tell / ChatChannel byte-exact encoding (including
  string16L padding edge cases).
- HearSpeech: local + ranged round-trip, wrong-opcode returns null.
- ChatLog: local / ranged / channel / tell / system / self echo,
  ring-buffer drops oldest, Clear empties.

Build green, 570 tests pass (up from 555).

With the chat wire layer in place, Phase H.1's "chat window panel"
(UI slice 05) is purely a UI task: instantiate ChatLog, bind to
EntryAppended, feed rows into the retail-UI widget toolkit. No more
protocol gaps.

Ref: r08 §3 (opcodes 0x0015, 0x005D, 0x0147), §2 (0x02BB, 0x02BC).
Ref: ACE GameMessageHearSpeech.cs + GameActionChannelBroadcast.cs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-18 17:03:45 +02:00
parent c95aedcd4a
commit 404cab55ba
6 changed files with 568 additions and 0 deletions

View file

@ -0,0 +1,133 @@
using System;
using System.Buffers.Binary;
using System.Text;
using AcDream.Core.Net.Messages;
using Xunit;
namespace AcDream.Core.Net.Tests.Messages;
public sealed class ChatTests
{
[Fact]
public void BuildTalk_EmitsOpcodeAndString16L()
{
byte[] body = ChatRequests.BuildTalk(gameActionSequence: 3, message: "hi");
Assert.Equal(ChatRequests.TalkOpcode,
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8)));
// Verify the string16L starts at offset 12.
ushort len = BinaryPrimitives.ReadUInt16LittleEndian(body.AsSpan(12));
Assert.Equal(2, len);
Assert.Equal("hi", Encoding.ASCII.GetString(body.AsSpan(14, 2)));
// Record size = 2+2 = 4, no padding needed.
Assert.Equal(16, body.Length);
}
[Fact]
public void BuildTalk_EmitsPadding_WhenMessageLengthRequiresIt()
{
byte[] body = ChatRequests.BuildTalk(gameActionSequence: 3, message: "h");
// 2+1=3 bytes record → pad 1 byte.
// Total body = 12 (envelope) + 4 (str16L aligned) = 16.
Assert.Equal(16, body.Length);
}
[Fact]
public void BuildTell_IncludesBothStrings()
{
byte[] body = ChatRequests.BuildTell(
gameActionSequence: 5, targetName: "Alice", message: "hey");
Assert.Equal(ChatRequests.TellOpcode,
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8)));
int pos = 12;
ushort len1 = BinaryPrimitives.ReadUInt16LittleEndian(body.AsSpan(pos));
Assert.Equal(5, len1);
Assert.Equal("Alice", Encoding.ASCII.GetString(body.AsSpan(pos + 2, 5)));
// "Alice" record = 2+5=7, pad 1 → advance by 8.
pos += 8;
ushort len2 = BinaryPrimitives.ReadUInt16LittleEndian(body.AsSpan(pos));
Assert.Equal(3, len2);
Assert.Equal("hey", Encoding.ASCII.GetString(body.AsSpan(pos + 2, 3)));
}
[Fact]
public void BuildChatChannel_IncludesChannelId()
{
byte[] body = ChatRequests.BuildChatChannel(
gameActionSequence: 1, channelId: 42, message: "tell me the good dungeons");
Assert.Equal(ChatRequests.ChatChannelOpcode,
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8)));
Assert.Equal(42u,
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(12)));
}
[Fact]
public void HearSpeech_TryParse_LocalRoundTrip()
{
// Build a 0x02BB message and re-parse it.
byte[] talkBody = ChatRequests.BuildTalk(gameActionSequence: 0, message: "hello");
// Now synthesize the inbound HearSpeech format.
byte[] msg = PackString16L("hello");
byte[] sender = PackString16L("Alice");
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), 0xCAFEu); pos += 4;
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0x0B); pos += 4; // Speech
var parsed = HearSpeech.TryParse(inbound);
Assert.NotNull(parsed);
Assert.Equal("hello", parsed!.Value.Text);
Assert.Equal("Alice", parsed.Value.SenderName);
Assert.Equal(0xCAFEu, parsed.Value.SenderGuid);
Assert.Equal(0x0Bu, parsed.Value.ChatType);
Assert.False(parsed.Value.IsRanged);
}
[Fact]
public void HearSpeech_TryParse_RangedFlag()
{
byte[] msg = PackString16L("X");
byte[] sender = PackString16L("Y");
byte[] inbound = new byte[4 + msg.Length + sender.Length + 8];
BinaryPrimitives.WriteUInt32LittleEndian(inbound, HearSpeech.RangedOpcode);
int 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), 0); pos += 4;
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0); pos += 4;
var parsed = HearSpeech.TryParse(inbound);
Assert.NotNull(parsed);
Assert.True(parsed!.Value.IsRanged);
}
[Fact]
public void HearSpeech_TryParse_WrongOpcode_ReturnsNull()
{
byte[] body = new byte[16];
BinaryPrimitives.WriteUInt32LittleEndian(body, 0xDEADBEEFu);
Assert.Null(HearSpeech.TryParse(body));
}
private static byte[] PackString16L(string s)
{
byte[] data = Encoding.ASCII.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,93 @@
using AcDream.Core.Chat;
using Xunit;
namespace AcDream.Core.Tests.Chat;
public sealed class ChatLogTests
{
[Fact]
public void OnLocalSpeech_AppendsEntry_FiresEvent()
{
var log = new ChatLog();
ChatEntry? seen = null;
log.EntryAppended += e => seen = e;
log.OnLocalSpeech("Alice", "hi", 0xAA, isRanged: false);
Assert.Equal(1, log.Count);
Assert.NotNull(seen);
Assert.Equal(ChatKind.LocalSpeech, seen!.Value.Kind);
Assert.Equal("Alice", seen.Value.Sender);
Assert.Equal("hi", seen.Value.Text);
}
[Fact]
public void OnLocalSpeech_Ranged_SetsRangedKind()
{
var log = new ChatLog();
log.OnLocalSpeech("Bob", "SHOUT", 0xBB, isRanged: true);
Assert.Equal(ChatKind.RangedSpeech, log.Snapshot()[0].Kind);
}
[Fact]
public void OnChannelBroadcast_SetsChannelId()
{
var log = new ChatLog();
log.OnChannelBroadcast(channelId: 42, sender: "Alice", text: "allegiance motd");
var e = log.Snapshot()[0];
Assert.Equal(42u, e.ChannelId);
Assert.Equal(ChatKind.Channel, e.Kind);
}
[Fact]
public void OnTellReceived_SetsTellKind()
{
var log = new ChatLog();
log.OnTellReceived("Alice", "psst", 0xAA);
Assert.Equal(ChatKind.Tell, log.Snapshot()[0].Kind);
}
[Fact]
public void OnSystemMessage_EncodesChatType_AsChannelId()
{
var log = new ChatLog();
log.OnSystemMessage("Your spell fizzled!", chatType: 5);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Equal(5u, e.ChannelId);
}
[Fact]
public void OnSelfSent_EchoesOutbound()
{
var log = new ChatLog();
log.OnSelfSent(ChatKind.Tell, "hey", targetOrChannel: "Alice");
var e = log.Snapshot()[0];
Assert.Equal("Alice", e.Sender);
Assert.Equal("hey", e.Text);
}
[Fact]
public void RingBuffer_DropsOldestBeyondCapacity()
{
var log = new ChatLog(maxEntries: 3);
log.OnLocalSpeech("A", "1", 0, false);
log.OnLocalSpeech("B", "2", 0, false);
log.OnLocalSpeech("C", "3", 0, false);
log.OnLocalSpeech("D", "4", 0, false);
var snap = log.Snapshot();
Assert.Equal(3, snap.Length);
Assert.Equal("2", snap[0].Text); // "1" was dropped
Assert.Equal("4", snap[2].Text);
}
[Fact]
public void Clear_EmptiesBuffer()
{
var log = new ChatLog();
log.OnLocalSpeech("A", "1", 0, false);
log.Clear();
Assert.Equal(0, log.Count);
}
}