refactor: use typed pointers throughout DisplayBase and sprite cache
Browse files- sprite_ptrs: [u32] → [*mut Sprite], sprite_banks: [u32] → [*mut SpriteBank]
- Rename sprite_palettes → sprite_layers (stores layer IDs, not palettes)
- SpriteCacheWrapper → SpriteCache with typed SpriteCacheVtable (1 slot)
- SpriteBufferCtrl.buffer: u32 → *mut u8, sprite_cache: u32 → *mut SpriteCache
- Fix SpriteBankVtable: 3 → 2 slots (slot 3 was actually SpriteCacheVtable)
- Make wa_free generic over T (eliminates `as *mut u8` casts at call sites)
- Simplify sprite cache buffer alloc: wa_malloc+write_bytes → wa_malloc_zeroed
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
crates/openwa-core/src/display/base.rs
CHANGED
|
@@ -22,26 +22,27 @@
|
|
| 22 |
/// vtable at a different address crashes even with identical content. We must
|
| 23 |
/// point to WA's vtable in .rdata and patch individual slots there in-place
|
| 24 |
/// (via VirtualProtect in `display.rs`).
|
| 25 |
-
use crate::
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
#[repr(C)]
|
| 31 |
pub struct DisplayBase<V: Vtable = *const DisplayBaseVtable> {
|
| 32 |
// +0x000: vtable pointer
|
| 33 |
pub vtable: V,
|
| 34 |
// +0x004: sprite cache wrapper (0x28-byte SpriteCacheWrapper)
|
| 35 |
-
pub sprite_cache:
|
| 36 |
-
// +0x008..0x1008: per-slot
|
| 37 |
// Stores which layer (1-3) each sprite slot belongs to.
|
| 38 |
-
pub
|
| 39 |
// +0x1008..0x2008: per-slot Sprite* pointers (0x400 entries), zeroed by ctor.
|
| 40 |
// Points to Sprite objects (0x70 bytes, vtable 0x66418C). Checked by IsSpriteLoaded/GetSpriteInfo.
|
| 41 |
-
pub sprite_ptrs: [
|
| 42 |
// +0x2008..0x3008: per-slot SpriteBank* pointers (0x400 entries), zeroed by ctor.
|
| 43 |
// Points to SpriteBank objects (0x17C bytes). Fallback path in GetSpriteInfo/LoadSpriteComplex.
|
| 44 |
-
pub sprite_banks: [
|
| 45 |
// +0x3008..0x3018: gap (0x10 bytes, 4 u32s: indices 0xC02..0xC05)
|
| 46 |
pub _gap_3008: [u32; 4],
|
| 47 |
// +0x3018: field at u32 index 0xC06, zeroed by ctor
|
|
@@ -136,7 +137,7 @@ pub struct DisplayBaseVtable {
|
|
| 136 |
#[repr(C)]
|
| 137 |
pub struct SpriteBufferCtrl {
|
| 138 |
/// Pointer to pixel buffer (0x80020 allocated, 0x80000 used)
|
| 139 |
-
pub buffer:
|
| 140 |
/// Buffer capacity (0x80000)
|
| 141 |
pub capacity: u32,
|
| 142 |
pub _fields_08: [u32; 5],
|
|
@@ -145,21 +146,31 @@ pub struct SpriteBufferCtrl {
|
|
| 145 |
|
| 146 |
const _: () = assert!(core::mem::size_of::<SpriteBufferCtrl>() == 0x3C);
|
| 147 |
|
| 148 |
-
/// Sprite cache
|
| 149 |
/// Constructed by FUN_004fa860 (receives `this` in EDI).
|
| 150 |
-
/// Has its own vtable (0x664188) and holds a pointer to [`SpriteBufferCtrl`].
|
| 151 |
#[repr(C)]
|
| 152 |
-
pub struct
|
| 153 |
-
/// Vtable pointer (0x664188 in WA,
|
| 154 |
-
pub vtable:
|
| 155 |
/// Pointer to the 0x3C-byte buffer control block
|
| 156 |
-
pub buffer_ctrl:
|
| 157 |
pub _pad_08: [u8; 0x28 - 0x08],
|
| 158 |
}
|
| 159 |
|
| 160 |
-
const _: () = assert!(core::mem::size_of::<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
/// Ghidra address of the
|
| 163 |
const SPRITE_CACHE_VTABLE: u32 = 0x0066_4188;
|
| 164 |
|
| 165 |
// ── Construction ──────────────────────────────────────────────────────────
|
|
@@ -180,7 +191,7 @@ impl DisplayBase {
|
|
| 180 |
pub unsafe fn new_headless() -> *mut Self {
|
| 181 |
use crate::address::va;
|
| 182 |
use crate::rebase::rb;
|
| 183 |
-
use crate::wa_alloc::
|
| 184 |
|
| 185 |
// Allocate and zero the entire struct.
|
| 186 |
let this = WABox::<Self>::alloc(0x3560, 0x3560).leak();
|
|
@@ -204,17 +215,16 @@ impl DisplayBase {
|
|
| 204 |
// 3. Allocates 0x3C buffer ctrl, sets ctrl[0] = buffer, ctrl[4] = capacity
|
| 205 |
// 4. Sets wrapper[4] = ctrl pointer
|
| 206 |
// 5. Returns wrapper in EAX → stored at this+4
|
| 207 |
-
let wrapper =
|
| 208 |
-
(*wrapper).vtable = rb(SPRITE_CACHE_VTABLE);
|
| 209 |
|
| 210 |
-
let ctrl =
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
(*ctrl).buffer = buf as u32;
|
| 214 |
(*ctrl).capacity = 0x80000;
|
| 215 |
|
| 216 |
-
(*wrapper).buffer_ctrl = ctrl
|
| 217 |
-
(*this).sprite_cache = wrapper
|
| 218 |
|
| 219 |
this
|
| 220 |
}
|
|
|
|
| 22 |
/// vtable at a different address crashes even with identical content. We must
|
| 23 |
/// point to WA's vtable in .rdata and patch individual slots there in-place
|
| 24 |
/// (via VirtualProtect in `display.rs`).
|
| 25 |
+
use crate::{
|
| 26 |
+
render::sprite::{Sprite, SpriteBank},
|
| 27 |
+
task::base::Vtable,
|
| 28 |
+
wa_alloc::{wa_malloc_struct_zeroed, wa_malloc_zeroed},
|
| 29 |
+
};
|
| 30 |
|
| 31 |
#[repr(C)]
|
| 32 |
pub struct DisplayBase<V: Vtable = *const DisplayBaseVtable> {
|
| 33 |
// +0x000: vtable pointer
|
| 34 |
pub vtable: V,
|
| 35 |
// +0x004: sprite cache wrapper (0x28-byte SpriteCacheWrapper)
|
| 36 |
+
pub sprite_cache: *mut SpriteCache,
|
| 37 |
+
// +0x008..0x1008: per-slot layer ID (0x400 entries), zeroed by ctor.
|
| 38 |
// Stores which layer (1-3) each sprite slot belongs to.
|
| 39 |
+
pub sprite_layers: [u32; 0x400],
|
| 40 |
// +0x1008..0x2008: per-slot Sprite* pointers (0x400 entries), zeroed by ctor.
|
| 41 |
// Points to Sprite objects (0x70 bytes, vtable 0x66418C). Checked by IsSpriteLoaded/GetSpriteInfo.
|
| 42 |
+
pub sprite_ptrs: [*mut Sprite; 0x400],
|
| 43 |
// +0x2008..0x3008: per-slot SpriteBank* pointers (0x400 entries), zeroed by ctor.
|
| 44 |
// Points to SpriteBank objects (0x17C bytes). Fallback path in GetSpriteInfo/LoadSpriteComplex.
|
| 45 |
+
pub sprite_banks: [*mut SpriteBank; 0x400],
|
| 46 |
// +0x3008..0x3018: gap (0x10 bytes, 4 u32s: indices 0xC02..0xC05)
|
| 47 |
pub _gap_3008: [u32; 4],
|
| 48 |
// +0x3018: field at u32 index 0xC06, zeroed by ctor
|
|
|
|
| 137 |
#[repr(C)]
|
| 138 |
pub struct SpriteBufferCtrl {
|
| 139 |
/// Pointer to pixel buffer (0x80020 allocated, 0x80000 used)
|
| 140 |
+
pub buffer: *mut u8,
|
| 141 |
/// Buffer capacity (0x80000)
|
| 142 |
pub capacity: u32,
|
| 143 |
pub _fields_08: [u32; 5],
|
|
|
|
| 146 |
|
| 147 |
const _: () = assert!(core::mem::size_of::<SpriteBufferCtrl>() == 0x3C);
|
| 148 |
|
| 149 |
+
/// Sprite cache (0x28 bytes).
|
| 150 |
/// Constructed by FUN_004fa860 (receives `this` in EDI).
|
| 151 |
+
/// Has its own vtable (0x664188, 1 slot) and holds a pointer to [`SpriteBufferCtrl`].
|
| 152 |
#[repr(C)]
|
| 153 |
+
pub struct SpriteCache {
|
| 154 |
+
/// Vtable pointer (0x664188 in WA, 1 slot)
|
| 155 |
+
pub vtable: *const SpriteCacheVtable,
|
| 156 |
/// Pointer to the 0x3C-byte buffer control block
|
| 157 |
+
pub buffer_ctrl: *mut SpriteBufferCtrl,
|
| 158 |
pub _pad_08: [u8; 0x28 - 0x08],
|
| 159 |
}
|
| 160 |
|
| 161 |
+
const _: () = assert!(core::mem::size_of::<SpriteCache>() == 0x28);
|
| 162 |
+
|
| 163 |
+
/// SpriteCache vtable (0x664188, 1 slot).
|
| 164 |
+
///
|
| 165 |
+
/// Single-slot vtable for the sprite cache object. Slot 0 is a destructor-like
|
| 166 |
+
/// function (0x4FA910).
|
| 167 |
+
#[repr(C)]
|
| 168 |
+
pub struct SpriteCacheVtable {
|
| 169 |
+
/// Slot 0: destructor / release (0x4FA910)
|
| 170 |
+
pub destructor: unsafe extern "thiscall" fn(this: *mut SpriteCache, flags: u32),
|
| 171 |
+
}
|
| 172 |
|
| 173 |
+
/// Ghidra address of the SpriteCache vtable.
|
| 174 |
const SPRITE_CACHE_VTABLE: u32 = 0x0066_4188;
|
| 175 |
|
| 176 |
// ── Construction ──────────────────────────────────────────────────────────
|
|
|
|
| 191 |
pub unsafe fn new_headless() -> *mut Self {
|
| 192 |
use crate::address::va;
|
| 193 |
use crate::rebase::rb;
|
| 194 |
+
use crate::wa_alloc::WABox;
|
| 195 |
|
| 196 |
// Allocate and zero the entire struct.
|
| 197 |
let this = WABox::<Self>::alloc(0x3560, 0x3560).leak();
|
|
|
|
| 215 |
// 3. Allocates 0x3C buffer ctrl, sets ctrl[0] = buffer, ctrl[4] = capacity
|
| 216 |
// 4. Sets wrapper[4] = ctrl pointer
|
| 217 |
// 5. Returns wrapper in EAX → stored at this+4
|
| 218 |
+
let wrapper = wa_malloc_struct_zeroed::<SpriteCache>();
|
| 219 |
+
(*wrapper).vtable = rb(SPRITE_CACHE_VTABLE) as *const SpriteCacheVtable;
|
| 220 |
|
| 221 |
+
let ctrl = wa_malloc_struct_zeroed::<SpriteBufferCtrl>();
|
| 222 |
+
// Original allocates 0x80020 but capacity is 0x80000 — extra 0x20 is guard margin.
|
| 223 |
+
(*ctrl).buffer = wa_malloc_zeroed(0x80020);
|
|
|
|
| 224 |
(*ctrl).capacity = 0x80000;
|
| 225 |
|
| 226 |
+
(*wrapper).buffer_ctrl = ctrl;
|
| 227 |
+
(*this).sprite_cache = wrapper;
|
| 228 |
|
| 229 |
this
|
| 230 |
}
|
crates/openwa-core/src/display/display_vtable.rs
CHANGED
|
@@ -795,8 +795,8 @@ pub unsafe extern "thiscall" fn is_sprite_loaded(this: *mut DisplayGfx, id: i32)
|
|
| 795 |
}
|
| 796 |
|
| 797 |
let base = &(*this).base;
|
| 798 |
-
if base.sprite_ptrs[id as usize]
|
| 799 |
-
|| base.sprite_banks[id as usize]
|
| 800 |
|| (*this).sprite_table[id as usize] != 0
|
| 801 |
{
|
| 802 |
1
|
|
@@ -830,26 +830,15 @@ pub unsafe extern "thiscall" fn get_sprite_info(
|
|
| 830 |
let base = &(*this).base;
|
| 831 |
|
| 832 |
// Path 1: Sprite* in sprite_ptrs
|
| 833 |
-
let
|
| 834 |
-
if
|
| 835 |
-
return sprite_info_from_sprite(
|
| 836 |
-
sprite_ptr as *const Sprite,
|
| 837 |
-
out_data,
|
| 838 |
-
out_flags,
|
| 839 |
-
out_width,
|
| 840 |
-
);
|
| 841 |
}
|
| 842 |
|
| 843 |
// Path 2: SpriteBank* in sprite_banks
|
| 844 |
-
let
|
| 845 |
-
if
|
| 846 |
-
return sprite_info_from_bank(
|
| 847 |
-
bank_ptr as *const SpriteBank,
|
| 848 |
-
layer,
|
| 849 |
-
out_data,
|
| 850 |
-
out_flags,
|
| 851 |
-
out_width,
|
| 852 |
-
);
|
| 853 |
}
|
| 854 |
|
| 855 |
0
|
|
|
|
| 795 |
}
|
| 796 |
|
| 797 |
let base = &(*this).base;
|
| 798 |
+
if !base.sprite_ptrs[id as usize].is_null()
|
| 799 |
+
|| !base.sprite_banks[id as usize].is_null()
|
| 800 |
|| (*this).sprite_table[id as usize] != 0
|
| 801 |
{
|
| 802 |
1
|
|
|
|
| 830 |
let base = &(*this).base;
|
| 831 |
|
| 832 |
// Path 1: Sprite* in sprite_ptrs
|
| 833 |
+
let sprite = base.sprite_ptrs[layer as usize];
|
| 834 |
+
if !sprite.is_null() {
|
| 835 |
+
return sprite_info_from_sprite(sprite, out_data, out_flags, out_width);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 836 |
}
|
| 837 |
|
| 838 |
// Path 2: SpriteBank* in sprite_banks
|
| 839 |
+
let bank = base.sprite_banks[layer as usize];
|
| 840 |
+
if !bank.is_null() {
|
| 841 |
+
return sprite_info_from_bank(bank, layer, out_data, out_flags, out_width);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
}
|
| 843 |
|
| 844 |
0
|
crates/openwa-core/src/display/mod.rs
CHANGED
|
@@ -9,7 +9,7 @@ pub mod palette;
|
|
| 9 |
pub mod render_context;
|
| 10 |
pub mod sprite_blit;
|
| 11 |
|
| 12 |
-
pub use base::{DisplayBase, DisplayBaseVtable, SpriteBufferCtrl,
|
| 13 |
pub use compat_renderer::CompatRenderer;
|
| 14 |
pub use display_vtable::{DisplayVtable, DrawScaledSpriteResult};
|
| 15 |
pub use gfx::DisplayGfx;
|
|
|
|
| 9 |
pub mod render_context;
|
| 10 |
pub mod sprite_blit;
|
| 11 |
|
| 12 |
+
pub use base::{DisplayBase, DisplayBaseVtable, SpriteBufferCtrl, SpriteCache};
|
| 13 |
pub use compat_renderer::CompatRenderer;
|
| 14 |
pub use display_vtable::{DisplayVtable, DrawScaledSpriteResult};
|
| 15 |
pub use gfx::DisplayGfx;
|
crates/openwa-core/src/render/sprite.rs
CHANGED
|
@@ -168,15 +168,13 @@ pub struct SpriteVtable {
|
|
| 168 |
pub slot_7: fn(this: *mut Sprite),
|
| 169 |
}
|
| 170 |
|
| 171 |
-
/// SpriteBank vtable (0x664180,
|
| 172 |
-
#[openwa_core::vtable(size =
|
| 173 |
pub struct SpriteBankVtable {
|
| 174 |
/// destructor (0x4F94E0)
|
| 175 |
pub destructor: fn(this: *mut SpriteBank, flags: u32),
|
| 176 |
/// init/load (0x4F9580)
|
| 177 |
pub slot_1: fn(this: *mut SpriteBank),
|
| 178 |
-
/// unknown (0x4FA910)
|
| 179 |
-
pub slot_2: fn(this: *mut SpriteBank),
|
| 180 |
}
|
| 181 |
|
| 182 |
crate::define_addresses! {
|
|
|
|
| 168 |
pub slot_7: fn(this: *mut Sprite),
|
| 169 |
}
|
| 170 |
|
| 171 |
+
/// SpriteBank vtable (0x664180, 2 slots).
|
| 172 |
+
#[openwa_core::vtable(size = 2, va = 0x0066_4180, class = "SpriteBank")]
|
| 173 |
pub struct SpriteBankVtable {
|
| 174 |
/// destructor (0x4F94E0)
|
| 175 |
pub destructor: fn(this: *mut SpriteBank, flags: u32),
|
| 176 |
/// init/load (0x4F9580)
|
| 177 |
pub slot_1: fn(this: *mut SpriteBank),
|
|
|
|
|
|
|
| 178 |
}
|
| 179 |
|
| 180 |
crate::define_addresses! {
|
crates/openwa-core/src/wa_alloc.rs
CHANGED
|
@@ -50,9 +50,9 @@ pub unsafe fn wa_malloc_zeroed(size: u32) -> *mut u8 {
|
|
| 50 |
///
|
| 51 |
/// # Safety
|
| 52 |
/// `ptr` must have been returned by `wa_malloc` (or null, which is a no-op).
|
| 53 |
-
pub unsafe fn wa_free(ptr: *mut
|
| 54 |
let f: unsafe extern "cdecl" fn(*mut u8) = core::mem::transmute(rb(va::WA_FREE) as usize);
|
| 55 |
-
f(ptr);
|
| 56 |
}
|
| 57 |
|
| 58 |
/// An allocation from WA's heap.
|
|
|
|
| 50 |
///
|
| 51 |
/// # Safety
|
| 52 |
/// `ptr` must have been returned by `wa_malloc` (or null, which is a no-op).
|
| 53 |
+
pub unsafe fn wa_free<T>(ptr: *mut T) {
|
| 54 |
let f: unsafe extern "cdecl" fn(*mut u8) = core::mem::transmute(rb(va::WA_FREE) as usize);
|
| 55 |
+
f(ptr as *mut u8);
|
| 56 |
}
|
| 57 |
|
| 58 |
/// An allocation from WA's heap.
|
crates/openwa-dll/src/replacements/display.rs
CHANGED
|
@@ -9,8 +9,8 @@ use crate::log_line;
|
|
| 9 |
use openwa_core::address::va;
|
| 10 |
use openwa_core::bitgrid::DisplayBitGrid;
|
| 11 |
use openwa_core::display::display_vtable::{self as display_vtable, DisplayVtable};
|
|
|
|
| 12 |
use openwa_core::display::DisplayGfx;
|
| 13 |
-
use openwa_core::display::{DisplayBase, SpriteBufferCtrl, SpriteCacheWrapper};
|
| 14 |
use openwa_core::fixed::Fixed;
|
| 15 |
use openwa_core::rebase::rb;
|
| 16 |
use openwa_core::vtable::patch_vtable;
|
|
@@ -31,22 +31,20 @@ unsafe extern "thiscall" fn headless_destructor(
|
|
| 31 |
this: *mut DisplayBase,
|
| 32 |
flags: u8,
|
| 33 |
) -> *mut DisplayBase {
|
| 34 |
-
let
|
| 35 |
-
if
|
| 36 |
-
let
|
| 37 |
-
|
| 38 |
-
if ctrl_addr != 0 {
|
| 39 |
-
let ctrl = ctrl_addr as *mut SpriteBufferCtrl;
|
| 40 |
let buf = (*ctrl).buffer;
|
| 41 |
-
if buf
|
| 42 |
-
wa_free(buf
|
| 43 |
}
|
| 44 |
-
wa_free(ctrl
|
| 45 |
}
|
| 46 |
-
wa_free(
|
| 47 |
}
|
| 48 |
if flags & 1 != 0 {
|
| 49 |
-
wa_free(this
|
| 50 |
}
|
| 51 |
this
|
| 52 |
}
|
|
|
|
| 9 |
use openwa_core::address::va;
|
| 10 |
use openwa_core::bitgrid::DisplayBitGrid;
|
| 11 |
use openwa_core::display::display_vtable::{self as display_vtable, DisplayVtable};
|
| 12 |
+
use openwa_core::display::DisplayBase;
|
| 13 |
use openwa_core::display::DisplayGfx;
|
|
|
|
| 14 |
use openwa_core::fixed::Fixed;
|
| 15 |
use openwa_core::rebase::rb;
|
| 16 |
use openwa_core::vtable::patch_vtable;
|
|
|
|
| 31 |
this: *mut DisplayBase,
|
| 32 |
flags: u8,
|
| 33 |
) -> *mut DisplayBase {
|
| 34 |
+
let sprite_cache = (*this).sprite_cache;
|
| 35 |
+
if !sprite_cache.is_null() {
|
| 36 |
+
let ctrl = (*sprite_cache).buffer_ctrl;
|
| 37 |
+
if !ctrl.is_null() {
|
|
|
|
|
|
|
| 38 |
let buf = (*ctrl).buffer;
|
| 39 |
+
if !buf.is_null() {
|
| 40 |
+
wa_free(buf);
|
| 41 |
}
|
| 42 |
+
wa_free(ctrl);
|
| 43 |
}
|
| 44 |
+
wa_free(sprite_cache);
|
| 45 |
}
|
| 46 |
if flags & 1 != 0 {
|
| 47 |
+
wa_free(this);
|
| 48 |
}
|
| 49 |
this
|
| 50 |
}
|