using System.Runtime.InteropServices; namespace XFEWarframe.Bridge; internal static unsafe partial class ExperimentalAbilityRuntime { private static int lifetimeGameplayCalls; private static uint SelfTestBankLifetime() { var memory = (nint)NativeMemory.AllocZeroed(0x6000); var savedUpdate = abilityUpdate; var savedCopy = referenceCopy; var savedEmpty = emptyReference; var savedInput = input; var savedDeactivate = deactivate; try { var suit = memory; var instances = memory + 0x500; var first = memory + 0x600; var hidden = memory + 0x620; var empty = memory + 0x640; var abilityA = memory + 0x1000; var abilityB = memory + 0x2000; *(nint*)(suit + SuitField(0x3448)) = instances; *(nint*)instances = first; *(nint*)first = abilityA; *(nint*)hidden = abilityB; *(int*)(first + 8) = *(int*)(first + 12) = *(int*)(hidden + 8) = *(int*)(hidden + 12) = 1; *(int*)(empty + 8) = *(int*)(empty + 12) = 16; var state = new BankState(new(101, 0, [101, 0, 0, 0]) { Banks = [[101, 102], [], [], []] }); state.References[0][0] = first; state.References[0][1] = hidden; abilityUpdate = (nint)(delegate* unmanaged)&LifetimeTick; referenceCopy = (nint)(delegate* unmanaged)&LifetimeCopy; input = (nint)(delegate* unmanaged)&LifetimeInput; deactivate = (nint)(delegate* unmanaged)&LifetimeDeactivate; emptyReference = empty; *(byte*)(abilityB + 0x678) = 1; UpdateRetained(suit, state, 0, .016f); if (*(int*)(abilityB + 0x50) != 1 || *(nint*)instances != first) return 60; *(byte*)(abilityB + 0x678) = 0; state.Settling[0][1] = 0; UpdateRetained(suit, state, 0, .016f); if (*(int*)(abilityB + 0x50) != 1) return 61; state.Settling[0][1] = 1; UpdateRetained(suit, state, 0, .016f); if (*(int*)(abilityB + 0x50) != 2) return 62; // Settlement may have already cleared the native slot array. Teardown // must release owned references without gameplay input or Lua callbacks. *(nint*)(suit + SuitField(0x3448)) = 0; state.ForwardedPress[0] = true; BankStates[suit] = state; lifetimeGameplayCalls = 0; ReleaseBanks(suit, deactivateRetained: false); if (lifetimeGameplayCalls != 0 || BankStates.ContainsKey(suit)) return 63; if (*(int*)(first + 8) != 0 || *(int*)(hidden + 8) != 0 || *(int*)(empty + 8) != 16 || *(int*)(empty + 12) != 16) return 64; return 0; } finally { BankStates.Remove(memory); abilityUpdate = savedUpdate; referenceCopy = savedCopy; emptyReference = savedEmpty; input = savedInput; deactivate = savedDeactivate; NativeMemory.Free((void*)memory); } } [UnmanagedCallersOnly] private static void LifetimeTick(nint ability, float _) => ++*(int*)(ability + 0x50); [UnmanagedCallersOnly] private static byte LifetimeInput(nint suit, int code, byte pressed) { lifetimeGameplayCalls++; return 1; } [UnmanagedCallersOnly] private static void LifetimeDeactivate(nint suit, byte slot) => lifetimeGameplayCalls++; [UnmanagedCallersOnly] private static void LifetimeCopy(nint* destination, nint* source) { --*(int*)(*destination + 8); --*(int*)(*destination + 12); ++*(int*)(*source + 8); ++*(int*)(*source + 12); *destination = *source; } }