using System.Collections.Concurrent;
using System.Runtime.InteropServices;
namespace XFEWarframe.Bridge;
internal static unsafe partial class GameTestLua
{
// Learn the engine's main VM from its protected-call boundary. The main-thread
// field is checked against the native pushthread/close implementations; a
// version-specific global pointer is no longer needed on newer executables.
private static readonly ConcurrentDictionary<nint, byte> LiveMachines = [];
[ThreadStatic] private static nint threadMachine;
[ThreadStatic] private static bool closingMachine;
private static nint protectedCall, closeMachine;
internal static void PrepareStateCapture(List<NativeHook> hooks)
{
if (protectedCall != 0) return;
var callHook = new NativeHook(image + GameAbi.Rva(0x13C58E0), (nint)(delegate* unmanaged<nint, int, int, int, int>)&ObserveProtectedCall);
protectedCall = callHook.Trampoline; hooks.Add(callHook);
if (GameAbi.Current.Legacy) return;
var closeHook = new NativeHook(image + GameAbi.Current.Address("LuaClose"), (nint)(delegate* unmanaged<nint, void>)&ObserveClose);
closeMachine = closeHook.Trampoline; hooks.Add(closeHook);
}
private static nint MainState()
{
if (GameAbi.Current.Legacy) return *(nint*)(image + GameAbi.Current.ScriptState);
return threadMachine != 0 && LiveMachines.ContainsKey(threadMachine) ? threadMachine : 0;
}
internal static nint ActiveMachine => MainState();
internal static nint LuaCallClosure(nint state, int arguments)
{
var index = Top(state) - arguments;
if (arguments < 0 || index < 1 || Type(state, index) != 8) return 0;
var closure = *(nint*)Value(state, index);
return closure != 0 && *(byte*)(closure + 3) == 0 ? closure : 0;
}
[UnmanagedCallersOnly] private static int ObserveProtectedCall(nint state, int arguments, int results, int handler)
{
if (!closingMachine && state != 0)
{
if (GameAbi.Current.Legacy)
{
if (state == MainState()) LauncherLoginUi.Capture(state, state, arguments);
}
else
{
var global = *(nint*)(state + 0x18);
var main = global == 0 ? 0 : *(nint*)(global + 0x2F8);
if (main != 0)
{
if (threadMachine != main || !LiveMachines.ContainsKey(main)) LiveMachines.TryAdd(main, 0);
threadMachine = main;
LauncherLoginUi.Capture(state, main, arguments);
}
}
}
return ((delegate* unmanaged<nint, int, int, int, int>)protectedCall)(state, arguments, results, handler);
}
[UnmanagedCallersOnly] private static void ObserveClose(nint state)
{
var main = *(nint*)(*(nint*)(state + 0x18) + 0x2F8);
LiveMachines.TryRemove(main, out _);
LauncherLoginUi.MachineClosed(main);
ExperimentalAbilityRuntime.ScriptMachineClosed();
// Native method identifiers belong to this executable, not to a Lua VM.
// Closing an auxiliary VM must not force expensive rebinding in the game loop.
failedFingerprintModule = 0;
if (threadMachine == main) threadMachine = 0;
var previous = closingMachine; closingMachine = true;
try { ((delegate* unmanaged<nint, void>)closeMachine)(state); }
finally { closingMachine = previous; }
}
}
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
namespace XFEWarframe.Bridge;
internal static unsafe partial class GameTestLua
{
// Learn the engine's main VM from its protected-call boundary. The main-thread
// field is checked against the native pushthread/close implementations; a
// version-specific global pointer is no longer needed on newer executables.
private static readonly ConcurrentDictionary<nint, byte> LiveMachines = [];
[ThreadStatic] private static nint threadMachine;
[ThreadStatic] private static bool closingMachine;
private static nint protectedCall, closeMachine;
internal static void PrepareStateCapture(List<NativeHook> hooks)
{
if (protectedCall != 0) return;
var callHook = new NativeHook(image + GameAbi.Rva(0x13C58E0), (nint)(delegate* unmanaged<nint, int, int, int, int>)&ObserveProtectedCall);
protectedCall = callHook.Trampoline; hooks.Add(callHook);
if (GameAbi.Current.Legacy) return;
var closeHook = new NativeHook(image + GameAbi.Current.Address("LuaClose"), (nint)(delegate* unmanaged<nint, void>)&ObserveClose);
closeMachine = closeHook.Trampoline; hooks.Add(closeHook);
}
private static nint MainState()
{
if (GameAbi.Current.Legacy) return *(nint*)(image + GameAbi.Current.ScriptState);
return threadMachine != 0 && LiveMachines.ContainsKey(threadMachine) ? threadMachine : 0;
}
internal static nint ActiveMachine => MainState();
internal static nint LuaCallClosure(nint state, int arguments)
{
var index = Top(state) - arguments;
if (arguments < 0 || index < 1 || Type(state, index) != 8) return 0;
var closure = *(nint*)Value(state, index);
return closure != 0 && *(byte*)(closure + 3) == 0 ? closure : 0;
}
[UnmanagedCallersOnly] private static int ObserveProtectedCall(nint state, int arguments, int results, int handler)
{
if (!closingMachine && state != 0)
{
if (GameAbi.Current.Legacy)
{
if (state == MainState()) LauncherLoginUi.Capture(state, state, arguments);
}
else
{
var global = *(nint*)(state + 0x18);
var main = global == 0 ? 0 : *(nint*)(global + 0x2F8);
if (main != 0)
{
if (threadMachine != main || !LiveMachines.ContainsKey(main)) LiveMachines.TryAdd(main, 0);
threadMachine = main;
LauncherLoginUi.Capture(state, main, arguments);
}
}
}
return ((delegate* unmanaged<nint, int, int, int, int>)protectedCall)(state, arguments, results, handler);
}
[UnmanagedCallersOnly] private static void ObserveClose(nint state)
{
var main = *(nint*)(*(nint*)(state + 0x18) + 0x2F8);
LiveMachines.TryRemove(main, out _);
LauncherLoginUi.MachineClosed(main);
ExperimentalAbilityRuntime.ScriptMachineClosed();
// Native method identifiers belong to this executable, not to a Lua VM.
// Closing an auxiliary VM must not force expensive rebinding in the game loop.
failedFingerprintModule = 0;
if (threadMachine == main) threadMachine = 0;
var previous = closingMachine; closingMachine = true;
try { ((delegate* unmanaged<nint, void>)closeMachine)(state); }
finally { closingMachine = previous; }
}
}