paavohtl Claude Opus 4.6 (1M context) commited on
Commit
a6758d9
·
1 Parent(s): 9db9cd5

feat: add Snapshot trait for canonicalized game state dumps

Browse files

Defines the core trait in openwa-core::snapshot with helpers:
- write_indent, fmt_ptr, fmt_ptr32 for formatting
- write_raw_region for hex-dumping unknown struct regions
with automatic pointer canonicalization

Trait impls for DDGame, WormEntry, CTaskWorm, CTaskMissile
and the capture/CLI infrastructure are next.

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

crates/openwa-core/src/lib.rs CHANGED
@@ -16,6 +16,7 @@ pub mod fixed;
16
  pub mod log;
17
  pub mod mem;
18
  pub mod rebase;
 
19
  pub mod task;
20
  pub mod vtable;
21
  pub mod wa;
 
16
  pub mod log;
17
  pub mod mem;
18
  pub mod rebase;
19
+ pub mod snapshot;
20
  pub mod task;
21
  pub mod vtable;
22
  pub mod wa;
crates/openwa-core/src/snapshot.rs ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //! Game state snapshot trait and helpers.
2
+ //!
3
+ //! Types implement [`Snapshot`] to produce canonicalized, diff-friendly text
4
+ //! representations of their state. Pointer values are replaced with `<ptr>`
5
+ //! or `null`, keeping only deterministic simulation state.
6
+
7
+ use core::fmt;
8
+
9
+ use crate::mem;
10
+ use crate::rebase::rb;
11
+ use crate::address::va;
12
+
13
+ /// Trait for types that can write a canonicalized snapshot.
14
+ ///
15
+ /// Implementations skip or canonicalize pointer fields and format values
16
+ /// for human-readable diffing. The output should be deterministic across
17
+ /// runs (same game state → same text).
18
+ pub trait Snapshot {
19
+ /// Write a human-readable, canonicalized representation.
20
+ ///
21
+ /// # Safety
22
+ /// `self` must point to valid, initialized memory.
23
+ unsafe fn write_snapshot(&self, w: &mut dyn fmt::Write, indent: usize) -> fmt::Result;
24
+ }
25
+
26
+ // ── Helpers ──
27
+
28
+ /// Write `indent * 2` spaces.
29
+ pub fn write_indent(w: &mut dyn fmt::Write, indent: usize) -> fmt::Result {
30
+ for _ in 0..indent {
31
+ w.write_str(" ")?;
32
+ }
33
+ Ok(())
34
+ }
35
+
36
+ /// Format a pointer field: `<ptr>` if non-null, `null` if null.
37
+ pub fn fmt_ptr(ptr: *const u8) -> &'static str {
38
+ if ptr.is_null() { "null" } else { "<ptr>" }
39
+ }
40
+
41
+ /// Format a pointer field for a raw u32 that might be a pointer.
42
+ pub fn fmt_ptr32(val: u32) -> &'static str {
43
+ if val == 0 { "null" } else { "<ptr>" }
44
+ }
45
+
46
+ /// Dump a raw memory region as canonicalized hex lines.
47
+ ///
48
+ /// Each line: `+OFFSET: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX`
49
+ ///
50
+ /// DWORD-aligned values that look like pointers (per `mem::classify_pointer`)
51
+ /// are replaced with `[ptr---]` to canonicalize heap addresses.
52
+ ///
53
+ /// # Safety
54
+ /// `base` must point to `size` readable bytes.
55
+ #[cfg(target_arch = "x86")]
56
+ pub unsafe fn write_raw_region(
57
+ w: &mut dyn fmt::Write,
58
+ base: *const u8,
59
+ size: usize,
60
+ indent: usize,
61
+ ) -> fmt::Result {
62
+ let delta = rb(va::IMAGE_BASE).wrapping_sub(va::IMAGE_BASE);
63
+
64
+ for row_start in (0..size).step_by(16) {
65
+ write_indent(w, indent)?;
66
+ write!(w, "+{:04X}:", row_start)?;
67
+
68
+ let row_end = (row_start + 16).min(size);
69
+
70
+ // Process as DWORDs for pointer detection
71
+ for dw_off in (row_start..row_end).step_by(4) {
72
+ w.write_char(' ')?;
73
+ if dw_off + 4 <= size {
74
+ let val = *(base.add(dw_off) as *const u32);
75
+ if is_likely_pointer(val, delta) {
76
+ w.write_str("[ptr---]")?;
77
+ } else {
78
+ write!(w, "{:08X}", val)?;
79
+ }
80
+ } else {
81
+ // Partial DWORD at end — dump remaining bytes
82
+ for b in dw_off..row_end {
83
+ write!(w, "{:02X}", *base.add(b))?;
84
+ }
85
+ }
86
+ }
87
+ w.write_char('\n')?;
88
+ }
89
+ Ok(())
90
+ }
91
+
92
+ /// Heuristic: is this value likely a heap/code/data pointer?
93
+ #[cfg(target_arch = "x86")]
94
+ fn is_likely_pointer(val: u32, delta: u32) -> bool {
95
+ if val < 0x10000 {
96
+ return false;
97
+ }
98
+ // Check if it's in a known WA section (.text through .data end)
99
+ let ghidra = val.wrapping_sub(delta);
100
+ if ghidra >= va::TEXT_START && ghidra < va::DATA_END {
101
+ return true;
102
+ }
103
+ // Heap pointer heuristic: > 1MB and readable
104
+ if val > 0x100000 {
105
+ return unsafe { mem::can_read(val, 4) };
106
+ }
107
+ false
108
+ }