acdream/src/AcDream.Runtime/Gameplay/TurbineChatMembershipGate.cs
Erik e07fba5731 fix(chat): CH3 review fixes — phantom UN-9, allegiance-broadcast echo, /a legacy fallback
Applies the Opus review of Campaign CH slice CH3 (614a1e05):

- B1: UN-9 was a phantom divergence — ACE's CharacterOptions1.cs:47
  OR-sum is 0x50C4A54A (its own comment confirms 1355064650), identical
  to acdream's literal. The wrong 0x50C48D4A existed only in the research
  doc. Row deleted, register §5 reverted to 4 rows, research doc corrected
  with dated notes.
- S1/S4: AllegianceBroadcast (0x02000000) is a server-echoing channel —
  ACE's GameActionChatChannel handler includes the sender in its real-name
  Allegiance.Members broadcast (retail's DoAllegianceBroadcast has no
  AddTextToScroll), so the client must skip its local optimistic echo, not
  keep it. ChatChannelInfo.Legacy.IsSelfEchoChannel() now returns true for
  it; RouteLegacyChannel's comment corrected; Turbine.IsSelfEchoChannel()'s
  backwards comment rewritten truthfully.
- S3: retail's /a stays on the legacy AllegianceBroadcast bitflag until
  StartupTurbineChatSystem successfully starts Turbine chat — "never
  started" (TurbineChatState.Enabled == false) now falls back to legacy in
  both LiveSessionCommandRouter.RouteChat and
  DirectGameRuntimeCommandAdapter.TrySendChannel, while "enabled but no
  allegiance room" still correctly refuses locally.
- S5: added a LiveSessionEventRouter test proving the Options.Replace ->
  OnCharacterOptionsChanged seeding order, and RuntimeSettingsTargets /
  GameWindowLiveSessionOwnershipTests tests proving the concrete
  ICommandBus.Publish wiring and the single LiveSessionCommandSurface
  construction site.
- S6: AP-181 rewritten to name both of retail's omitted pre-send checks
  (IsMessageSafe silent-drop, then IsMessageSpam) and stop misattributing
  either to RouteLegacyChannel, which has no such gates.
- N1-N7: CharacterOptionId moved below SocialActions so its doc comment
  re-attaches; TurbineChatMembershipGate reuses TurbineChatDisplayNames
  instead of a duplicate table; the gate-to-refusal-text mapping is now
  shared via TurbineChatMembershipGate.ResolveRefusalText instead of
  duplicated in both hosts; ChatSettings.Default now matches ACE's real
  CharacterOptions2.Default (Roleplay/Society start off); a doc-comment
  clarifies only the five Hear toggles are server-backed; the register's
  §3 header recounted 129 -> 128.

Suite: 11,964 passed / 4 skipped / 0 failed (baseline 11,957/4/0 + 7 new
tests). Campaign ledger CH3 review column updated to APPROVE-WITH-FIXES.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-09 20:24:29 +02:00

169 lines
6.9 KiB
C#

