diff --git a/src/RynthCore.Engine/Compatibility/AcCharacterOptions.cs b/src/RynthCore.Engine/Compatibility/AcCharacterOptions.cs new file mode 100644 index 0000000..3604af8 --- /dev/null +++ b/src/RynthCore.Engine/Compatibility/AcCharacterOptions.cs @@ -0,0 +1,240 @@ +// ============================================================================ +// RynthCore.Engine - Compatibility/AcCharacterOptions.cs +// +// AC's own "Hear Chat" character options (General, Trade, LFG, +// Roleplay), set the way ticking the box in AC's Options panel does: the +// client's own option bit changes, AC's UI is told, and AC itself sends the +// SetSingleCharacterOption game action (0x0005) so the server agrees, no +// relog. Spike for RynthChat's network tab (docs/plans/2026-11-15-rynthchat- +// network-tab-pins.md, Task 10). Probe: /rc chatoption [on|off]. +// +// Read off acclient.exe (4,841,472 bytes, SHA256 69AC7517...) 2026-11-15, +// offline with Iced; nothing in the client is written or patched: +// CPlayerSystem ctor 0x005611E0 (stores this to 0x0087119C) builds a member +// at +0x30 (0x0059B760); its PlayerModule part sits at +0x34 (vtable +// 0x007E6404, the same +0x34 AcShortcuts reads). Option bits: +0x8C, +0x90. +// PlayerModule::SetOption(PlayerOption, int) 0x005D4F20, thiscall, ret 8. +// The Options panel's checkbox (0x00487020, its only direct caller) calls +// exactly this on that object: push value, push option id, lea ecx,[edi+4]. +// For 0x23..0x26 it goes to a one-option setter (0x005D45C0 for General): +// unchanged -> returns, nothing sent; changed -> flips the bit in +0x90 +// (General 0x100, Trade 0x200, LFG 0x400, Roleplay 0x800) and tail-calls +// vtable slot +0x14 with the option id. +// Slot +0x14 = 0x0059B8F0 (the option-changed handler of this class): +// 0x0047A260(po) tells AC's UI listeners (notice 0x004DD21F, each one's +// virtual +0x244(po)); a side-effect switch that has no case for the chat +// options; then, since its table 0x0059B62C says "send now" for 0x23..0x26, +// GetOption(po) and CM_Character's sender 0x006A2330(po, value): opcode 5, +// then the option id, then the value (two uint32s: what ACE's +// GameActionSetSingleCharacterOption reads). +// PlayerModule::GetOption(PlayerOption) 0x005D4AF0, thiscall, ret 4: a pure +// read of the same bits (General: +0x90 bit 8). +// Guards before calling: all three functions found by unique pattern (never by +// a guessed address); the handler's call at +0xCC must reach a function that +// stores opcode 5 at +0x5A; at run time the object's vtable slot +0x14 must be +// that handler (so it is that class, and the send path is the one read here). +// AC's main thread only (the send writes AC's outgoing blob; a /rc command runs +// there). +// ============================================================================ + +using System; +using System.Runtime.InteropServices; + +namespace RynthCore.Engine.Compatibility; + +internal static unsafe class AcCharacterOptions +{ + /// The four rooms whose "Hear ... Chat" option this sets. + public enum Room { General, Trade, Lfg, Roleplay } + + /// AC's PlayerOption id (ACE CharacterOption): HearGeneralChat 0x23 .. HearRoleplayChat 0x26. + public static uint OptionId(Room room) => room switch + { + Room.General => 0x23, + Room.Trade => 0x24, + Room.Lfg => 0x25, + _ => 0x26, + }; + + public static bool TryParseRoom(string word, out Room room) + { + switch (word.Trim().ToLowerInvariant()) + { + case "general": room = Room.General; return true; + case "trade": room = Room.Trade; return true; + case "lfg": room = Room.Lfg; return true; + case "roleplay": case "rp": room = Room.Roleplay; return true; + default: room = Room.General; return false; + } + } + + public static string Name(Room room) => room switch + { + Room.General => "General", + Room.Trade => "Trade", + Room.Lfg => "LFG", + _ => "Roleplay", + }; + + private const int PlayerModuleOffset = 0x34; // CPlayerSystem + 0x34 (AcShortcuts uses the same) + private const int ModuleReadSpan = 0x94; // the option words at +0x8C and +0x90 + private const int OptionChangedSlot = 0x14; // vtable slot the one-option setters tail-call + private const int HandlerSendCallOffset = 0xCC; // 0x0059B9BC - 0x0059B8F0: call sender + private const int SenderOpcodeStoreOffset = 0x5A; // 0x006A238A - 0x006A2330: mov dword ptr [edx],5 + + // One per line: tools/pe_pattern.py CHECK reads only the first name of a combined declaration. + private const int SetOptionVa = 0x005D4F20; + private const int GetOptionVa = 0x005D4AF0; + private const int OptionChangedVa = 0x0059B8F0; + + // PlayerModule::SetOption(po, value): option 0 is bit 1 of +0x8C, compared byte-wise with the value. + private static readonly byte?[] SetOptionPattern = + [0x8B, 0x44, 0x24, 0x04, 0x85, 0xC0, 0x75, 0x47, 0x8B, 0x91, 0x8C, 0x00, 0x00, 0x00, 0x8A, 0x44, 0x24, 0x08]; + + // PlayerModule::GetOption(po): option 0 is (+0x8C >> 1) & 1, ret 4. + private static readonly byte?[] GetOptionPattern = + [0x8B, 0x44, 0x24, 0x04, 0x85, 0xC0, 0x75, 0x0E, 0x8B, 0x81, 0x8C, 0x00, 0x00, 0x00, 0xD1, 0xE8, 0x83, 0xE0, 0x01, 0xC2, 0x04, 0x00]; + + // The option-changed handler: notify UI (call), then switch on po-2 against 0x2E. + private static readonly byte?[] OptionChangedPattern = + [0x56, 0x57, 0x8B, 0x7C, 0x24, 0x0C, 0x57, 0x8B, 0xF1, 0xE8, null, null, null, null, 0x8D, 0x47, 0xFE, 0x83, 0xC4, 0x04, 0x83, 0xF8, 0x2E]; + + private static readonly byte[] OpcodeFiveStore = [0xC7, 0x02, 0x05, 0x00, 0x00, 0x00]; + + private static IntPtr _setOption, _getOption, _optionChanged; + private static bool _probed; + private static string _status = "not looked for yet"; + + public static string Status => _status; + + private static void Probe() + { + _probed = true; + if (!AcClientModule.TryReadTextSection(out AcClientTextSection text)) + { + _status = "acclient.exe not available"; + return; + } + // Pattern only: a fallback-VA result is never called. Written out per call so + // tools/pe_pattern.py CHECK parses and verifies each pattern against the client. + static IntPtr PatternOnly(HookResolver.ResolveResult r) => + r.Success && r.Source == HookResolver.ResolveSource.PatternScan ? r.Address : IntPtr.Zero; + IntPtr set = PatternOnly(HookResolver.Resolve(text, "AcCharacterOptions.PlayerModule::SetOption", SetOptionPattern, SetOptionVa)); + IntPtr get = PatternOnly(HookResolver.Resolve(text, "AcCharacterOptions.PlayerModule::GetOption", GetOptionPattern, GetOptionVa)); + IntPtr changed = PatternOnly(HookResolver.Resolve(text, "AcCharacterOptions.PlayerModule::OptionChanged", OptionChangedPattern, OptionChangedVa)); + if (set == IntPtr.Zero || get == IntPtr.Zero || changed == IntPtr.Zero) + { + _status = $"not found in this client (set {Ok(set)}, get {Ok(get)}, changed {Ok(changed)})"; + RynthLog.Compat("AcCharacterOptions: " + _status); + return; + } + + // The handler must send through a function that writes game action 5 (SetSingleCharacterOption). + byte[] bytes = text.Bytes; + int call = changed.ToInt32() - text.TextBaseVa + HandlerSendCallOffset; + if (call < 0 || call + 5 > bytes.Length || bytes[call] != 0xE8) + { + _status = "the option handler's send call isn't where it was read"; + RynthLog.Compat("AcCharacterOptions: " + _status); + return; + } + int sender = call + 5 + BitConverter.ToInt32(bytes, call + 1); + if (sender < 0 || sender + SenderOpcodeStoreOffset + OpcodeFiveStore.Length > bytes.Length + || !PatternScanner.VerifyBytes(bytes, sender + SenderOpcodeStoreOffset, OpcodeFiveStore)) + { + _status = $"the option handler's call (0x{text.TextBaseVa + sender:X8}) isn't the 0x0005 sender"; + RynthLog.Compat("AcCharacterOptions: " + _status); + return; + } + + _setOption = set; + _getOption = get; + _optionChanged = changed; + _status = $"SetOption 0x{set.ToInt32():X8}, GetOption 0x{get.ToInt32():X8}, handler 0x{changed.ToInt32():X8} -> sender 0x{text.TextBaseVa + sender:X8}"; + RynthLog.Compat("AcCharacterOptions: " + _status); + + static string Ok(IntPtr p) => p != IntPtr.Zero ? "ok" : "missing"; + } + + /// The PlayerModule, checked to be the class whose option handler was read; or why not. + private static IntPtr Module(out string why) + { + why = ""; + if (!MainThreadGuard.IsOnMainThread()) { why = "not on AC's thread"; return IntPtr.Zero; } + if (!LoginLifecycleHooks.HasObservedLoginComplete) { why = "not in the world yet"; return IntPtr.Zero; } + if (!_probed) Probe(); + if (_setOption == IntPtr.Zero) { why = _status; return IntPtr.Zero; } + IntPtr ps = CharacterManagementHooks.PlayerSystemOnMainThread(); + if (ps == IntPtr.Zero) { why = "AC's player system isn't there"; return IntPtr.Zero; } + IntPtr module = ps + PlayerModuleOffset; + if (!ClientObjectHooks.IsReadableSpan(module, ModuleReadSpan)) { why = "AC's player module can't be read"; return IntPtr.Zero; } + IntPtr vtable = Marshal.ReadIntPtr(module); + if (vtable == IntPtr.Zero || !ClientObjectHooks.IsReadableSpan(vtable, OptionChangedSlot + 4)) + { + why = "AC's player module has no vtable"; + return IntPtr.Zero; + } + IntPtr slot = Marshal.ReadIntPtr(vtable, OptionChangedSlot); + if (slot != _optionChanged) + { + why = $"AC's player module isn't the expected class (slot 0x{slot.ToInt32():X8})"; + return IntPtr.Zero; + } + return module; + } + + /// The client's current value of the room's option. AC's main thread. + public static bool TryGet(Room room, out bool on, out string why) + { + on = false; + IntPtr module = Module(out why); + if (module == IntPtr.Zero) return false; + try + { + on = (((delegate* unmanaged[Thiscall])_getOption)(module, OptionId(room)) & 1) != 0; + return true; + } + catch (Exception ex) + { + why = $"{ex.GetType().Name}: {ex.Message}"; + return false; + } + } + + /// + /// Sets "Hear Chat" as AC's Options panel does. True when the client's + /// value is afterwards; says what happened (already + /// so: AC sends nothing, as the panel). AC's main thread. + /// + public static bool TrySet(Room room, bool on, out string why) + { + IntPtr module = Module(out why); + if (module == IntPtr.Zero) return false; + uint id = OptionId(room); + try + { + var get = (delegate* unmanaged[Thiscall])_getOption; + bool before = (get(module, id) & 1) != 0; + if (before == on) + { + why = $"Hear {Name(room)} Chat is already {(on ? "on" : "off")}; nothing sent"; + return true; + } + ((delegate* unmanaged[Thiscall])_setOption)(module, id, on ? 1 : 0); + bool after = (get(module, id) & 1) != 0; + if (after != on) + { + why = $"AC kept Hear {Name(room)} Chat {(after ? "on" : "off")}"; + return false; + } + why = $"Hear {Name(room)} Chat {(on ? "on" : "off")} (AC sends option 0x{id:X2} to the server)"; + RynthLog.Compat("AcCharacterOptions: " + why); + return true; + } + catch (Exception ex) + { + why = $"{ex.GetType().Name}: {ex.Message}"; + return false; + } + } +} diff --git a/src/RynthCore.Engine/Compatibility/ChatCommandReplies.cs b/src/RynthCore.Engine/Compatibility/ChatCommandReplies.cs index 0d28ae2..5d41b76 100644 --- a/src/RynthCore.Engine/Compatibility/ChatCommandReplies.cs +++ b/src/RynthCore.Engine/Compatibility/ChatCommandReplies.cs @@ -127,6 +127,18 @@ internal static class ChatCommandReplies public const string ServerUsage = "Usage: /rc server [ auto|on|off] | pick |auto | [aelrynth|other|auto] | names ilt [add|remove ]"; + public const string ChatOptionUsage = + "Usage: /rc chatoption [on|off] (alone: the client's current value)"; + + /// "/rc chatoption": a room (rp = roleplay), then nothing, on or off. + private static bool ChatOptionArgs(string a) + { + string[] w = a.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (w.Length is < 1 or > 2) return false; + if (w[0].ToLowerInvariant() is not ("general" or "trade" or "lfg" or "roleplay" or "rp")) return false; + return w.Length == 1 || w[1].ToLowerInvariant() is "on" or "off"; + } + public const string HotkeyUsage = "Usage: /rc hotkey list | add | remove | enable|disable | actions | status | on|off"; @@ -187,6 +199,7 @@ internal static class ChatCommandReplies V("keymap", "Usage: /rc keymap check|bind|unbind | dump, e.g. /rc keymap bind UseQuickSlot_10 F11", KeymapArgs), V("panels", "Usage: /rc panels [open|close|toggle ] | replace ", PanelsArgs), V("title", "Usage: /rc title ", a => uint.TryParse(a.Trim(), out uint t) && t != 0), + V("chatoption", ChatOptionUsage, ChatOptionArgs), V("hotkey", HotkeyUsage, null), V("rl", "Usage: /rc rl (reloads RynthCore, as the bar's RL button)", NoArgs, "reload"), V("inventory", "Usage: /rc inventory (opens or closes the Inventory panel)", NoArgs, "inv"), diff --git a/src/RynthCore.Engine/Compatibility/RynthCoreChatCommands.cs b/src/RynthCore.Engine/Compatibility/RynthCoreChatCommands.cs index e0eed66..01ba16b 100644 --- a/src/RynthCore.Engine/Compatibility/RynthCoreChatCommands.cs +++ b/src/RynthCore.Engine/Compatibility/RynthCoreChatCommands.cs @@ -647,6 +647,30 @@ internal static class RynthCoreChatCommands else Fail($"Couldn't set title {titleId}: {why}."); return; } + // /rc chatoption <general|trade|lfg|roleplay> [on|off]: AC's own "Hear <room> Chat" option, + // set as its Options panel does (AcCharacterOptions; spike for RynthChat's network tab). + if (IsVerb(sub, "chatoption", out string chatOptionArgs)) + { + string[] co = chatOptionArgs.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (co.Length is < 1 or > 2 || !AcCharacterOptions.TryParseRoom(co[0], out var room) + || (co.Length == 2 && !co[1].Equals("on", StringComparison.OrdinalIgnoreCase) + && !co[1].Equals("off", StringComparison.OrdinalIgnoreCase))) + { + Fail(ChatCommandReplies.ChatOptionUsage); + return; + } + if (co.Length == 1) + { + if (AcCharacterOptions.TryGet(room, out bool isOn, out string getWhy)) + Reply($"Hear {AcCharacterOptions.Name(room)} Chat is {(isOn ? "on" : "off")} in the client."); + else Fail($"Couldn't read Hear {AcCharacterOptions.Name(room)} Chat: {getWhy}."); + return; + } + bool wantOn = co[1].Equals("on", StringComparison.OrdinalIgnoreCase); + if (AcCharacterOptions.TrySet(room, wantOn, out string setWhy)) Reply(setWhy + "."); + else Fail($"Couldn't set Hear {AcCharacterOptions.Name(room)} Chat: {setWhy}."); + return; + } if (IsVerb(sub, "settings", out string settingsArgs)) { SettingsCommand(settingsArgs); diff --git a/tools/ChatCommandReplyTests/Program.cs b/tools/ChatCommandReplyTests/Program.cs index b0f2d29..43d3764 100644 --- a/tools/ChatCommandReplyTests/Program.cs +++ b/tools/ChatCommandReplyTests/Program.cs @@ -96,6 +96,9 @@ internal static class Program ("panels", "panels", "panels open 1 2"), ("title", "title 42", "title banana"), ("title", "title 7", "title"), + ("chatoption", "chatoption general on", "chatoption general maybe"), + ("chatoption", "chatoption rp", "chatoption global off"), + ("chatoption", "chatoption LFG OFF", "chatoption"), ("rl", "rl", "rl now"), ("rl", "reload", "reload now"), ("inventory", "inv", "inv now"),