paavohtl Claude Opus 4.6 (1M context) commited on
Commit
dfcb0dd
·
1 Parent(s): 02fb53a

refactor: add ReplayTeamEntry struct, eliminate per-field offset constants

Browse files

Create ReplayTeamEntry (0xD7B bytes, #[repr(C)]) in openwa-core with
named fields for all team data: team_type, alliance, config_abbrev,
team_name, config_name, worm_count, color, grave, soundbank, weapons.

Replace all `wb(va::G_TEAM_*_OFF + team_idx * 0xD7B, ...)` patterns
with direct struct field writes via `team.field = value`. Remove 13
G_TEAM_*_OFF constants from address.rs that are now expressed as struct
field offsets.

Log writer also updated to read team data via ReplayTeamEntry pointers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

crates/openwa-core/src/address.rs CHANGED
@@ -965,32 +965,6 @@ pub mod va {
965
  /// Worm names base (8 worms × teams, stride 0x11 per name).
966
  pub const G_WORM_NAMES: u32 = 0x0087_8097;
967
 
968
- // --- Replay team data field offsets from G_TEAM_DATA + team_idx * 0xD7B ---
969
- /// Team flag (nonzero = team present). Offset 0x124 from per-team base.
970
- pub const G_TEAM_FLAG_OFF: u32 = 0x0087_8120;
971
- /// Worm count (unvalidated). Offset 0x96 from per-team base.
972
- pub const G_TEAM_WORM_COUNT_RAW_OFF: u32 = 0x0087_8092;
973
- /// Team name (0x41 bytes). Offset 0x14 from per-team base.
974
- pub const G_TEAM_NAME_OFF: u32 = 0x0087_8010;
975
- /// Team extra byte (if obs_count > 13). Offset 0x97 from per-team base.
976
- pub const G_TEAM_EXTRA_OFF: u32 = 0x0087_8093;
977
- /// Config name (0x41 bytes). Offset 0x55 from per-team base.
978
- pub const G_TEAM_CONFIG_NAME_OFF: u32 = 0x0087_8051;
979
- /// Worm count (validated 1-8). Offset 0x98 from per-team base.
980
- pub const G_TEAM_WORM_COUNT_OFF: u32 = 0x0087_8094;
981
- /// Team color. Offset 0x99 from per-team base.
982
- pub const G_TEAM_COLOR_OFF: u32 = 0x0087_8095;
983
- /// Team flag2. Offset 0x9A from per-team base.
984
- pub const G_TEAM_FLAG2_OFF: u32 = 0x0087_8096;
985
- /// Team grave. Offset 0x123 from per-team base.
986
- pub const G_TEAM_GRAVE_OFF: u32 = 0x0087_811F;
987
- /// Team soundbank. Offset 0x125 from per-team base.
988
- pub const G_TEAM_SOUNDBANK_OFF: u32 = 0x0087_8121;
989
- /// Team soundbank extra. Offset 0x126 from per-team base.
990
- pub const G_TEAM_SOUNDBANK2_OFF: u32 = 0x0087_8122;
991
- /// Team weapon data (0xC54 bytes). Offset 0x127 from per-team base.
992
- pub const G_TEAM_WEAPONS_OFF: u32 = 0x0087_8123;
993
-
994
  // --- Replay function addresses ---
995
  /// Load string resource by ID — stdcall(id) → *const u8. RET 0x4.
996
  pub const WA_LOAD_STRING: u32 = 0x0059_3180;
 
965
  /// Worm names base (8 worms × teams, stride 0x11 per name).
966
  pub const G_WORM_NAMES: u32 = 0x0087_8097;
967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
968
  // --- Replay function addresses ---
969
  /// Load string resource by ID — stdcall(id) → *const u8. RET 0x4.
970
  pub const WA_LOAD_STRING: u32 = 0x0059_3180;
crates/openwa-core/src/engine/replay.rs CHANGED
@@ -26,6 +26,54 @@ pub const REPLAY_MAX_VERSION: u16 = 0x13;
26
  /// XOR key for game ID integrity check.
27
  pub const REPLAY_XOR_KEY: u32 = 0xEF5B_5C49;
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  /// Cursor over a replay payload byte buffer.
30
  ///
31
  /// Mirrors the 3-DWORD stream context used by WA's stream helper functions:
 
26
  /// XOR key for game ID integrity check.
27
  pub const REPLAY_XOR_KEY: u32 = 0xEF5B_5C49;
28
 
29
+ /// Per-team data in the replay global buffer (stride 0xD7B = 3,451 bytes).
30
+ ///
31
+ /// 6 entries at `G_TEAM_DATA` (0x877FFC). Populated by ReplayLoader during
32
+ /// replay file parsing. The `flag` field at offset 0x124 indicates whether
33
+ /// the team slot is active.
34
+ #[repr(C)]
35
+ pub struct ReplayTeamEntry {
36
+ /// 0x000: Team type (i8 signed, validated by `validate_team_type`).
37
+ pub team_type: u8,
38
+ /// 0x001: Alliance ID (0-5).
39
+ pub alliance: u8,
40
+ /// 0x002: Unknown byte.
41
+ pub unknown_02: u8,
42
+ /// 0x003: Config abbreviation / pre-loop worm name (null-terminated, 0x11 bytes).
43
+ pub config_abbrev: [u8; 0x11],
44
+ /// 0x014: Team name (null-terminated, 0x41 bytes).
45
+ pub team_name: [u8; 0x41],
46
+ /// 0x055: Config name (null-terminated, 0x41 bytes).
47
+ pub config_name: [u8; 0x41],
48
+ /// 0x096: Worm count (unvalidated, stored as-is from stream).
49
+ pub worm_count_raw: u8,
50
+ /// 0x097: Extra byte (only written if observer count > 13).
51
+ pub extra_byte: u8,
52
+ /// 0x098: Worm count (validated 1-8).
53
+ pub worm_count: u8,
54
+ /// 0x099: Team color index.
55
+ pub color: u8,
56
+ /// 0x09A: Secondary flag.
57
+ pub flag2: u8,
58
+ /// 0x09B-0x122: Unknown gap.
59
+ pub _unknown_09b: [u8; 0x123 - 0x09B],
60
+ /// 0x123: Grave type.
61
+ pub grave: u8,
62
+ /// 0x124: Team present flag (nonzero = active).
63
+ pub flag: u8,
64
+ /// 0x125: Speech soundbank index.
65
+ pub soundbank: u8,
66
+ /// 0x126: Speech soundbank extra.
67
+ pub soundbank_extra: u8,
68
+ /// 0x127: Weapon data (4 blocks: 0x400 + 0x154 + 0x400 + 0x300 = 0xC54 bytes).
69
+ pub weapons: [u8; 0xC54],
70
+ }
71
+
72
+ const _: () = assert!(core::mem::size_of::<ReplayTeamEntry>() == 0xD7B);
73
+
74
+ /// Maximum number of teams in a replay.
75
+ pub const REPLAY_MAX_TEAMS: usize = 6;
76
+
77
  /// Cursor over a replay payload byte buffer.
78
  ///
79
  /// Mirrors the 3-DWORD stream context used by WA's stream helper functions:
crates/openwa-wormkit/src/replacements/replay.rs CHANGED
@@ -6,7 +6,7 @@
6
  use crate::hook;
7
  use crate::log_line;
8
  use openwa_core::address::va;
9
- use openwa_core::engine::replay::{self, ReplayError, ReplayStream};
10
  use openwa_core::rebase::rb;
11
  use openwa_core::wa_alloc::{wa_free, wa_malloc};
12
 
@@ -430,35 +430,27 @@ unsafe fn parse_and_write_v2plus(
430
  wd(va::G_REPLAY_GAME_ID, xor_a ^ replay::REPLAY_XOR_KEY);
431
  }
432
 
 
433
  let mut team_count: u8 = 0;
434
- for team_idx in 0..6u32 {
435
- let tb = va::G_TEAM_DATA + team_idx * 0xD7B; // per-team base
436
  let team_flag = s.read_u8()?;
437
- wb(va::G_TEAM_FLAG_OFF + team_idx * 0xD7B, team_flag);
438
  if team_flag == 0 { continue; }
439
  team_count += 1;
440
-
441
  let team_type = s.read_u8()? as i8;
442
  if !replay::validate_team_type(team_type) { return Err(ReplayError::InvalidFormat); }
443
- wb(tb, team_type as u8);
444
-
445
- let alliance = s.read_u8_validated(0, 5)?;
446
- wb(tb + 1, alliance);
447
-
448
- let unk = s.read_u8()?;
449
- wb(tb + 2, unk);
450
 
451
- // Pre-loop worm name (config abbreviation)
452
- // Pre-loop worm name at +0x03 from per-team base
453
- // DIFF showed: 0x877FFF = team_base + 0x03 for team 0
454
- let pre_name_dest = rb(tb + 3) as *mut u8;
455
- let mut pre_name = [0u8; 0x11];
456
- s.read_worm_name(&mut pre_name, use_fixed_names)?;
457
- ptr::copy_nonoverlapping(pre_name.as_ptr(), pre_name_dest, 0x11);
458
 
459
- // 8 worm names
460
  for worm_idx in 0..8u32 {
461
- let name_off = ((team_idx as usize) * 0xCB + worm_idx as usize) * 0x11;
462
  let dest = rb(va::G_WORM_NAMES) as *mut u8;
463
  if use_fixed_names {
464
  let slice = s.advance_raw(0x11)?;
@@ -470,48 +462,33 @@ unsafe fn parse_and_write_v2plus(
470
  }
471
  }
472
 
473
- // Worm count (unvalidated)
474
- let worm_count_raw = s.read_u8()?;
475
- wb(va::G_TEAM_WORM_COUNT_RAW_OFF + team_idx * 0xD7B, worm_count_raw);
476
 
477
- // Team name
478
- let mut team_name = [0u8; 0x41];
479
- s.read_prefixed_string(&mut team_name)?;
480
- ptr::copy_nonoverlapping(team_name.as_ptr(), rb(va::G_TEAM_NAME_OFF + team_idx * 0xD7B) as *mut u8, 0x41);
481
-
482
- // Extra byte if obs_count > 13
483
  if obs_count > 13 {
484
- let extra = s.read_u8()?;
485
- wb(va::G_TEAM_EXTRA_OFF + team_idx * 0xD7B, extra);
486
  }
487
 
488
- // Config name
489
- let mut config_name = [0u8; 0x41];
490
- s.read_prefixed_string(&mut config_name)?;
491
- ptr::copy_nonoverlapping(config_name.as_ptr(), rb(va::G_TEAM_CONFIG_NAME_OFF + team_idx * 0xD7B) as *mut u8, 0x41);
492
 
493
- // Worm count (validated 1-8)
494
  let worm_count = s.read_u8()?;
495
  if worm_count == 0 || worm_count > 8 { return Err(ReplayError::InvalidFormat); }
496
- wb(va::G_TEAM_WORM_COUNT_OFF + team_idx * 0xD7B, worm_count);
497
-
498
- // Color, flag, grave, soundbank
499
- wb(va::G_TEAM_COLOR_OFF + team_idx * 0xD7B, s.read_u8()?);
500
- wb(va::G_TEAM_FLAG2_OFF + team_idx * 0xD7B, s.read_u8()?);
501
- wb(va::G_TEAM_GRAVE_OFF + team_idx * 0xD7B, s.read_u8()?);
502
- wb(va::G_TEAM_SOUNDBANK_OFF + team_idx * 0xD7B, s.read_u8()?);
503
- wb(va::G_TEAM_SOUNDBANK2_OFF + team_idx * 0xD7B, s.read_u8()?);
504
-
505
- // Weapon data blocks
506
- let weapons_dest = rb(va::G_TEAM_WEAPONS_OFF + team_idx * 0xD7B) as *mut u8;
507
  let w1 = s.advance_raw(0x400)?;
508
- ptr::copy_nonoverlapping(w1.as_ptr(), weapons_dest, 0x400);
509
  let w2 = s.advance_raw(0x154)?;
510
- ptr::copy_nonoverlapping(w2.as_ptr(), weapons_dest.add(0x400), 0x154);
511
  let w3 = s.advance_raw(0x400)?;
512
- ptr::copy_nonoverlapping(w3.as_ptr(), weapons_dest.add(0x554), 0x400);
513
  let w4 = s.advance_raw(0x300)?;
514
- ptr::copy_nonoverlapping(w4.as_ptr(), weapons_dest.add(0x954), 0x300);
515
  }
516
 
517
  if team_count == 0 { return Err(ReplayError::InvalidFormat); }
@@ -716,35 +693,31 @@ unsafe fn write_replay_log(state: u32, log_file: &mut File) -> Result<(), Replay
716
  ];
717
 
718
  // Find max color name length for alignment — only active teams
 
719
  let mut max_color_len = 0usize;
720
- for slot in 0..6u32 {
721
- let flag = *(rb(va::G_TEAM_FLAG_OFF + slot * 0xD7B) as *const u8);
722
- if flag == 0 { continue; }
723
- let ci = *(rb(va::G_TEAM_DATA + slot * 0xD7B + 1) as *const u8) as usize;
724
  if ci < 6 {
725
  let len = c_strlen(color_names[ci]);
726
  if len > max_color_len { max_color_len = len; }
727
  }
728
  }
729
 
730
- // Team data is in the team header buffer at 0x877FFC + i*0xD7B.
731
- // Per-team: +0x00=type, +0x01=alliance(color), +0x14=team_name(prefixed)
732
- let _team_count = *(rb(va::G_TEAM_COUNT) as *const u8) as u32;
733
-
734
- for slot in 0..6u32 {
735
- let tb = rb(va::G_TEAM_DATA + slot * 0xD7B) as *const u8;
736
- let flag = *(rb(va::G_TEAM_FLAG_OFF + slot * 0xD7B) as *const u8);
737
- if flag == 0 { continue; }
738
 
739
- let team_type = *tb as i8;
740
- let color_idx = *tb.add(1) as usize;
741
 
742
  let color = if color_idx < 6 { color_names[color_idx] } else { color_names[0] };
743
  let clen = c_strlen(color);
744
 
745
- // The displayed team name comes from the pre-loop name field at +0x03
746
- // (e.g., "CPU 2"), not the custom team name at +0x14 ("thrombosis1").
747
- let team_name = tb.add(0x03);
748
 
749
  let color_len = clen;
750
 
 
6
  use crate::hook;
7
  use crate::log_line;
8
  use openwa_core::address::va;
9
+ use openwa_core::engine::replay::{self, ReplayError, ReplayStream, ReplayTeamEntry};
10
  use openwa_core::rebase::rb;
11
  use openwa_core::wa_alloc::{wa_free, wa_malloc};
12
 
 
430
  wd(va::G_REPLAY_GAME_ID, xor_a ^ replay::REPLAY_XOR_KEY);
431
  }
432
 
433
+ let teams = rb(va::G_TEAM_DATA) as *mut ReplayTeamEntry;
434
  let mut team_count: u8 = 0;
435
+ for team_idx in 0..6usize {
436
+ let team = &mut *teams.add(team_idx);
437
  let team_flag = s.read_u8()?;
438
+ team.flag = team_flag;
439
  if team_flag == 0 { continue; }
440
  team_count += 1;
441
+
442
  let team_type = s.read_u8()? as i8;
443
  if !replay::validate_team_type(team_type) { return Err(ReplayError::InvalidFormat); }
444
+ team.team_type = team_type as u8;
445
+ team.alliance = s.read_u8_validated(0, 5)?;
446
+ team.unknown_02 = s.read_u8()?;
 
 
 
 
447
 
448
+ // Config abbreviation / pre-loop worm name
449
+ s.read_worm_name(&mut team.config_abbrev, use_fixed_names)?;
 
 
 
 
 
450
 
451
+ // 8 worm names (stored in separate global array, not in this struct)
452
  for worm_idx in 0..8u32 {
453
+ let name_off = ((team_idx) * 0xCB + worm_idx as usize) * 0x11;
454
  let dest = rb(va::G_WORM_NAMES) as *mut u8;
455
  if use_fixed_names {
456
  let slice = s.advance_raw(0x11)?;
 
462
  }
463
  }
464
 
465
+ team.worm_count_raw = s.read_u8()?;
466
+ s.read_prefixed_string(&mut team.team_name)?;
 
467
 
 
 
 
 
 
 
468
  if obs_count > 13 {
469
+ team.extra_byte = s.read_u8()?;
 
470
  }
471
 
472
+ s.read_prefixed_string(&mut team.config_name)?;
 
 
 
473
 
 
474
  let worm_count = s.read_u8()?;
475
  if worm_count == 0 || worm_count > 8 { return Err(ReplayError::InvalidFormat); }
476
+ team.worm_count = worm_count;
477
+ team.color = s.read_u8()?;
478
+ team.flag2 = s.read_u8()?;
479
+ team.grave = s.read_u8()?;
480
+ team.soundbank = s.read_u8()?;
481
+ team.soundbank_extra = s.read_u8()?;
482
+
483
+ // Weapon data: 4 contiguous blocks copied into weapons[0xC54]
 
 
 
484
  let w1 = s.advance_raw(0x400)?;
485
+ ptr::copy_nonoverlapping(w1.as_ptr(), team.weapons.as_mut_ptr(), 0x400);
486
  let w2 = s.advance_raw(0x154)?;
487
+ ptr::copy_nonoverlapping(w2.as_ptr(), team.weapons.as_mut_ptr().add(0x400), 0x154);
488
  let w3 = s.advance_raw(0x400)?;
489
+ ptr::copy_nonoverlapping(w3.as_ptr(), team.weapons.as_mut_ptr().add(0x554), 0x400);
490
  let w4 = s.advance_raw(0x300)?;
491
+ ptr::copy_nonoverlapping(w4.as_ptr(), team.weapons.as_mut_ptr().add(0x954), 0x300);
492
  }
493
 
494
  if team_count == 0 { return Err(ReplayError::InvalidFormat); }
 
693
  ];
694
 
695
  // Find max color name length for alignment — only active teams
696
+ let teams = rb(va::G_TEAM_DATA) as *const ReplayTeamEntry;
697
  let mut max_color_len = 0usize;
698
+ for slot in 0..6usize {
699
+ let team = &*teams.add(slot);
700
+ if team.flag == 0 { continue; }
701
+ let ci = team.alliance as usize;
702
  if ci < 6 {
703
  let len = c_strlen(color_names[ci]);
704
  if len > max_color_len { max_color_len = len; }
705
  }
706
  }
707
 
708
+ for slot in 0..6usize {
709
+ let team = &*teams.add(slot);
710
+ if team.flag == 0 { continue; }
 
 
 
 
 
711
 
712
+ let team_type = team.team_type as i8;
713
+ let color_idx = team.alliance as usize;
714
 
715
  let color = if color_idx < 6 { color_names[color_idx] } else { color_names[0] };
716
  let clen = c_strlen(color);
717
 
718
+ // The displayed team name comes from the config_abbrev field
719
+ // (e.g., "CPU 2"), not the custom team_name ("thrombosis1").
720
+ let team_name = team.config_abbrev.as_ptr();
721
 
722
  let color_len = clen;
723