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,99 @@
using System;
using System.Buffers.Binary;
using System.Text;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Outbound chat GameActions — Talk (local /say), Tell (whisper),
/// ChatChannel (allegiance / general).
///
/// <para>
/// All three ride inside the <c>0xF7B1</c> GameAction envelope with
/// the same leading fields:
/// <code>
/// u32 0xF7B1
/// u32 gameActionSequence
/// u32 subOpcode
/// &lt;payload&gt;
/// </code>
/// </para>
///
/// <para>
/// String16L wire shape mirrors the other outbound AC messages:
/// <code>
/// u16 length // byte count (not char count)
/// byte[] ascii // ASCII bytes, no terminator
/// pad to 4-byte boundary
/// </code>
/// </para>
///
/// <para>
/// Source of truth: r08 §3 rows 0x0015 / 0x005D / 0x0147.
/// </para>
/// </summary>
public static class ChatRequests
{
public const uint GameActionEnvelope = 0xF7B1u;
public const uint TalkOpcode = 0x0015u;
public const uint TellOpcode = 0x005Du;
public const uint ChatChannelOpcode = 0x0147u;
/// <summary>Send a local /say message (heard by anyone within ~20m).</summary>
public static byte[] BuildTalk(uint gameActionSequence, string message)
{
ArgumentNullException.ThrowIfNull(message);
byte[] msg = PackString16L(message);
byte[] body = new byte[12 + msg.Length];
BinaryPrimitives.WriteUInt32LittleEndian(body, GameActionEnvelope);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(4), gameActionSequence);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(8), TalkOpcode);
Array.Copy(msg, 0, body, 12, msg.Length);
return body;
}
/// <summary>Send a /tell (whisper) by target character name.</summary>
public static byte[] BuildTell(uint gameActionSequence, string targetName, string message)
{
ArgumentNullException.ThrowIfNull(targetName);
ArgumentNullException.ThrowIfNull(message);
byte[] name = PackString16L(targetName);
byte[] msg = PackString16L(message);
byte[] body = new byte[12 + name.Length + msg.Length];
BinaryPrimitives.WriteUInt32LittleEndian(body, GameActionEnvelope);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(4), gameActionSequence);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(8), TellOpcode);
Array.Copy(name, 0, body, 12, name.Length);
Array.Copy(msg, 0, body, 12 + name.Length, msg.Length);
return body;
}
/// <summary>Send to a chat channel (allegiance, general, trade, etc).</summary>
public static byte[] BuildChatChannel(uint gameActionSequence, uint channelId, string message)
{
ArgumentNullException.ThrowIfNull(message);
byte[] msg = PackString16L(message);
byte[] body = new byte[16 + msg.Length];
BinaryPrimitives.WriteUInt32LittleEndian(body, GameActionEnvelope);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(4), gameActionSequence);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(8), ChatChannelOpcode);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(12), channelId);
Array.Copy(msg, 0, body, 16, msg.Length);
return body;
}
private static byte[] PackString16L(string s)
{
byte[] data = Encoding.ASCII.GetBytes(s);
if (data.Length > ushort.MaxValue)
throw new ArgumentException("String too long for 16-bit length prefix.", nameof(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);
// trailing bytes are already zero from new[]
return result;
}
}

View file

@ -0,0 +1,85 @@
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");
string result = Encoding.ASCII.GetString(source.Slice(pos, length));
pos += length;
int recordSize = 2 + length;
int padding = (4 - (recordSize & 3)) & 3;
pos += padding;
return result;
}
}

View file

@ -102,6 +102,13 @@ public sealed class WorldSession : IDisposable
/// </summary>
public event Action<uint>? TeleportStarted;
/// <summary>
/// Phase H.1: fires when a local or ranged speech message (0x02BB /
/// 0x02BC) is received. Subscribers typically feed these into a
/// <c>ChatLog</c>.
/// </summary>
public event Action<HearSpeech.Parsed>? SpeechHeard;
/// <summary>
/// Allow re-sending LoginComplete after a portal teleport. The normal
/// _loginCompleteSent latch prevents duplicate sends on the initial spawn
@ -487,6 +494,15 @@ public sealed class WorldSession : IDisposable
posUpdate.Value.Velocity));
}
}
else if (op == HearSpeech.LocalOpcode || op == HearSpeech.RangedOpcode)
{
// Phase H.1: local/ranged chat. Standalone GameMessage
// (NOT wrapped in 0xF7B0). Payload layout is documented
// on HearSpeech.TryParse.
var parsed = HearSpeech.TryParse(body);
if (parsed is not null)
SpeechHeard?.Invoke(parsed.Value);
}
else if (op == GameEventEnvelope.Opcode)
{
// Phase F.1: 0xF7B0 is the GameEvent envelope. Parse the