using AcDream.Core.Chat;
using AcDream.Core.Net.Messages;
using AcDream.Runtime.Session;
namespace AcDream.Runtime.Gameplay;
/// <summary>
/// Outcome of <see cref="TurbineChatMembershipGate.Evaluate"/>.
/// </summary>
public enum TurbineChatGateStatus
{
/// <summary>The send may proceed to the wire.</summary>
Allowed,
/// <summary>
/// Turbine chat is disabled, or this room's id is still 0 (not
/// populated by <c>SetTurbineChatChannels</c> — e.g. no allegiance, no
/// society). Retail: <c>"Turbine chat is not available."</c>
/// (<see cref="ClientTextRefusals.TurbineChatUnavailable"/>), raised
/// locally with NOTHING sent.
/// </summary>
Unavailable,
/// <summary>
/// The room exists and Turbine chat is enabled, but the player's own
/// <c>Hear*Chat</c> option (or, for Olthoi, heritage) is off. Retail:
/// <c>WeenieErrorWithString 0x0551 YouAreNotListeningTo_Channel</c>,
/// raised locally with NOTHING sent.
/// </summary>
NotListening,
}
/// <summary>One evaluated gate decision, carrying the room/chatType/display
/// name a caller needs whether the send proceeds or gets refused.</summary>
public readonly record struct TurbineChatGateResult(
TurbineChatGateStatus Status,
uint RoomId,
uint ChatType,
string DisplayName);
/// <summary>
/// Retail's local pre-send membership gate for a Turbine-room chat channel,
/// ported from <c>ClientCommunicationSystem::SendTurbineChat @0x0057db10</c>
/// (Sept 2013 EoR build; research doc
/// <c>docs/research/2026-08-09-chat-side-channels-vs-ace.md</c> §4.2/§6.2).
///
/// <para>
/// Both the graphical (<c>LiveSessionCommandRouter</c>) and headless
/// (<c>DirectGameRuntimeCommandAdapter</c>) outbound chat paths call this
/// SAME chokepoint so the two hosts can never diverge on which channels are
/// joined and which local refusal a blocked send produces. Retail refuses
/// LOCALLY (nothing reaches the wire) in exactly two cases — Turbine chat
/// off / room id 0, or the player's own <c>Hear*Chat</c> option off — before
/// ever building a <c>TurbineChatBlob</c>.
/// </para>
/// </summary>
public static class TurbineChatMembershipGate
{
public static TurbineChatGateResult Evaluate(
ChatChannelKindLite kind,
TurbineChatState turbineChat,
RuntimeCharacterOptionsState options,
bool isOlthoiPlayer)
{
ArgumentNullException.ThrowIfNull(turbineChat);
ArgumentNullException.ThrowIfNull(options);
// N2 (CH3 Opus review): reuse TurbineChatDisplayNames.Resolve — the
// SAME assembly already has this room/chatType-to-display-name
// table (LiveSessionEventRouter's inbound path uses it) rather than
// maintaining a second copy of the same seven strings here.
(uint room, uint chatType) = kind switch
{
ChatChannelKindLite.Allegiance => (
turbineChat.AllegianceRoom,
(uint)TurbineChat.ChatType.Allegiance),
ChatChannelKindLite.General => (
turbineChat.GeneralRoom,
(uint)TurbineChat.ChatType.General),
ChatChannelKindLite.Trade => (
turbineChat.TradeRoom,
(uint)TurbineChat.ChatType.Trade),
ChatChannelKindLite.Lfg => (
turbineChat.LfgRoom,
(uint)TurbineChat.ChatType.Lfg),
ChatChannelKindLite.Roleplay => (
turbineChat.RoleplayRoom,
(uint)TurbineChat.ChatType.Roleplay),
ChatChannelKindLite.Society => (
turbineChat.SocietyRoom,
(uint)TurbineChat.ChatType.Society),
ChatChannelKindLite.Olthoi => (
turbineChat.OlthoiRoom,
(uint)TurbineChat.ChatType.Olthoi),
_ => (0u, 0u),
};
string name = TurbineChatDisplayNames.Resolve(room, chatType);
if (!turbineChat.Enabled || room == 0u)
{
return new TurbineChatGateResult(
TurbineChatGateStatus.Unavailable, room, chatType, name);
}
bool hearOption = kind switch
{
ChatChannelKindLite.Allegiance =>
(options.Options1
& (uint)PlayerDescriptionParser.CharacterOptions1.HearAllegianceChat)
!= 0u,
ChatChannelKindLite.General =>
(options.Options2
& (uint)PlayerDescriptionParser.CharacterOptions2.HearGeneralChat)
!= 0u,
ChatChannelKindLite.Trade =>
(options.Options2
& (uint)PlayerDescriptionParser.CharacterOptions2.HearTradeChat)
!= 0u,
ChatChannelKindLite.Lfg =>
(options.Options2
& (uint)PlayerDescriptionParser.CharacterOptions2.HearLFGChat)
!= 0u,
ChatChannelKindLite.Roleplay =>
(options.Options2
& (uint)PlayerDescriptionParser.CharacterOptions2.HearRoleplayChat)
!= 0u,
ChatChannelKindLite.Society =>
(options.Options2
& (uint)PlayerDescriptionParser.CharacterOptions2.HearSocietyChat)
!= 0u,
// Retail's Olthoi entry point passes CPlayerSystem::IsOlthoi()
// as its hearOption argument instead of a PlayerModule flag —
// there is no "Hear Olthoi chat" toggle, only heritage.
ChatChannelKindLite.Olthoi => isOlthoiPlayer,
_ => true,
};
return hearOption
? new TurbineChatGateResult(TurbineChatGateStatus.Allowed, room, chatType, name)
: new TurbineChatGateResult(TurbineChatGateStatus.NotListening, room, chatType, name);
}
/// <summary>
/// N3 (CH3 Opus review): the single shared mapping from an evaluated
/// <see cref="TurbineChatGateResult"/> to the retail-exact local
/// refusal text/type an <c>AddText</c> caller should raise instead of
/// sending — collapses the identical
/// <c>switch (gate.Status) { case Unavailable: ...; case NotListening:
/// ...; }</c> block that used to live independently in both
/// <c>LiveSessionCommandRouter.RouteTurbineChat</c> and
/// <c>DirectGameRuntimeCommandAdapter.TrySendChannel</c>. Returns
/// <c>null</c> when the gate allows the send to proceed to the wire.
/// </summary>
public static (string Text, RetailLogTextType Type)? ResolveRefusalText(
TurbineChatGateResult gate)
{
switch (gate.Status)
{
case TurbineChatGateStatus.Unavailable:
return (ClientTextRefusals.TurbineChatUnavailable, RetailLogTextType.Default);
case TurbineChatGateStatus.NotListening:
(string? text, RetailLogTextType type) =
WeenieErrorMessages.Resolve(0x0551u, gate.DisplayName);
return text is not null ? (text, type) : null;
default:
return null;
}
}
}