-- FL27 Normal referee match calibration and delayed FallDown review. local ffi = ffi local m = {} local CODE_BYTES, LEDGER_BYTES = 0x1000, 0x590 local CONTEXT = 0x36f3dc8 local MATCH_GATES, MATCH_BINDING = 0x580, 0x588 local POINTERS = 0x6f0 local SECTIONS = { {0, 0x5b5, 0x3b1afd4, 0x2ebd2c0, "3a765b00f27f5b00c0d2eb02", "2108020008680b0070745b003a765b00a8d2eb02"}, {0x5c0, 0x6ea, 0x3c9033c, 0x1b952544, "9016f703f616f7034425951b", "0106020006320230"}, } local THRESHOLD_SITE = 0x3fa8802 local THRESHOLD_BYTES = "66448b3dbeb53004664181f7fc506644893feb0566c7072850" local SITES = { {0x5b7db9, "488b0508c01303", 0x240}, {0x5b776f, "488b86b8030000", 0}, {0x3f716b4, "38d9760831c0", 0x5c0}, } local function bytes(hex) return (hex:gsub("%s+", ""):gsub("..", function(pair) return string.char(tonumber(pair, 16)) end)) end -- Generated from fl27_referee_delayed_falldown.asm without relocations. local RELAYS = bytes([[ 4889042448895424084c894424104c894c2418807e08000f85f4010000488b842488000000 807801110f85e20100004183fe160f83d80100004183ff160f83ce0100004183fe0b410f93 c04183ff0b410f93c24538d00f84b50100004c8b4c2470418079010d0f85a5010000450fb6 51444183ea024183fa010f8792010000450fb7511c4585d20f8484010000664539511a0f87 79010000488b5424584439ba781400000f8567010000450fb651444439522c0f8558010000 448b82741400004585c00f8448010000488b0520060000488b004885c00f8435010000488b 80700200004885c00f842501000083b86c110000010f851801000083b854150000000f850b 010000443b80981100000f87fe0000004c8b1dde0500004589f241c1e2064d01d3f0410fba 2b000f82e100000041837b04010f85cf00000080ba8c140000010f84c200000041f7410800 8000000f85b4000000493943080f85aa000000493973100f85a0000000493953180f859600 0000453943200f858c00000045397b240f8582000000450fb7511866453953287576450fb7 511c664539532c756a450fb651444538532e755f4c8b92801400004d3953347552448b9288 1400004539533c7545448b9098110000453b53307638450fb7511a66453b532a722c41c743 040200000041c70300000000488b0424488b5424084c8b4424104c8b4c24184c8b1d010500 0041ffe341c70300000000488b0424488b5424084c8b4424104c8b4c2418488b86b8030000 4c8b1dce04000041ffe3cccccccccccccccccccccc4889042448895424084c894424104c89 4c24184183fe160f83310300004183ff160f83270300004183fe0b410f93c04183ff0b410f 93c24538d00f840e0300004c8b4c2470418079010d0f85fe020000450fb651444183ea0241 83fa010f87eb020000450fb7511c4585d20f84dd020000664539511a0f87d2020000488b54 24584439ba781400000f85c0020000450fb651444439522c0f85b1020000448b8274140000 4585c00f84a1020000488b05fc030000488b004885c00f848e020000488b80700200004885 c00f847e02000083b86c110000010f857102000083b854150000000f8564020000443b8098 1100000f87570200004c8b1dba0300004589f241c1e2064d01d3f0410fba2b000f823a0200 0041837b04000f84bc000000493943080f85b2000000493973100f85a8000000493953180f 859e000000453943200f859400000045397b240f858a000000450fb751186645395328757e 450fb7511c664539532c7572450fb651444538532e75674c8b92801400004d395334755a44 8b92881400004539533c754d448b9098110000453b5330724041837b04020f84a10100004c 8b94248800000041807a01110f848601000085ff0f857e010000837c2444000f8573010000 807e08000f8569010000e96c0100004c8b94248800000041807a01100f855901000041f741 08008000000f844b0100004989430849897310498953184589432045897b24458b51184589 5328450fb7511c664589532c450fb651444588532e448b9098110000458953304c8b928014 00004d895334448b92881400004589533c85ff0f85eb000000837c2444000f85e000000080 7e08000f85d600000041ba28000000f74424600008000074254c8b1558020000450fb64201 4183f050450fb6124181f2fc0000004183f8500f858d0000004183ff160f83830000004183 fa2874064183fa3c757783b86c11000001756e83b854150000007565488b15eb0100004c8b 82800500004d85c074524c8b8a880500004d85c975134989c131c0f04c0fb18a8805000075 034c89c84c39c8752e4c3b828005000075254183fa3c750441c1e8104183ff0b720441c1e8 08450fb6c867418d51ff83fa4e77034589ca4438942410010000730a41c7430401000000eb 0841c743040200000041c70300000000488b0424488b5424084c8b4424104c8b4c2418488b 0548010000488b004c8b1d5e01000041ffe3cccccccccccccccccccccc488b442428483b05 54010000740d483b05530100000f85f500000066817c2439506e0f85e80000004183fe160f 83de0000004183ff160f83d40000004183fe0b410f93c04183ff0b410f93c14538c80f84bb 000000440fb6d1488b05d0000000488b004885c00f84a4000000488b80700200004885c00f 84940000004183ff160f838a0000004183fa2874064183fa3c757e83b86c11000001757583 b85415000000756c488b158d0000004c8b82800500004d85c074594c8b8a880500004d85c9 75134989c131c0f04c0fb18a8805000075034c89c84c39c875354c3b8280050000752c4183 fa3c750441c1e8104183ff0b720441c1e808450fb6c867418d51ff83fa4e770a4589ca4138 da7612eb0438d9760c31c04c8b1d5300000041ffe34c8b1d5100000041ffe3cccccccccccc 88776655443322119988776655443322aa99887766554433bbaa998877665544ccbbaa9988 776655ddccbbaa99887766eeddccbbaa998877ffeeddccbbaa998800ffeeddccbbaa991100 ffeeddccbbaa ]]) local function i32(value) return memory.pack("i32", value) end local function u64(value) return memory.pack("u64", value) end function m.preflight(base, name) if type(name) ~= "string" or name:lower() ~= "fl_2027.exe" then return false, "requires Normal FL27" end if not base or base == 0 or memory.read(base, 2) ~= "MZ" then return false, "invalid image" end local offset = memory.unpack("u32", memory.read(base + 0x3c, 4)) if offset < 0x40 or offset >= 0x1000 then return false, "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\0\0\x64\x86" or image_size < 0x1b95254c or image_size >= 0x60000000 or memory.read(base + THRESHOLD_SITE, #THRESHOLD_BYTES / 2) ~= bytes(THRESHOLD_BYTES) then return false, "unsupported FL27 judge or threshold layout" end for _, section in ipairs(SECTIONS) do if memory.read(base + section[3], 12) ~= bytes(section[5]) or memory.read(base + section[4], #section[6] / 2) ~= bytes(section[6]) then return false, "unsupported FL27 unwind layout" end end for _, spec in ipairs(SITES) do local expected = bytes(spec[2]) if memory.read(base + spec[1], #expected) ~= expected then return false, string.format("judge instruction mismatch at RVA %x", spec[1]) end end return true end function m.build(base, code, ledger) local pointers = { base + CONTEXT, ledger, base + 0x5b7776, base + 0x5b77ba, base + 0x5b7dc0, base + 0x82b3dc8, base + 0x5b787a, base + 0x5b78e9, base + 0x3f716ba, base + 0x3f716c0, } local targets = {} for index, address in ipairs(pointers) do targets[index] = u64(address) end local blob = RELAYS:sub(1, POINTERS) .. table.concat(targets) local entries = {} local relocation = code - base for index, section in ipairs(SECTIONS) do entries[index] = i32(relocation + section[1]) .. i32(relocation + section[2]) .. i32(relocation + #blob) blob = blob .. "\x21\0\0\0" .. bytes(section[5]) end local table_offset = #blob blob = blob .. table.concat(entries) local sites = {} for _, spec in ipairs(SITES) do local address, original = base + spec[1], bytes(spec[2]) local distance = code + spec[3] - address - 5 assert(distance >= -2147483648 and distance <= 2147483647, "relay out of range") sites[#sites+1] = { address = address, original = original, patch = "\xe9" .. i32(distance) .. string.rep("\x90", #original - 5), } end return blob, sites, table_offset, #entries end function m.gates(original, shared, normal) local midpoint = original * (1 + shared) local difference = 1 + 0.02 * midpoint * normal local center = math.max(1, math.min(79, math.floor(midpoint + 0.5))) local offset = math.floor(math.abs(difference) + 0.5) offset = math.min(offset, center - 1, 79 - center) if difference < 0 then offset = -offset end return center - offset, center + offset end function m.normals(first, second) local radius = math.sqrt(-2 * math.log((first + 0.5) / 4294967296)) local angle = 2 * math.pi * (second + 0.5) / 4294967296 return 0.05 * radius * math.cos(angle), radius * math.sin(angle) end local function pointer(address) return ffi.cast("void*", address) end local function protect(address, length, mode, old) return ffi.C.VirtualProtect(pointer(address), length, mode, old) ~= 0 end local function flush(address, length) return ffi.C.FlushInstructionCache(ffi.C.GetCurrentProcess(), pointer(address), length) ~= 0 end local function put(address, value) ffi.copy(pointer(address), value, #value) return ffi.string(pointer(address), #value) == value end local function write_site(site, data) local old = ffi.new("uint32_t[1]") if not protect(site.address, #data, 0x40, old) then return false, false end site.protection = site.protection or tonumber(old[0]) local copied = put(site.address, data) local flushed = flush(site.address, #data) local restored = protect(site.address, #data, site.protection, old) return copied and flushed and restored, true end local function allocate_near(base) local pe = base + memory.unpack("u32", memory.read(base + 0x3c, 4)) local image_size = memory.unpack("u32", memory.read(pe + 80, 4)) local first = math.ceil((base + image_size) / 0x10000) * 0x10000 for address = first, first + 0x10000000 - 1, 0x10000 do local result = ffi.C.VirtualAlloc(pointer(address), CODE_BYTES, 0x3000, 0x04) if result ~= nil then return tonumber(ffi.cast("uintptr_t", result)) end end end local function release(address) if address then ffi.C.VirtualFree(pointer(address), 0, 0x8000) end end local active = false local function activate(base, name) if active then return true, "already active" end local valid, message = m.preflight(base, name) if not valid then return false, message end local allocated = ffi.C.VirtualAlloc(nil, LEDGER_BYTES, 0x3000, 0x04) if allocated == nil then return false, "contact ledger allocation failed" end local ledger = tonumber(ffi.cast("uintptr_t", allocated)) local code = allocate_near(base) if not code then release(ledger); return false, "near relay allocation failed" end local blob, sites, table_offset, unwind_count = m.build(base, code, ledger) if #blob > CODE_BYTES or not put(code, blob) or not protect(code, CODE_BYTES, 0x20, ffi.new("uint32_t[1]")) or not flush(code, #blob) then release(code); release(ledger) return false, "unable to prepare relay code" end local function_table = ffi.cast("uint32_t*", code + table_offset) if ffi.C.RtlAddFunctionTable(function_table, unwind_count, base) == 0 then release(code); release(ledger) return false, "unable to register judge unwind information" end local done = {} for _, site in ipairs(sites) do if memory.read(site.address, #site.original) ~= site.original then message = "judge changed during activation" break end local changed, attempted = write_site(site, site.patch) if attempted then done[#done+1] = site end if not changed then message = "judge relay write failed" break end end if message then local rollback = true for index = #done, 1, -1 do local site = done[index] rollback = write_site(site, site.original) and rollback end if rollback and ffi.C.RtlDeleteFunctionTable(function_table) ~= 0 then release(code); release(ledger) end return false, message .. "; rollback=" .. tostring(rollback) end active = true return true, "delayed FallDown and match referee gates ready", ledger end local function link_match_setup(ctx, base, ledger, rng) local bias = ffi.cast("uint64_t*", ledger + MATCH_GATES) local binding = ffi.cast("uint64_t*", ledger + MATCH_BINDING) local seed = ffi.new("uint32_t[2]") local address_value = ffi.new("uint64_t[1]") local phase = ffi.new("uint32_t[1]") local process = ffi.C.GetCurrentProcess() local serial, current = 0, nil local function is_running_bound_match(match) if match == 0 then return false end -- set_teams can run during world teardown or before the new world exists. local read = ffi.C.ReadProcessMemory if read(process, pointer(base + CONTEXT), address_value, 8, nil) == 0 then return false end local root = tonumber(address_value[0]) if root == 0 or read(process, pointer(root + 0x270), address_value, 8, nil) == 0 or tonumber(address_value[0]) ~= match then return false end return read(process, pointer(match + 0x116c), phase, 4, nil) ~= 0 and phase[0] == 1 end -- Sider retains the module table in the Lua state that owns the callback. -- Its separate event stack does not keep this closure alive for that GC. m.set_teams = function(info, home, away) if type(home) ~= "number" or type(away) ~= "number" or home < 0 or home >= 0x1ffff or away < 0 or away >= 0x1ffff then bias[0] = 0 binding[0] = 0 current = nil return end if current and current.home == home and current.away == away and current.id == info.match_id and current.leg == info.match_leg and is_running_bound_match(tonumber(binding[0])) then return -- duplicate setup during the already running match end -- Publish zero while replacing the match binding, so the native -- selector can always fall back to the committed stock threshold. bias[0] = 0 binding[0] = 0 current = nil if rng.BCryptGenRandom(nil, ffi.cast("uint8_t*", seed), 8, 2) ~= 0 then log("fl27-referee-match-bias: stock gates for this match (Windows RNG unavailable)") return end local shared, normal = m.normals(tonumber(seed[0]), tonumber(seed[1])) local home40, away40 = m.gates(40, shared, normal) local home60, away60 = m.gates(60, shared, normal) serial = serial % 65535 + 1 local publication = serial * 4294967296 + home40 + away40 * 256 + home60 * 65536 + away60 * 16777216 current = {home = home, away = away, id = info.match_id, leg = info.match_leg} bias[0] = publication log(string.format("fl27-referee-match-bias: draw %d home=%d away=%d shared=%+.3f%% Z=%+.5f effective victim H/A 40=%d/%d 60=%d/%d (recorder lists native thresholds)", serial, home, away, shared * 100, normal, home40, away40, home60, away60)) end ctx.register("set_teams", m.set_teams) end function m.init(ctx) if not ffi then log("fl27-referee-delayed-falldown: SKIPPED LuaJIT FFI 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 protection, uint32_t* old); void* GetCurrentProcess(void); int ReadProcessMemory(void* process, const void* address, void* buffer, size_t size, size_t* read); 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); int32_t BCryptGenRandom(void* algorithm, uint8_t* buffer, uint32_t size, uint32_t flags); ]] local image = ffi.C.GetModuleHandleA(nil) if image == nil then log("fl27-referee-delayed-falldown: SKIPPED main image unavailable"); return end local filename = ffi.new("char[1024]") local length = ffi.C.GetModuleFileNameA(image, filename, 1024) if length == 0 or length >= 1024 then log("fl27-referee-delayed-falldown: SKIPPED image name unavailable") return end local base = tonumber(ffi.cast("uintptr_t", image)) local name = ffi.string(filename, length):match("([^\\/]+)$") if active then log("fl27-referee-delayed-falldown: ACTIVE already active"); return end local rng = ctx and ctx.register and ffi.load("bcrypt") local ok, message, ledger = activate(base, name) if ok and rng then link_match_setup(ctx, base, ledger, rng) end log("fl27-referee-delayed-falldown: " .. (ok and "ACTIVE " or "SKIPPED ") .. message) end return m