using System.Runtime.InteropServices; using XFEWarframe.Client; namespace XFEWarframe.Bridge; internal static unsafe class AirSlideHeavyRuntime { private static AirSlideHeavyAbi? binding; private static Action log = _ => { }; private static int observed; // ECX=R13D command, EDX=BL combo, R8D=[RBP-73] air, R9D=[RBP-76] slide. private static readonly byte[] InputArguments = [0x44,0x89,0xE9,0x0F,0xB6,0xD3,0x44,0x0F,0xB6,0x45,0x8D,0x44,0x0F,0xB6,0x4D,0x8A]; private static readonly byte[] QueuedArguments = [0x4C,0x89,0xE9,0x44,0x89,0xFA,0x44,0x0F,0xB6,0xC0]; internal static void Prepare(nint module, ClientConfiguration configuration, List hooks, Action writeLog) { if (!configuration.RestoreAirSlideHeavyAttacks) { writeLog("Legacy air-slide heavy attacks disabled by configuration."); return; } using var image = new BinaryImage(Environment.ProcessPath!); var pending = new List(); try { var resolved = AirSlideHeavyAbi.Resolve(image, GameAbi.Current.HashSeed); foreach (var (rva, bytes) in resolved.Signatures) if (!new ReadOnlySpan((void*)(module + rva), bytes.Length).SequenceEqual(bytes)) throw new InvalidDataException("Air-slide heavy in-memory contract changed."); pending.Add(NativeHook.BranchWhen(module + resolved.InputSelection, (nint)(delegate* unmanaged)&RestoreInput, module + resolved.InputContinuation, InputArguments)); pending.Add(NativeHook.BranchWhen(module + resolved.QueuedSelection, (nint)(delegate* unmanaged)&RestoreQueued, module + resolved.QueuedContinuation, QueuedArguments)); binding = resolved; log = writeLog; hooks.AddRange(pending); writeLog("Legacy air-slide heavy attacks ready: native frame classifiers and buffered avatar posture verified."); } catch (Exception ex) when (ex is InvalidDataException or ArgumentException or OverflowException) { foreach (var hook in pending) hook.Rollback(); binding = null; writeLog("Legacy air-slide heavy attacks unavailable; original input retained: " + ex.Message); } } private static int Observe(bool selected) { if (!selected) return 0; if (Interlocked.Exchange(ref observed, 1) == 0) log("Legacy air-slide heavy: native heavy combo selected; ground-slam branch bypassed."); return 1; } private static bool InputMatches(int command, int attack, int air, int slide) => AirSlideHeavyAbi.ShouldRestore(command, attack, air != 0 ? AirSlideHeavyAbi.InAir : 0, slide != 0); [UnmanagedCallersOnly] private static int RestoreInput(int command, int attack, int air, int slide) => Observe(InputMatches(command, attack, air, slide)); [UnmanagedCallersOnly] private static int RestoreQueued(nint avatar, int command, int air) => Observe(binding is { } abi && QueuedMatches(avatar, command, air, abi.PostureOffset)); private static bool QueuedMatches(nint avatar, int command, int air, int posture) => command == AirSlideHeavyAbi.HeavyCommand && air != 0 && NativeMemoryRange.Readable(avatar + posture, 1) && *(byte*)(avatar + posture) == 5; internal static uint SelfTest() { var memory = Native.VirtualAlloc(0, 4096, 0x3000, 0x40); if (memory == 0) return 201; NativeHook? hook = null; try { // R15 is deliberately zero, as in the captured failing game frame. byte[] prefix = [0x55,0x53,0x41,0x55,0x41,0x57,0x48,0x81,0xEC,0xA8,0,0,0,0x48,0x8D,0xAC,0x24,0x80,0,0,0, 0x41,0x89,0xCD,0x88,0xD3,0x44,0x88,0x45,0x8D,0x44,0x88,0x4D,0x8A,0x45,0x31,0xFF]; byte[] suffix = [0x48,0x81,0xC4,0xA8,0,0,0,0x41,0x5F,0x41,0x5D,0x5B,0x5D,0xC3]; NativeHook.Write(memory, prefix); NativeHook.Write(memory + prefix.Length, new byte[] { 0xB8,42,0,0,0 }.Concat(Enumerable.Repeat((byte)0x90, 10)).Concat(suffix).ToArray()); NativeHook.Write(memory + 128, new byte[] { 0xB8,99,0,0,0 }.Concat(suffix).ToArray()); hook = NativeHook.BranchWhen(memory + prefix.Length, (nint)(delegate* unmanaged)&SelfTestInput, memory + 128, InputArguments); hook.Enable(); var input = (delegate* unmanaged)memory; foreach (var attack in new[] {4,5,17}) if (input(259,attack,1,1) != 99) return 202; if (input(259,4,1,0) != 42 || input(259,4,0,1) != 42 || input(259,7,1,1) != 42 || input(0x76,4,1,1) != 42) return 203; hook.Rollback(); hook = null; if (input(259,4,1,1) != 42) return 204; prefix = [0x41,0x55,0x41,0x57,0x53,0x48,0x83,0xEC,0x20,0x49,0x89,0xCD,0x41,0x89,0xD7,0x44,0x89,0xC0]; suffix = [0x48,0x83,0xC4,0x20,0x5B,0x41,0x5F,0x41,0x5D,0xC3]; NativeHook.Write(memory, prefix); NativeHook.Write(memory + prefix.Length, new byte[] { 0xB8,42,0,0,0 }.Concat(Enumerable.Repeat((byte)0x90,10)).Concat(suffix).ToArray()); NativeHook.Write(memory + 128, new byte[] { 0xB8,99,0,0,0 }.Concat(suffix).ToArray()); hook = NativeHook.BranchWhen(memory + prefix.Length, (nint)(delegate* unmanaged)&SelfTestQueued, memory + 128, QueuedArguments); hook.Enable(); var queued = (delegate* unmanaged)memory; *(byte*)(memory + 512 + 0x20) = 5; if (queued(memory+512,259,1) != 99 || queued(memory+512,259,0) != 42 || queued(memory+512,0x76,1) != 42 || queued(0,259,1) != 42) return 205; *(byte*)(memory + 512 + 0x20) = 0; if (queued(memory+512,259,1) != 42) return 206; return 0; } finally { hook?.Rollback(); Native.VirtualFree(memory,0,0x8000); } } [UnmanagedCallersOnly] private static int SelfTestInput(int command, int attack, int air, int slide) => InputMatches(command, attack, air, slide) ? 1 : 0; [UnmanagedCallersOnly] private static int SelfTestQueued(nint avatar, int command, int air) => QueuedMatches(avatar, command, air, 0x20) ? 1 : 0; }