<?phpnamespace App\Alis;// Port of alis/src/patches.c (MIT, Olivier Huguenot, Vadim Kindl).// Script bug fixes, applied at load on an exact byte match (tables in PatchesVars)./** $data: offset of the unpacked script in Alis::$mem. */function script_fix(string $name, int $data, int $data_sz): void{ $kFixes = count(PatchesVars::script_fixes); $at = []; for ($f = 0; $f < $kFixes; $f++) { [$uid, $what, $len, $le_old, $be_old, $le_new, $be_new, $call_fix, $call_at, $call_to] = PatchesVars::script_fixes[$f]; $at[$f] = -1; if ($uid != Alis::$platform->uid) { continue; } if ($call_fix && $at[$call_fix - 1] < 0) { continue; // call target not in this script } $old = pack('C*', ...constant(PatchesVars::class . '::' . (Alis::$platform->is_little_endian ? $le_old : $be_old))); $rep = pack('C*', ...constant(PatchesVars::class . '::' . (Alis::$platform->is_little_endian ? $le_new : $be_new))); $old = str_pad($old, $len, "\0"); $rep = str_pad($rep, $len, "\0"); // unused tail is zero (never reached) if ($len > $data_sz) { continue; } $i = strpos(substr(Alis::$mem, $data, $data_sz), $old); if ($i === false) { continue; } mem_write($data + $i, $rep); if ($call_fix) { $off = $at[$call_fix - 1] + $call_to - ($i + $call_at + 2); $p = $data + $i + $call_at; if (Alis::$platform->is_little_endian) { Alis::$mem[$p] = chr($off & 0xff); Alis::$mem[$p + 1] = chr(($off >> 8) & 0xff); } else { Alis::$mem[$p] = chr(($off >> 8) & 0xff); Alis::$mem[$p + 1] = chr($off & 0xff); } } $at[$f] = $i; }}