refactor: extract GfxHandler subsystem to render/gfx_handler.rs
Browse filesMove GfxDir/GfxHandler resource loading functions out of ddgame.rs:
- gfx_dir_hash, gfx_dir_find_entry, gfx_handler_reset
- gfx_handler_load_dir, gfx_resource_create
- call_gfx_find_and_load, call_gfx_load_and_wrap
- call_gfx_load_image, call_gfx_load_dir (asm bridges)
- GFX_LOAD_IMAGE_ADDR, GFX_LOAD_DIR_ADDR statics
ddgame.rs: 2640 → 2250 lines (-390)
New: render/gfx_handler.rs (422 lines)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
crates/openwa-core/src/engine/ddgame.rs
CHANGED
|
@@ -9,6 +9,13 @@ use crate::engine::ddgame_wrapper::DDGameWrapper;
|
|
| 9 |
use crate::engine::game_info::GameInfo;
|
| 10 |
use crate::input::keyboard::DDKeyboard;
|
| 11 |
use crate::rebase::rb;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
use crate::render::landscape::PCLandscape;
|
| 13 |
use crate::render::queue::RenderQueue;
|
| 14 |
use crate::render::turn_order::TurnOrderWidget;
|
|
@@ -563,333 +570,17 @@ pub unsafe fn display_layer_color_init(wrapper: *mut DDGameWrapper) {
|
|
| 563 |
set_color(display, 3, 0x70);
|
| 564 |
}
|
| 565 |
|
| 566 |
-
/// Hash function for GfxDir entry lookup (FUN_566390).
|
| 567 |
-
///
|
| 568 |
-
/// 10-bit CRC-like hash over the global name buffer at 0x8ACF58.
|
| 569 |
-
/// Operates on the already-lowercased name.
|
| 570 |
-
///
|
| 571 |
-
/// # Safety
|
| 572 |
-
/// `name` must be a valid null-terminated C string pointer.
|
| 573 |
-
unsafe fn gfx_dir_hash(name: *const u8) -> u32 {
|
| 574 |
-
let mut hash: u32 = 0;
|
| 575 |
-
let mut p = name;
|
| 576 |
-
while *p != 0 {
|
| 577 |
-
let bit9 = (hash >> 9) & 1;
|
| 578 |
-
hash = ((hash << 1) | bit9) & 0x3FF;
|
| 579 |
-
hash = hash.wrapping_add(*p as u32) & 0x3FF;
|
| 580 |
-
p = p.add(1);
|
| 581 |
-
}
|
| 582 |
-
hash
|
| 583 |
-
}
|
| 584 |
-
|
| 585 |
-
/// Pure Rust implementation of GfxDir__FindEntry (0x566520).
|
| 586 |
-
///
|
| 587 |
-
/// Convention: usercall(EAX=name) + 1 stack(gfx_handler), RET 0x4.
|
| 588 |
-
///
|
| 589 |
-
/// Looks up a name in the GfxHandler's hash table. Names are case-insensitive.
|
| 590 |
-
/// Supports `|`-separated fallback names (e.g. "path\\file.img|fallback.img").
|
| 591 |
-
///
|
| 592 |
-
/// Returns entry pointer or null. Entry layout:
|
| 593 |
-
/// - entry+0x00: next pointer (linked list)
|
| 594 |
-
/// - entry+0x04: value (passed to vtable[2] for cached load)
|
| 595 |
-
/// - entry+0x08: unknown
|
| 596 |
-
/// - entry+0x0C: name string (null-terminated, lowercase)
|
| 597 |
-
///
|
| 598 |
-
/// # Safety
|
| 599 |
-
/// `gfx_handler` must be a valid GfxHandler with initialized hash table at +0x04.
|
| 600 |
-
/// `name` must be a valid null-terminated C string.
|
| 601 |
-
#[cfg(target_arch = "x86")]
|
| 602 |
-
pub unsafe fn gfx_dir_find_entry(name: *const u8, gfx_handler: *mut u8) -> *mut u8 {
|
| 603 |
-
let mut current_name = name;
|
| 604 |
-
|
| 605 |
-
loop {
|
| 606 |
-
// Copy name to stack buffer (max 0xFF chars + null)
|
| 607 |
-
let mut buf = [0u8; 0x100];
|
| 608 |
-
let mut i = 0usize;
|
| 609 |
-
let mut src = current_name;
|
| 610 |
-
while *src != 0 && i < 0xFF {
|
| 611 |
-
buf[i] = *src;
|
| 612 |
-
src = src.add(1);
|
| 613 |
-
i += 1;
|
| 614 |
-
}
|
| 615 |
-
buf[i] = 0;
|
| 616 |
-
|
| 617 |
-
// Find '|' separator in buffer
|
| 618 |
-
let mut pipe_pos: Option<usize> = None;
|
| 619 |
-
for j in 0..i {
|
| 620 |
-
if buf[j] == b'|' {
|
| 621 |
-
buf[j] = 0; // truncate at pipe
|
| 622 |
-
pipe_pos = Some(j);
|
| 623 |
-
break;
|
| 624 |
-
}
|
| 625 |
-
}
|
| 626 |
-
|
| 627 |
-
// Compute next_name: pointer into original string after '|'
|
| 628 |
-
let next_name: *const u8 = if let Some(pos) = pipe_pos {
|
| 629 |
-
// Offset of '|' in buffer = pos
|
| 630 |
-
// Same offset in original string → current_name + pos + 1
|
| 631 |
-
current_name.add(pos + 1)
|
| 632 |
-
} else {
|
| 633 |
-
core::ptr::null()
|
| 634 |
-
};
|
| 635 |
-
|
| 636 |
-
// Lowercase A-Z in buffer
|
| 637 |
-
for j in 0..buf.len() {
|
| 638 |
-
if buf[j] == 0 {
|
| 639 |
-
break;
|
| 640 |
-
}
|
| 641 |
-
if buf[j] >= b'A' && buf[j] <= b'Z' {
|
| 642 |
-
buf[j] += 0x20;
|
| 643 |
-
}
|
| 644 |
-
}
|
| 645 |
-
|
| 646 |
-
// Hash the lowercased name
|
| 647 |
-
let bucket = gfx_dir_hash(buf.as_ptr());
|
| 648 |
-
|
| 649 |
-
// Walk linked list in hash bucket
|
| 650 |
-
// gfx_handler+4 = pointer to bucket array (1024 entries)
|
| 651 |
-
let bucket_array = *(gfx_handler.add(4) as *const *const u32);
|
| 652 |
-
let mut entry = *bucket_array.add(bucket as usize) as *mut u8;
|
| 653 |
-
|
| 654 |
-
while !entry.is_null() {
|
| 655 |
-
// Compare entry name at entry+0x0C with our buffer
|
| 656 |
-
let entry_name = entry.add(0x0C);
|
| 657 |
-
let mut match_found = true;
|
| 658 |
-
let mut k = 0usize;
|
| 659 |
-
loop {
|
| 660 |
-
let a = *entry_name.add(k);
|
| 661 |
-
let b = buf[k];
|
| 662 |
-
if a != b {
|
| 663 |
-
match_found = false;
|
| 664 |
-
break;
|
| 665 |
-
}
|
| 666 |
-
if a == 0 {
|
| 667 |
-
break;
|
| 668 |
-
}
|
| 669 |
-
k += 1;
|
| 670 |
-
}
|
| 671 |
-
|
| 672 |
-
if match_found {
|
| 673 |
-
return entry;
|
| 674 |
-
}
|
| 675 |
-
|
| 676 |
-
// Next in linked list: entry+0x00
|
| 677 |
-
entry = *(entry as *const *mut u8);
|
| 678 |
-
}
|
| 679 |
-
|
| 680 |
-
// Not found — try fallback name after '|'
|
| 681 |
-
if next_name.is_null() {
|
| 682 |
-
return core::ptr::null_mut();
|
| 683 |
-
}
|
| 684 |
-
current_name = next_name;
|
| 685 |
-
}
|
| 686 |
-
}
|
| 687 |
-
|
| 688 |
-
/// Pure Rust implementation of FUN_5665F0 (GfxHandler reset).
|
| 689 |
-
///
|
| 690 |
-
/// Convention: usercall(ESI=handler), plain RET. Called at start of LoadDir.
|
| 691 |
-
unsafe fn gfx_handler_reset(handler: *mut u8) {
|
| 692 |
-
*(handler.add(0x04) as *mut u32) = 0;
|
| 693 |
-
*(handler.add(0x08) as *mut u32) = 0;
|
| 694 |
-
*(handler.add(0x194) as *mut u32) = 0;
|
| 695 |
-
|
| 696 |
-
for i in 0..16u32 {
|
| 697 |
-
// handler+0x0C + i*0x10 = 0 (16 entries at stride 0x10)
|
| 698 |
-
*(handler.add(0x0C + i as usize * 0x10) as *mut u32) = 0;
|
| 699 |
-
// handler+0x10C + i*4 = i (index array)
|
| 700 |
-
*(handler.add(0x10C + i as usize * 4) as *mut u32) = i;
|
| 701 |
-
}
|
| 702 |
-
|
| 703 |
-
*(handler.add(400) as *mut u32) = 0; // 0x190
|
| 704 |
-
*(handler.add(0x18C) as *mut u32) = 0x10;
|
| 705 |
-
}
|
| 706 |
-
|
| 707 |
-
/// Pure Rust implementation of GfxHandler__LoadDir (0x5663E0).
|
| 708 |
-
///
|
| 709 |
-
/// Convention: usercall(EAX=handler), plain RET. Returns 1 on success, 0 on failure.
|
| 710 |
-
///
|
| 711 |
-
/// Reads a .dir file through the handler's vtable I/O methods:
|
| 712 |
-
/// - vtable[0]: read(buf, size) → thiscall, returns bytes read
|
| 713 |
-
/// - vtable[1]: seek/reposition(size) → thiscall
|
| 714 |
-
/// - vtable[2]: allocate(size) → thiscall, returns buffer or null
|
| 715 |
-
///
|
| 716 |
-
/// .dir file format:
|
| 717 |
-
/// - 4 bytes: magic "DIR\x1A" (0x1A524944)
|
| 718 |
-
/// - 4 bytes: total_file_size
|
| 719 |
-
/// - 4 bytes: data_size (hash table + entries)
|
| 720 |
-
/// - data: 1024-bucket hash table followed by linked list nodes
|
| 721 |
-
/// All pointers are relative offsets from (data_start + 4)
|
| 722 |
-
///
|
| 723 |
-
/// # Safety
|
| 724 |
-
/// `handler` must be a valid GfxHandler with file handle at +0x198.
|
| 725 |
-
#[cfg(target_arch = "x86")]
|
| 726 |
-
pub unsafe fn gfx_handler_load_dir(handler: *mut u8) -> i32 {
|
| 727 |
-
gfx_handler_reset(handler);
|
| 728 |
-
|
| 729 |
-
let vt = *(handler as *const *const u32);
|
| 730 |
-
// vtable[0] = read: thiscall(handler, buf, size) -> bytes_read
|
| 731 |
-
let vt_read: unsafe extern "thiscall" fn(*mut u8, *mut u8, u32) -> u32 =
|
| 732 |
-
core::mem::transmute(*vt);
|
| 733 |
-
// vtable[1] = seek: thiscall(handler, size)
|
| 734 |
-
let vt_seek: unsafe extern "thiscall" fn(*mut u8, u32) = core::mem::transmute(*vt.add(1));
|
| 735 |
-
// vtable[2] = allocate: thiscall(handler, size) -> *mut u8
|
| 736 |
-
let vt_alloc: unsafe extern "thiscall" fn(*mut u8, u32) -> *mut u8 =
|
| 737 |
-
core::mem::transmute(*vt.add(2));
|
| 738 |
-
|
| 739 |
-
// Read and validate magic
|
| 740 |
-
let mut magic: u32 = 0;
|
| 741 |
-
if vt_read(handler, &mut magic as *mut u32 as *mut u8, 4) != 4 {
|
| 742 |
-
return 0;
|
| 743 |
-
}
|
| 744 |
-
if magic != 0x1A524944 {
|
| 745 |
-
// "DIR\x1A"
|
| 746 |
-
return 0;
|
| 747 |
-
}
|
| 748 |
-
|
| 749 |
-
// Read total_file_size and data_size
|
| 750 |
-
let mut total_file_size: u32 = 0;
|
| 751 |
-
if vt_read(handler, &mut total_file_size as *mut u32 as *mut u8, 4) != 4 {
|
| 752 |
-
return 0;
|
| 753 |
-
}
|
| 754 |
-
let mut data_size: u32 = 0;
|
| 755 |
-
if vt_read(handler, &mut data_size as *mut u32 as *mut u8, 4) != 4 {
|
| 756 |
-
return 0;
|
| 757 |
-
}
|
| 758 |
-
|
| 759 |
-
let alloc_size = data_size + 4;
|
| 760 |
-
|
| 761 |
-
// Try fast path: vtable[2] allocate (memory-maps the data)
|
| 762 |
-
let data = vt_alloc(handler, alloc_size);
|
| 763 |
-
*(handler.add(4) as *mut *mut u8) = data;
|
| 764 |
-
|
| 765 |
-
if data.is_null() {
|
| 766 |
-
// Fallback: seek past header, then malloc + read entire data block.
|
| 767 |
-
// Original calls vt_seek(alloc_size) then WA_MallocMemset(EDI=read_size).
|
| 768 |
-
// WA_MallocMemset allocates (size+3)&~3 + 0x20 and memsets size bytes.
|
| 769 |
-
vt_seek(handler, alloc_size);
|
| 770 |
-
|
| 771 |
-
let read_size = total_file_size - data_size - 4;
|
| 772 |
-
let malloc_size = ((read_size + 3) & !3) + 0x20;
|
| 773 |
-
let buf = wa_malloc(malloc_size);
|
| 774 |
-
if buf.is_null() {
|
| 775 |
-
return 0;
|
| 776 |
-
}
|
| 777 |
-
core::ptr::write_bytes(buf, 0, read_size as usize);
|
| 778 |
-
*(handler.add(4) as *mut *mut u8) = buf;
|
| 779 |
-
|
| 780 |
-
let bytes_read = vt_read(handler, buf, read_size);
|
| 781 |
-
if bytes_read != read_size {
|
| 782 |
-
crate::wa_alloc::wa_free(buf);
|
| 783 |
-
return 0;
|
| 784 |
-
}
|
| 785 |
-
|
| 786 |
-
// Mark as "loaded from fallback" at handler+8
|
| 787 |
-
*(handler.add(8) as *mut u32) = 1;
|
| 788 |
-
}
|
| 789 |
-
|
| 790 |
-
// Fix up relative pointers in the hash table
|
| 791 |
-
// 1024 buckets at data[0..0x1000], each is a pointer to a linked list node
|
| 792 |
-
let data = *(handler.add(4) as *const *mut u8);
|
| 793 |
-
let base = data as u32;
|
| 794 |
-
|
| 795 |
-
for bucket in 0..1024u32 {
|
| 796 |
-
let bucket_ptr = data.add(bucket as usize * 4) as *mut u32;
|
| 797 |
-
let entry_offset = *bucket_ptr;
|
| 798 |
-
if entry_offset == 0 {
|
| 799 |
-
continue;
|
| 800 |
-
}
|
| 801 |
-
|
| 802 |
-
// Convert relative offset to absolute: offset + base - 4
|
| 803 |
-
let entry_addr = entry_offset.wrapping_add(base).wrapping_sub(4);
|
| 804 |
-
*bucket_ptr = entry_addr;
|
| 805 |
-
|
| 806 |
-
// Walk linked list, fix up each next pointer
|
| 807 |
-
let mut node = entry_addr as *mut u32;
|
| 808 |
-
loop {
|
| 809 |
-
if node.is_null() {
|
| 810 |
-
break;
|
| 811 |
-
}
|
| 812 |
-
let next_offset = *node;
|
| 813 |
-
if next_offset == 0 {
|
| 814 |
-
break;
|
| 815 |
-
}
|
| 816 |
-
let next_addr = next_offset.wrapping_add(base).wrapping_sub(4);
|
| 817 |
-
*node = next_addr;
|
| 818 |
-
node = next_addr as *mut u32;
|
| 819 |
-
}
|
| 820 |
-
}
|
| 821 |
-
|
| 822 |
-
*(handler.add(0x194) as *mut u32) = 1;
|
| 823 |
-
|
| 824 |
-
1 // success
|
| 825 |
-
}
|
| 826 |
-
|
| 827 |
-
/// Pure Rust implementation of GfxResource__Create_Maybe (0x4F6300).
|
| 828 |
-
///
|
| 829 |
-
/// Convention: usercall(ECX=gfx_handler, EAX=name) + 1 stack(output), RET 0x4.
|
| 830 |
-
///
|
| 831 |
-
/// Looks up `name` in the GfxHandler's directory, tries cached load, wraps
|
| 832 |
-
/// as DisplayGfx. Falls back to loading the raw image and decoding it.
|
| 833 |
-
///
|
| 834 |
-
/// # Safety
|
| 835 |
-
/// All pointers must be valid WA objects.
|
| 836 |
-
#[cfg(target_arch = "x86")]
|
| 837 |
-
pub unsafe fn gfx_resource_create(
|
| 838 |
-
gfx_handler: *mut u8,
|
| 839 |
-
name: *const u8,
|
| 840 |
-
output: *mut u8,
|
| 841 |
-
) -> *mut u8 {
|
| 842 |
-
// 1. Try FindEntry → cached load → DisplayGfx wrap
|
| 843 |
-
let entry = gfx_dir_find_entry(name, gfx_handler);
|
| 844 |
-
if !entry.is_null() {
|
| 845 |
-
// gfx_handler->vtable[2](entry->field_4) — cached load
|
| 846 |
-
let vt = *(gfx_handler as *const *const u32);
|
| 847 |
-
let load_cached: unsafe extern "thiscall" fn(*mut u8, u32) -> *mut u8 =
|
| 848 |
-
core::mem::transmute(*vt.add(2));
|
| 849 |
-
let entry_val = *(entry.add(4) as *const u32);
|
| 850 |
-
let cached = load_cached(gfx_handler, entry_val);
|
| 851 |
-
if !cached.is_null() {
|
| 852 |
-
// DisplayGfx__Constructor_Maybe: stdcall(raw_image), RET 0x4
|
| 853 |
-
let ctor: unsafe extern "stdcall" fn(*mut u8) -> *mut u8 =
|
| 854 |
-
core::mem::transmute(rb(va::DISPLAYGFX_CONSTRUCTOR) as usize);
|
| 855 |
-
return ctor(cached);
|
| 856 |
-
}
|
| 857 |
-
}
|
| 858 |
-
|
| 859 |
-
// 2. Fallback: LoadImage → IMG_Decode
|
| 860 |
-
let raw_image = call_gfx_load_image(gfx_handler, name);
|
| 861 |
-
if raw_image.is_null() {
|
| 862 |
-
return core::ptr::null_mut();
|
| 863 |
-
}
|
| 864 |
-
|
| 865 |
-
// IMG_Decode: stdcall(output, raw_image, 1), RET 0xC
|
| 866 |
-
let decode: unsafe extern "stdcall" fn(*mut u8, *mut u8, i32) -> *mut u8 =
|
| 867 |
-
core::mem::transmute(rb(va::IMG_DECODE) as usize);
|
| 868 |
-
let result = decode(output, raw_image, 1);
|
| 869 |
-
|
| 870 |
-
// Release raw image: raw_image->vtable[0](1)
|
| 871 |
-
let img_vt = *(raw_image as *const *const u32);
|
| 872 |
-
let fn_ptr = *img_vt;
|
| 873 |
-
let release: unsafe extern "thiscall" fn(*mut u8, i32) = core::mem::transmute(fn_ptr);
|
| 874 |
-
release(raw_image, 1);
|
| 875 |
-
|
| 876 |
-
result
|
| 877 |
-
}
|
| 878 |
-
|
| 879 |
-
static mut GFX_LOAD_DIR_ADDR: u32 = 0;
|
| 880 |
-
|
| 881 |
/// Initialize runtime addresses for the constructor bridges.
|
| 882 |
/// Must be called once at DLL startup (from lib.rs or similar).
|
| 883 |
pub fn init_constructor_addrs() {
|
| 884 |
unsafe {
|
| 885 |
SPRITE_REGION_CTOR_ADDR = rb(va::SPRITE_REGION_CONSTRUCTOR);
|
| 886 |
FUN_570A90_ADDR = rb(va::FUN_570A90);
|
| 887 |
-
GFX_LOAD_IMAGE_ADDR = rb(va::GFX_DIR_LOAD_IMAGE);
|
| 888 |
-
GFX_LOAD_DIR_ADDR = rb(va::GFX_HANDLER_LOAD_DIR);
|
| 889 |
FUN_570F30_ADDR = rb(va::DDGAME_INIT_SOUND_PATHS);
|
| 890 |
LOAD_SPEECH_BANKS_ADDR = rb(va::DSSOUND_LOAD_ALL_SPEECH_BANKS);
|
| 891 |
LOADING_PROGRESS_TICK_ADDR = rb(va::DDGAME_WRAPPER_LOADING_PROGRESS_TICK);
|
| 892 |
}
|
|
|
|
| 893 |
}
|
| 894 |
|
| 895 |
/// Bridge to DDGame__InitVersionFlags (0x525BE0).
|
|
@@ -1043,7 +734,9 @@ unsafe fn init_graphics_and_resources(
|
|
| 1043 |
for (i, path) in paths.iter().enumerate() {
|
| 1044 |
let fp = fopen(path.as_ptr(), b"rb\0".as_ptr());
|
| 1045 |
*(gfx1.add(0x198) as *mut *mut u8) = fp;
|
| 1046 |
-
if !fp.is_null()
|
|
|
|
|
|
|
| 1047 |
gfx_loaded_idx = i as u32;
|
| 1048 |
break;
|
| 1049 |
}
|
|
@@ -1074,10 +767,14 @@ unsafe fn init_graphics_and_resources(
|
|
| 1074 |
|
| 1075 |
let fp = fopen(gfx_c_path.as_ptr(), b"rb\0".as_ptr());
|
| 1076 |
*(gfx2.add(0x198) as *mut *mut u8) = fp;
|
| 1077 |
-
if fp.is_null()
|
|
|
|
|
|
|
| 1078 |
let fp2 = fopen(b"data\\Gfx\\Gfx.dir\0".as_ptr(), b"rb\0".as_ptr());
|
| 1079 |
*(gfx2.add(0x198) as *mut *mut u8) = fp2;
|
| 1080 |
-
if fp2.is_null()
|
|
|
|
|
|
|
| 1081 |
panic!("DDGame: couldn't open secondary Gfx.dir");
|
| 1082 |
}
|
| 1083 |
}
|
|
@@ -1668,35 +1365,6 @@ unsafe fn init_graphics_and_resources(
|
|
| 1668 |
let _ = crate::log::log_line("[DDGame] init_graphics_and_resources DONE");
|
| 1669 |
}
|
| 1670 |
|
| 1671 |
-
/// Helper: find entry in GfxDir and load image, or load directly.
|
| 1672 |
-
/// Returns a DisplayGfx/sprite pointer or null.
|
| 1673 |
-
#[cfg(target_arch = "x86")]
|
| 1674 |
-
unsafe fn call_gfx_find_and_load(
|
| 1675 |
-
gfx_dir: *mut u8,
|
| 1676 |
-
name: *const u8,
|
| 1677 |
-
display_ctx: *mut u8,
|
| 1678 |
-
) -> *mut u8 {
|
| 1679 |
-
let entry = gfx_dir_find_entry(name, gfx_dir);
|
| 1680 |
-
|
| 1681 |
-
if !entry.is_null() {
|
| 1682 |
-
// Try cached load: gfx_dir->vtable[2](entry->field_4)
|
| 1683 |
-
let dir_vt = *(gfx_dir as *const *const u32);
|
| 1684 |
-
let load_cached: unsafe extern "thiscall" fn(*mut u8, u32) -> *mut u8 =
|
| 1685 |
-
core::mem::transmute(*dir_vt.add(2));
|
| 1686 |
-
let cached = load_cached(gfx_dir, *(entry.add(4) as *const u32));
|
| 1687 |
-
if !cached.is_null() {
|
| 1688 |
-
// Wrap with DisplayGfx__Constructor_Maybe (0x4F5E80)
|
| 1689 |
-
// This is stdcall(1 param), RET 0x4
|
| 1690 |
-
let ctor: unsafe extern "stdcall" fn(*mut u8) -> *mut u8 =
|
| 1691 |
-
core::mem::transmute(rb(0x4F5E80) as usize);
|
| 1692 |
-
return ctor(cached);
|
| 1693 |
-
}
|
| 1694 |
-
}
|
| 1695 |
-
|
| 1696 |
-
// Fallback: load image directly
|
| 1697 |
-
call_gfx_load_and_wrap(gfx_dir, name, display_ctx)
|
| 1698 |
-
}
|
| 1699 |
-
|
| 1700 |
// ── Complex gradient computation ──────────────────────────────────────────────
|
| 1701 |
|
| 1702 |
/// Palette context for gradient color mapping.
|
|
@@ -2024,64 +1692,6 @@ unsafe fn call_usercall_ecx(wrapper: *mut DDGameWrapper, addr: u32) {
|
|
| 2024 |
f(wrapper);
|
| 2025 |
}
|
| 2026 |
|
| 2027 |
-
/// Bridge to GfxHandler__LoadDir (0x5663E0).
|
| 2028 |
-
/// Convention: usercall(EAX=handler), plain RET. Returns nonzero on success.
|
| 2029 |
-
#[cfg(target_arch = "x86")]
|
| 2030 |
-
unsafe fn call_gfx_load_dir(handler: *mut u8, addr: u32) -> i32 {
|
| 2031 |
-
let result: i32;
|
| 2032 |
-
core::arch::asm!(
|
| 2033 |
-
"call {addr}",
|
| 2034 |
-
addr = in(reg) addr,
|
| 2035 |
-
inlateout("eax") handler => result,
|
| 2036 |
-
out("ecx") _,
|
| 2037 |
-
out("edx") _,
|
| 2038 |
-
clobber_abi("C"),
|
| 2039 |
-
);
|
| 2040 |
-
result
|
| 2041 |
-
}
|
| 2042 |
-
|
| 2043 |
-
// GfxDir__LoadImage is usercall(ESI=gfx_handler) + 1 stack(name), RET 0x4.
|
| 2044 |
-
// Returns raw image pointer or null.
|
| 2045 |
-
static mut GFX_LOAD_IMAGE_ADDR: u32 = 0;
|
| 2046 |
-
|
| 2047 |
-
#[cfg(target_arch = "x86")]
|
| 2048 |
-
#[unsafe(naked)]
|
| 2049 |
-
unsafe extern "C" fn call_gfx_load_image(_gfx_handler: *mut u8, _name: *const u8) -> *mut u8 {
|
| 2050 |
-
core::arch::naked_asm!(
|
| 2051 |
-
"pushl %esi",
|
| 2052 |
-
"movl 8(%esp), %esi", // ESI = gfx_handler
|
| 2053 |
-
"pushl 12(%esp)", // push name (callee cleans via RET 0x4)
|
| 2054 |
-
"calll *({addr})",
|
| 2055 |
-
"popl %esi",
|
| 2056 |
-
"retl",
|
| 2057 |
-
addr = sym GFX_LOAD_IMAGE_ADDR,
|
| 2058 |
-
options(att_syntax),
|
| 2059 |
-
);
|
| 2060 |
-
}
|
| 2061 |
-
|
| 2062 |
-
/// Helper: load image via GfxDir__LoadImage + wrap as DisplayGfx.
|
| 2063 |
-
/// Used by arrow sprite loop when GfxDir__FindEntry returns null.
|
| 2064 |
-
#[cfg(target_arch = "x86")]
|
| 2065 |
-
unsafe fn call_gfx_load_and_wrap(
|
| 2066 |
-
gfx_handler: *mut u8,
|
| 2067 |
-
name: *const u8,
|
| 2068 |
-
display_ctx: *mut u8,
|
| 2069 |
-
) -> *mut u8 {
|
| 2070 |
-
let image = call_gfx_load_image(gfx_handler, name);
|
| 2071 |
-
if image.is_null() {
|
| 2072 |
-
return core::ptr::null_mut();
|
| 2073 |
-
}
|
| 2074 |
-
// FUN_004F5F80(display_ctx, image, 1) — stdcall, RET 0xC (3 params)
|
| 2075 |
-
let f: unsafe extern "stdcall" fn(*mut u8, *mut u8, u32) -> *mut u8 =
|
| 2076 |
-
core::mem::transmute(rb(0x4F5F80) as usize);
|
| 2077 |
-
let result = f(display_ctx, image, 1);
|
| 2078 |
-
// Release the raw image
|
| 2079 |
-
let image_vt = *(image as *const *const u32);
|
| 2080 |
-
let destroy: unsafe extern "thiscall" fn(*mut u8, u32) = core::mem::transmute(*image_vt);
|
| 2081 |
-
destroy(image, 1);
|
| 2082 |
-
result
|
| 2083 |
-
}
|
| 2084 |
-
|
| 2085 |
/// Bridge to SpriteRegion__Constructor (0x57DB20).
|
| 2086 |
/// Convention: fastcall(ECX, EDX) + 6 stack params, RET 0x18.
|
| 2087 |
#[cfg(target_arch = "x86")]
|
|
|
|
| 9 |
use crate::engine::game_info::GameInfo;
|
| 10 |
use crate::input::keyboard::DDKeyboard;
|
| 11 |
use crate::rebase::rb;
|
| 12 |
+
// Re-export public GfxHandler functions so existing `engine::ddgame::*` imports keep working.
|
| 13 |
+
use crate::render::gfx_handler::{
|
| 14 |
+
call_gfx_find_and_load, call_gfx_load_and_wrap, call_gfx_load_dir,
|
| 15 |
+
};
|
| 16 |
+
pub use crate::render::gfx_handler::{
|
| 17 |
+
gfx_dir_find_entry, gfx_handler_load_dir, gfx_resource_create,
|
| 18 |
+
};
|
| 19 |
use crate::render::landscape::PCLandscape;
|
| 20 |
use crate::render::queue::RenderQueue;
|
| 21 |
use crate::render::turn_order::TurnOrderWidget;
|
|
|
|
| 570 |
set_color(display, 3, 0x70);
|
| 571 |
}
|
| 572 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
/// Initialize runtime addresses for the constructor bridges.
|
| 574 |
/// Must be called once at DLL startup (from lib.rs or similar).
|
| 575 |
pub fn init_constructor_addrs() {
|
| 576 |
unsafe {
|
| 577 |
SPRITE_REGION_CTOR_ADDR = rb(va::SPRITE_REGION_CONSTRUCTOR);
|
| 578 |
FUN_570A90_ADDR = rb(va::FUN_570A90);
|
|
|
|
|
|
|
| 579 |
FUN_570F30_ADDR = rb(va::DDGAME_INIT_SOUND_PATHS);
|
| 580 |
LOAD_SPEECH_BANKS_ADDR = rb(va::DSSOUND_LOAD_ALL_SPEECH_BANKS);
|
| 581 |
LOADING_PROGRESS_TICK_ADDR = rb(va::DDGAME_WRAPPER_LOADING_PROGRESS_TICK);
|
| 582 |
}
|
| 583 |
+
crate::render::gfx_handler::init_addrs();
|
| 584 |
}
|
| 585 |
|
| 586 |
/// Bridge to DDGame__InitVersionFlags (0x525BE0).
|
|
|
|
| 734 |
for (i, path) in paths.iter().enumerate() {
|
| 735 |
let fp = fopen(path.as_ptr(), b"rb\0".as_ptr());
|
| 736 |
*(gfx1.add(0x198) as *mut *mut u8) = fp;
|
| 737 |
+
if !fp.is_null()
|
| 738 |
+
&& call_gfx_load_dir(gfx1, crate::render::gfx_handler::gfx_load_dir_addr()) != 0
|
| 739 |
+
{
|
| 740 |
gfx_loaded_idx = i as u32;
|
| 741 |
break;
|
| 742 |
}
|
|
|
|
| 767 |
|
| 768 |
let fp = fopen(gfx_c_path.as_ptr(), b"rb\0".as_ptr());
|
| 769 |
*(gfx2.add(0x198) as *mut *mut u8) = fp;
|
| 770 |
+
if fp.is_null()
|
| 771 |
+
|| call_gfx_load_dir(gfx2, crate::render::gfx_handler::gfx_load_dir_addr()) == 0
|
| 772 |
+
{
|
| 773 |
let fp2 = fopen(b"data\\Gfx\\Gfx.dir\0".as_ptr(), b"rb\0".as_ptr());
|
| 774 |
*(gfx2.add(0x198) as *mut *mut u8) = fp2;
|
| 775 |
+
if fp2.is_null()
|
| 776 |
+
|| call_gfx_load_dir(gfx2, crate::render::gfx_handler::gfx_load_dir_addr()) == 0
|
| 777 |
+
{
|
| 778 |
panic!("DDGame: couldn't open secondary Gfx.dir");
|
| 779 |
}
|
| 780 |
}
|
|
|
|
| 1365 |
let _ = crate::log::log_line("[DDGame] init_graphics_and_resources DONE");
|
| 1366 |
}
|
| 1367 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1368 |
// ── Complex gradient computation ──────────────────────────────────────────────
|
| 1369 |
|
| 1370 |
/// Palette context for gradient color mapping.
|
|
|
|
| 1692 |
f(wrapper);
|
| 1693 |
}
|
| 1694 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1695 |
/// Bridge to SpriteRegion__Constructor (0x57DB20).
|
| 1696 |
/// Convention: fastcall(ECX, EDX) +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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//! GfxHandler/GfxDir resource loading subsystem.
|
| 2 |
+
|
| 3 |
+
use crate::address::va;
|
| 4 |
+
use crate::rebase::rb;
|
| 5 |
+
use crate::wa_alloc::wa_malloc;
|
| 6 |
+
|
| 7 |
+
/// Hash function for GfxDir entry lookup (FUN_566390).
|
| 8 |
+
///
|
| 9 |
+
/// 10-bit CRC-like hash over the global name buffer at 0x8ACF58.
|
| 10 |
+
/// Operates on the already-lowercased name.
|
| 11 |
+
///
|
| 12 |
+
/// # Safety
|
| 13 |
+
/// `name` must be a valid null-terminated C string pointer.
|
| 14 |
+
unsafe fn gfx_dir_hash(name: *const u8) -> u32 {
|
| 15 |
+
let mut hash: u32 = 0;
|
| 16 |
+
let mut p = name;
|
| 17 |
+
while *p != 0 {
|
| 18 |
+
let bit9 = (hash >> 9) & 1;
|
| 19 |
+
hash = ((hash << 1) | bit9) & 0x3FF;
|
| 20 |
+
hash = hash.wrapping_add(*p as u32) & 0x3FF;
|
| 21 |
+
p = p.add(1);
|
| 22 |
+
}
|
| 23 |
+
hash
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
/// Pure Rust implementation of GfxDir__FindEntry (0x566520).
|
| 27 |
+
///
|
| 28 |
+
/// Convention: usercall(EAX=name) + 1 stack(gfx_handler), RET 0x4.
|
| 29 |
+
///
|
| 30 |
+
/// Looks up a name in the GfxHandler's hash table. Names are case-insensitive.
|
| 31 |
+
/// Supports `|`-separated fallback names (e.g. "path\\file.img|fallback.img").
|
| 32 |
+
///
|
| 33 |
+
/// Returns entry pointer or null. Entry layout:
|
| 34 |
+
/// - entry+0x00: next pointer (linked list)
|
| 35 |
+
/// - entry+0x04: value (passed to vtable[2] for cached load)
|
| 36 |
+
/// - entry+0x08: unknown
|
| 37 |
+
/// - entry+0x0C: name string (null-terminated, lowercase)
|
| 38 |
+
///
|
| 39 |
+
/// # Safety
|
| 40 |
+
/// `gfx_handler` must be a valid GfxHandler with initialized hash table at +0x04.
|
| 41 |
+
/// `name` must be a valid null-terminated C string.
|
| 42 |
+
#[cfg(target_arch = "x86")]
|
| 43 |
+
pub unsafe fn gfx_dir_find_entry(name: *const u8, gfx_handler: *mut u8) -> *mut u8 {
|
| 44 |
+
let mut current_name = name;
|
| 45 |
+
|
| 46 |
+
loop {
|
| 47 |
+
// Copy name to stack buffer (max 0xFF chars + null)
|
| 48 |
+
let mut buf = [0u8; 0x100];
|
| 49 |
+
let mut i = 0usize;
|
| 50 |
+
let mut src = current_name;
|
| 51 |
+
while *src != 0 && i < 0xFF {
|
| 52 |
+
buf[i] = *src;
|
| 53 |
+
src = src.add(1);
|
| 54 |
+
i += 1;
|
| 55 |
+
}
|
| 56 |
+
buf[i] = 0;
|
| 57 |
+
|
| 58 |
+
// Find '|' separator in buffer
|
| 59 |
+
let mut pipe_pos: Option<usize> = None;
|
| 60 |
+
for j in 0..i {
|
| 61 |
+
if buf[j] == b'|' {
|
| 62 |
+
buf[j] = 0; // truncate at pipe
|
| 63 |
+
pipe_pos = Some(j);
|
| 64 |
+
break;
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// Compute next_name: pointer into original string after '|'
|
| 69 |
+
let next_name: *const u8 = if let Some(pos) = pipe_pos {
|
| 70 |
+
// Offset of '|' in buffer = pos
|
| 71 |
+
// Same offset in original string → current_name + pos + 1
|
| 72 |
+
current_name.add(pos + 1)
|
| 73 |
+
} else {
|
| 74 |
+
core::ptr::null()
|
| 75 |
+
};
|
| 76 |
+
|
| 77 |
+
// Lowercase A-Z in buffer
|
| 78 |
+
for j in 0..buf.len() {
|
| 79 |
+
if buf[j] == 0 {
|
| 80 |
+
break;
|
| 81 |
+
}
|
| 82 |
+
if buf[j] >= b'A' && buf[j] <= b'Z' {
|
| 83 |
+
buf[j] += 0x20;
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// Hash the lowercased name
|
| 88 |
+
let bucket = gfx_dir_hash(buf.as_ptr());
|
| 89 |
+
|
| 90 |
+
// Walk linked list in hash bucket
|
| 91 |
+
// gfx_handler+4 = pointer to bucket array (1024 entries)
|
| 92 |
+
let bucket_array = *(gfx_handler.add(4) as *const *const u32);
|
| 93 |
+
let mut entry = *bucket_array.add(bucket as usize) as *mut u8;
|
| 94 |
+
|
| 95 |
+
while !entry.is_null() {
|
| 96 |
+
// Compare entry name at entry+0x0C with our buffer
|
| 97 |
+
let entry_name = entry.add(0x0C);
|
| 98 |
+
let mut match_found = true;
|
| 99 |
+
let mut k = 0usize;
|
| 100 |
+
loop {
|
| 101 |
+
let a = *entry_name.add(k);
|
| 102 |
+
let b = buf[k];
|
| 103 |
+
if a != b {
|
| 104 |
+
match_found = false;
|
| 105 |
+
break;
|
| 106 |
+
}
|
| 107 |
+
if a == 0 {
|
| 108 |
+
break;
|
| 109 |
+
}
|
| 110 |
+
k += 1;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
if match_found {
|
| 114 |
+
return entry;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// Next in linked list: entry+0x00
|
| 118 |
+
entry = *(entry as *const *mut u8);
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
// Not found — try fallback name after '|'
|
| 122 |
+
if next_name.is_null() {
|
| 123 |
+
return core::ptr::null_mut();
|
| 124 |
+
}
|
| 125 |
+
current_name = next_name;
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
/// Pure Rust implementation of FUN_5665F0 (GfxHandler reset).
|
| 130 |
+
///
|
| 131 |
+
/// Convention: usercall(ESI=handler), plain RET. Called at start of LoadDir.
|
| 132 |
+
unsafe fn gfx_handler_reset(handler: *mut u8) {
|
| 133 |
+
*(handler.add(0x04) as *mut u32) = 0;
|
| 134 |
+
*(handler.add(0x08) as *mut u32) = 0;
|
| 135 |
+
*(handler.add(0x194) as *mut u32) = 0;
|
| 136 |
+
|
| 137 |
+
for i in 0..16u32 {
|
| 138 |
+
// handler+0x0C + i*0x10 = 0 (16 entries at stride 0x10)
|
| 139 |
+
*(handler.add(0x0C + i as usize * 0x10) as *mut u32) = 0;
|
| 140 |
+
// handler+0x10C + i*4 = i (index array)
|
| 141 |
+
*(handler.add(0x10C + i as usize * 4) as *mut u32) = i;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
*(handler.add(400) as *mut u32) = 0; // 0x190
|
| 145 |
+
*(handler.add(0x18C) as *mut u32) = 0x10;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
/// Pure Rust implementation of GfxHandler__LoadDir (0x5663E0).
|
| 149 |
+
///
|
| 150 |
+
/// Convention: usercall(EAX=handler), plain RET. Returns 1 on success, 0 on failure.
|
| 151 |
+
///
|
| 152 |
+
/// Reads a .dir file through the handler's vtable I/O methods:
|
| 153 |
+
/// - vtable[0]: read(buf, size) → thiscall, returns bytes read
|
| 154 |
+
/// - vtable[1]: seek/reposition(size) → thiscall
|
| 155 |
+
/// - vtable[2]: allocate(size) → thiscall, returns buffer or null
|
| 156 |
+
///
|
| 157 |
+
/// .dir file format:
|
| 158 |
+
/// - 4 bytes: magic "DIR\x1A" (0x1A524944)
|
| 159 |
+
/// - 4 bytes: total_file_size
|
| 160 |
+
/// - 4 bytes: data_size (hash table + entries)
|
| 161 |
+
/// - data: 1024-bucket hash table followed by linked list nodes
|
| 162 |
+
/// All pointers are relative offsets from (data_start + 4)
|
| 163 |
+
///
|
| 164 |
+
/// # Safety
|
| 165 |
+
/// `handler` must be a valid GfxHandler with file handle at +0x198.
|
| 166 |
+
#[cfg(target_arch = "x86")]
|
| 167 |
+
pub unsafe fn gfx_handler_load_dir(handler: *mut u8) -> i32 {
|
| 168 |
+
gfx_handler_reset(handler);
|
| 169 |
+
|
| 170 |
+
let vt = *(handler as *const *const u32);
|
| 171 |
+
// vtable[0] = read: thiscall(handler, buf, size) -> bytes_read
|
| 172 |
+
let vt_read: unsafe extern "thiscall" fn(*mut u8, *mut u8, u32) -> u32 =
|
| 173 |
+
core::mem::transmute(*vt);
|
| 174 |
+
// vtable[1] = seek: thiscall(handler, size)
|
| 175 |
+
let vt_seek: unsafe extern "thiscall" fn(*mut u8, u32) = core::mem::transmute(*vt.add(1));
|
| 176 |
+
// vtable[2] = allocate: thiscall(handler, size) -> *mut u8
|
| 177 |
+
let vt_alloc: unsafe extern "thiscall" fn(*mut u8, u32) -> *mut u8 =
|
| 178 |
+
core::mem::transmute(*vt.add(2));
|
| 179 |
+
|
| 180 |
+
// Read and validate magic
|
| 181 |
+
let mut magic: u32 = 0;
|
| 182 |
+
if vt_read(handler, &mut magic as *mut u32 as *mut u8, 4) != 4 {
|
| 183 |
+
return 0;
|
| 184 |
+
}
|
| 185 |
+
if magic != 0x1A524944 {
|
| 186 |
+
// "DIR\x1A"
|
| 187 |
+
return 0;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
// Read total_file_size and data_size
|
| 191 |
+
let mut total_file_size: u32 = 0;
|
| 192 |
+
if vt_read(handler, &mut total_file_size as *mut u32 as *mut u8, 4) != 4 {
|
| 193 |
+
return 0;
|
| 194 |
+
}
|
| 195 |
+
let mut data_size: u32 = 0;
|
| 196 |
+
if vt_read(handler, &mut data_size as *mut u32 as *mut u8, 4) != 4 {
|
| 197 |
+
return 0;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
let alloc_size = data_size + 4;
|
| 201 |
+
|
| 202 |
+
// Try fast path: vtable[2] allocate (memory-maps the data)
|
| 203 |
+
let data = vt_alloc(handler, alloc_size);
|
| 204 |
+
*(handler.add(4) as *mut *mut u8) = data;
|
| 205 |
+
|
| 206 |
+
if data.is_null() {
|
| 207 |
+
// Fallback: seek past header, then malloc + read entire data block.
|
| 208 |
+
// Original calls vt_seek(alloc_size) then WA_MallocMemset(EDI=read_size).
|
| 209 |
+
// WA_MallocMemset allocates (size+3)&~3 + 0x20 and memsets size bytes.
|
| 210 |
+
vt_seek(handler, alloc_size);
|
| 211 |
+
|
| 212 |
+
let read_size = total_file_size - data_size - 4;
|
| 213 |
+
let malloc_size = ((read_size + 3) & !3) + 0x20;
|
| 214 |
+
let buf = wa_malloc(malloc_size);
|
| 215 |
+
if buf.is_null() {
|
| 216 |
+
return 0;
|
| 217 |
+
}
|
| 218 |
+
core::ptr::write_bytes(buf, 0, read_size as usize);
|
| 219 |
+
*(handler.add(4) as *mut *mut u8) = buf;
|
| 220 |
+
|
| 221 |
+
let bytes_read = vt_read(handler, buf, read_size);
|
| 222 |
+
if bytes_read != read_size {
|
| 223 |
+
crate::wa_alloc::wa_free(buf);
|
| 224 |
+
return 0;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
// Mark as "loaded from fallback" at handler+8
|
| 228 |
+
*(handler.add(8) as *mut u32) = 1;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
// Fix up relative pointers in the hash table
|
| 232 |
+
// 1024 buckets at data[0..0x1000], each is a pointer to a linked list node
|
| 233 |
+
let data = *(handler.add(4) as *const *mut u8);
|
| 234 |
+
let base = data as u32;
|
| 235 |
+
|
| 236 |
+
for bucket in 0..1024u32 {
|
| 237 |
+
let bucket_ptr = data.add(bucket as usize * 4) as *mut u32;
|
| 238 |
+
let entry_offset = *bucket_ptr;
|
| 239 |
+
if entry_offset == 0 {
|
| 240 |
+
continue;
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
// Convert relative offset to absolute: offset + base - 4
|
| 244 |
+
let entry_addr = entry_offset.wrapping_add(base).wrapping_sub(4);
|
| 245 |
+
*bucket_ptr = entry_addr;
|
| 246 |
+
|
| 247 |
+
// Walk linked list, fix up each next pointer
|
| 248 |
+
let mut node = entry_addr as *mut u32;
|
| 249 |
+
loop {
|
| 250 |
+
if node.is_null() {
|
| 251 |
+
break;
|
| 252 |
+
}
|
| 253 |
+
let next_offset = *node;
|
| 254 |
+
if next_offset == 0 {
|
| 255 |
+
break;
|
| 256 |
+
}
|
| 257 |
+
let next_addr = next_offset.wrapping_add(base).wrapping_sub(4);
|
| 258 |
+
*node = next_addr;
|
| 259 |
+
node = next_addr as *mut u32;
|
| 260 |
+
}
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
*(handler.add(0x194) as *mut u32) = 1;
|
| 264 |
+
|
| 265 |
+
1 // success
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
/// Pure Rust implementation of GfxResource__Create_Maybe (0x4F6300).
|
| 269 |
+
///
|
| 270 |
+
/// Convention: usercall(ECX=gfx_handler, EAX=name) + 1 stack(output), RET 0x4.
|
| 271 |
+
///
|
| 272 |
+
/// Looks up `name` in the GfxHandler's directory, tries cached load, wraps
|
| 273 |
+
/// as DisplayGfx. Falls back to loading the raw image and decoding it.
|
| 274 |
+
///
|
| 275 |
+
/// # Safety
|
| 276 |
+
/// All pointers must be valid WA objects.
|
| 277 |
+
#[cfg(target_arch = "x86")]
|
| 278 |
+
pub unsafe fn gfx_resource_create(
|
| 279 |
+
gfx_handler: *mut u8,
|
| 280 |
+
name: *const u8,
|
| 281 |
+
output: *mut u8,
|
| 282 |
+
) -> *mut u8 {
|
| 283 |
+
// 1. Try FindEntry → cached load → DisplayGfx wrap
|
| 284 |
+
let entry = gfx_dir_find_entry(name, gfx_handler);
|
| 285 |
+
if !entry.is_null() {
|
| 286 |
+
// gfx_handler->vtable[2](entry->field_4) — cached load
|
| 287 |
+
let vt = *(gfx_handler as *const *const u32);
|
| 288 |
+
let load_cached: unsafe extern "thiscall" fn(*mut u8, u32) -> *mut u8 =
|
| 289 |
+
core::mem::transmute(*vt.add(2));
|
| 290 |
+
let entry_val = *(entry.add(4) as *const u32);
|
| 291 |
+
let cached = load_cached(gfx_handler, entry_val);
|
| 292 |
+
if !cached.is_null() {
|
| 293 |
+
// DisplayGfx__Constructor_Maybe: stdcall(raw_image), RET 0x4
|
| 294 |
+
let ctor: unsafe extern "stdcall" fn(*mut u8) -> *mut u8 =
|
| 295 |
+
core::mem::transmute(rb(va::DISPLAYGFX_CONSTRUCTOR) as usize);
|
| 296 |
+
return ctor(cached);
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
// 2. Fallback: LoadImage → IMG_Decode
|
| 301 |
+
let raw_image = call_gfx_load_image(gfx_handler, name);
|
| 302 |
+
if raw_image.is_null() {
|
| 303 |
+
return core::ptr::null_mut();
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
// IMG_Decode: stdcall(output, raw_image, 1), RET 0xC
|
| 307 |
+
let decode: unsafe extern "stdcall" fn(*mut u8, *mut u8, i32) -> *mut u8 =
|
| 308 |
+
core::mem::transmute(rb(va::IMG_DECODE) as usize);
|
| 309 |
+
let result = decode(output, raw_image, 1);
|
| 310 |
+
|
| 311 |
+
// Release raw image: raw_image->vtable[0](1)
|
| 312 |
+
let img_vt = *(raw_image as *const *const u32);
|
| 313 |
+
let fn_ptr = *img_vt;
|
| 314 |
+
let release: unsafe extern "thiscall" fn(*mut u8, i32) = core::mem::transmute(fn_ptr);
|
| 315 |
+
release(raw_image, 1);
|
| 316 |
+
|
| 317 |
+
result
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
/// Helper: find entry in GfxDir and load image, or load directly.
|
| 321 |
+
/// Returns a DisplayGfx/sprite pointer or null.
|
| 322 |
+
#[cfg(target_arch = "x86")]
|
| 323 |
+
pub(crate) unsafe fn call_gfx_find_and_load(
|
| 324 |
+
gfx_dir: *mut u8,
|
| 325 |
+
name: *const u8,
|
| 326 |
+
display_ctx: *mut u8,
|
| 327 |
+
) -> *mut u8 {
|
| 328 |
+
let entry = gfx_dir_find_entry(name, gfx_dir);
|
| 329 |
+
|
| 330 |
+
if !entry.is_null() {
|
| 331 |
+
// Try cached load: gfx_dir->vtable[2](entry->field_4)
|
| 332 |
+
let dir_vt = *(gfx_dir as *const *const u32);
|
| 333 |
+
let load_cached: unsafe extern "thiscall" fn(*mut u8, u32) -> *mut u8 =
|
| 334 |
+
core::mem::transmute(*dir_vt.add(2));
|
| 335 |
+
let cached = load_cached(gfx_dir, *(entry.add(4) as *const u32));
|
| 336 |
+
if !cached.is_null() {
|
| 337 |
+
// Wrap with DisplayGfx__Constructor_Maybe (0x4F5E80)
|
| 338 |
+
// This is stdcall(1 param), RET 0x4
|
| 339 |
+
let ctor: unsafe extern "stdcall" fn(*mut u8) -> *mut u8 =
|
| 340 |
+
core::mem::transmute(rb(0x4F5E80) as usize);
|
| 341 |
+
return ctor(cached);
|
| 342 |
+
}
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
// Fallback: load image directly
|
| 346 |
+
call_gfx_load_and_wrap(gfx_dir, name, display_ctx)
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
/// Helper: load image via GfxDir__LoadImage + wrap as DisplayGfx.
|
| 350 |
+
/// Used by arrow sprite loop when GfxDir__FindEntry returns null.
|
| 351 |
+
#[cfg(target_arch = "x86")]
|
| 352 |
+
pub(crate) unsafe fn call_gfx_load_and_wrap(
|
| 353 |
+
gfx_handler: *mut u8,
|
| 354 |
+
name: *const u8,
|
| 355 |
+
display_ctx: *mut u8,
|
| 356 |
+
) -> *mut u8 {
|
| 357 |
+
let image = call_gfx_load_image(gfx_handler, name);
|
| 358 |
+
if image.is_null() {
|
| 359 |
+
return core::ptr::null_mut();
|
| 360 |
+
}
|
| 361 |
+
// FUN_004F5F80(display_ctx, image, 1) — stdcall, RET 0xC (3 params)
|
| 362 |
+
let f: unsafe extern "stdcall" fn(*mut u8, *mut u8, u32) -> *mut u8 =
|
| 363 |
+
core::mem::transmute(rb(0x4F5F80) as usize);
|
| 364 |
+
let result = f(display_ctx, image, 1);
|
| 365 |
+
// Release the raw image
|
| 366 |
+
let image_vt = *(image as *const *const u32);
|
| 367 |
+
let destroy: unsafe extern "thiscall" fn(*mut u8, u32) = core::mem::transmute(*image_vt);
|
| 368 |
+
destroy(image, 1);
|
| 369 |
+
result
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
static mut GFX_LOAD_DIR_ADDR: u32 = 0;
|
| 373 |
+
|
| 374 |
+
// GfxDir__LoadImage is usercall(ESI=gfx_handler) + 1 stack(name), RET 0x4.
|
| 375 |
+
// Returns raw image pointer or null.
|
| 376 |
+
static mut GFX_LOAD_IMAGE_ADDR: u32 = 0;
|
| 377 |
+
|
| 378 |
+
/// Bridge to GfxHandler__LoadDir (0x5663E0).
|
| 379 |
+
/// Convention: usercall(EAX=handler), plain RET. Returns nonzero on success.
|
| 380 |
+
#[cfg(target_arch = "x86")]
|
| 381 |
+
pub(crate) unsafe fn call_gfx_load_dir(handler: *mut u8, addr: u32) -> i32 {
|
| 382 |
+
let result: i32;
|
| 383 |
+
core::arch::asm!(
|
| 384 |
+
"call {addr}",
|
| 385 |
+
addr = in(reg) addr,
|
| 386 |
+
inlateout("eax") handler => result,
|
| 387 |
+
out("ecx") _,
|
| 388 |
+
out("edx") _,
|
| 389 |
+
clobber_abi("C"),
|
| 390 |
+
);
|
| 391 |
+
result
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
#[cfg(target_arch = "x86")]
|
| 395 |
+
#[unsafe(naked)]
|
| 396 |
+
unsafe extern "C" fn call_gfx_load_image(_gfx_handler: *mut u8, _name: *const u8) -> *mut u8 {
|
| 397 |
+
core::arch::naked_asm!(
|
| 398 |
+
"pushl %esi",
|
| 399 |
+
"movl 8(%esp), %esi", // ESI = gfx_handler
|
| 400 |
+
"pushl 12(%esp)", // push name (callee cleans via RET 0x4)
|
| 401 |
+
"calll *({addr})",
|
| 402 |
+
"popl %esi",
|
| 403 |
+
"retl",
|
| 404 |
+
addr = sym GFX_LOAD_IMAGE_ADDR,
|
| 405 |
+
options(att_syntax),
|
| 406 |
+
);
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
/// Initialize runtime addresses for GfxHandler bridges.
|
| 410 |
+
/// Must be called once at DLL startup.
|
| 411 |
+
pub fn init_addrs() {
|
| 412 |
+
unsafe {
|
| 413 |
+
GFX_LOAD_IMAGE_ADDR = rb(va::GFX_DIR_LOAD_IMAGE);
|
| 414 |
+
GFX_LOAD_DIR_ADDR = rb(va::GFX_HANDLER_LOAD_DIR);
|
| 415 |
+
}
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
/// Returns the rebased runtime address for GfxHandler__LoadDir.
|
| 419 |
+
/// Used by callers that pass the address to `call_gfx_load_dir`.
|
| 420 |
+
pub fn gfx_load_dir_addr() -> u32 {
|
| 421 |
+
unsafe { GFX_LOAD_DIR_ADDR }
|
| 422 |
+
}
|
crates/openwa-core/src/render/mod.rs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
pub mod landscape;
|
| 2 |
pub mod queue;
|
| 3 |
pub mod sprite;
|
|
|
|
| 1 |
+
pub mod gfx_handler;
|
| 2 |
pub mod landscape;
|
| 3 |
pub mod queue;
|
| 4 |
pub mod sprite;
|