paavohtl Claude Opus 4.6 (1M context) commited on
Commit
4acd2d4
·
1 Parent(s): b169d13

feat: parse version 2+ team data — full replay validation

Browse files

Correctly parses per-team data: type, alliance, pre-name (config),
8 worm names (prefixed for game_ver >= 10, fixed for < 10),
worm count (unvalidated + validated), team name, config name,
color/flag/grave/soundbank bytes, and 4 weapon data blocks
(0x400 + 0x154 + 0x400 + 0x300 = 3156 bytes per team).

Key findings from hex dump analysis:
- use_fixed_names is INVERTED: game_ver < 10 = fixed, >= 10 = prefixed
(assembly: SETL, not SETGE)
- First worm_count byte (DAT_00878092) stored WITHOUT validation
- Second worm_count byte (DAT_00878094) validated 1-8
- Team type 0xD8/-40 (CPU level 2) and 0xEC/-20 (CPU level 1) are valid
negative team types for AI teams

Test replay (bots.WAgame): 2 teams parsed, 0 observers, game_ver=500,
scheme v1. Headless PASS.

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

crates/openwa-wormkit/src/replacements/replay.rs CHANGED
@@ -327,7 +327,9 @@ unsafe fn parse_v2plus_payload(
327
  return Err(ReplayError::VersionTooNew);
328
  }
329
 
330
- let _use_fixed_names = game_version_id >= 10;
 
 
331
 
332
  // ─── Scheme presence flag ────────────────────────────────────────────
333
 
@@ -418,16 +420,95 @@ unsafe fn parse_v2plus_payload(
418
  let _byte2 = s.read_u8()?;
419
  }
420
 
421
- // ─── XOR game ID + team entries ──────────────────────────────────────
422
- // TODO: Team parsing needs more decompilation work.
423
- // The per-team structure (flag, type, alliance, worm names, weapon blocks)
424
- // has version-dependent fields that need careful tracing from assembly.
425
- // For now, return what we've successfully parsed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426
 
427
  Ok(ReplayInfo {
428
  game_version_id,
429
  scheme_version,
430
- team_count: 0, // TODO: parse teams
431
  observer_count,
432
  })
433
  }
 
327
  return Err(ReplayError::VersionTooNew);
328
  }
329
 
330
+ // Assembly: CMP EBX,0xA; SETL → flag=1 if game_ver < 10
331
+ // Fixed 0x11-byte names for OLD formats (< 10), prefixed for modern (>= 10)
332
+ let use_fixed_names = game_version_id < 10;
333
 
334
  // ─── Scheme presence flag ────────────────────────────────────────────
335
 
 
420
  let _byte2 = s.read_u8()?;
421
  }
422
 
423
+ // ─── XOR game ID (obs_count >= 16) ──────────────────────────────────
424
+
425
+ if obs_count >= 16 {
426
+ let xor_a = s.read_u32()?;
427
+ let _xor_b = s.read_u32()?;
428
+ let _game_id = xor_a ^ replay::REPLAY_XOR_KEY;
429
+ }
430
+
431
+ // ─── Team entries (up to 6, stride 0xD7B in global buffer) ───────────
432
+
433
+ let mut team_count: u8 = 0;
434
+
435
+ for _team_idx in 0..6u32 {
436
+ // Team flag: 0 = empty slot, non-zero = active team
437
+ let team_flag = s.read_u8()?;
438
+ if team_flag == 0 {
439
+ continue;
440
+ }
441
+ team_count += 1;
442
+
443
+ // Per-team structure (traced from hex dump + decompilation):
444
+ // type, alliance(0-5), unk_byte, pre-name(prefixed), 8×worm_name,
445
+ // worm_count, team_name(prefixed), [extra if obs>13], config_name,
446
+ // worm_count2, color, flag, grave, soundbank_flag, soundbank
447
+
448
+ let team_type = s.read_u8()? as i8;
449
+ if !replay::validate_team_type(team_type) {
450
+ return Err(ReplayError::InvalidFormat);
451
+ }
452
+
453
+ let _alliance = s.read_u8_validated(0, 5)?;
454
+ let _unk_byte = s.read_u8()?;
455
+
456
+ // Pre-loop name field (ReadWormName in decompilation)
457
+ // For bots.WAgame this reads "CPU 2" / "CPU 1" — NOT the team name
458
+ let mut _pre_name = [0u8; 0x11];
459
+ s.read_worm_name(&mut _pre_name, use_fixed_names)?;
460
+
461
+ // 8 worm names
462
+ for _worm_idx in 0..8u32 {
463
+ let mut _worm_name = [0u8; 0x11];
464
+ s.read_worm_name(&mut _worm_name, use_fixed_names)?;
465
+ }
466
+
467
+ // Worm count byte 1 (DAT_00878092) — stored WITHOUT validation
468
+ let _worm_count_raw = s.read_u8()?;
469
+
470
+ // Team name (prefixed string, max 0x40)
471
+ let mut _team_name = [0u8; 0x41];
472
+ s.read_prefixed_string(&mut _team_name)?;
473
+
474
+ // Extra byte if obs_count > 13
475
+ if obs_count > 13 {
476
+ let _extra = s.read_u8()?;
477
+ }
478
+
479
+ // Config/country name (prefixed string, max 0x40)
480
+ let mut _config_name = [0u8; 0x41];
481
+ s.read_prefixed_string(&mut _config_name)?;
482
+
483
+ // Worm count byte 2 (DAT_00878094) — validated 1-8
484
+ // Assembly: if (7 < (value - 1) unsigned) throw
485
+ let _worm_count = s.read_u8()?;
486
+ if _worm_count == 0 || _worm_count > 8 {
487
+ return Err(ReplayError::InvalidFormat);
488
+ }
489
+
490
+ // Color, flag, grave, soundbank_flag, soundbank
491
+ let _color = s.read_u8()?;
492
+ let _flag_byte = s.read_u8()?;
493
+ let _grave = s.read_u8()?;
494
+ let _soundbank_flag = s.read_u8()?;
495
+ let _soundbank = s.read_u8()?;
496
+
497
+ // Weapon data blocks
498
+ s.skip(0x400)?; // weapon ammo (1024 bytes)
499
+ s.skip(0x154)?; // weapon delay (340 bytes)
500
+ s.skip(0x400)?; // weapon ammo 2 (1024 bytes)
501
+ s.skip(0x300)?; // weapon data 3 (768 bytes)
502
+ }
503
+
504
+ if team_count == 0 {
505
+ return Err(ReplayError::InvalidFormat);
506
+ }
507
 
508
  Ok(ReplayInfo {
509
  game_version_id,
510
  scheme_version,
511
+ team_count,
512
  observer_count,
513
  })
514
  }