paavohtl Claude Opus 4.6 (1M context) commited on
Commit
853d394
Β·
1 Parent(s): 9c16cae

feat: add DisplayBase vtable struct, Rust headless vtable, and purecall patching

Browse files

Rename DDDisplayBase -> DisplayBase across codebase. Define DisplayBase
struct with named fields from constructor analysis, DisplayBaseVtable
with 32 typed slots, SpriteCollection sub-object, and static
HEADLESS_VTABLE with all no-op stubs.

Patch WA.exe's primary DisplayBase vtable (0x6645F8) at init time:
replace 22 _purecall slots with safe Rust no-ops, benefiting both
headless (DisplayBase) and normal (DisplayGfx) modes.

Add wa_free() to wa_alloc module alongside wa_malloc().

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

crates/openwa-core/src/address.rs CHANGED
@@ -45,9 +45,11 @@ pub mod va {
45
  pub const DDKEYBOARD_VTABLE: u32 = 0x0066_AEC8;
46
  /// Palette vtable (0x28-byte palette object)
47
  pub const PALETTE_VTABLE_MAYBE: u32 = 0x0066_A2E4;
48
- /// DDDisplayBase headless vtable β€” overlaid after DDDisplayBase__Constructor
49
- /// in headless mode, filling in stub slots for headless operation.
50
- pub const DD_DISPLAY_BASE_HEADLESS_VTABLE: u32 = 0x0066_A0F8;
 
 
51
  /// Input controller vtable (0x1800-byte object, set inline before FUN_0058C0D0)
52
  pub const INPUT_CTRL_VTABLE: u32 = 0x0066_B3FC;
53
  /// TaskStateMachine vtable
@@ -277,9 +279,9 @@ pub mod va {
277
  pub const DDISPLAY_INIT: u32 = 0x0056_9D00;
278
  /// Alias kept for callers that use the old name.
279
  pub const CONSTRUCT_DD_DISPLAY: u32 = DDISPLAY_INIT;
280
- /// DDDisplayBase__Constructor β€” stdcall(this) β†’ DDDisplayBase*.
281
  /// Constructs the 0x3560-byte base display object (used standalone in headless mode).
282
- pub const DD_DISPLAY_BASE_CTOR: u32 = 0x0052_2DB0;
283
  /// Streaming audio constructor β€” stdcall(IDirectSound*, path_config_ptr) β†’ *mut u8.
284
  /// Constructs 0x354-byte streaming audio object (only if GameInfo+0xDAA4 != 0).
285
  pub const STREAMING_AUDIO_CTOR: u32 = 0x0058_BC10;
 
45
  pub const DDKEYBOARD_VTABLE: u32 = 0x0066_AEC8;
46
  /// Palette vtable (0x28-byte palette object)
47
  pub const PALETTE_VTABLE_MAYBE: u32 = 0x0066_A2E4;
48
+ /// DisplayBase primary vtable (set by constructor, has _purecall slots)
49
+ pub const DISPLAY_BASE_VTABLE: u32 = 0x0066_45F8;
50
+ /// DisplayBase headless vtable β€” overlaid after constructor in headless mode,
51
+ /// filling in stub slots for headless operation.
52
+ pub const DISPLAY_BASE_HEADLESS_VTABLE: u32 = 0x0066_A0F8;
53
  /// Input controller vtable (0x1800-byte object, set inline before FUN_0058C0D0)
54
  pub const INPUT_CTRL_VTABLE: u32 = 0x0066_B3FC;
55
  /// TaskStateMachine vtable
 
279
  pub const DDISPLAY_INIT: u32 = 0x0056_9D00;
280
  /// Alias kept for callers that use the old name.
281
  pub const CONSTRUCT_DD_DISPLAY: u32 = DDISPLAY_INIT;
282
+ /// DisplayBase__Constructor β€” stdcall(this) β†’ DisplayBase*.
283
  /// Constructs the 0x3560-byte base display object (used standalone in headless mode).
284
+ pub const DISPLAY_BASE_CTOR: u32 = 0x0052_2DB0;
285
  /// Streaming audio constructor β€” stdcall(IDirectSound*, path_config_ptr) β†’ *mut u8.
286
  /// Constructs 0x354-byte streaming audio object (only if GameInfo+0xDAA4 != 0).
287
  pub const STREAMING_AUDIO_CTOR: u32 = 0x0058_BC10;
crates/openwa-core/src/dd_display_base.rs DELETED
@@ -1,44 +0,0 @@
1
- /// DDDisplayBase β€” base class of the display subsystem hierarchy.
2
- ///
3
- /// Constructor: DDDisplayBase__Constructor (0x522DB0), stdcall(this) β†’ DDDisplayBase*.
4
- /// Vtable (primary): 0x6645F8 (set by constructor).
5
- /// Vtable (headless overlay): 0x66A0F8 (fills in stub slots for headless mode).
6
- /// Size: 0x3560 bytes.
7
- ///
8
- /// Inheritance:
9
- /// ```text
10
- /// DDDisplayBase (this) ← vtable 0x6645F8 / 0x66A0F8
11
- /// └─ DisplayGfx (derived) ← vtable 0x66A218
12
- /// ```
13
- ///
14
- /// In headless mode (`GameInfo.headless_mode != 0`), only the base is constructed
15
- /// with the headless vtable overlay. In normal mode, `DisplayGfx` (derived) is
16
- /// constructed instead. The session's `display` field holds a polymorphic pointer
17
- /// to whichever variant.
18
- #[repr(C)]
19
- pub struct DDDisplayBase {
20
- pub vtable: *mut u8,
21
- pub _unknown_004: [u8; 0x3560 - 4],
22
- }
23
-
24
- const _: () = assert!(core::mem::size_of::<DDDisplayBase>() == 0x3560);
25
-
26
- impl DDDisplayBase {
27
- /// Allocate and construct a DDDisplayBase for headless mode.
28
- ///
29
- /// Calls WA's native constructor, then overlays the headless vtable.
30
- ///
31
- /// # Safety
32
- /// Must be called from within the WA.exe process.
33
- pub unsafe fn construct() -> *mut Self {
34
- use crate::address::va;
35
- use crate::rebase::rb;
36
- use crate::wa_alloc::WABox;
37
- let this = WABox::<Self>::alloc(0x3560, 0x3560).leak();
38
- let ctor: unsafe extern "stdcall" fn(*mut Self) -> *mut Self =
39
- core::mem::transmute(rb(va::DD_DISPLAY_BASE_CTOR) as usize);
40
- ctor(this);
41
- (*this).vtable = rb(va::DD_DISPLAY_BASE_HEADLESS_VTABLE) as *mut u8;
42
- this
43
- }
44
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
crates/openwa-core/src/display_base.rs ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /// DisplayBase β€” base class of the display subsystem hierarchy.
2
+ ///
3
+ /// Constructor: DisplayBase__Constructor (0x522DB0), stdcall(this) β†’ DisplayBase*.
4
+ /// Vtable (primary): 0x6645F8 (set by constructor, has _purecall slots).
5
+ /// Vtable (headless overlay): 0x66A0F8 (fills in stub slots for headless mode).
6
+ /// Size: 0x3560 bytes.
7
+ ///
8
+ /// Inheritance:
9
+ /// ```text
10
+ /// DisplayBase (this) ← vtable 0x6645F8 / 0x66A0F8
11
+ /// └─ DisplayGfx (derived) ← vtable 0x66A218
12
+ /// ```
13
+ ///
14
+ /// In headless mode (`GameInfo.headless_mode != 0`), only the base is constructed
15
+ /// with the headless vtable overlay. In normal mode, `DisplayGfx` (derived) is
16
+ /// constructed instead. The session's `display` field holds a polymorphic pointer
17
+ /// to whichever variant.
18
+
19
+ /// Ptr32 alias for raw pointer fields (compiles on 64-bit host).
20
+ type Ptr32 = u32;
21
+
22
+ #[repr(C)]
23
+ pub struct DisplayBase {
24
+ // +0x000: vtable pointer
25
+ pub vtable: *const DisplayBaseVtable,
26
+ // +0x004: sprite collection sub-object (0x3C control block + 0x80000 buffer)
27
+ pub sprite_collection: Ptr32,
28
+ // +0x008..0x3008: three contiguous 0x400-entry u32 arrays, all zeroed by ctor
29
+ pub _buf_008: [u32; 0xC00],
30
+ // +0x3008..0x3018: gap (0x10 bytes, 4 u32s: indices 0xC02..0xC05)
31
+ pub _gap_3008: [u32; 4],
32
+ // +0x3018: field at u32 index 0xC06, zeroed by ctor
33
+ pub _field_3018: u32,
34
+ // +0x301C..0x309C: gap (0x80 bytes, 32 u32s: indices 0xC07..0xC26)
35
+ pub _gap_301c: [u32; 32],
36
+ // +0x309C..0x311C: indices 0xC27..0xC46 (32 entries), zeroed by ctor
37
+ pub _fields_309c: [u32; 32],
38
+ // +0x311C..0x312C: indices 0xC47..0xC4A (4 entries), zeroed by ctor
39
+ pub _fields_311c: [u32; 4],
40
+ // +0x312C: index 0xC4B, zeroed by ctor
41
+ pub _field_312c: u32,
42
+ // +0x3130..0x352C: slot_table β€” 0xFF entries, all initialized to 1 by ctor
43
+ pub slot_table: [u32; 0xFF],
44
+ // +0x352C: index 0xD4B = 0xFFFFFFFF
45
+ pub _field_352c: u32,
46
+ // +0x3530..0x3540: indices 0xD4C..0xD4F (4 entries), zeroed by ctor
47
+ pub _fields_3530: [u32; 4],
48
+ // +0x3540..0x3560: remaining padding to fill 0x3560 total
49
+ pub _pad_3540: [u8; 0x3560 - 0x3540],
50
+ }
51
+
52
+ const _: () = assert!(core::mem::size_of::<DisplayBase>() == 0x3560);
53
+
54
+ /// Vtable layout for DisplayBase (32 slots based on headless vtable at 0x66A0F8).
55
+ ///
56
+ /// In headless mode, most slots are no-op stubs. The primary vtable (0x6645F8)
57
+ /// has _purecall for the drawing slots; the headless overlay replaces them.
58
+ #[repr(C)]
59
+ pub struct DisplayBaseVtable {
60
+ /// Slot 0: destructor β€” thiscall(this, flags)
61
+ pub destructor: unsafe extern "thiscall" fn(*mut DisplayBase, u8) -> *mut DisplayBase,
62
+ /// Slot 1: thiscall(this)
63
+ pub slot_01: unsafe extern "thiscall" fn(*mut DisplayBase),
64
+ /// Slot 2-3: thiscall(this) β€” identical in headless (0x4AA060)
65
+ pub slot_02: unsafe extern "thiscall" fn(*mut DisplayBase),
66
+ pub slot_03: unsafe extern "thiscall" fn(*mut DisplayBase),
67
+ pub slot_04: unsafe extern "thiscall" fn(*mut DisplayBase),
68
+ pub slot_05: unsafe extern "thiscall" fn(*mut DisplayBase),
69
+ pub slot_06: unsafe extern "thiscall" fn(*mut DisplayBase),
70
+ pub slot_07: unsafe extern "thiscall" fn(*mut DisplayBase),
71
+ pub slot_08: unsafe extern "thiscall" fn(*mut DisplayBase),
72
+ pub slot_09: unsafe extern "thiscall" fn(*mut DisplayBase),
73
+ pub slot_10: unsafe extern "thiscall" fn(*mut DisplayBase),
74
+ pub slot_11: unsafe extern "thiscall" fn(*mut DisplayBase),
75
+ pub slot_12: unsafe extern "thiscall" fn(*mut DisplayBase),
76
+ pub slot_13: unsafe extern "thiscall" fn(*mut DisplayBase),
77
+ pub slot_14: unsafe extern "thiscall" fn(*mut DisplayBase),
78
+ pub slot_15: unsafe extern "thiscall" fn(*mut DisplayBase),
79
+ pub slot_16: unsafe extern "thiscall" fn(*mut DisplayBase),
80
+ pub slot_17: unsafe extern "thiscall" fn(*mut DisplayBase),
81
+ pub slot_18: unsafe extern "thiscall" fn(*mut DisplayBase),
82
+ pub slot_19: unsafe extern "thiscall" fn(*mut DisplayBase),
83
+ pub slot_20: unsafe extern "thiscall" fn(*mut DisplayBase),
84
+ pub slot_21: unsafe extern "thiscall" fn(*mut DisplayBase),
85
+ pub slot_22: unsafe extern "thiscall" fn(*mut DisplayBase),
86
+ pub slot_23: unsafe extern "thiscall" fn(*mut DisplayBase),
87
+ pub slot_24: unsafe extern "thiscall" fn(*mut DisplayBase),
88
+ pub slot_25: unsafe extern "thiscall" fn(*mut DisplayBase),
89
+ pub slot_26: unsafe extern "thiscall" fn(*mut DisplayBase),
90
+ pub slot_27: unsafe extern "thiscall" fn(*mut DisplayBase),
91
+ pub slot_28: unsafe extern "thiscall" fn(*mut DisplayBase),
92
+ pub slot_29: unsafe extern "thiscall" fn(*mut DisplayBase),
93
+ pub slot_30: unsafe extern "thiscall" fn(*mut DisplayBase),
94
+ pub slot_31: unsafe extern "thiscall" fn(*mut DisplayBase),
95
+ }
96
+
97
+ const _: () = assert!(core::mem::size_of::<DisplayBaseVtable>() == 32 * 4);
98
+
99
+ // ── No-op stub functions for headless vtable ──────────────────────────────
100
+
101
+ unsafe extern "thiscall" fn noop_thiscall(_this: *mut DisplayBase) {}
102
+
103
+ unsafe extern "thiscall" fn headless_destructor(
104
+ this: *mut DisplayBase,
105
+ flags: u8,
106
+ ) -> *mut DisplayBase {
107
+ // Free sprite collection sub-object if present.
108
+ let sc = (*this).sprite_collection;
109
+ if sc != 0 {
110
+ let sc_ptr = sc as *mut SpriteCollection;
111
+ // Free the buffer, then the control block.
112
+ let buf = (*sc_ptr).buffer;
113
+ if buf != 0 {
114
+ wa_free(buf as *mut u8);
115
+ }
116
+ wa_free(sc_ptr as *mut u8);
117
+ }
118
+ // If bit 0 of flags is set, free `this` too (scalar delete).
119
+ if flags & 1 != 0 {
120
+ wa_free(this as *mut u8);
121
+ }
122
+ this
123
+ }
124
+
125
+ /// Static headless vtable β€” all slots are no-ops except the destructor.
126
+ ///
127
+ /// In headless mode there's no rendering, so every drawing method is a no-op.
128
+ /// The "shared real implementation" slots (4-10, 27, 29-31) that exist in the
129
+ /// original vtable are also no-ops here since they're never called in headless.
130
+ static HEADLESS_VTABLE: DisplayBaseVtable = DisplayBaseVtable {
131
+ destructor: headless_destructor,
132
+ slot_01: noop_thiscall,
133
+ slot_02: noop_thiscall,
134
+ slot_03: noop_thiscall,
135
+ slot_04: noop_thiscall,
136
+ slot_05: noop_thiscall,
137
+ slot_06: noop_thiscall,
138
+ slot_07: noop_thiscall,
139
+ slot_08: noop_thiscall,
140
+ slot_09: noop_thiscall,
141
+ slot_10: noop_thiscall,
142
+ slot_11: noop_thiscall,
143
+ slot_12: noop_thiscall,
144
+ slot_13: noop_thiscall,
145
+ slot_14: noop_thiscall,
146
+ slot_15: noop_thiscall,
147
+ slot_16: noop_thiscall,
148
+ slot_17: noop_thiscall,
149
+ slot_18: noop_thiscall,
150
+ slot_19: noop_thiscall,
151
+ slot_20: noop_thiscall,
152
+ slot_21: noop_thiscall,
153
+ slot_22: noop_thiscall,
154
+ slot_23: noop_thiscall,
155
+ slot_24: noop_thiscall,
156
+ slot_25: noop_thiscall,
157
+ slot_26: noop_thiscall,
158
+ slot_27: noop_thiscall,
159
+ slot_28: noop_thiscall,
160
+ slot_29: noop_thiscall,
161
+ slot_30: noop_thiscall,
162
+ slot_31: noop_thiscall,
163
+ };
164
+
165
+ // ── Sprite collection sub-object ──────────────────────────────────────────
166
+
167
+ /// Sprite collection control block (0x3C bytes).
168
+ /// Created by FUN_004fa860 in the original constructor.
169
+ #[repr(C)]
170
+ pub struct SpriteCollection {
171
+ /// Pointer to 0x80000-byte buffer
172
+ pub buffer: Ptr32,
173
+ /// Buffer capacity
174
+ pub capacity: u32,
175
+ pub _fields_08: [u32; 5],
176
+ pub _pad_1c: [u8; 0x3C - 0x1C],
177
+ }
178
+
179
+ const _: () = assert!(core::mem::size_of::<SpriteCollection>() == 0x3C);
180
+
181
+ use crate::wa_alloc::wa_free;
182
+
183
+ // ── Construction ──────────────────────────────────────────────────────────
184
+
185
+ impl DisplayBase {
186
+ /// Construct a DisplayBase for headless mode, entirely in Rust.
187
+ ///
188
+ /// Replaces the WA native constructor (0x522DB0) + headless vtable overlay.
189
+ /// Allocates the struct and sprite collection sub-object on WA's heap.
190
+ ///
191
+ /// # Safety
192
+ /// Must be called from within the WA.exe process (uses wa_malloc).
193
+ pub unsafe fn new_headless() -> *mut Self {
194
+ use crate::wa_alloc::WABox;
195
+
196
+ let this = WABox::<Self>::alloc(0x3560, 0x3560).leak();
197
+
198
+ // Set vtable to our Rust headless vtable.
199
+ (*this).vtable = &HEADLESS_VTABLE;
200
+
201
+ // Initialize slot_table: 0xFF entries = 1
202
+ for slot in &mut (*this).slot_table {
203
+ *slot = 1;
204
+ }
205
+
206
+ // _field_352c = 0xFFFFFFFF (sentinel)
207
+ (*this)._field_352c = 0xFFFF_FFFF;
208
+
209
+ // Create sprite collection sub-object.
210
+ let sc = WABox::<SpriteCollection>::alloc(0x3C, 0x3C).leak();
211
+ let buf = crate::wa_alloc::wa_malloc(0x80020);
212
+ core::ptr::write_bytes(buf, 0, 0x80000);
213
+ (*sc).buffer = buf as u32;
214
+ (*sc).capacity = 0x80000;
215
+
216
+ (*this).sprite_collection = sc as u32;
217
+ // NOTE: The original FUN_004fa860 also sets a secondary vtable (0x664188)
218
+ // on the parent object via implicit EDI. This is not yet replicated here,
219
+ // which is why new_headless() doesn't work yet β€” see TODO in hardware_init.rs.
220
+
221
+ this
222
+ }
223
+
224
+ /// Allocate and construct a DisplayBase using WA's native constructor (FFI).
225
+ ///
226
+ /// # Safety
227
+ /// Must be called from within the WA.exe process.
228
+ pub unsafe fn construct() -> *mut Self {
229
+ use crate::address::va;
230
+ use crate::rebase::rb;
231
+ use crate::wa_alloc::WABox;
232
+ let this = WABox::<Self>::alloc(0x3560, 0x3560).leak();
233
+ let ctor: unsafe extern "stdcall" fn(*mut Self) -> *mut Self =
234
+ core::mem::transmute(rb(va::DISPLAY_BASE_CTOR) as usize);
235
+ ctor(this);
236
+ (*this).vtable = rb(va::DISPLAY_BASE_HEADLESS_VTABLE) as *const DisplayBaseVtable;
237
+ this
238
+ }
239
+ }
crates/openwa-core/src/display_gfx.rs CHANGED
@@ -1,14 +1,14 @@
1
- /// DisplayGfx β€” full display/graphics subsystem (derived from DDDisplayBase).
2
  ///
3
  /// Constructor: DisplayGfx__Constructor (0x569C10), stdcall(this) β†’ DisplayGfx*.
4
  /// Size: 0x24E28 bytes.
5
  ///
6
- /// Inheritance: DDDisplayBase (0x3560) β†’ DisplayGfx (0x24E28).
7
- /// The constructor calls DDDisplayBase__Constructor first, then sets the
8
  /// DisplayGfx vtable (0x66A218) and initializes display-specific fields.
9
  ///
10
  /// Created by GameEngine__InitHardware in normal (non-headless) mode.
11
- /// Stored in the session's `display` field (shared with DDDisplayBase in headless).
12
  ///
13
  /// OPAQUE: Internal layout not yet mapped.
14
  #[repr(C)]
 
1
+ /// DisplayGfx β€” full display/graphics subsystem (derived from DisplayBase).
2
  ///
3
  /// Constructor: DisplayGfx__Constructor (0x569C10), stdcall(this) β†’ DisplayGfx*.
4
  /// Size: 0x24E28 bytes.
5
  ///
6
+ /// Inheritance: DisplayBase (0x3560) β†’ DisplayGfx (0x24E28).
7
+ /// The constructor calls DisplayBase__Constructor first, then sets the
8
  /// DisplayGfx vtable (0x66A218) and initializes display-specific fields.
9
  ///
10
  /// Created by GameEngine__InitHardware in normal (non-headless) mode.
11
+ /// Stored in the session's `display` field (shared with DisplayBase in headless).
12
  ///
13
  /// OPAQUE: Internal layout not yet mapped.
14
  #[repr(C)]
crates/openwa-core/src/game_session.rs CHANGED
@@ -74,7 +74,7 @@ pub struct GameSession {
74
  pub keyboard: *mut DDKeyboard,
75
  /// 0x0A8: `DSSound*` β€” 0xBE0 bytes, vtable `DSSound_vtable` (0x66AF20)
76
  pub sound: *mut DSSound,
77
- /// 0x0AC: Polymorphic display β€” `DisplayGfx*` (normal) or `DDDisplayBase*` (headless).
78
  /// Stays `*mut u8` because the concrete type depends on mode.
79
  pub display: *mut u8,
80
  /// 0x0B0: `Palette*` β€” 0x28 bytes, vtable `Palette_vtable_Maybe`
 
74
  pub keyboard: *mut DDKeyboard,
75
  /// 0x0A8: `DSSound*` β€” 0xBE0 bytes, vtable `DSSound_vtable` (0x66AF20)
76
  pub sound: *mut DSSound,
77
+ /// 0x0AC: Polymorphic display β€” `DisplayGfx*` (normal) or `DisplayBase*` (headless).
78
  /// Stays `*mut u8` because the concrete type depends on mode.
79
  pub display: *mut u8,
80
  /// 0x0B0: `Palette*` β€” 0x28 bytes, vtable `Palette_vtable_Maybe`
crates/openwa-core/src/lib.rs CHANGED
@@ -37,4 +37,4 @@ pub mod input_ctrl;
37
  pub mod game_timer;
38
  pub mod streaming_audio;
39
  pub mod ddnetgame_wrapper;
40
- pub mod dd_display_base;
 
37
  pub mod game_timer;
38
  pub mod streaming_audio;
39
  pub mod ddnetgame_wrapper;
40
+ pub mod display_base;
crates/openwa-core/src/wa_alloc.rs CHANGED
@@ -13,6 +13,16 @@ pub unsafe fn wa_malloc(size: u32) -> *mut u8 {
13
  f(size)
14
  }
15
 
 
 
 
 
 
 
 
 
 
 
16
  /// An allocation from WA's heap.
17
  ///
18
  /// Owns a pointer obtained from [`wa_malloc`]. Call [`.leak()`](WABox::leak) to transfer
 
13
  f(size)
14
  }
15
 
16
+ /// Free a pointer allocated by [`wa_malloc`] (WA's statically-linked CRT `free`).
17
+ ///
18
+ /// # Safety
19
+ /// `ptr` must have been returned by `wa_malloc` (or null, which is a no-op).
20
+ pub unsafe fn wa_free(ptr: *mut u8) {
21
+ let f: unsafe extern "cdecl" fn(*mut u8) =
22
+ core::mem::transmute(rb(va::WA_FREE) as usize);
23
+ f(ptr);
24
+ }
25
+
26
  /// An allocation from WA's heap.
27
  ///
28
  /// Owns a pointer obtained from [`wa_malloc`]. Call [`.leak()`](WABox::leak) to transfer
crates/openwa-wormkit/src/replacements/display.rs ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //! Display subsystem patches.
2
+ //!
3
+ //! Patches the DisplayBase primary vtable (0x6645F8) to replace _purecall
4
+ //! slots with safe Rust no-op stubs. This benefits both headless mode
5
+ //! (DisplayBase) and normal mode (DisplayGfx, which inherits from DisplayBase).
6
+
7
+ use core::mem::size_of;
8
+
9
+ use openwa_core::address::va;
10
+ use openwa_core::rebase::rb;
11
+ use crate::log_line;
12
+
13
+ /// The _purecall function address (calls abort).
14
+ const PURECALL: u32 = 0x005D_4E16;
15
+
16
+ /// Number of slots in the DisplayBase vtable.
17
+ const VTABLE_SLOTS: usize = 32;
18
+
19
+ unsafe extern "thiscall" fn noop_thiscall(_this: *mut u8) {}
20
+
21
+ pub fn install() -> Result<(), String> {
22
+ let _ = log_line("[Display] Patching DisplayBase primary vtable");
23
+
24
+ unsafe {
25
+ let vtable_addr = rb(va::DISPLAY_BASE_VTABLE) as *mut u32;
26
+ let purecall_addr = rb(PURECALL);
27
+ let noop_addr = noop_thiscall as *const () as u32;
28
+
29
+ // Make vtable writable.
30
+ let mut old_protect: u32 = 0;
31
+ let ok = windows_sys::Win32::System::Memory::VirtualProtect(
32
+ vtable_addr as *mut core::ffi::c_void,
33
+ (VTABLE_SLOTS * size_of::<u32>()) as usize,
34
+ 0x04, // PAGE_READWRITE
35
+ &mut old_protect,
36
+ );
37
+ if ok == 0 {
38
+ return Err("VirtualProtect failed on DisplayBase vtable".to_string());
39
+ }
40
+
41
+ let mut patched = 0u32;
42
+ for i in 0..VTABLE_SLOTS {
43
+ let slot = vtable_addr.add(i);
44
+ if *slot == purecall_addr {
45
+ *slot = noop_addr;
46
+ patched += 1;
47
+ }
48
+ }
49
+
50
+ // Restore protection.
51
+ windows_sys::Win32::System::Memory::VirtualProtect(
52
+ vtable_addr as *mut core::ffi::c_void,
53
+ (VTABLE_SLOTS * size_of::<u32>()) as usize,
54
+ old_protect,
55
+ &mut old_protect,
56
+ );
57
+
58
+ let _ = log_line(&format!(
59
+ "[Display] Patched {patched}/{VTABLE_SLOTS} _purecall slots with no-op stubs"
60
+ ));
61
+ }
62
+
63
+ Ok(())
64
+ }
crates/openwa-wormkit/src/replacements/hardware_init.rs CHANGED
@@ -31,7 +31,7 @@
31
  //! IF GameInfo.speech_enabled != 0 AND DSSound OK: streaming audio β†’ session+0xB4
32
  //!
33
  //! ELSE (headless):
34
- //! GameStats (0x3560, stdcall ctor + vtable override) β†’ session+0xAC
35
  //! session+0xA4/0xA8/0xB0/0xB4 = null
36
  //!
37
  //! ALWAYS:
@@ -52,7 +52,7 @@ use openwa_core::dssound::DSSound;
52
  use openwa_core::palette::Palette;
53
  use openwa_core::input_ctrl::{InputCtrl, InputCtrlVtable};
54
  use openwa_core::game_timer::GameTimer;
55
- use openwa_core::dd_display_base::DDDisplayBase;
56
  use openwa_core::display_gfx::DisplayGfx;
57
  use openwa_core::streaming_audio::StreamingAudio;
58
  use openwa_core::ddnetgame_wrapper::DDNetGameWrapper;
@@ -430,7 +430,10 @@ unsafe extern "cdecl" fn impl_init_hardware(
430
  }
431
  } else {
432
  // ── Headless / stats mode ─────────────────────────────────────────────
433
- (*session).display = DDDisplayBase::construct() as *mut u8;
 
 
 
434
  (*session).keyboard = core::ptr::null_mut();
435
  (*session).sound = core::ptr::null_mut();
436
  (*session).palette = core::ptr::null_mut();
 
31
  //! IF GameInfo.speech_enabled != 0 AND DSSound OK: streaming audio β†’ session+0xB4
32
  //!
33
  //! ELSE (headless):
34
+ //! DisplayBase (0x3560, stdcall ctor + vtable override) β†’ session+0xAC
35
  //! session+0xA4/0xA8/0xB0/0xB4 = null
36
  //!
37
  //! ALWAYS:
 
52
  use openwa_core::palette::Palette;
53
  use openwa_core::input_ctrl::{InputCtrl, InputCtrlVtable};
54
  use openwa_core::game_timer::GameTimer;
55
+ use openwa_core::display_base::DisplayBase;
56
  use openwa_core::display_gfx::DisplayGfx;
57
  use openwa_core::streaming_audio::StreamingAudio;
58
  use openwa_core::ddnetgame_wrapper::DDNetGameWrapper;
 
430
  }
431
  } else {
432
  // ── Headless / stats mode ─────────────────────────────────────────────
433
+ // TODO: Replace with DisplayBase::new_headless() once sprite collection
434
+ // sub-object construction is fully understood (FUN_004fa860 sets a secondary
435
+ // vtable via implicit EDI that we haven't replicated yet).
436
+ (*session).display = DisplayBase::construct() as *mut u8;
437
  (*session).keyboard = core::ptr::null_mut();
438
  (*session).sound = core::ptr::null_mut();
439
  (*session).palette = core::ptr::null_mut();
crates/openwa-wormkit/src/replacements/mod.rs CHANGED
@@ -1,4 +1,5 @@
1
  mod config;
 
2
  mod frontend;
3
  mod game_session;
4
  mod hardware_init;
@@ -13,6 +14,7 @@ mod weapon;
13
 
14
  pub fn install_all() -> Result<(), String> {
15
  headless::install()?;
 
16
  game_session::install()?;
17
  hardware_init::install()?;
18
  frontend::install()?;
 
1
  mod config;
2
+ mod display;
3
  mod frontend;
4
  mod game_session;
5
  mod hardware_init;
 
14
 
15
  pub fn install_all() -> Result<(), String> {
16
  headless::install()?;
17
+ display::install()?;
18
  game_session::install()?;
19
  hardware_init::install()?;
20
  frontend::install()?;