feat(chat): CT-A2 — parse retail's inline chat tag markup

Campaign CT slice A2. Pure parser, no UI, nothing wired yet.

Retail's client composes chat lines with the markup already embedded —
Handle_Communication__HearSpeech @0x005712A0 sprintf's it — and the text
element recognises it while appending (UIElement_Text::InqGlyphs @0x00468EA0),
calling TextTagFactory::MakeTag @0x00478480 per marker. A tagged speaker name
arrives as:

    <Tell:IIDString:1342177290:Dww>Dww<\Tell> tells you, "hello"

ChatTagMarkup.Parse splits that into spans, consuming the markers: the name
under a tag, the remainder untagged.

The rule that decides where a tag ENDS is the one worth being careful about.
It is the absence of a colon, not the backslash: MakeTag requires a ':' to
succeed, so ANY bracketed text it cannot parse closes the open tag, and the
backslash in retail's own closer (TextTag::BuildEndTag @0x00479190) is
incidental to that. Porting "a closer starts with a backslash" would look
correct on every retail line and then diverge on everything else, so the test
pins all three of <\Tell>, <Tell> and <anything> as closers.

Two details taken from the decomp rather than guessed: only the FIRST colon of
an IIDString payload separates the id from the name, so a name containing a
colon survives intact (ParseStartTag @0x00478910); and an unterminated '<' is
ordinary text, so a player typing "is 3 < 4 really" does not lose the rest of
their sentence.

The parse also upholds the contract CT-A1's draw side enforces — the
concatenated span text always reproduces the visible line, because selection
and hit-testing index into that flat string.

Solution builds clean; full hermetic gate green.

Note for the record: PreparedAssetVerificationCacheTests.BackupRecoveryHashes-
TheBackupEvenWhenTheLiveCacheIsValid failed once during this slice's gate and
then passed isolated, as a class, and on a full-gate rerun. This branch touches
no launcher code, so it is load-sensitive rather than caused here — flagging it
rather than silently re-running, since a test that only fails under parallel
load is worth someone classifying.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-08-21 07:23:07 +02:00
parent cf41b27c9a
commit 2621fbf4a7
2 changed files with 265 additions and 0 deletions

View file

@ -0,0 +1,147 @@
namespace AcDream.Core.Chat;
/// <summary>
/// One retail text tag: the <c>TYPE</c>, <c>FORMAT</c> and <c>DATA</c> of a
/// <c>&lt;TYPE:FORMAT:DATA&gt;</c> marker.
/// </summary>
/// <param name="Type">
/// The tag kind, e.g. <c>Tell</c>. Retail resolves this through the DAT
/// EnumMapper (category <c>0x18</c>) to the numeric type its colour rule keys
/// on; only <c>Tell</c> is confirmed in the build we target.
/// </param>
/// <param name="Format">
/// The payload shape, e.g. <c>IIDString</c>. Retail has four
/// (<c>DID</c>, <c>IID</c>, <c>IIDEnum</c>, <c>IIDString</c>) but only
/// <c>IIDString</c> has a listener.
/// </param>
/// <param name="Data">Everything after the second colon, unparsed.</param>
public readonly record struct ChatTextTag(string Type, string Format, string Data)
{
/// <summary>
/// The <c>IIDString</c> payload: an object id and a name. Returns
/// <see langword="false"/> for any other format, or a malformed payload.
/// </summary>
/// <remarks>
/// The name may itself contain colons, so only the FIRST colon separates
/// the id from the name — retail's
/// <c>TextTag_IIDString::ParseStartTag @0x00478910</c> reads the id then
/// takes the remainder verbatim.
/// </remarks>
public bool TryGetIidString(out uint objectId, out string name)
{
objectId = 0;
name = string.Empty;
if (!string.Equals(Format, "IIDString", StringComparison.Ordinal))
return false;
int split = Data.IndexOf(':');
if (split <= 0 || split == Data.Length - 1)
return false;
if (!uint.TryParse(
Data.AsSpan(0, split),
System.Globalization.NumberStyles.Integer,
System.Globalization.CultureInfo.InvariantCulture,
out objectId))
{
return false;
}
name = Data[(split + 1)..];
return true;
}
}
/// <summary>One stretch of chat text, and the tag covering it (if any).</summary>
public readonly record struct ChatTextSpan(string Text, ChatTextTag? Tag);
/// <summary>
/// Parses retail's inline chat tag markup into spans.
/// </summary>
/// <remarks>
/// <para>
/// Retail's client composes chat lines with the markup already embedded —
/// <c>ClientCommunicationSystem::Handle_Communication__HearSpeech
/// @0x005712A0</c> sprintf's it — and the text element recognises it while
/// appending (<c>UIElement_Text::InqGlyphs @0x00468EA0</c>), calling
/// <c>TextTagFactory::MakeTag @0x00478480</c> per marker. A tagged speaker
/// name arrives looking like:
/// </para>
/// <code>
/// &lt;Tell:IIDString:1342177290:Dww&gt;Dww&lt;\Tell&gt; tells you, "hello"
/// </code>
/// <para>
/// <b>What closes a tag is the absence of a colon, not the backslash.</b>
/// <c>MakeTag</c> requires a <c>:</c> to succeed, so ANY bracketed text it
/// cannot parse closes the open tag — the backslash in retail's own closer
/// (<c>TextTag::BuildEndTag @0x00479190</c>) is incidental to that rule, not
/// the mechanism. Porting "a closer starts with a backslash" would look right
/// on every retail line and then diverge on everything else.
/// </para>
/// </remarks>
public static class ChatTagMarkup
{
/// <summary>
/// Splits <paramref name="text"/> into spans, consuming the markers.
/// </summary>
/// <remarks>
/// An unterminated <c>&lt;</c> (no closing <c>&gt;</c> anywhere after it)
/// is ordinary text — there is no marker to consume.
/// </remarks>
public static IReadOnlyList<ChatTextSpan> Parse(string? text)
{
if (string.IsNullOrEmpty(text))
return Array.Empty<ChatTextSpan>();
if (text.IndexOf('<') < 0)
return new[] { new ChatTextSpan(text, null) };
var spans = new List<ChatTextSpan>();
ChatTextTag? open = null;
int runStart = 0;
for (int i = 0; i < text.Length; i++)
{
if (text[i] != '<')
continue;
int close = text.IndexOf('>', i + 1);
if (close < 0)
break; // unterminated: the rest is plain text
// Flush the text before this marker under whatever tag was open.
if (i > runStart)
spans.Add(new ChatTextSpan(text[runStart..i], open));
open = TryParseStartTag(text[(i + 1)..close]);
runStart = close + 1;
i = close;
}
if (runStart < text.Length)
spans.Add(new ChatTextSpan(text[runStart..], open));
return spans;
}
/// <summary>
/// The marker's tag, or <see langword="null"/> if it does not parse as one
/// — which is retail's close.
/// </summary>
private static ChatTextTag? TryParseStartTag(string inner)
{
int first = inner.IndexOf(':');
if (first <= 0 || first == inner.Length - 1)
return null;
int second = inner.IndexOf(':', first + 1);
if (second < 0)
{
// TYPE:FORMAT with no payload. Still a tag — the data is empty.
return new ChatTextTag(inner[..first], inner[(first + 1)..], string.Empty);
}
return new ChatTextTag(
inner[..first],
inner[(first + 1)..second],
inner[(second + 1)..]);
}
}