feat: fix critical bugs — ReplayLoader fully functional standalone
Browse filesThree critical bugs fixed:
1. Random seed: decompiler showed save/restore but assembly reveals
ReadU32 overwrites the save destination. Seed must be REPLACED
with stream value, not restored to old value.
2. FUN_00449B60 (map info copy): usercall(ESI=map_obj), was called
as cdecl with no ESI — corrupted game state.
3. FUN_004D4840 (scheme loader): stdcall(2 params), was cdecl —
stack corruption causing crash on return.
Also added:
- FUN_0053ee00 cleanup call (usercall ESI bridge)
- RegisterObserver bridge (usercall ESI + stdcall 1 param)
- Naked asm bridges: call_usercall_esi, call_register_observer
- Pre-loop worm name written to correct global offset (+0x03)
- ValidateTeamSetup: stdcall(1 param), not cdecl()
- Map loading: alloc(0x29628), construct, load, copy info, release
Headful: PASS 133/0, validation completed, no timeout.
Headless: FAIL (log output not yet implemented).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@@ -235,11 +235,20 @@ unsafe fn parse_and_write_v2plus(
|
|
| 235 |
obs_count = s.read_u16_validated(1, version as u16)?;
|
| 236 |
wd(0x88AF4C, obs_count as u32);
|
| 237 |
|
| 238 |
-
//
|
| 239 |
-
|
|
|
|
|
|
|
| 240 |
loop {
|
| 241 |
-
let
|
| 242 |
let obs_type = s.read_u8_validated(0, 2)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
if obs_type == 0 { break; }
|
| 244 |
}
|
| 245 |
}
|
|
@@ -325,10 +334,12 @@ unsafe fn parse_and_write_v2plus(
|
|
| 325 |
if r != 0 { return Err(ReplayError::InvalidFormat); }
|
| 326 |
}
|
| 327 |
|
| 328 |
-
// Random seed
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
| 332 |
} else {
|
| 333 |
// No scheme path — fall back to delegation for now
|
| 334 |
// (ProcessAllianceData reads from stream via usercall EAX)
|
|
@@ -421,10 +432,12 @@ unsafe fn parse_and_write_v2plus(
|
|
| 421 |
wb(tb + 2, unk);
|
| 422 |
|
| 423 |
// Pre-loop worm name (config abbreviation)
|
|
|
|
|
|
|
|
|
|
| 424 |
let mut pre_name = [0u8; 0x11];
|
| 425 |
s.read_worm_name(&mut pre_name, use_fixed_names)?;
|
| 426 |
-
|
| 427 |
-
// TODO: trace exact destination
|
| 428 |
|
| 429 |
// 8 worm names
|
| 430 |
for worm_idx in 0..8u32 {
|
|
@@ -486,6 +499,10 @@ unsafe fn parse_and_write_v2plus(
|
|
| 486 |
|
| 487 |
if team_count == 0 { return Err(ReplayError::InvalidFormat); }
|
| 488 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
let _ = log_line("[Replay] CP: teams done"); // ── Team count + ProcessTeamColors ────────────────────────────────────
|
| 490 |
wb(0x87D0E0, team_count);
|
| 491 |
|
|
@@ -494,7 +511,10 @@ unsafe fn parse_and_write_v2plus(
|
|
| 494 |
core::mem::transmute(rb(va::REPLAY_PROCESS_TEAM_COLORS));
|
| 495 |
process_colors(rb(va::G_REPLAY_STATE));
|
| 496 |
|
| 497 |
-
|
|
|
|
|
|
|
|
|
|
| 498 |
let map_seed = s.read_u16()?;
|
| 499 |
wd(0x87D430, map_seed as u32);
|
| 500 |
let _ = log_line(&format!("[Replay] CP: map_seed={map_seed}"));
|
|
@@ -503,7 +523,10 @@ unsafe fn parse_and_write_v2plus(
|
|
| 503 |
let fun_45d640: unsafe extern "stdcall" fn(u32) =
|
| 504 |
core::mem::transmute(rb(0x45D640));
|
| 505 |
fun_45d640(rb(va::G_REPLAY_STATE));
|
| 506 |
-
|
|
|
|
|
|
|
|
|
|
| 507 |
|
| 508 |
if map_seed == 0 || map_seed == 0xFFFF {
|
| 509 |
call_process_scheme_defaults(rb(va::G_REPLAY_STATE), rb(va::REPLAY_PROCESS_SCHEME_DEFAULTS));
|
|
@@ -518,14 +541,15 @@ unsafe fn parse_and_write_v2plus(
|
|
| 518 |
validate_setup(rb(va::G_REPLAY_STATE));
|
| 519 |
let _ = log_line("[Replay] CP: ValidateTeamSetup done");
|
| 520 |
|
| 521 |
-
|
|
|
|
| 522 |
let srand: unsafe extern "cdecl" fn(u32) = core::mem::transmute(rb(0x5D293E));
|
| 523 |
let rand_fn: unsafe extern "cdecl" fn() -> i32 = core::mem::transmute(rb(0x5D294B));
|
| 524 |
-
srand(
|
| 525 |
-
let r1 = rand_fn();
|
| 526 |
-
let r2 = rand_fn();
|
| 527 |
-
wd(va::G_RANDOM_SEED,
|
| 528 |
-
wd(va::G_SAVED_RANDOM_SEED,
|
| 529 |
let _ = log_line("[Replay] CP: random seed done");
|
| 530 |
|
| 531 |
let ver = *(rb(va::G_REPLAY_VERSION_ID) as *const i32);
|
|
@@ -538,11 +562,56 @@ unsafe fn parse_and_write_v2plus(
|
|
| 538 |
// The map was already written to playback.thm in the header section.
|
| 539 |
// The original loads it here via FUN_00447e80 + FUN_0044a9a0.
|
| 540 |
// For positive sub-version (our test case), we need to:
|
| 541 |
-
// Map loading:
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
// ── Log output ───────────────────────────────────────────────────────
|
| 548 |
// TODO: Port the ~600-line /getlog formatted output.
|
|
@@ -553,17 +622,135 @@ unsafe fn parse_and_write_v2plus(
|
|
| 553 |
Ok(())
|
| 554 |
}
|
| 555 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
// ─── Naked asm bridge for ProcessSchemeDefaults (usercall ESI=state) ─────────
|
| 557 |
|
| 558 |
-
///
|
| 559 |
-
/// ESI/EDI are LLVM-reserved, so must use naked asm.
|
| 560 |
#[unsafe(naked)]
|
| 561 |
-
unsafe extern "cdecl" fn
|
| 562 |
core::arch::naked_asm!(
|
| 563 |
"push esi",
|
| 564 |
"push edi",
|
| 565 |
-
"mov esi, [esp+12]", //
|
| 566 |
-
"mov eax, [esp+16]", //
|
| 567 |
"call eax",
|
| 568 |
"pop edi",
|
| 569 |
"pop esi",
|
|
@@ -571,6 +758,32 @@ unsafe extern "cdecl" fn call_process_scheme_defaults(_state: u32, _func: u32) {
|
|
| 571 |
);
|
| 572 |
}
|
| 573 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
// ─── ParseReplayPosition ─────────────────────────────────────────────────────
|
| 575 |
|
| 576 |
unsafe extern "stdcall" fn hook_parse_replay_position(input: *const u8) -> i32 {
|
|
|
|
| 235 |
obs_count = s.read_u16_validated(1, version as u16)?;
|
| 236 |
wd(0x88AF4C, obs_count as u32);
|
| 237 |
|
| 238 |
+
// FUN_0053ee00: cleanup observer array. usercall(ESI=0x88C35C). Plain RET.
|
| 239 |
+
call_usercall_esi(rb(0x88C35C), rb(0x53EE00));
|
| 240 |
+
|
| 241 |
+
// Observer team loop: register each observer via naked bridge
|
| 242 |
loop {
|
| 243 |
+
let team_id = s.read_u32()?;
|
| 244 |
let obs_type = s.read_u8_validated(0, 2)?;
|
| 245 |
+
// Build 4-DWORD struct: [team_id, 0, obs_type_as_u32, ???]
|
| 246 |
+
// Original: aiStack_260[0x2d]=team_id, [0x2e]=0, [0x2f]=obs_type
|
| 247 |
+
let obs_data: [u32; 4] = [team_id, 0, obs_type as u32, 0];
|
| 248 |
+
// RegisterObserver: usercall(ESI=0x88C35C) + stdcall(1 param = &data). RET 0x4.
|
| 249 |
+
call_register_observer(
|
| 250 |
+
rb(0x88C35C), obs_data.as_ptr() as u32, rb(va::REPLAY_REGISTER_OBSERVER),
|
| 251 |
+
);
|
| 252 |
if obs_type == 0 { break; }
|
| 253 |
}
|
| 254 |
}
|
|
|
|
| 334 |
if r != 0 { return Err(ReplayError::InvalidFormat); }
|
| 335 |
}
|
| 336 |
|
| 337 |
+
// Random seed: the stream value REPLACES the current seed.
|
| 338 |
+
// The decompiler misleadingly shows save/restore, but ReadU32
|
| 339 |
+
// overwrites the save destination (same EBP+0x48 local).
|
| 340 |
+
// Assembly: MOV [0x88D0B4], stream_value
|
| 341 |
+
let seed_from_stream = s.read_u32()?;
|
| 342 |
+
wd(va::G_RANDOM_SEED, seed_from_stream);
|
| 343 |
} else {
|
| 344 |
// No scheme path — fall back to delegation for now
|
| 345 |
// (ProcessAllianceData reads from stream via usercall EAX)
|
|
|
|
| 432 |
wb(tb + 2, unk);
|
| 433 |
|
| 434 |
// Pre-loop worm name (config abbreviation)
|
| 435 |
+
// Pre-loop worm name at +0x03 from per-team base
|
| 436 |
+
// DIFF showed: 0x877FFF = team_base + 0x03 for team 0
|
| 437 |
+
let pre_name_dest = rb(tb + 3) as *mut u8;
|
| 438 |
let mut pre_name = [0u8; 0x11];
|
| 439 |
s.read_worm_name(&mut pre_name, use_fixed_names)?;
|
| 440 |
+
ptr::copy_nonoverlapping(pre_name.as_ptr(), pre_name_dest, 0x11);
|
|
|
|
| 441 |
|
| 442 |
// 8 worm names
|
| 443 |
for worm_idx in 0..8u32 {
|
|
|
|
| 499 |
|
| 500 |
if team_count == 0 { return Err(ReplayError::InvalidFormat); }
|
| 501 |
|
| 502 |
+
{
|
| 503 |
+
let seed = *(rb(va::G_RANDOM_SEED) as *const u32);
|
| 504 |
+
let _ = log_line(&format!("[Replay] SEED pre-process: 0x{seed:08X}"));
|
| 505 |
+
}
|
| 506 |
let _ = log_line("[Replay] CP: teams done"); // ── Team count + ProcessTeamColors ────────────────────────────────────
|
| 507 |
wb(0x87D0E0, team_count);
|
| 508 |
|
|
|
|
| 511 |
core::mem::transmute(rb(va::REPLAY_PROCESS_TEAM_COLORS));
|
| 512 |
process_colors(rb(va::G_REPLAY_STATE));
|
| 513 |
|
| 514 |
+
{
|
| 515 |
+
let seed = *(rb(va::G_RANDOM_SEED) as *const u32);
|
| 516 |
+
let _ = log_line(&format!("[Replay] SEED post-ProcessTeamColors: 0x{seed:08X}"));
|
| 517 |
+
}
|
| 518 |
let map_seed = s.read_u16()?;
|
| 519 |
wd(0x87D430, map_seed as u32);
|
| 520 |
let _ = log_line(&format!("[Replay] CP: map_seed={map_seed}"));
|
|
|
|
| 523 |
let fun_45d640: unsafe extern "stdcall" fn(u32) =
|
| 524 |
core::mem::transmute(rb(0x45D640));
|
| 525 |
fun_45d640(rb(va::G_REPLAY_STATE));
|
| 526 |
+
{
|
| 527 |
+
let seed = *(rb(va::G_RANDOM_SEED) as *const u32);
|
| 528 |
+
let _ = log_line(&format!("[Replay] SEED post-FUN_45d640: 0x{seed:08X}"));
|
| 529 |
+
}
|
| 530 |
|
| 531 |
if map_seed == 0 || map_seed == 0xFFFF {
|
| 532 |
call_process_scheme_defaults(rb(va::G_REPLAY_STATE), rb(va::REPLAY_PROCESS_SCHEME_DEFAULTS));
|
|
|
|
| 541 |
validate_setup(rb(va::G_REPLAY_STATE));
|
| 542 |
let _ = log_line("[Replay] CP: ValidateTeamSetup done");
|
| 543 |
|
| 544 |
+
// Assembly: MOV ESI,[seed]; PUSH ESI; CALL srand; rand(); SHL<<16; rand(); ADD
|
| 545 |
+
let current_seed = *(rb(va::G_RANDOM_SEED) as *const u32);
|
| 546 |
let srand: unsafe extern "cdecl" fn(u32) = core::mem::transmute(rb(0x5D293E));
|
| 547 |
let rand_fn: unsafe extern "cdecl" fn() -> i32 = core::mem::transmute(rb(0x5D294B));
|
| 548 |
+
srand(current_seed);
|
| 549 |
+
let r1 = rand_fn() as u32;
|
| 550 |
+
let r2 = rand_fn() as u32;
|
| 551 |
+
wd(va::G_RANDOM_SEED, r2 + (r1 << 16)); // new seed
|
| 552 |
+
wd(va::G_SAVED_RANDOM_SEED, current_seed); // save old seed
|
| 553 |
let _ = log_line("[Replay] CP: random seed done");
|
| 554 |
|
| 555 |
let ver = *(rb(va::G_REPLAY_VERSION_ID) as *const i32);
|
|
|
|
| 562 |
// The map was already written to playback.thm in the header section.
|
| 563 |
// The original loads it here via FUN_00447e80 + FUN_0044a9a0.
|
| 564 |
// For positive sub-version (our test case), we need to:
|
| 565 |
+
// Map loading: construct map object, load playback.thm, copy info, release.
|
| 566 |
+
if *(state as *const i32).byte_add(0xDB1C) >= 1 {
|
| 567 |
+
let _ = log_line("[Replay] CP: map load start");
|
| 568 |
+
// Alloc: PUSH 0x29628; CALL 005C0AB8; ADD ESP,4 — cdecl(size)
|
| 569 |
+
let alloc: unsafe extern "cdecl" fn(u32) -> u32 =
|
| 570 |
+
core::mem::transmute(rb(0x5C0AB8));
|
| 571 |
+
let buf = alloc(0x29628);
|
| 572 |
+
let _ = log_line(&format!("[Replay] CP: alloc buf=0x{buf:08X}"));
|
| 573 |
+
let map_obj = if buf != 0 {
|
| 574 |
+
// Construct: PUSH 1; PUSH buf; CALL 00447E80 — stdcall(buf, 1)
|
| 575 |
+
let construct: unsafe extern "stdcall" fn(u32, i32) -> *mut u32 =
|
| 576 |
+
core::mem::transmute(rb(0x447E80));
|
| 577 |
+
construct(buf, 1)
|
| 578 |
+
} else {
|
| 579 |
+
ptr::null_mut()
|
| 580 |
+
};
|
| 581 |
+
let _ = log_line(&format!("[Replay] CP: map_obj=0x{:08X}", map_obj as u32));
|
| 582 |
+
|
| 583 |
+
// Load: PUSH 0; PUSH path; PUSH map_obj; CALL 0044A9A0 — stdcall(3)
|
| 584 |
+
let load: unsafe extern "stdcall" fn(*mut u32, *const u8, i32) -> i32 =
|
| 585 |
+
core::mem::transmute(rb(0x44A9A0));
|
| 586 |
+
let ok = load(map_obj, b"data\\playback.thm\0".as_ptr(), 0);
|
| 587 |
+
let _ = log_line(&format!("[Replay] CP: load={ok}"));
|
| 588 |
+
|
| 589 |
+
if ok == 0 {
|
| 590 |
+
// Load failed — release map_obj and return error
|
| 591 |
+
if !map_obj.is_null() {
|
| 592 |
+
let vtable = *map_obj;
|
| 593 |
+
let release: unsafe extern "thiscall" fn(*mut u32, i32) =
|
| 594 |
+
core::mem::transmute(*(vtable as *const u32).add(1));
|
| 595 |
+
release(map_obj, 1);
|
| 596 |
+
}
|
| 597 |
+
return Err(ReplayError::MapLoadFailure);
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
// FUN_00449B60: usercall(ESI=map_obj). Copies map info to game state.
|
| 601 |
+
call_usercall_esi(map_obj as u32, rb(0x449B60));
|
| 602 |
+
let _ = log_line("[Replay] CP: map info copied");
|
| 603 |
+
|
| 604 |
+
// Terrain flag: CMP [ESI+0x29618],0; SETZ
|
| 605 |
+
*(state as *mut u8).add(0xD98B) =
|
| 606 |
+
(*(map_obj as *const u8).add(0x29618) == 0) as u8;
|
| 607 |
+
|
| 608 |
+
// Release: PUSH 1; MOV ECX,ESI; CALL [vtable+4] — thiscall(this, 1)
|
| 609 |
+
let vtable = *map_obj;
|
| 610 |
+
let release: unsafe extern "thiscall" fn(*mut u32, i32) =
|
| 611 |
+
core::mem::transmute(*(vtable as *const u32).add(1));
|
| 612 |
+
release(map_obj, 1);
|
| 613 |
+
let _ = log_line("[Replay] CP: map released");
|
| 614 |
+
}
|
| 615 |
|
| 616 |
// ── Log output ───────────────────────────────────────────────────────
|
| 617 |
// TODO: Port the ~600-line /getlog formatted output.
|
|
|
|
| 622 |
Ok(())
|
| 623 |
}
|
| 624 |
|
| 625 |
+
/// Dump key replay globals for comparison.
|
| 626 |
+
unsafe fn dump_replay_globals(label: &str) {
|
| 627 |
+
let state = rb(va::G_REPLAY_STATE);
|
| 628 |
+
let team_base = rb(0x877FFC);
|
| 629 |
+
|
| 630 |
+
// Key scalars
|
| 631 |
+
let ver_id = *(rb(va::G_REPLAY_VERSION_ID) as *const i32);
|
| 632 |
+
let scheme = *(rb(va::G_REPLAY_SCHEME_PRESENT) as *const u32);
|
| 633 |
+
let team_count = *(rb(0x87D0E0) as *const u8);
|
| 634 |
+
let seed = *(rb(va::G_RANDOM_SEED) as *const u32);
|
| 635 |
+
let saved_seed = *(rb(va::G_SAVED_RANDOM_SEED) as *const u32);
|
| 636 |
+
let map_seed = *(rb(0x87D430) as *const u32);
|
| 637 |
+
let host = *(rb(0x8779E0) as *const u32);
|
| 638 |
+
|
| 639 |
+
let _ = log_line(&format!(
|
| 640 |
+
"[{label}] ver_id={ver_id} scheme={scheme} teams={team_count} seed=0x{seed:08X} saved=0x{saved_seed:08X} map_seed={map_seed} host=0x{host:08X}"
|
| 641 |
+
));
|
| 642 |
+
|
| 643 |
+
// Per-team: dump first 32 bytes of per-team region for comparison
|
| 644 |
+
for i in 0..2u32 {
|
| 645 |
+
let off = (i * 0xD7B) as usize;
|
| 646 |
+
let base = team_base as *const u8;
|
| 647 |
+
// Dump bytes at key offsets: +0 (type), +1 (alliance), +2, then +0x14 (team name area)
|
| 648 |
+
let typ = *base.add(off);
|
| 649 |
+
let flag = *base.add(off + 0x124);
|
| 650 |
+
let wc = *base.add(off + 0x98);
|
| 651 |
+
// Team name: offset +0x14 in per-team block, prefixed string
|
| 652 |
+
let n_base = base.add(off + 0x14);
|
| 653 |
+
let n_hex: String = core::slice::from_raw_parts(n_base, 16).iter()
|
| 654 |
+
.map(|b| format!("{b:02X}")).collect::<Vec<_>>().join(" ");
|
| 655 |
+
let _ = log_line(&format!(
|
| 656 |
+
"[{label}] team[{i}]: flag={flag} type=0x{typ:02X} wc={wc} name_area={n_hex}"
|
| 657 |
+
));
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
// Checksums of major global regions
|
| 661 |
+
let team_hdr = core::slice::from_raw_parts(rb(va::G_TEAM_HEADER_DATA) as *const u8, 0x5728);
|
| 662 |
+
let team_sec = core::slice::from_raw_parts(rb(va::G_TEAM_SECONDARY_DATA) as *const u8, 0xD9DC);
|
| 663 |
+
let scheme_area = core::slice::from_raw_parts(rb(0x88DACC) as *const u8, 0x200);
|
| 664 |
+
let hdr_sum: u32 = team_hdr.iter().fold(0u32, |a, &b| a.wrapping_add(b as u32));
|
| 665 |
+
let sec_sum: u32 = team_sec.iter().fold(0u32, |a, &b| a.wrapping_add(b as u32));
|
| 666 |
+
let sch_sum: u32 = scheme_area.iter().fold(0u32, |a, &b| a.wrapping_add(b as u32));
|
| 667 |
+
let _ = log_line(&format!(
|
| 668 |
+
"[{label}] checksums: team_hdr=0x{hdr_sum:08X} team_sec=0x{sec_sum:08X} scheme=0x{sch_sum:08X}"
|
| 669 |
+
));
|
| 670 |
+
|
| 671 |
+
// Save RUST snapshot, then on ORIG pass find first 5 differences
|
| 672 |
+
// Check critical scalar globals
|
| 673 |
+
let sub_fmt = *(rb(va::G_REPLAY_SUB_FORMAT) as *const u32);
|
| 674 |
+
let af42 = *(rb(0x88AF42) as *const u8);
|
| 675 |
+
let af44 = *(rb(0x88AF44) as *const u8);
|
| 676 |
+
let af4c = *(rb(0x88AF4C) as *const u32);
|
| 677 |
+
let xor_id = *(rb(va::G_REPLAY_GAME_ID) as *const u32);
|
| 678 |
+
let map_b1 = *(rb(0x87250C) as *const u8);
|
| 679 |
+
let map_b2 = *(rb(0x872508) as *const u8);
|
| 680 |
+
let d0de = *(rb(0x87D0DE) as *const u8);
|
| 681 |
+
let d0e0 = *(rb(0x87D0E0) as *const u8);
|
| 682 |
+
let state_40 = *(rb(va::G_REPLAY_STATE + 0x40) as *const u8);
|
| 683 |
+
let state_48c = *(rb(va::G_REPLAY_STATE + 0x48C) as *const u8);
|
| 684 |
+
let state_fcc = *(rb(va::G_REPLAY_STATE + 0xFCBC) as *const u32);
|
| 685 |
+
let _ = log_line(&format!(
|
| 686 |
+
"[{label}] sub_fmt={sub_fmt} af42={af42} af44={af44} af4c={af4c} xor=0x{xor_id:08X}"
|
| 687 |
+
));
|
| 688 |
+
let _ = log_line(&format!(
|
| 689 |
+
"[{label}] map=({map_b1},{map_b2}) d0de={d0de} d0e0={d0e0} st40={state_40} st48c={state_48c} fcc=0x{state_fcc:08X}"
|
| 690 |
+
));
|
| 691 |
+
|
| 692 |
+
// Also compare team_sec
|
| 693 |
+
static mut RUST_SEC: Option<*mut u8> = None;
|
| 694 |
+
if label == "RUST" {
|
| 695 |
+
let buf = wa_malloc(0xD9DC);
|
| 696 |
+
ptr::copy_nonoverlapping(team_sec.as_ptr(), buf, 0xD9DC);
|
| 697 |
+
RUST_SEC = Some(buf);
|
| 698 |
+
} else if let Some(rust_buf) = RUST_SEC {
|
| 699 |
+
let rust_data = core::slice::from_raw_parts(rust_buf, 0xD9DC);
|
| 700 |
+
let mut diffs = 0;
|
| 701 |
+
for i in 0..0xD9DCusize {
|
| 702 |
+
if rust_data[i] != team_sec[i] {
|
| 703 |
+
if diffs < 10 {
|
| 704 |
+
let ghidra_addr = 0x87D438u32 + i as u32;
|
| 705 |
+
let _ = log_line(&format!(
|
| 706 |
+
"[DIFF2] sec[0x{i:04X}] @0x{ghidra_addr:08X}: RUST=0x{:02X} ORIG=0x{:02X}",
|
| 707 |
+
rust_data[i], team_sec[i]
|
| 708 |
+
));
|
| 709 |
+
}
|
| 710 |
+
diffs += 1;
|
| 711 |
+
}
|
| 712 |
+
}
|
| 713 |
+
let _ = log_line(&format!("[DIFF2] Total team_sec diffs: {diffs}"));
|
| 714 |
+
wa_free(rust_buf);
|
| 715 |
+
RUST_SEC = None;
|
| 716 |
+
}
|
| 717 |
+
|
| 718 |
+
static mut RUST_HDR: Option<*mut u8> = None;
|
| 719 |
+
if label == "RUST" {
|
| 720 |
+
let buf = wa_malloc(0x5728);
|
| 721 |
+
ptr::copy_nonoverlapping(team_hdr.as_ptr(), buf, 0x5728);
|
| 722 |
+
RUST_HDR = Some(buf);
|
| 723 |
+
} else if let Some(rust_buf) = RUST_HDR {
|
| 724 |
+
let rust_data = core::slice::from_raw_parts(rust_buf, 0x5728);
|
| 725 |
+
let mut diffs = 0;
|
| 726 |
+
for i in 0..0x5728usize {
|
| 727 |
+
if rust_data[i] != team_hdr[i] {
|
| 728 |
+
if diffs < 10 {
|
| 729 |
+
let ghidra_addr = 0x8779E4u32 + i as u32;
|
| 730 |
+
let _ = log_line(&format!(
|
| 731 |
+
"[DIFF] hdr[0x{i:04X}] @0x{ghidra_addr:08X}: RUST=0x{:02X} ORIG=0x{:02X}",
|
| 732 |
+
rust_data[i], team_hdr[i]
|
| 733 |
+
));
|
| 734 |
+
}
|
| 735 |
+
diffs += 1;
|
| 736 |
+
}
|
| 737 |
+
}
|
| 738 |
+
let _ = log_line(&format!("[DIFF] Total team_hdr diffs: {diffs}"));
|
| 739 |
+
wa_free(rust_buf);
|
| 740 |
+
RUST_HDR = None;
|
| 741 |
+
}
|
| 742 |
+
}
|
| 743 |
+
|
| 744 |
// ─── Naked asm bridge for ProcessSchemeDefaults (usercall ESI=state) ─────────
|
| 745 |
|
| 746 |
+
/// Bridge for usercall(ESI=value) + plain call. cdecl(esi_val, func_addr).
|
|
|
|
| 747 |
#[unsafe(naked)]
|
| 748 |
+
unsafe extern "cdecl" fn call_usercall_esi(_esi_val: u32, _func: u32) {
|
| 749 |
core::arch::naked_asm!(
|
| 750 |
"push esi",
|
| 751 |
"push edi",
|
| 752 |
+
"mov esi, [esp+12]", // esi_val
|
| 753 |
+
"mov eax, [esp+16]", // func_addr
|
| 754 |
"call eax",
|
| 755 |
"pop edi",
|
| 756 |
"pop esi",
|
|
|
|
| 758 |
);
|
| 759 |
}
|
| 760 |
|
| 761 |
+
/// Bridge for RegisterObserver: usercall(ESI=array) + stdcall(1 param=data_ptr).
|
| 762 |
+
/// cdecl(esi_val, data_ptr, func_addr).
|
| 763 |
+
#[unsafe(naked)]
|
| 764 |
+
unsafe extern "cdecl" fn call_register_observer(
|
| 765 |
+
_esi_val: u32, _data_ptr: u32, _func: u32,
|
| 766 |
+
) {
|
| 767 |
+
core::arch::naked_asm!(
|
| 768 |
+
"push esi",
|
| 769 |
+
"push edi",
|
| 770 |
+
"mov esi, [esp+12]", // esi_val (0x88C35C rebased)
|
| 771 |
+
"push [esp+16]", // data_ptr → stdcall param
|
| 772 |
+
"mov eax, [esp+24]", // func_addr (shifted by push)
|
| 773 |
+
"call eax", // stdcall cleans 1 param (4 bytes)
|
| 774 |
+
"pop edi",
|
| 775 |
+
"pop esi",
|
| 776 |
+
"ret",
|
| 777 |
+
);
|
| 778 |
+
}
|
| 779 |
+
|
| 780 |
+
/// Bridge for ProcessSchemeDefaults: usercall(ESI=state).
|
| 781 |
+
/// Reuses call_usercall_esi.
|
| 782 |
+
#[inline]
|
| 783 |
+
unsafe fn call_process_scheme_defaults(state: u32, func: u32) {
|
| 784 |
+
call_usercall_esi(state, func);
|
| 785 |
+
}
|
| 786 |
+
|
| 787 |
// ─── ParseReplayPosition ─────────────────────────────────────────────────────
|
| 788 |
|
| 789 |
unsafe extern "stdcall" fn hook_parse_replay_position(input: *const u8) -> i32 {
|