-- FL27 Normal defensive AI for ordinary Superstar live play. local m = {} local CODE_BYTES = 0x2000 local WORLD_GLOBAL = 0x36f3dc8 local TEAM_CONTROL_MODE = 0xa55d40 local RAW_MODE = 0x3812f0 local PRESS_SITE = 0x633e82 local PRESS_EXPECTED = "3bb424b0010000" local PRESS_TACKLE_SITE = 0x9713f7 local PRESS_TACKLE_EXPECTED = "f30f10050542bf01" -- Clear the timed pressure state's timestamp and counter, then fall through -- to the native flag clear. Force the same flag to zero on copy and restore. local PRESSURE_STATE_SITES = { { 0x536180, "b902000000e8d6c2650183f8057d12", "48c78340a500000000000090909090" }, { 0x41706f, "0fb6863ca50000", "b8000000006690" }, { 0x215d533, "660f7ec8", "b0009090" }, } -- Entries replace individual native calls. The last field identifies the -- defending player at that call, independently of the current team cursor. local CALLS = { { 0x528b6c, "e8cfd15200", TEAM_CONTROL_MODE, "4489e0" }, -- MARK scoring, R12D { 0x528bf7, "e844d15200", TEAM_CONTROL_MODE, "4489e0" }, -- MARK admission { 0x621fd3, "e8683d4300", TEAM_CONTROL_MODE, "8b06" }, -- COVER/MARK positioning, [RSI] { 0x632612, "e829374200", TEAM_CONTROL_MODE, "89f8" }, -- near-shot PRESS, EDI { 0x62a0a2, "e899bc4200", TEAM_CONTROL_MODE, "418b442404" }, -- MATCH_UP, [R12+4] { 0x697e8b, "e8b0de3b00", TEAM_CONTROL_MODE, "418b4504" }, -- CUT/MARK/DELAY route geometry { 0x697a38, "e803e33b00", TEAM_CONTROL_MODE, "418b4504" }, -- CUT/MARK/DELAY route, [R13+4] { 0x965da2, "e899ff0e00", TEAM_CONTROL_MODE, "8b4704" }, -- DELAY fallback, [RDI+4] { 0x965ee9, "e852fe0e00", TEAM_CONTROL_MODE, "418b4504" }, -- DELAY third route, [R13+4] { 0x96aad0, "e86bb20e00", TEAM_CONTROL_MODE, "488b85c80000008b4004" }, -- DELAY target, request slot { 0x96ab62, "e8d9b10e00", TEAM_CONTROL_MODE, "488b85c80000008b4004" }, { 0x96ac16, "e825b10e00", TEAM_CONTROL_MODE, "488b85c80000008b4004" }, { 0xa0977a, "e8c1c50400", TEAM_CONTROL_MODE, "4489e0" }, -- Block range, R12D { 0x971e90, "e85bf4a0ff", RAW_MODE, "418b4504" }, -- Sliding, other controller { 0x729489, "e8627ec5ff", RAW_MODE, "block_slot" }, -- Block animation resource { 0x21149f6, "e8451394fe", TEAM_CONTROL_MODE, "418b4604" }, -- keeper save eligibility, [R14+4] { 0x2115f0a, "e831fe93fe", TEAM_CONTROL_MODE, "keeper_move" }, -- keeper forecast eligibility } local PRESS_ROUTES = { { site = 0x516ffc, original = "42807c010d00", caller = 0x962beb, call = "e83043bbff", stack_offset = 0x148, actor = "edi", allow = 0x517009, resume = 0x517002, prologue = 0x516f2f, prologue_bytes = "55535657415441554157488d6c24f04881ec10010000", resume_bytes = "0f95c084c0749f", frame = { 0x516f20, 0x517012, 0x2eb00c8 }, }, { site = 0x697a10, original = "42387c090d", caller = 0x962c35, call = "e8364dd3ff", stack_offset = 0x138, actor = "ebx", allow = 0x6979ce, resume = 0x697a15, prologue = 0x697975, prologue_bytes = "555356574155488d6c24f04881ec10010000", resume_bytes = "0f95c084c075b2", frame = { 0x6979ff, 0x697a2a, 0x2ed0670 }, }, { site = 0x962ff0, original = "423874090d", caller = 0x962c6b, call = "e8e0020000", stack_offset = 0xe8, actor = "edi", allow = 0x962fae, resume = 0x962ff5, prologue = 0x962f55, prologue_bytes = "55565741554157488d6c24c94881ecc0000000", resume_bytes = "0f95c084c075b2", frame = { 0x962fdf, 0x96300b, 0x2f06c34 }, }, } -- R633e82 is inside an existing Win64 frame. This block changes only the -- ordinary type-1 distance, using effective defensive awareness (PlayerInfo -- byte +0x85). It skips the retained-assignee +3 bonus on the patched branch. -- The three absolute pointers at 0x170 are filled in at load time. local PRESS_CODE = "83bc24b8010000010f85510100004183fe050f854701000083bfa4b80000030f8c3a01000083fe150f87" .. "31010000660f7ef83d000020410f840b0000003d000070410f85170100004c8b15210100004d8b124d85" .. "d20f84040100004d8b9a200200004d85db0f84f400000041813bf40100000f85e70000004d8b9a281900" .. "004939fb0f8412000000493bba501900000f85ca000000e9070000004d8b9a501900004d85db0f84b500" .. "00004180bb9ca20000000f85a7000000418b8394a200002df401000083f8640f83920000004d8b9a8001" .. "00004d85db0f8482000000b918000000413973200f84110000004983c374ffc90f85eaffffffe90b0000" .. "0041807b0d000f84570000006bc628498b8402f82500004885c00f84430000000fb6808500000031d283" .. "e8280f4cc2ba3b00000039d00f4fc2f30f2ac0f30f590547000000f30f5e0543000000f30f58053f0000" .. "00f30f10f84c8b1d1c00000041ffe33bb424b00100004c8b1d1300000041ffe300000000000000000000" .. "00000000000000000000000000000000a04000006c4200002041" local function bytes(hex) return (hex:gsub("..", function(pair) return string.char(tonumber(pair, 16)) end)) end local function i32(value) return memory.pack("i32", value) end local function u64(value) return memory.pack("u64", value) end local function emitter(code) local op = { code = code, size = 0, pieces = {}, labels = {}, pending = {} } function op:add(data) self.pieces[#self.pieces + 1] = data self.size = self.size + #data end function op:hex(hex) self:add(bytes(hex)) end function op:label(name) self.labels[name] = self.size end function op:jump(opcode, target) self:hex(opcode) self.pending[#self.pending + 1] = { #self.pieces + 1, self.size + 4, target } self:add("\x00\x00\x00\x00") end function op:finish() for _, jump in ipairs(self.pending) do local target = type(jump[3]) == "number" and jump[3] or self.code + assert(self.labels[jump[3]]) self.pieces[jump[1]] = i32(target - (self.code + jump[2])) end return table.concat(self.pieces) end return op end local function emit_teammate_check(s, base) s:label("teammate") local function fail() s:jump("0f84", "no_teammate") end local function different() s:jump("0f85", "no_teammate") end s:hex("83f815") -- valid candidate slot in EAX s:jump("0f87", "no_teammate") s:hex("448bc0") s:jump("4c8b1d", "world_address") -- mov r11,[RIP+world_address] s:hex("4d8b1b4d85db"); fail() -- root s:hex("4d8b93700200004d85d2"); fail() -- match s:hex("4183ba6c11000001"); different() -- live play s:hex("4183ba5415000000"); different() -- ordinary match stage s:hex("4380bc024817000000"); fail() -- candidate active s:hex("498b8b200200004885c9"); fail() -- menu difficulty s:hex("8139f4010000"); different() s:hex("498b8b28190000498b93501900004885c9"); fail() s:hex("4885d2"); fail() s:hex("81b994a20000f4010000"); different() -- home effective Superstar s:hex("81ba94a20000f4010000"); different() -- away effective Superstar s:hex("80b99ca2000000"); different() -- difficulty bypass flags s:hex("80ba9ca2000000"); different() s:hex("4531c94183f80b410f93c14585c9480f45ca") -- candidate side; select team s:hex("6683b9f000000000"); different() -- leave explicit team orders alone s:hex("498b93800100004885d2"); fail() -- controller rows s:hex("4531dbb918000000") -- R11D: seen slots, top bit is a valid teammate s:label("row") s:hex("8b422083f815") s:jump("0f87", "next_row") s:hex("443bc0") -- protect even ambiguous duplicate cursor records s:jump("0f85", "dedup_row") s:hex("807a0d00"); fail() s:label("dedup_row") s:hex("410fabc3") -- first record for each slot wins s:jump("0f82", "next_row") s:hex("807a0d00") s:jump("0f85", "next_row") s:hex("807a0600") s:jump("0f85", "next_row") s:hex("4180bc024817000000") s:jump("0f84", "next_row") s:hex("83f80b0f93c04438c8") -- active row on the same side? s:jump("0f85", "next_row") s:hex("4181cb00000080") s:label("next_row") s:hex("4883c274ffc9") s:jump("0f85", "row") s:hex("418bc3c1e81fc3") -- found a different local controller s:label("no_teammate") s:hex("31c0c3") s:label("world_address") s:add(u64(base + WORLD_GLOBAL)) end local function emit_keeper_slot(s, fallback) -- The keeper helper saves the actor's Move and PlayerInfo at native -- [rsp+0x40] and [rsp+0x60]. The call plus the wrapper frame add 0x60. s:hex("488b8424a00000004885c0") s:jump("0f84", fallback) s:hex("4c8b8c24c00000004d85c9") s:jump("0f84", fallback) s:jump("4c8b1d", "world_address") s:hex("4d8b1b4d85db") s:jump("0f84", fallback) s:hex("4d8d9bd81d000031d241baffffffff") local loop = fallback .. "_keeper_scan" local next_slot = fallback .. "_keeper_next" s:label(loop) s:hex("493903") s:jump("0f85", next_slot) s:hex("4d398b20080000") s:jump("0f85", next_slot) s:hex("4183faff") s:jump("0f85", fallback) s:hex("448bd2") s:label(next_slot) s:hex("4983c328ffc283fa16") s:jump("0f82", loop) s:hex("418bc2") end local function emit_press_route(s, base, route) local fallback = "route_fallback_" .. route.site local caller = u64(base + route.caller + 5) -- Preserve the native frame and compare all 64 bits before sampling scope. for offset = 0, 4, 4 do s:hex("81bc24") -- cmp dword ptr [rsp+disp32], imm32 s:add(i32(route.stack_offset + offset)) s:add(caller:sub(offset + 1, offset + 4)) s:jump("0f85", fallback) end s:jump("e8", "press_scope_" .. route.actor) s:jump("0f82", fallback) s:hex("b00184c0") -- reproduce SETNE AL; TEST AL,AL on the allowed path s:jump("0f85", base + route.allow) -- ZF is clear; Jcc does not look like an epilogue s:label(fallback) s:hex(route.original) s:jump("0f84", base + route.resume) s:jump("0f85", base + route.resume) end local function emit_press_tackle(s, base) -- Native PRESS52 and direct-control checks precede this site. s:jump("e8", "press_scope_tackle") s:jump("0f82", "press_tackle_stock") s:hex("84c0") -- restore TEST AL,AL from the native guard s:jump("0f85", base + 0x971437) -- same accepted path as non-manual Press s:label("press_tackle_stock") s:jump("f30f1005", base + 0x2565604) -- relocate the original RIP-relative MOVSS s:hex("84c0") s:jump("0f84", base + PRESS_TACKLE_SITE + 8) s:jump("0f85", base + PRESS_TACKLE_SITE + 8) end local function emit_press_scope(s, actor) local restore = "press_scope_restore_" .. actor local denied = "press_scope_denied_" .. actor local function zero() s:jump("0f84", denied) end local function different() s:jump("0f85", denied) end local tackle = actor == "tackle" -- Private frame preserves its scratch registers and returns the scope -- decision in carry without using the caller's home space. s:label("press_scope_" .. actor) s:hex("4883ec18488904244c89542408") if tackle then s:hex("48894c2410") end -- save RCX for the slot loaded from RDI s:jump("4c8b15", "world_address") s:hex("4d8b124d85d2"); zero() s:hex("498b82700200004885c0"); zero() s:hex("83b86c11000001"); different() -- active play s:hex("83b85415000000"); different() -- ordinary match stage if tackle then s:hex("8b4f0483f915") -- mov ecx,[rdi+4]; cmp ecx,21 else s:hex(actor == "ebx" and "83fb15" or "83ff15") end s:jump("0f87", denied) if tackle then s:hex("80bc084817000000") -- cmp byte ptr [rax+rcx+0x1748],0 else s:hex(actor == "ebx" and "80bc184817000000" or "80bc384817000000") end zero() -- actor on the field s:hex("498b82200200004885c0"); zero() s:hex("8138f4010000"); different() -- menu Superstar for _, field in ipairs({ "498b8228190000", "498b8250190000" }) do s:hex(field .. "4885c0"); zero() s:hex("81b894a20000f4010000"); different() -- effective Superstar s:hex("80b89ca2000000"); different() end s:hex("f8") -- clear carry for the allowed path s:jump("e9", restore) s:label(denied) s:hex("f9") s:label(restore) if tackle then s:hex("488b4c2410") end s:hex("488b04244c8b542408488d642418") -- restore scratch and RSP without changing carry local restored = s.size s:hex("c3") -- restored RET unwinds as a leaf return restored end function m.build(base, code) local s = emitter(code) local sites = {} for _, spec in ipairs(PRESSURE_STATE_SITES) do local original, replacement = bytes(spec[2]), bytes(spec[3]) assert(#original == #replacement) sites[#sites + 1] = { address = base + spec[1], original = original, replacement = replacement } end local function site(rva, original_hex, opcode, target) local original = bytes(original_hex) sites[#sites + 1] = { address = base + rva, original = original, replacement = bytes(opcode) .. i32(code + target - (base + rva + 5)) .. string.rep("\x90", #original - 5), } end local frames = {} local save = "48894c242048895424284c894424304c894c24384c895424404c895c2448" local restore = "488b4c2420488b5424284c8b4424304c8b4c24384c8b5424404c8b5c2448" for index, spec in ipairs(CALLS) do local rva, original, stock, slot = spec[1], spec[2], spec[3], spec[4] local begin = s.size site(rva, original, "e8", begin) local fallback = "stock_" .. index local stock_pointer = "stock_pointer_" .. index -- This frame reserves the helper's home space and saves all six -- volatile argument/scratch registers, including for the raw leaf getter. s:hex("4883ec58") s:hex(save) if slot == "block_slot" then s:hex("488b0649bb") s:add(u64(base + 0x25e3508)) -- known block actor vtable s:hex("4c39d8") s:jump("0f85", fallback) s:hex("0fbe86a0190000") -- same slot as the native vtable getter elseif slot == "keeper_move" then emit_keeper_slot(s, fallback) else s:hex(slot) end if stock == TEAM_CONTROL_MODE then s:hex("31c983f80b0f93c13b4c2428") -- actor side must equal the original queried side s:jump("0f85", fallback) end s:jump("e8", "teammate") s:hex("85c0") s:jump("0f84", fallback) s:hex(restore) s:hex("31c04883c458c3") -- return the computer-side predicate or resource mode s:label(fallback) s:hex(restore) s:hex("4883c458") s:jump("ff25", stock_pointer) frames[#frames + 1] = { begin, s.size } s:label(stock_pointer) s:add(u64(base + stock)) end emit_teammate_check(s, base) local press = bytes(PRESS_CODE) assert(#press == 404) local press_begin = s.size site(PRESS_SITE, PRESS_EXPECTED, "e9", press_begin) s:jump("4c8b1d", "world_address") s:hex("4d8b1b4d85db") s:jump("0f84", "press_stock") s:hex("4d8b93700200004d85d2") s:jump("0f84", "press_stock") s:hex("4183ba6c11000001") s:jump("0f85", "press_stock") s:hex("4183ba5415000000") s:jump("0f85", "press_stock") for _, field in ipairs({ "498b8328190000", "498b8350190000" }) do s:hex(field .. "4885c0") s:jump("0f84", "press_stock") s:hex("81b894a20000f4010000") s:jump("0f85", "press_stock") s:hex("80b89ca2000000") s:jump("0f85", "press_stock") end s:hex("6683bff000000000") -- explicit team orders s:jump("0f84", "press_limit") s:label("press_stock") s:hex(PRESS_EXPECTED) s:hex("49bb") s:add(u64(base + 0x633e89)) s:hex("41ffe3") -- register jump is not a Win64 epilog s:label("press_limit") local press_end = s.size + 0x170 s:add(press:sub(1, 0x170) .. u64(base + WORLD_GLOBAL) .. u64(base + 0x633e93) .. u64(base + 0x633e89) .. press:sub(0x189)) local press_frames = {} for _, route in ipairs(PRESS_ROUTES) do local begin = s.size site(route.site, route.original, "e9", begin) emit_press_route(s, base, route) press_frames[#press_frames + 1] = { begin = begin, finish = s.size, parent = route.frame } end for _, actor in ipairs({ "edi", "ebx" }) do local begin = s.size press_frames[#press_frames + 1] = { begin = begin, finish = emit_press_scope(s, actor) } end local tackle_begin = s.size site(PRESS_TACKLE_SITE, PRESS_TACKLE_EXPECTED, "e9", tackle_begin) emit_press_tackle(s, base) press_frames[#press_frames + 1] = { begin = tackle_begin, finish = s.size, parent = { 0x971290, 0x97143e, 0x2f07a40 }, } local tackle_scope_begin = s.size press_frames[#press_frames + 1] = { begin = tackle_scope_begin, finish = emit_press_scope(s, "tackle"), } while s.size % 4 ~= 0 do s:hex("90") end local wrapper_unwind = s.size s:hex("0104010004a20000") -- 4-byte prologue, UWOP_ALLOC_SMALL(0x58) local press_unwind = s.size -- CHAININFO points to the existing entry for the overwritten instructions. s:hex("21000000a03a630066446300ec6eec02") local scope_unwind = s.size s:hex("0104010004220000") -- UWOP_ALLOC_SMALL(0x18) for _, range in ipairs(press_frames) do range.unwind = scope_unwind if range.parent then range.unwind = s.size s:hex("21000000") for _, field in ipairs(range.parent) do s:add(i32(field)) end end end local pdata = s.size local function frame(begin, finish, unwind) s:add(i32(code - base + begin) .. i32(code - base + finish) .. i32(code - base + unwind)) end for _, range in ipairs(frames) do frame(range[1], range[2], wrapper_unwind) end frame(press_begin, press_end, press_unwind) for _, range in ipairs(press_frames) do frame(range.begin, range.finish, range.unwind) end return s:finish(), sites, pdata, #frames + 1 + #press_frames end function m.preflight(base, name) if type(name) ~= "string" or name:lower() ~= "fl_2027.exe" then return nil, "requires Normal FL27" end if not base or memory.read(base, 2) ~= "MZ" then return nil, "invalid main image" end local offset = memory.unpack("u32", memory.read(base + 0x3c, 4)) if offset < 0x40 or offset > 0x1000 then return nil, "invalid PE header" end local pe = base + offset local image_size = memory.unpack("u32", memory.read(pe + 80, 4)) if memory.read(pe, 6) ~= "PE\x00\x00\x64\x86" or image_size < 0x36f3dd0 or image_size >= 0x60000000 then return nil, "unsupported FL27 image" end local anchors = { { 0x53618f, "c6833ca5000000" }, { PRESS_SITE, PRESS_EXPECTED }, { 0x633e89, "7508f30f583dd183f501" }, { 0x3812f0, "0fb6410ac3" }, { 0x6ea810, "0fbe81a0190000c3" }, { 0x9713df, "83bed005000034754f8b5704498bcfe87d80b0ff84c07440" }, { PRESS_TACKLE_SITE, PRESS_TACKLE_EXPECTED }, { 0x9713ff, "488d935c050000" }, { 0x971437, "b001e958fcffff" }, } for _, entry in ipairs(PRESSURE_STATE_SITES) do anchors[#anchors + 1] = entry end for _, entry in ipairs(CALLS) do anchors[#anchors + 1] = entry end for _, route in ipairs(PRESS_ROUTES) do anchors[#anchors + 1] = { route.site, route.original } anchors[#anchors + 1] = { route.caller, route.call } anchors[#anchors + 1] = { route.prologue, route.prologue_bytes } anchors[#anchors + 1] = { route.resume, route.resume_bytes } end for _, entry in ipairs(anchors) do local expected = bytes(entry[2]) if memory.read(base + entry[1], #expected) ~= expected then return nil, string.format("unexpected instructions at RVA %x", entry[1]) end end return true end local function pointer(address) return ffi.cast("void*", address) end local function write_code(site, data) local address = pointer(site.address) local old = ffi.new("uint32_t[1]") if ffi.C.VirtualProtect(address, #data, 0x40, old) == 0 then return false end site.protection = site.protection or tonumber(old[0]) ffi.copy(address, data, #data) local flushed = ffi.C.FlushInstructionCache(ffi.C.GetCurrentProcess(), address, #data) ~= 0 local protected = ffi.C.VirtualProtect(address, #data, site.protection, old) ~= 0 return flushed and protected and memory.read(site.address, #data) == data end local function install(base, name) local ok, message = m.preflight(base, name) if not ok then return false, message end local pe = base + memory.unpack("u32", memory.read(base + 0x3c, 4)) local image_size = memory.unpack("u32", memory.read(pe + 80, 4)) local start = math.ceil((base + image_size) / 0x10000) * 0x10000 local code_page for address = start, start + 0x10000000 - 1, 0x10000 do code_page = ffi.C.VirtualAlloc(pointer(address), CODE_BYTES, 0x3000, 0x04) if code_page ~= nil then break end end if code_page == nil then return false, "near code allocation failed" end local code = tonumber(ffi.cast("uintptr_t", code_page)) local blob, sites, pdata, frame_count = m.build(base, code) if #blob <= CODE_BYTES then ffi.copy(code_page, blob, #blob) end if #blob > CODE_BYTES or ffi.string(code_page, #blob) ~= blob or ffi.C.VirtualProtect(code_page, CODE_BYTES, 0x20, ffi.new("uint32_t[1]")) == 0 or ffi.C.FlushInstructionCache(ffi.C.GetCurrentProcess(), code_page, #blob) == 0 then ffi.C.VirtualFree(code_page, 0, 0x8000) return false, "unable to prepare native code" end local unwind = ffi.cast("uint32_t*", code + pdata) if ffi.C.RtlAddFunctionTable(unwind, frame_count, base) == 0 then ffi.C.VirtualFree(code_page, 0, 0x8000) return false, "unable to register native frame" end local changed = {} for _, site in ipairs(sites) do if memory.read(site.address, #site.original) ~= site.original then message = "patch site changed during activation" break end changed[#changed + 1] = site if not write_code(site, site.replacement) then message = "native patch write failed" break end end if message then local restored = true for index = #changed, 1, -1 do local site = changed[index] if site.protection or memory.read(site.address, #site.original) ~= site.original then restored = write_code(site, site.original) and restored end end if restored and ffi.C.RtlDeleteFunctionTable(unwind) ~= 0 then ffi.C.VirtualFree(code_page, 0, 0x8000) end return false, message .. "; rollback=" .. tostring(restored) end return true, string.format("Superstar defensive AI, %d native sites, timed pressure state disabled", #sites) end function m.init() if not ffi then log("fl27-defensive-ai: SKIPPED LuaJIT extensions required"); return end ffi.cdef[[ uint8_t* GetModuleHandleA(const char* name); uint32_t GetModuleFileNameA(void* module, char* filename, uint32_t size); uint8_t* VirtualAlloc(void* address, size_t size, uint32_t kind, uint32_t protection); int VirtualFree(void* address, size_t size, uint32_t kind); int VirtualProtect(void* address, size_t size, uint32_t protect, uint32_t* old); void* GetCurrentProcess(void); int FlushInstructionCache(void* process, const void* address, uint64_t size); uint8_t RtlAddFunctionTable(uint32_t* table, uint32_t count, uint64_t base); uint8_t RtlDeleteFunctionTable(uint32_t* table); ]] local module = ffi.C.GetModuleHandleA(nil) if module == nil then log("fl27-defensive-ai: SKIPPED main image unavailable"); return end local filename = ffi.new("char[1024]") local length = ffi.C.GetModuleFileNameA(module, filename, 1024) if length == 0 or length >= 1024 then log("fl27-defensive-ai: SKIPPED image name unavailable") return end local base = tonumber(ffi.cast("uintptr_t", module)) local name = ffi.string(filename, length):match("([^\\/]+)$") local ok, result = install(base, name) log("fl27-defensive-ai: " .. (ok and "ACTIVE " or "SKIPPED ") .. result) end return m