feat: ReplayLoader mode routing + header parsing in Rust
Browse files- Mode 1 (play): Rust parses header (magic, version, payload)
- Modes 2/3/4: delegate to original trampoline
- Uses WA's own CRT functions (fopen/fread/fclose/malloc/free) via
rebased addresses to avoid CRT instance mismatch
- ReplayGuard RAII struct for file/payload cleanup on all paths
- Artclass counter uses signed comparison (JL) matching original
- Version-specific payload parsing still delegates to original
- Headless PASS, headful 133/0
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
crates/openwa-wormkit/src/replacements/replay.rs
CHANGED
|
@@ -1,12 +1,82 @@
|
|
| 1 |
//! Hooks for ReplayLoader and ParseReplayPosition.
|
| 2 |
//!
|
| 3 |
-
//! ReplayLoader (0x462DF0): stdcall(
|
| 4 |
//! ParseReplayPosition (0x4E3490): stdcall(input), RET 0x4.
|
| 5 |
|
| 6 |
use crate::hook;
|
| 7 |
use crate::log_line;
|
| 8 |
use openwa_core::address::va;
|
| 9 |
-
use openwa_core::engine::replay;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
// βββ Trampoline storage βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 12 |
|
|
@@ -14,39 +84,197 @@ static mut REPLAY_LOADER_ORIG: *const () = core::ptr::null();
|
|
| 14 |
#[allow(dead_code)]
|
| 15 |
static mut PARSE_POSITION_ORIG: *const () = core::ptr::null();
|
| 16 |
|
| 17 |
-
// βββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
let _ = log_line(&format!(
|
| 22 |
-
"[Replay] ReplayLoader state=0x{
|
| 23 |
));
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
let orig: unsafe extern "stdcall" fn(u32, i32) -> u32 =
|
| 26 |
core::mem::transmute(REPLAY_LOADER_ORIG);
|
| 27 |
-
let result = orig(
|
| 28 |
|
| 29 |
-
// Log fields the original populated
|
| 30 |
if result == 0 {
|
| 31 |
-
|
| 32 |
-
let replay_active = *(param_1 as *const u8).add(0xDB48);
|
| 33 |
-
let _ = log_line(&format!(
|
| 34 |
-
"[Replay] OK: version={version} active={replay_active}"
|
| 35 |
-
));
|
| 36 |
} else {
|
| 37 |
-
let _ = log_line(&format!(
|
| 38 |
-
|
| 39 |
-
));
|
| 40 |
}
|
| 41 |
-
|
| 42 |
-
result
|
| 43 |
}
|
| 44 |
|
| 45 |
// βββ ParseReplayPosition full replacement ββββββββββββββββββββββββββββββββββββ
|
| 46 |
|
| 47 |
/// ParseReplayPosition: stdcall(input: *const u8) -> i32. RET 0x4.
|
| 48 |
unsafe extern "stdcall" fn hook_parse_replay_position(input: *const u8) -> i32 {
|
| 49 |
-
// Build a safe slice from the null-terminated C string
|
| 50 |
let mut len = 0usize;
|
| 51 |
while *input.add(len) != 0 {
|
| 52 |
len += 1;
|
|
@@ -54,7 +282,7 @@ unsafe extern "stdcall" fn hook_parse_replay_position(input: *const u8) -> i32 {
|
|
| 54 |
break;
|
| 55 |
}
|
| 56 |
}
|
| 57 |
-
let slice = core::slice::from_raw_parts(input, len + 1);
|
| 58 |
replay::parse_replay_position(slice)
|
| 59 |
}
|
| 60 |
|
|
|
|
| 1 |
//! Hooks for ReplayLoader and ParseReplayPosition.
|
| 2 |
//!
|
| 3 |
+
//! ReplayLoader (0x462DF0): stdcall(state, mode), RET 0x8.
|
| 4 |
//! ParseReplayPosition (0x4E3490): stdcall(input), RET 0x4.
|
| 5 |
|
| 6 |
use crate::hook;
|
| 7 |
use crate::log_line;
|
| 8 |
use openwa_core::address::va;
|
| 9 |
+
use openwa_core::engine::replay::{self, ReplayError};
|
| 10 |
+
use openwa_core::rebase::rb;
|
| 11 |
+
|
| 12 |
+
use core::ffi::c_void;
|
| 13 |
+
use core::ptr;
|
| 14 |
+
|
| 15 |
+
// βββ WA CRT function wrappers ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 16 |
+
//
|
| 17 |
+
// WA.exe statically links MSVC 2005 CRT. We must call WA's own CRT functions
|
| 18 |
+
// via rebased addresses β NOT the Rust DLL's UCRT β because FILE* and heap
|
| 19 |
+
// state are CRT-instance-specific.
|
| 20 |
+
|
| 21 |
+
#[allow(non_camel_case_types)]
|
| 22 |
+
type FILE = c_void;
|
| 23 |
+
|
| 24 |
+
/// WA's fopen (0x5D3271)
|
| 25 |
+
unsafe fn wa_fopen(filename: *const u8, mode: *const u8) -> *mut FILE {
|
| 26 |
+
let f: unsafe extern "cdecl" fn(*const u8, *const u8) -> *mut FILE =
|
| 27 |
+
core::mem::transmute(rb(0x005D_3271));
|
| 28 |
+
f(filename, mode)
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
/// WA's fclose (0x5D399B)
|
| 32 |
+
unsafe fn wa_fclose(stream: *mut FILE) {
|
| 33 |
+
let f: unsafe extern "cdecl" fn(*mut FILE) = core::mem::transmute(rb(0x005D_399B));
|
| 34 |
+
f(stream);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
/// WA's fread wrapper (0x5D4531): fread(buf, size, count, file) β items read
|
| 38 |
+
unsafe fn wa_fread(buf: *mut c_void, size: u32, count: u32, file: *mut FILE) -> u32 {
|
| 39 |
+
let f: unsafe extern "cdecl" fn(*mut c_void, u32, u32, *mut FILE) -> u32 =
|
| 40 |
+
core::mem::transmute(rb(0x005D_4531));
|
| 41 |
+
f(buf, size, count, file)
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
/// WA's _fwrite (0x5D3B76)
|
| 45 |
+
unsafe fn wa_fwrite(buf: *const c_void, size: u32, count: u32, file: *mut FILE) -> u32 {
|
| 46 |
+
let f: unsafe extern "cdecl" fn(*const c_void, u32, u32, *mut FILE) -> u32 =
|
| 47 |
+
core::mem::transmute(rb(0x005D_3B76));
|
| 48 |
+
f(buf, size, count, file)
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
/// WA's malloc (0x5D0F65 β calls through to MSVC CRT malloc)
|
| 52 |
+
unsafe fn wa_malloc(size: u32) -> *mut u8 {
|
| 53 |
+
let f: unsafe extern "cdecl" fn(u32) -> *mut u8 =
|
| 54 |
+
core::mem::transmute(rb(0x005D_0F65));
|
| 55 |
+
f(size)
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
/// WA's free (0x5D0D2B)
|
| 59 |
+
unsafe fn wa_free(ptr: *mut u8) {
|
| 60 |
+
let f: unsafe extern "cdecl" fn(*mut u8) = core::mem::transmute(rb(0x005D_0D2B));
|
| 61 |
+
f(ptr);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
/// WA's _fileno (0x5D5155)
|
| 65 |
+
unsafe fn wa_fileno(stream: *mut FILE) -> i32 {
|
| 66 |
+
let f: unsafe extern "cdecl" fn(*mut FILE) -> i32 =
|
| 67 |
+
core::mem::transmute(rb(0x005D_5155));
|
| 68 |
+
f(stream)
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/// WA's _filelengthi64 (0x5D4FE1)
|
| 72 |
+
unsafe fn wa_filelengthi64(fd: i32) -> i64 {
|
| 73 |
+
let f: unsafe extern "cdecl" fn(i32) -> i64 = core::mem::transmute(rb(0x005D_4FE1));
|
| 74 |
+
f(fd)
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
extern "stdcall" {
|
| 78 |
+
fn SetCurrentDirectoryA(path: *const u8) -> i32;
|
| 79 |
+
}
|
| 80 |
|
| 81 |
// βββ Trampoline storage βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 82 |
|
|
|
|
| 84 |
#[allow(dead_code)]
|
| 85 |
static mut PARSE_POSITION_ORIG: *const () = core::ptr::null();
|
| 86 |
|
| 87 |
+
// βββ RAII cleanup guard βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 88 |
+
|
| 89 |
+
/// Ensures file handle and malloc'd buffer are freed on all exit paths.
|
| 90 |
+
struct ReplayGuard {
|
| 91 |
+
file: *mut FILE,
|
| 92 |
+
payload: *mut u8,
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
impl ReplayGuard {
|
| 96 |
+
fn new() -> Self {
|
| 97 |
+
Self {
|
| 98 |
+
file: ptr::null_mut(),
|
| 99 |
+
payload: ptr::null_mut(),
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
|
| 104 |
+
impl Drop for ReplayGuard {
|
| 105 |
+
fn drop(&mut self) {
|
| 106 |
+
unsafe {
|
| 107 |
+
if !self.payload.is_null() {
|
| 108 |
+
wa_free(self.payload);
|
| 109 |
+
}
|
| 110 |
+
if !self.file.is_null() {
|
| 111 |
+
wa_fclose(self.file);
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// βββ ReplayLoader replacement ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 118 |
+
|
| 119 |
+
/// ReplayLoader: stdcall(state: u32, mode: i32) -> u32. RET 0x8.
|
| 120 |
+
unsafe extern "stdcall" fn hook_replay_loader(state: u32, mode: i32) -> u32 {
|
| 121 |
+
// Modes other than 1 (play): delegate to original
|
| 122 |
+
if mode != 1 {
|
| 123 |
+
let orig: unsafe extern "stdcall" fn(u32, i32) -> u32 =
|
| 124 |
+
core::mem::transmute(REPLAY_LOADER_ORIG);
|
| 125 |
+
return orig(state, mode);
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
// Mode 1 (play): Rust implementation
|
| 129 |
+
match replay_loader_play(state) {
|
| 130 |
+
Ok(()) => 0u32,
|
| 131 |
+
Err(e) => e as i32 as u32,
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/// Rust implementation of ReplayLoader mode 1 (play).
|
| 136 |
+
///
|
| 137 |
+
/// Currently: artclass guard + delegation to original.
|
| 138 |
+
/// Incrementally replacing sections (header, payload, version parsing).
|
| 139 |
+
unsafe fn replay_loader_play(state: u32) -> Result<(), ReplayError> {
|
| 140 |
let _ = log_line(&format!(
|
| 141 |
+
"[Replay] ReplayLoader Rust path: state=0x{state:08X}"
|
| 142 |
));
|
| 143 |
|
| 144 |
+
// Guard: ArtClass counter. Assembly: CMP [0x88c790], 0x34; JL ok
|
| 145 |
+
// JL = signed less-than. Counter can be -1 (0xFFFFFFFF) which passes.
|
| 146 |
+
let artclass_counter = *(rb(va::G_ARTCLASS_COUNTER) as *const i32);
|
| 147 |
+
if artclass_counter >= 0x34 {
|
| 148 |
+
return Err(ReplayError::ArtClassLimit);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
let s = state as *mut u8;
|
| 152 |
+
|
| 153 |
+
// Set replay active flag and init counter (same as original)
|
| 154 |
+
*s.add(0xDB48) = 1;
|
| 155 |
+
*(s.add(0xEF60) as *mut u32) = 0;
|
| 156 |
+
|
| 157 |
+
// βββ Header: open file, read magic, validate version βββββββββββββββββ
|
| 158 |
+
|
| 159 |
+
SetCurrentDirectoryA(rb(0x0088_E078) as *const u8);
|
| 160 |
+
let file = wa_fopen(s.add(0xDB60), b"rb\0".as_ptr());
|
| 161 |
+
SetCurrentDirectoryA(rb(0x0088_E17D) as *const u8);
|
| 162 |
+
|
| 163 |
+
if file.is_null() {
|
| 164 |
+
return Err(ReplayError::FileNotFound);
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
let mut guard = ReplayGuard::new();
|
| 168 |
+
guard.file = file;
|
| 169 |
+
|
| 170 |
+
// File size
|
| 171 |
+
let fd = wa_fileno(file);
|
| 172 |
+
let file_size = wa_filelengthi64(fd) as u64;
|
| 173 |
+
|
| 174 |
+
// Read 4-byte header
|
| 175 |
+
let mut header: u32 = 0;
|
| 176 |
+
if wa_fread(&mut header as *mut u32 as *mut c_void, 4, 1, file) == 0 {
|
| 177 |
+
return Err(ReplayError::InvalidFormat);
|
| 178 |
+
}
|
| 179 |
+
if (header & 0xFFFF) != replay::REPLAY_MAGIC as u32 {
|
| 180 |
+
return Err(ReplayError::InvalidFormat);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
// Version: upper 16 bits, valid 1..=20
|
| 184 |
+
let version = header >> 16;
|
| 185 |
+
if version == 0 || version > 20 {
|
| 186 |
+
return Err(ReplayError::VersionTooNew);
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
*(s.add(0xDB50) as *mut u32) = version;
|
| 190 |
+
*(s.add(0xDB54) as *mut u32) = version;
|
| 191 |
+
*(s.add(0xDB58) as *mut u32) = 0xFFFF_FFFF;
|
| 192 |
+
|
| 193 |
+
// Payload size
|
| 194 |
+
let mut payload_size: u32 = 0;
|
| 195 |
+
if wa_fread(&mut payload_size as *mut u32 as *mut c_void, 4, 1, file) == 0 {
|
| 196 |
+
return Err(ReplayError::InvalidFormat);
|
| 197 |
+
}
|
| 198 |
+
if (payload_size as u64 + 8) > file_size {
|
| 199 |
+
return Err(ReplayError::InvalidFormat);
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
// Read payload
|
| 203 |
+
let payload = wa_malloc(payload_size);
|
| 204 |
+
if payload.is_null() {
|
| 205 |
+
return Err(ReplayError::MallocFailure);
|
| 206 |
+
}
|
| 207 |
+
guard.payload = payload;
|
| 208 |
+
|
| 209 |
+
if wa_fread(payload as *mut c_void, payload_size, 1, file) == 0 {
|
| 210 |
+
return Err(ReplayError::FileNotFound);
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
// βββ First dword: sub-version / content type βββββββββββββββββββββββββ
|
| 214 |
+
|
| 215 |
+
let first_dword = *(payload as *const i32);
|
| 216 |
+
*(s.add(0xDB1C) as *mut i32) = first_dword;
|
| 217 |
+
|
| 218 |
+
if first_dword >= 1 {
|
| 219 |
+
// Write payload to playback.thm
|
| 220 |
+
let thm = wa_fopen(b"data\\playback.thm\0".as_ptr(), b"wb\0".as_ptr());
|
| 221 |
+
if !thm.is_null() {
|
| 222 |
+
wa_fwrite(payload as *const c_void, 1, payload_size, thm);
|
| 223 |
+
wa_fclose(thm);
|
| 224 |
+
}
|
| 225 |
+
} else {
|
| 226 |
+
*(s.add(0xDB20) as *mut i32) = *(payload.add(4) as *const i32);
|
| 227 |
+
if first_dword >= -4 && first_dword < -2 {
|
| 228 |
+
*(s.add(0xDB24) as *mut i32) = *(payload.add(8) as *const i32);
|
| 229 |
+
} else if first_dword == -2 {
|
| 230 |
+
let copy_len = payload_size.saturating_sub(8) as usize;
|
| 231 |
+
if copy_len > 0x20 {
|
| 232 |
+
return Err(ReplayError::InvalidFormat);
|
| 233 |
+
}
|
| 234 |
+
ptr::copy_nonoverlapping(payload.add(8), s.add(0xDB24), copy_len);
|
| 235 |
+
*s.add(0xDB1C + payload_size as usize) = 0;
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
// Free payload and close file
|
| 240 |
+
wa_free(payload);
|
| 241 |
+
guard.payload = ptr::null_mut();
|
| 242 |
+
wa_fclose(file);
|
| 243 |
+
guard.file = ptr::null_mut();
|
| 244 |
+
|
| 245 |
+
// βββ Clear global buffers ββββββββββββββββββββββββββββββββββββββββββββ
|
| 246 |
+
|
| 247 |
+
core::ptr::write_bytes(rb(va::G_TEAM_HEADER_DATA) as *mut u8, 0, 0x5728);
|
| 248 |
+
core::ptr::write_bytes(rb(va::G_TEAM_SECONDARY_DATA) as *mut u8, 0, 0xD9DC);
|
| 249 |
+
|
| 250 |
+
let _ = log_line(&format!(
|
| 251 |
+
"[Replay] Header OK: ver={version} payload={payload_size} sub={}",
|
| 252 |
+
first_dword
|
| 253 |
+
));
|
| 254 |
+
|
| 255 |
+
// βββ Delegate version-specific parsing to original βββββββββββββββββββ
|
| 256 |
+
//
|
| 257 |
+
// The original re-parses from scratch. This is safe for read-only mode 1.
|
| 258 |
+
// We've already set up DB48/EF60/DB50/DB54/DB58/DB1C and cleared globals,
|
| 259 |
+
// which the original will overwrite with identical values.
|
| 260 |
+
|
| 261 |
+
let _ = log_line("[Replay] Delegating to original for version-specific parsing");
|
| 262 |
let orig: unsafe extern "stdcall" fn(u32, i32) -> u32 =
|
| 263 |
core::mem::transmute(REPLAY_LOADER_ORIG);
|
| 264 |
+
let result = orig(state, 1);
|
| 265 |
|
|
|
|
| 266 |
if result == 0 {
|
| 267 |
+
Ok(())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
} else {
|
| 269 |
+
let _ = log_line(&format!("[Replay] Original error: {}", result as i32));
|
| 270 |
+
Err(ReplayError::FileNotFound)
|
|
|
|
| 271 |
}
|
|
|
|
|
|
|
| 272 |
}
|
| 273 |
|
| 274 |
// βββ ParseReplayPosition full replacement ββββββββββββββββββββββββββββββββββββ
|
| 275 |
|
| 276 |
/// ParseReplayPosition: stdcall(input: *const u8) -> i32. RET 0x4.
|
| 277 |
unsafe extern "stdcall" fn hook_parse_replay_position(input: *const u8) -> i32 {
|
|
|
|
| 278 |
let mut len = 0usize;
|
| 279 |
while *input.add(len) != 0 {
|
| 280 |
len += 1;
|
|
|
|
| 282 |
break;
|
| 283 |
}
|
| 284 |
}
|
| 285 |
+
let slice = core::slice::from_raw_parts(input, len + 1);
|
| 286 |
replay::parse_replay_position(slice)
|
| 287 |
}
|
| 288 |
|