using System.Text; namespace XFEWarframe.Client; public sealed record LuaInterruptAbi(int Guard, int Counter, int Offset) { public static LuaInterruptAbi Resolve(BinaryImage image) { var guards = image.Find("85 D2 79 ?? 53 48 83 EC 20 48 8B D9 E8 ?? ?? ?? ?? 3D 00 35 0C 00 7F ??"); if (guards is not [var guard] || image.FunctionStart(guard) != guard) throw new InvalidDataException("Lua interrupt guard is missing or ambiguous."); var counter = image.RelativeTarget(guard + 13); var code = image.Read(counter, 13); if (new BytePattern("FF 81 ?? ?? ?? ?? 8B 81 ?? ?? ?? ?? C3").Find(code).Count != 1) throw new InvalidDataException("Lua interrupt counter contract changed."); var offset = BitConverter.ToInt32(code, 2); if (offset != BitConverter.ToInt32(code, 8) || offset is < 0x40 or > 0x200 || (offset & 3) != 0) throw new InvalidDataException("Lua interrupt read/write fields disagree."); var errors = new BytePattern("48 8D 15 ?? ?? ?? ??").Find(image.Read(guard, 64)); if (errors is not [var at] || !image.Read(image.RelativeTarget(guard + at + 3), 22).AsSpan().SequenceEqual(Encoding.ASCII.GetBytes("Infinite loop detected"))) throw new InvalidDataException("Lua interrupt guard error identity changed."); return new(guard, counter, offset); } }