refactor: replace inline thiscall transmutes with typed GfxDirVtable pattern
Browse filesAdd GfxDirVtable via #[vtable] macro (4 slots: read/seek/load_cached/release)
and GfxDirStream::total_size_raw (slot 4, formerly _slot_4). Replace all
inline transmute vtable calls across gfx_dir.rs, ddgame_constructor.rs, and
font.rs with typed _raw methods generated by bind_GfxDirVtable!. Change
call_gfx_load_image return type to *mut GfxDirStream. Rename GfxHandler and
GfxDirStream vtable functions in Ghidra accordingly.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
crates/openwa-core/src/engine/ddgame_constructor.rs
CHANGED
|
@@ -24,7 +24,7 @@ use crate::render::display::gradient::compute_complex_gradient;
|
|
| 24 |
use crate::render::display::palette::Palette;
|
| 25 |
use crate::render::landscape::PCLandscape;
|
| 26 |
use crate::render::sprite::gfx_dir::{
|
| 27 |
-
call_gfx_find_and_load, call_gfx_load_and_wrap, call_gfx_load_dir, GfxDir,
|
| 28 |
};
|
| 29 |
pub use crate::render::sprite::gfx_dir::{gfx_dir_find_entry, gfx_resource_create};
|
| 30 |
use crate::wa_alloc::{wa_malloc, wa_malloc_zeroed};
|
|
@@ -432,7 +432,7 @@ unsafe fn init_graphics_and_resources(
|
|
| 432 |
use core::ffi::c_char;
|
| 433 |
let fopen: unsafe extern "cdecl" fn(*const c_char, *const c_char) -> *mut u8 =
|
| 434 |
core::mem::transmute(rb(va::WA_FOPEN) as usize);
|
| 435 |
-
let gfx_dir_vtable = rb(va::GFX_DIR_VTABLE) as
|
| 436 |
|
| 437 |
// ── GfxDir #1 (primary) ──
|
| 438 |
let gfx1 = GfxDir::alloc(gfx_dir_vtable);
|
|
@@ -772,12 +772,8 @@ unsafe fn init_graphics_and_resources(
|
|
| 772 |
|
| 773 |
let sprite: *mut u8;
|
| 774 |
if !entry.is_null() {
|
| 775 |
-
// Try gfx_dir->vtable[2](entry->field_4)
|
| 776 |
-
let gfx_vt = *(gfx_dir as *const *const u32);
|
| 777 |
-
let load_cached: unsafe extern "thiscall" fn(*mut GfxDir, u32) -> *mut u8 =
|
| 778 |
-
core::mem::transmute(*gfx_vt.add(2));
|
| 779 |
let entry_val = *(entry.add(4) as *const u32);
|
| 780 |
-
let cached =
|
| 781 |
if !cached.is_null() {
|
| 782 |
sprite = wa_displaygfx_ctor(cached);
|
| 783 |
} else {
|
|
@@ -1029,10 +1025,7 @@ unsafe fn init_graphics_and_resources(
|
|
| 1029 |
// ── Release primary GfxHandler (vtable[3] = release, param 1 = free) ──
|
| 1030 |
let primary_gfx_dir = (*wrapper).primary_gfx_dir;
|
| 1031 |
if !primary_gfx_dir.is_null() {
|
| 1032 |
-
|
| 1033 |
-
let release: unsafe extern "thiscall" fn(*mut GfxDir, u32) =
|
| 1034 |
-
core::mem::transmute(*gfx_vt.add(3));
|
| 1035 |
-
release(primary_gfx_dir, 1);
|
| 1036 |
}
|
| 1037 |
|
| 1038 |
if !is_headless {
|
|
|
|
| 24 |
use crate::render::display::palette::Palette;
|
| 25 |
use crate::render::landscape::PCLandscape;
|
| 26 |
use crate::render::sprite::gfx_dir::{
|
| 27 |
+
call_gfx_find_and_load, call_gfx_load_and_wrap, call_gfx_load_dir, GfxDir, GfxDirVtable,
|
| 28 |
};
|
| 29 |
pub use crate::render::sprite::gfx_dir::{gfx_dir_find_entry, gfx_resource_create};
|
| 30 |
use crate::wa_alloc::{wa_malloc, wa_malloc_zeroed};
|
|
|
|
| 432 |
use core::ffi::c_char;
|
| 433 |
let fopen: unsafe extern "cdecl" fn(*const c_char, *const c_char) -> *mut u8 =
|
| 434 |
core::mem::transmute(rb(va::WA_FOPEN) as usize);
|
| 435 |
+
let gfx_dir_vtable = rb(va::GFX_DIR_VTABLE) as *const GfxDirVtable;
|
| 436 |
|
| 437 |
// ── GfxDir #1 (primary) ──
|
| 438 |
let gfx1 = GfxDir::alloc(gfx_dir_vtable);
|
|
|
|
| 772 |
|
| 773 |
let sprite: *mut u8;
|
| 774 |
if !entry.is_null() {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
let entry_val = *(entry.add(4) as *const u32);
|
| 776 |
+
let cached = GfxDir::load_cached_raw(gfx_dir, entry_val);
|
| 777 |
if !cached.is_null() {
|
| 778 |
sprite = wa_displaygfx_ctor(cached);
|
| 779 |
} else {
|
|
|
|
| 1025 |
// ── Release primary GfxHandler (vtable[3] = release, param 1 = free) ──
|
| 1026 |
let primary_gfx_dir = (*wrapper).primary_gfx_dir;
|
| 1027 |
if !primary_gfx_dir.is_null() {
|
| 1028 |
+
GfxDir::release_raw(primary_gfx_dir, 1);
|
|
|
|
|
|
|
|
|
|
| 1029 |
}
|
| 1030 |
|
| 1031 |
if !is_headless {
|
crates/openwa-core/src/render/display/font.rs
CHANGED
|
@@ -222,34 +222,28 @@ pub unsafe fn font_load_from_gfx(
|
|
| 222 |
palette_ctx: *mut PaletteContext,
|
| 223 |
name: *const c_char,
|
| 224 |
) -> u32 {
|
| 225 |
-
use crate::render::sprite::gfx_dir::{
|
|
|
|
|
|
|
| 226 |
use crate::wa_alloc::wa_malloc;
|
| 227 |
|
| 228 |
// 1. Try FindEntry → gfx_dir->vtable[2](entry->value) for cached load.
|
| 229 |
let entry = gfx_dir_find_entry(name, gfx_dir);
|
| 230 |
if !entry.is_null() {
|
| 231 |
-
let vt = *(gfx_dir as *const *const u32);
|
| 232 |
-
let load_cached: unsafe extern "thiscall" fn(*mut GfxDir, u32) -> *mut FntHeader =
|
| 233 |
-
core::mem::transmute(*vt.add(2));
|
| 234 |
let entry_val = (*(entry as *const GfxDirEntry)).value;
|
| 235 |
-
let cached =
|
| 236 |
if !cached.is_null() {
|
| 237 |
return font_parse_data(font_obj, cached, palette_ctx);
|
| 238 |
}
|
| 239 |
}
|
| 240 |
|
| 241 |
-
// 2. Fallback: LoadImage →
|
| 242 |
let image = call_gfx_load_image(gfx_dir, name);
|
| 243 |
if image.is_null() {
|
| 244 |
return 0;
|
| 245 |
}
|
| 246 |
|
| 247 |
-
let
|
| 248 |
-
|
| 249 |
-
// vtable[4] — get size (thiscall, no args)
|
| 250 |
-
let get_size: unsafe extern "thiscall" fn(*mut u8) -> u32 =
|
| 251 |
-
core::mem::transmute(*image_vt.add(4));
|
| 252 |
-
let size = get_size(image);
|
| 253 |
|
| 254 |
// Match the original's allocation: round size up to 4-byte multiple, add 0x20 guard.
|
| 255 |
let alloc_size = ((size + 3) & !3u32).wrapping_add(0x20);
|
|
@@ -259,14 +253,8 @@ pub unsafe fn font_load_from_gfx(
|
|
| 259 |
core::ptr::write_bytes(buffer, 0, size as usize);
|
| 260 |
}
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
core::mem::transmute(*image_vt.add(5));
|
| 265 |
-
read_into(image, buffer, size);
|
| 266 |
-
|
| 267 |
-
// vtable[0] — destroy image (thiscall with 1 arg: flags = 1)
|
| 268 |
-
let destroy: unsafe extern "thiscall" fn(*mut u8, u32) = core::mem::transmute(*image_vt);
|
| 269 |
-
destroy(image, 1);
|
| 270 |
|
| 271 |
font_parse_data(font_obj, buffer as *mut FntHeader, palette_ctx)
|
| 272 |
}
|
|
|
|
| 222 |
palette_ctx: *mut PaletteContext,
|
| 223 |
name: *const c_char,
|
| 224 |
) -> u32 {
|
| 225 |
+
use crate::render::sprite::gfx_dir::{
|
| 226 |
+
call_gfx_load_image, gfx_dir_find_entry, GfxDirEntry, GfxDirStream,
|
| 227 |
+
};
|
| 228 |
use crate::wa_alloc::wa_malloc;
|
| 229 |
|
| 230 |
// 1. Try FindEntry → gfx_dir->vtable[2](entry->value) for cached load.
|
| 231 |
let entry = gfx_dir_find_entry(name, gfx_dir);
|
| 232 |
if !entry.is_null() {
|
|
|
|
|
|
|
|
|
|
| 233 |
let entry_val = (*(entry as *const GfxDirEntry)).value;
|
| 234 |
+
let cached = GfxDir::load_cached_raw(gfx_dir, entry_val) as *mut FntHeader;
|
| 235 |
if !cached.is_null() {
|
| 236 |
return font_parse_data(font_obj, cached, palette_ctx);
|
| 237 |
}
|
| 238 |
}
|
| 239 |
|
| 240 |
+
// 2. Fallback: LoadImage → get_total_size → wa_malloc → read → destroy
|
| 241 |
let image = call_gfx_load_image(gfx_dir, name);
|
| 242 |
if image.is_null() {
|
| 243 |
return 0;
|
| 244 |
}
|
| 245 |
|
| 246 |
+
let size = GfxDirStream::total_size_raw(image);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
// Match the original's allocation: round size up to 4-byte multiple, add 0x20 guard.
|
| 249 |
let alloc_size = ((size + 3) & !3u32).wrapping_add(0x20);
|
|
|
|
| 253 |
core::ptr::write_bytes(buffer, 0, size as usize);
|
| 254 |
}
|
| 255 |
|
| 256 |
+
GfxDirStream::read_raw(image, buffer, size);
|
| 257 |
+
GfxDirStream::destroy_raw(image);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
font_parse_data(font_obj, buffer as *mut FntHeader, palette_ctx)
|
| 260 |
}
|
crates/openwa-core/src/render/display/vtable.rs
CHANGED
|
@@ -2488,7 +2488,7 @@ pub unsafe fn load_sprite_by_name(
|
|
| 2488 |
(*sprite).palette_ctx = palette_ctx;
|
| 2489 |
|
| 2490 |
// 3. Load image stream from GfxDir
|
| 2491 |
-
let stream = call_gfx_load_image(gfx_dir, name)
|
| 2492 |
if stream.is_null() {
|
| 2493 |
return 0;
|
| 2494 |
}
|
|
|
|
| 2488 |
(*sprite).palette_ctx = palette_ctx;
|
| 2489 |
|
| 2490 |
// 3. Load image stream from GfxDir
|
| 2491 |
+
let stream = call_gfx_load_image(gfx_dir, name);
|
| 2492 |
if stream.is_null() {
|
| 2493 |
return 0;
|
| 2494 |
}
|
crates/openwa-core/src/render/sprite/gfx_dir.rs
CHANGED
|
@@ -4,6 +4,8 @@
|
|
| 4 |
//! `Gfx0.dir`). It provides name→resource lookup via a 1024-bucket hash table
|
| 5 |
//! and delegates file I/O and caching to vtable methods.
|
| 6 |
|
|
|
|
|
|
|
| 7 |
use crate::address::va;
|
| 8 |
use crate::rebase::rb;
|
| 9 |
use crate::wa_alloc::wa_malloc;
|
|
@@ -15,7 +17,7 @@ use crate::wa_alloc::wa_malloc;
|
|
| 15 |
#[repr(C)]
|
| 16 |
pub struct GfxDir {
|
| 17 |
/// 0x000: Vtable pointer (0x66B280).
|
| 18 |
-
pub vtable:
|
| 19 |
/// 0x004: Bucket array — 1024 pointers to GfxDirEntry linked lists.
|
| 20 |
pub bucket_array: *mut u8,
|
| 21 |
/// 0x008: 1 if bucket_array was allocated via malloc fallback (needs free).
|
|
@@ -40,7 +42,7 @@ const _: () = assert!(core::mem::size_of::<GfxDir>() == 0x19C);
|
|
| 40 |
|
| 41 |
impl GfxDir {
|
| 42 |
/// Allocate and initialize a new GfxDir with the given vtable.
|
| 43 |
-
pub unsafe fn alloc(vtable:
|
| 44 |
let ptr = wa_malloc(core::mem::size_of::<Self>() as u32) as *mut Self;
|
| 45 |
if !ptr.is_null() {
|
| 46 |
core::ptr::write_bytes(ptr as *mut u8, 0, core::mem::size_of::<Self>());
|
|
@@ -50,6 +52,24 @@ impl GfxDir {
|
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
/// Entry in a GfxDir hash bucket linked list.
|
| 54 |
/// Each entry maps a name string to a cached resource value.
|
| 55 |
#[repr(C)]
|
|
@@ -231,20 +251,11 @@ unsafe fn gfx_dir_reset(handler: *mut u8) {
|
|
| 231 |
pub unsafe fn gfx_dir_load_dir(handler: *mut u8) -> i32 {
|
| 232 |
gfx_dir_reset(handler);
|
| 233 |
let gfx = &mut *(handler as *mut GfxDir);
|
| 234 |
-
|
| 235 |
-
let vt = *(handler as *const *const u32);
|
| 236 |
-
// vtable[0] = read: thiscall(handler, buf, size) -> bytes_read
|
| 237 |
-
let vt_read: unsafe extern "thiscall" fn(*mut u8, *mut u8, u32) -> u32 =
|
| 238 |
-
core::mem::transmute(*vt);
|
| 239 |
-
// vtable[1] = seek: thiscall(handler, size)
|
| 240 |
-
let vt_seek: unsafe extern "thiscall" fn(*mut u8, u32) = core::mem::transmute(*vt.add(1));
|
| 241 |
-
// vtable[2] = allocate: thiscall(handler, size) -> *mut u8
|
| 242 |
-
let vt_alloc: unsafe extern "thiscall" fn(*mut u8, u32) -> *mut u8 =
|
| 243 |
-
core::mem::transmute(*vt.add(2));
|
| 244 |
|
| 245 |
// Read and validate magic
|
| 246 |
let mut magic: u32 = 0;
|
| 247 |
-
if
|
| 248 |
return 0;
|
| 249 |
}
|
| 250 |
if magic != 0x1A524944 {
|
|
@@ -254,23 +265,23 @@ pub unsafe fn gfx_dir_load_dir(handler: *mut u8) -> i32 {
|
|
| 254 |
|
| 255 |
// Read total_file_size and data_size
|
| 256 |
let mut total_file_size: u32 = 0;
|
| 257 |
-
if
|
| 258 |
return 0;
|
| 259 |
}
|
| 260 |
let mut data_size: u32 = 0;
|
| 261 |
-
if
|
| 262 |
return 0;
|
| 263 |
}
|
| 264 |
|
| 265 |
let alloc_size = data_size + 4;
|
| 266 |
|
| 267 |
-
// Try fast path: vtable[2]
|
| 268 |
-
let data =
|
| 269 |
gfx.bucket_array = data;
|
| 270 |
|
| 271 |
if data.is_null() {
|
| 272 |
// Fallback: seek past header, then malloc + read entire data block
|
| 273 |
-
|
| 274 |
|
| 275 |
let read_size = total_file_size - data_size - 4;
|
| 276 |
let malloc_size = ((read_size + 3) & !3) + 0x20;
|
|
@@ -281,7 +292,7 @@ pub unsafe fn gfx_dir_load_dir(handler: *mut u8) -> i32 {
|
|
| 281 |
core::ptr::write_bytes(buf, 0, read_size as usize);
|
| 282 |
gfx.bucket_array = buf;
|
| 283 |
|
| 284 |
-
let bytes_read =
|
| 285 |
if bytes_read != read_size {
|
| 286 |
crate::wa_alloc::wa_free(buf);
|
| 287 |
return 0;
|
|
@@ -346,11 +357,8 @@ pub unsafe fn gfx_resource_create(
|
|
| 346 |
let entry = gfx_dir_find_entry(name, gfx_dir);
|
| 347 |
if !entry.is_null() {
|
| 348 |
// gfx_dir->vtable[2](entry->field_4) — cached load
|
| 349 |
-
let vt = *(gfx_dir as *const *const u32);
|
| 350 |
-
let load_cached: unsafe extern "thiscall" fn(*mut GfxDir, u32) -> *mut u8 =
|
| 351 |
-
core::mem::transmute(*vt.add(2));
|
| 352 |
let entry_val = (*(entry as *const GfxDirEntry)).value;
|
| 353 |
-
let cached =
|
| 354 |
if !cached.is_null() {
|
| 355 |
// DisplayGfx__Constructor_Maybe: stdcall(raw_image), RET 0x4
|
| 356 |
let ctor: unsafe extern "stdcall" fn(*mut u8) -> *mut u8 =
|
|
@@ -368,13 +376,9 @@ pub unsafe fn gfx_resource_create(
|
|
| 368 |
// IMG_Decode: stdcall(output, raw_image, 1), RET 0xC
|
| 369 |
let decode: unsafe extern "stdcall" fn(*mut u8, *mut u8, i32) -> *mut u8 =
|
| 370 |
core::mem::transmute(rb(va::IMG_DECODE) as usize);
|
| 371 |
-
let result = decode(output, raw_image, 1);
|
| 372 |
|
| 373 |
-
|
| 374 |
-
let img_vt = *(raw_image as *const *const u32);
|
| 375 |
-
let fn_ptr = *img_vt;
|
| 376 |
-
let release: unsafe extern "thiscall" fn(*mut u8, i32) = core::mem::transmute(fn_ptr);
|
| 377 |
-
release(raw_image, 1);
|
| 378 |
|
| 379 |
result
|
| 380 |
}
|
|
@@ -392,10 +396,7 @@ pub(crate) unsafe fn call_gfx_find_and_load(
|
|
| 392 |
|
| 393 |
if !entry.is_null() {
|
| 394 |
// Try cached load: gfx_dir->vtable[2](entry->field_4)
|
| 395 |
-
let
|
| 396 |
-
let load_cached: unsafe extern "thiscall" fn(*mut GfxDir, u32) -> *mut u8 =
|
| 397 |
-
core::mem::transmute(*dir_vt.add(2));
|
| 398 |
-
let cached = load_cached(gfx_dir, (*(entry as *const GfxDirEntry)).value);
|
| 399 |
if !cached.is_null() {
|
| 400 |
// Wrap with DisplayGfx__Constructor_Maybe (0x4F5E80)
|
| 401 |
// This is stdcall(1 param), RET 0x4
|
|
@@ -424,11 +425,8 @@ pub(crate) unsafe fn call_gfx_load_and_wrap(
|
|
| 424 |
// FUN_004F5F80(display_ctx, image, 1) — stdcall, RET 0xC (3 params)
|
| 425 |
let f: unsafe extern "stdcall" fn(*mut u8, *mut u8, u32) -> *mut u8 =
|
| 426 |
core::mem::transmute(rb(va::IMG_DECODE) as usize);
|
| 427 |
-
let result = f(display_ctx, image, 1);
|
| 428 |
-
|
| 429 |
-
let image_vt = *(image as *const *const u32);
|
| 430 |
-
let destroy: unsafe extern "thiscall" fn(*mut u8, u32) = core::mem::transmute(*image_vt);
|
| 431 |
-
destroy(image, 1);
|
| 432 |
result
|
| 433 |
}
|
| 434 |
|
|
@@ -459,8 +457,8 @@ pub struct GfxDirStreamVtable {
|
|
| 459 |
pub remaining: unsafe extern "thiscall" fn(this: *mut GfxDirStream) -> u32,
|
| 460 |
/// Slot 3 (0x566270): Unknown.
|
| 461 |
pub _slot_3: usize,
|
| 462 |
-
/// Slot 4 (0x5662C0):
|
| 463 |
-
pub
|
| 464 |
/// Slot 5 (0x5662F0): Read `size` bytes into `dest`.
|
| 465 |
pub read: unsafe extern "thiscall" fn(this: *mut GfxDirStream, dest: *mut u8, size: u32),
|
| 466 |
}
|
|
@@ -481,6 +479,12 @@ impl GfxDirStream {
|
|
| 481 |
((*(*this).vtable).remaining)(this)
|
| 482 |
}
|
| 483 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
/// Destroy the stream reader, releasing its cache slot.
|
| 485 |
#[inline]
|
| 486 |
pub unsafe fn destroy_raw(this: *mut Self) {
|
|
@@ -515,7 +519,7 @@ pub(crate) unsafe fn call_gfx_load_dir(handler: *mut u8, addr: u32) -> i32 {
|
|
| 515 |
pub unsafe extern "C" fn call_gfx_load_image(
|
| 516 |
_gfx_dir: *mut GfxDir,
|
| 517 |
_name: *const core::ffi::c_char,
|
| 518 |
-
) -> *mut
|
| 519 |
core::arch::naked_asm!(
|
| 520 |
"pushl %esi",
|
| 521 |
"movl 8(%esp), %esi", // ESI = gfx_dir
|
|
|
|
| 4 |
//! `Gfx0.dir`). It provides name→resource lookup via a 1024-bucket hash table
|
| 5 |
//! and delegates file I/O and caching to vtable methods.
|
| 6 |
|
| 7 |
+
use openwa_core::vtable;
|
| 8 |
+
|
| 9 |
use crate::address::va;
|
| 10 |
use crate::rebase::rb;
|
| 11 |
use crate::wa_alloc::wa_malloc;
|
|
|
|
| 17 |
#[repr(C)]
|
| 18 |
pub struct GfxDir {
|
| 19 |
/// 0x000: Vtable pointer (0x66B280).
|
| 20 |
+
pub vtable: *const GfxDirVtable,
|
| 21 |
/// 0x004: Bucket array — 1024 pointers to GfxDirEntry linked lists.
|
| 22 |
pub bucket_array: *mut u8,
|
| 23 |
/// 0x008: 1 if bucket_array was allocated via malloc fallback (needs free).
|
|
|
|
| 42 |
|
| 43 |
impl GfxDir {
|
| 44 |
/// Allocate and initialize a new GfxDir with the given vtable.
|
| 45 |
+
pub unsafe fn alloc(vtable: *const GfxDirVtable) -> *mut Self {
|
| 46 |
let ptr = wa_malloc(core::mem::size_of::<Self>() as u32) as *mut Self;
|
| 47 |
if !ptr.is_null() {
|
| 48 |
core::ptr::write_bytes(ptr as *mut u8, 0, core::mem::size_of::<Self>());
|
|
|
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
+
/// Vtable for `GfxDir` / GfxHandler (4 slots at 0x66B280).
|
| 56 |
+
///
|
| 57 |
+
/// Provides sequential I/O into the open `.dir` file and a cached resource
|
| 58 |
+
/// access path.
|
| 59 |
+
#[vtable(size = 4, va = 0x0066_B280, class = "GfxDir")]
|
| 60 |
+
pub struct GfxDirVtable {
|
| 61 |
+
/// Slot 0 (0x58BBD0): Read `size` bytes into `buf`. Returns bytes read.
|
| 62 |
+
pub read: fn(this: *mut GfxDir, buf: *mut u8, size: u32) -> u32,
|
| 63 |
+
/// Slot 1 (0x58BBB0): Seek forward `skip` bytes in the file.
|
| 64 |
+
pub seek: fn(this: *mut GfxDir, skip: u32),
|
| 65 |
+
/// Slot 2 (0x571AF0): Return cached data pointer for `entry_val`, or null if not mapped.
|
| 66 |
+
pub load_cached: fn(this: *mut GfxDir, entry_val: u32) -> *mut u8,
|
| 67 |
+
/// Slot 3 (0x58BB70): Release resources; if `flags & 1`, free the object.
|
| 68 |
+
pub release: fn(this: *mut GfxDir, flags: u32),
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
bind_GfxDirVtable!(GfxDir, vtable);
|
| 72 |
+
|
| 73 |
/// Entry in a GfxDir hash bucket linked list.
|
| 74 |
/// Each entry maps a name string to a cached resource value.
|
| 75 |
#[repr(C)]
|
|
|
|
| 251 |
pub unsafe fn gfx_dir_load_dir(handler: *mut u8) -> i32 {
|
| 252 |
gfx_dir_reset(handler);
|
| 253 |
let gfx = &mut *(handler as *mut GfxDir);
|
| 254 |
+
let gfx_ptr = handler as *mut GfxDir;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
// Read and validate magic
|
| 257 |
let mut magic: u32 = 0;
|
| 258 |
+
if GfxDir::read_raw(gfx_ptr, &mut magic as *mut u32 as *mut u8, 4) != 4 {
|
| 259 |
return 0;
|
| 260 |
}
|
| 261 |
if magic != 0x1A524944 {
|
|
|
|
| 265 |
|
| 266 |
// Read total_file_size and data_size
|
| 267 |
let mut total_file_size: u32 = 0;
|
| 268 |
+
if GfxDir::read_raw(gfx_ptr, &mut total_file_size as *mut u32 as *mut u8, 4) != 4 {
|
| 269 |
return 0;
|
| 270 |
}
|
| 271 |
let mut data_size: u32 = 0;
|
| 272 |
+
if GfxDir::read_raw(gfx_ptr, &mut data_size as *mut u32 as *mut u8, 4) != 4 {
|
| 273 |
return 0;
|
| 274 |
}
|
| 275 |
|
| 276 |
let alloc_size = data_size + 4;
|
| 277 |
|
| 278 |
+
// Try fast path: vtable[2] load_cached (memory-maps the data)
|
| 279 |
+
let data = GfxDir::load_cached_raw(gfx_ptr, alloc_size);
|
| 280 |
gfx.bucket_array = data;
|
| 281 |
|
| 282 |
if data.is_null() {
|
| 283 |
// Fallback: seek past header, then malloc + read entire data block
|
| 284 |
+
GfxDir::seek_raw(gfx_ptr, alloc_size);
|
| 285 |
|
| 286 |
let read_size = total_file_size - data_size - 4;
|
| 287 |
let malloc_size = ((read_size + 3) & !3) + 0x20;
|
|
|
|
| 292 |
core::ptr::write_bytes(buf, 0, read_size as usize);
|
| 293 |
gfx.bucket_array = buf;
|
| 294 |
|
| 295 |
+
let bytes_read = GfxDir::read_raw(gfx_ptr, buf, read_size);
|
| 296 |
if bytes_read != read_size {
|
| 297 |
crate::wa_alloc::wa_free(buf);
|
| 298 |
return 0;
|
|
|
|
| 357 |
let entry = gfx_dir_find_entry(name, gfx_dir);
|
| 358 |
if !entry.is_null() {
|
| 359 |
// gfx_dir->vtable[2](entry->field_4) — cached load
|
|
|
|
|
|
|
|
|
|
| 360 |
let entry_val = (*(entry as *const GfxDirEntry)).value;
|
| 361 |
+
let cached = GfxDir::load_cached_raw(gfx_dir, entry_val);
|
| 362 |
if !cached.is_null() {
|
| 363 |
// DisplayGfx__Constructor_Maybe: stdcall(raw_image), RET 0x4
|
| 364 |
let ctor: unsafe extern "stdcall" fn(*mut u8) -> *mut u8 =
|
|
|
|
| 376 |
// IMG_Decode: stdcall(output, raw_image, 1), RET 0xC
|
| 377 |
let decode: unsafe extern "stdcall" fn(*mut u8, *mut u8, i32) -> *mut u8 =
|
| 378 |
core::mem::transmute(rb(va::IMG_DECODE) as usize);
|
| 379 |
+
let result = decode(output, raw_image as *mut u8, 1);
|
| 380 |
|
| 381 |
+
GfxDirStream::destroy_raw(raw_image);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
result
|
| 384 |
}
|
|
|
|
| 396 |
|
| 397 |
if !entry.is_null() {
|
| 398 |
// Try cached load: gfx_dir->vtable[2](entry->field_4)
|
| 399 |
+
let cached = GfxDir::load_cached_raw(gfx_dir, (*(entry as *const GfxDirEntry)).value);
|
|
|
|
|
|
|
|
|
|
| 400 |
if !cached.is_null() {
|
| 401 |
// Wrap with DisplayGfx__Constructor_Maybe (0x4F5E80)
|
| 402 |
// This is stdcall(1 param), RET 0x4
|
|
|
|
| 425 |
// FUN_004F5F80(display_ctx, image, 1) — stdcall, RET 0xC (3 params)
|
| 426 |
let f: unsafe extern "stdcall" fn(*mut u8, *mut u8, u32) -> *mut u8 =
|
| 427 |
core::mem::transmute(rb(va::IMG_DECODE) as usize);
|
| 428 |
+
let result = f(display_ctx, image as *mut u8, 1);
|
| 429 |
+
GfxDirStream::destroy_raw(image);
|
|
|
|
|
|
|
|
|
|
| 430 |
result
|
| 431 |
}
|
| 432 |
|
|
|
|
| 457 |
pub remaining: unsafe extern "thiscall" fn(this: *mut GfxDirStream) -> u32,
|
| 458 |
/// Slot 3 (0x566270): Unknown.
|
| 459 |
pub _slot_3: usize,
|
| 460 |
+
/// Slot 4 (0x5662C0): Return total byte size of this stream's data region.
|
| 461 |
+
pub get_total_size: unsafe extern "thiscall" fn(this: *mut GfxDirStream) -> u32,
|
| 462 |
/// Slot 5 (0x5662F0): Read `size` bytes into `dest`.
|
| 463 |
pub read: unsafe extern "thiscall" fn(this: *mut GfxDirStream, dest: *mut u8, size: u32),
|
| 464 |
}
|
|
|
|
| 479 |
((*(*this).vtable).remaining)(this)
|
| 480 |
}
|
| 481 |
|
| 482 |
+
/// Returns the total byte size of this stream's data region.
|
| 483 |
+
#[inline]
|
| 484 |
+
pub unsafe fn total_size_raw(this: *mut Self) -> u32 {
|
| 485 |
+
((*(*this).vtable).get_total_size)(this)
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
/// Destroy the stream reader, releasing its cache slot.
|
| 489 |
#[inline]
|
| 490 |
pub unsafe fn destroy_raw(this: *mut Self) {
|
|
|
|
| 519 |
pub unsafe extern "C" fn call_gfx_load_image(
|
| 520 |
_gfx_dir: *mut GfxDir,
|
| 521 |
_name: *const core::ffi::c_char,
|
| 522 |
+
) -> *mut GfxDirStream {
|
| 523 |
core::arch::naked_asm!(
|
| 524 |
"pushl %esi",
|
| 525 |
"movl 8(%esp), %esi", // ESI = gfx_dir
|