[, $file, $from, $to] = $argv + [null, null, null, null]; if ($file === null || $from === null || $to === null) { fwrite(STDERR, "usage: php rewrite_paths.php \n"); exit(2); } $d = file_get_contents($file); if (substr($d, 0, 4) !== 'CRYO') { fwrite(STDERR, "$file is not a .cryo file\n"); exit(1); } $out = substr($d, 0, 16); $off = 16; $changed = 0; while ($off + 12 <= strlen($d)) { $tag = substr($d, $off, 4); $len = unpack('P', $d, $off + 4)[1]; $payload = substr($d, $off + 12, $len); if ($tag === 'STRS') { $n = unpack('V', $payload)[1]; $q = 4; $strs = ''; for ($i = 0; $i < $n; $i++) { $l = unpack('V', $payload, $q)[1]; $s = substr($payload, $q + 4, $l); $q += 4 + $l; if (str_contains($s, $from)) { $s = str_replace($from, $to, $s); $changed++; } $strs .= pack('V', strlen($s)) . $s; } $payload = pack('V', $n) . $strs; } $out .= $tag . pack('P', strlen($payload)) . $payload; $off += 12 + $len; } file_put_contents($file, $out); echo basename($file), ": $changed strings rewritten\n";