using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; namespace RynthCore.Plugin.RynthAi; /// /// Key and mouse input posted to AC's own window, for the Mag-Tools /mt verbs that drive the /// client directly: send, click, movement, strafe, get xy, /// client. A port of Mag-Tools' PostMessageTools (Mag-Plugins\Shared\PostMessageTools.cs): /// the same virtual keys, scan codes (lParam) and click spots, so metas written for Mag-Tools /// behave the same. /// /// The window is the one in this process with AC's class ("Turbine Device Class"), found once. /// PostMessage is safe from any thread; a key held for a time is released by a one-shot timer. /// internal static class GameInput { private const uint WM_KEYDOWN = 0x0100, WM_KEYUP = 0x0101, WM_MOUSEMOVE = 0x0200, WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202; private const byte VK_RETURN = 0x0D, VK_SHIFT = 0x10, VK_CONTROL = 0x11, VK_PAUSE = 0x13, VK_SPACE = 0x20, VK_DELETE = 0x2E, VK_F4 = 0x73, VK_F12 = 0x7B; private const int SW_MINIMIZE = 6, SW_RESTORE = 9; private static IntPtr _hwnd; // Pending key releases: kept alive until they fire. private static readonly System.Collections.Generic.List Pending = new(); /// AC's game window in this process, or IntPtr.Zero. public static IntPtr GameWindow { get { if (_hwnd != IntPtr.Zero && IsWindow(_hwnd)) return _hwnd; // Every top-level AC window on the desktop, keeping this process's (several clients run). // FindWindowEx rather than EnumWindows: no callback delegate to marshal under NativeAOT. uint pid = (uint)Environment.ProcessId; IntPtr found = IntPtr.Zero, h = IntPtr.Zero; for (int i = 0; i < 64; i++) { h = FindWindowEx(IntPtr.Zero, h, "Turbine Device Class", null); if (h == IntPtr.Zero) break; GetWindowThreadProcessId(h, out uint p); if (p == pid) { found = h; break; } } _hwnd = found; return found; } } public static bool Available => GameWindow != IntPtr.Zero; // ── Keys (Mag-Tools PostMessageTools) ────────────────────────────────────── public static void SendEnter() => Key(VK_RETURN, 0x001C0001, 0xC01C0001); public static void SendPause() => Key(VK_PAUSE, 0x00450001, 0xC0450001); public static void SendDel() => Key(VK_DELETE, 0x01530001, 0x01530001); /// F4: opens / closes the fellowship panel. public static void SendF4() => Key(VK_F4, 0x003E0001, 0xC03E0001); /// F12: opens / closes the main pack. public static void SendF12() => Key(VK_F12, 0x00580001, 0xC0580001); /// Ctrl + a letter. public static void SendCntrl(char ch) { Post(WM_KEYDOWN, VK_CONTROL, 0x001D0001); Post(WM_KEYDOWN, CharCode(ch), 0x00100001); Post(WM_KEYUP, CharCode(ch), 0xC0100001); Post(WM_KEYUP, VK_CONTROL, 0xC01D0001); } /// Types key by key (letters, space and "/"; as Mag-Tools). public static void SendMsg(string msg) { foreach (char ch in msg) { uint lparam = (uint)((ScanCode(ch) << 16) | 1); Post(WM_KEYDOWN, CharCode(ch), lparam); Post(WM_KEYUP, CharCode(ch), 0xC0000000 | lparam); } } /// Space (a jump), held ms, with the shift / w z x c keys /// pressed as it is released (Mag-Tools /mt jump). public static void SendSpace(int msToHoldDown = 0, bool shift = false, bool w = false, bool z = false, bool x = false, bool c = false) { Post(WM_KEYDOWN, VK_SPACE, 0x00390001); void Release() { if (shift) Post(WM_KEYDOWN, VK_SHIFT, 0x002A0001); if (w) Post(WM_KEYDOWN, CharCode('w'), 0x00110001); if (z) Post(WM_KEYDOWN, CharCode('z'), 0x002C0001); if (x) Post(WM_KEYDOWN, CharCode('x'), 0x002D0001); if (c) Post(WM_KEYDOWN, CharCode('c'), 0x002E0001); Post(WM_KEYUP, VK_SPACE, 0xC0390001); if (w) Post(WM_KEYUP, CharCode('w'), 0xC0110001); if (z) Post(WM_KEYUP, CharCode('z'), 0xC02C0001); if (x) Post(WM_KEYUP, CharCode('x'), 0xC02D0001); if (c) Post(WM_KEYUP, CharCode('c'), 0xC02E0001); if (shift) Post(WM_KEYUP, VK_SHIFT, 0xC02A0001); } After(msToHoldDown, Release); } /// A movement key (a letter of AC's keymap) held ms. public static void SendMovement(char ch, int msToHoldDown = 0) { uint scan = (uint)ScanCode(ch) * 0x10000; Post(WM_KEYDOWN, CharCode(ch), 0x00000001 + scan); After(msToHoldDown, () => Post(WM_KEYUP, CharCode(ch), 0xC0000001 + scan)); } // ── Mouse ───────────────────────────────────────────────────────────────── /// A left click at window coordinates (, ). public static void SendMouseClick(int x, int y) { uint loc = (uint)((y << 16) | (x & 0xFFFF)); Post(WM_MOUSEMOVE, 0, loc); Post(WM_LBUTTONDOWN, 1, loc); Post(WM_LBUTTONUP, 0, loc); } // AC's confirmation box buttons, relative to the window's centre. Three heights each, as Mag-Tools: // windowed and windowless clients put the box a few pixels apart. public static void ClickOK() => ClickCentre(0); public static void ClickYes() => ClickCentre(-80); public static void ClickNo() => ClickCentre(80); private static void ClickCentre(int dx) { if (!GetWindowRect(GameWindow, out RECT r)) return; int w = r.Right - r.Left, h = r.Bottom - r.Top; foreach (int dy in new[] { 18, 25, 31 }) SendMouseClick(w / 2 + dx, h / 2 + dy); } /// The cursor relative to the game window's top-left (Mag-Tools /mt get xy), or false. public static bool TryCursorInWindow(out int x, out int y) { x = y = 0; if (!GetCursorPos(out POINT p) || !GetWindowRect(GameWindow, out RECT r)) return false; x = p.X - r.Left; y = p.Y - r.Top; return true; } // ── Window ─────────────────────────────────────────────────────────────── public static bool Minimize() => ShowWindow(GameWindow, SW_MINIMIZE); public static bool Restore() => ShowWindow(GameWindow, SW_RESTORE); // ── Internals ──────────────────────────────────────────────────────────── private static void Key(byte vk, uint down, uint up) { Post(WM_KEYDOWN, vk, down); Post(WM_KEYUP, vk, up); } private static void Post(uint msg, uint wParam, uint lParam) { IntPtr h = GameWindow; if (h != IntPtr.Zero) PostMessage(h, msg, (IntPtr)wParam, (IntPtr)unchecked((int)lParam)); } /// Runs now, or after on a one-shot timer. private static void After(int ms, Action action) { if (ms <= 0) { action(); return; } Timer? t = null; t = new Timer(_ => { try { action(); } catch (Exception ex) { Debug.WriteLine("GameInput release failed: " + ex.Message); } finally { lock (Pending) { if (t != null) Pending.Remove(t); } t?.Dispose(); } }, null, Timeout.Infinite, Timeout.Infinite); lock (Pending) Pending.Add(t); t.Change(ms, Timeout.Infinite); } private static byte ScanCode(char ch) => char.ToLowerInvariant(ch) switch { 'a' => 0x1E, 'b' => 0x30, 'c' => 0x2E, 'd' => 0x20, 'e' => 0x12, 'f' => 0x21, 'g' => 0x22, 'h' => 0x23, 'i' => 0x17, 'j' => 0x24, 'k' => 0x25, 'l' => 0x26, 'm' => 0x32, 'n' => 0x31, 'o' => 0x18, 'p' => 0x19, 'q' => 0x10, 'r' => 0x13, 's' => 0x1F, 't' => 0x14, 'u' => 0x16, 'v' => 0x2F, 'w' => 0x11, 'x' => 0x2D, 'y' => 0x15, 'z' => 0x2C, '/' => 0x35, ' ' => 0x39, _ => 0, }; private static byte CharCode(char ch) { char c = char.ToLowerInvariant(ch); if (c >= 'a' && c <= 'z') return (byte)(0x41 + (c - 'a')); return c == '/' ? (byte)0xBF : (byte)0x20; } [StructLayout(LayoutKind.Sequential)] private struct RECT { public int Left, Top, Right, Bottom; } [StructLayout(LayoutKind.Sequential)] private struct POINT { public int X, Y; } [DllImport("user32.dll")] private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr after, string className, string? title); [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid); [DllImport("user32.dll")] private static extern bool IsWindow(IntPtr hWnd); [DllImport("user32.dll")] private static extern bool GetWindowRect(IntPtr hWnd, out RECT rect); [DllImport("user32.dll")] private static extern bool GetCursorPos(out POINT p); [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int cmd); }