feat: full Rust replacement for DrawSpriteGlobal and DrawSpriteLocal
Browse filesReplace both sprite draw functions (0x541FE0, 0x542060) with Rust
implementations that enqueue RenderCommands to the downward-growing
render queue buffer. Add RenderQueue/RenderCommand types with an
alloc_entry() helper, and a new usercall_trampoline macro arm for
2 registers + 4 stack params.
Verified: 96 PASS / 0 FAIL in replay test.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Files changed (5)
7 unmodified lines89101112137 unmodified linespub mod ddgame;pub mod ddgame_wrapper;pub mod landscape;pub mod dssound;pub mod opengl;pub mod frontend;7 unmodified lines8910111213147 unmodified linespub mod ddgame;pub mod ddgame_wrapper;pub mod landscape;pub mod render;pub mod dssound;pub mod opengl;pub mod frontend;
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768use crate::task::Ptr32;
/// Render command entry (0x18 bytes).////// Enqueued by DrawSpriteGlobal (type 4), DrawSpriteLocal (type 5),/// and other drawing functions.#[repr(C)]pub struct RenderCommand { /// Command type: 4 = sprite global (world-space), 5 = sprite local (screen-space) pub command_type: u32, pub layer: u32, /// X position, upper 16 bits used (Fixed16 format) pub x_pos: u32, /// Y position, upper 16 bits used (Fixed16 format) pub y_pos: u32, pub sprite_id: u32, pub frame: u32,}
const _: () = assert!(core::mem::size_of::<RenderCommand>() == 0x18);
/// Render queue — manages a downward-growing buffer of RenderCommand entries.////// Passed as `this` (ECX) to DrawSpriteGlobal/Local. The buffer area sits/// between offset 0x04 and the entry metadata at 0x10000+. Entries are/// allocated from the end of the buffer downward (offset decrements by 0x18).////// Max 0x800 (2048) entries per frame.#[repr(C)]pub struct RenderQueue { /// 0x00000: Buffer write offset (i32, decrements by 0x18 per entry) pub buffer_offset: i32, /// 0x00004 - 0x10003: Buffer area for RenderCommand entries pub _buffer: [u8; 0x10000], /// 0x10004: Number of enqueued entries (max 0x800) pub entry_count: u32, /// 0x10008: Pointer array — entry_ptrs[N] points to the N-th RenderCommand pub entry_ptrs: [Ptr32; 0x800],}
impl RenderQueue { /// Allocate a new entry from the downward-growing buffer. /// /// Returns `None` if the queue is full (>= 0x800 entries or buffer exhausted). /// The returned reference is registered in `entry_ptrs` and `entry_count` is incremented. pub unsafe fn alloc_entry(&mut self) -> Option<&mut RenderCommand> { if self.entry_count >= 0x800 { return None; } let new_offset = self.buffer_offset - core::mem::size_of::<RenderCommand>() as i32; if new_offset < 0 { return None; } self.buffer_offset = new_offset;
let entry = &mut *(self._buffer.as_mut_ptr().add(new_offset as usize) as *mut RenderCommand); self.entry_ptrs[self.entry_count as usize] = entry as *mut RenderCommand as u32; self.entry_count += 1; Some(entry) }}
/// Render command type constants.pub mod command_type { pub const DRAW_SPRITE_GLOBAL: u32 = 4; pub const DRAW_SPRITE_LOCAL: u32 = 5;}112 unmodified lines113114115116117118112 unmodified lines}};// 2 register args, 3 stack params, ret N(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];stack_params = 3; ret_bytes = $ret:literal) => {112 unmodified lines113114115116117118119120121122123124125126127128129130131132133134135136137138139140112 unmodified lines}};// 2 register args, 4 stack params, ret N(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];stack_params = 4; ret_bytes = $ret:literal) => {#[unsafe(naked)]unsafe extern "C" fn $name() {core::arch::naked_asm!("push edx","push [esp+20]","push [esp+20]","push [esp+20]","push [esp+20]",concat!("push ", stringify!($r2)),concat!("push ", stringify!($r1)),"call {impl_fn}","add esp, 24","pop edx",concat!("ret ", $ret),impl_fn = sym $impl,);}};// 2 register args, 3 stack params, ret N(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];stack_params = 3; ret_bytes = $ret:literal) => {
1234563 unmodified lines1011121314mod config;mod frontend;mod scheme;mod team;mod weapon;3 unmodified linesconfig::install()?;weapon::install()?;team::install()?;Ok(())}12345673 unmodified lines111213141516mod config;mod frontend;mod scheme;mod sprite;mod team;mod weapon;3 unmodified linesconfig::install()?;weapon::install()?;team::install()?;unsafe { sprite::install()? };Ok(())}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495//! Sprite drawing hooks — full Rust replacements.//!//! - DrawSpriteGlobal (0x541FE0): enqueue world-space sprite (command type 4)//! - DrawSpriteLocal (0x542060): enqueue screen-space sprite (command type 5)//!//! Both functions are __thiscall + EAX=y_pos (usercall), 4 stack params, RET 0x10.
use openwa_types::address::va;use openwa_types::render::{command_type, RenderCommand, RenderQueue};
use crate::hook::{self, usercall_trampoline};
// ---------------------------------------------------------------------------// DrawSpriteGlobal — full Rust replacement// ---------------------------------------------------------------------------
usercall_trampoline!(fn trampoline_draw_sprite_global; impl_fn = draw_sprite_global_impl; regs = [eax, ecx]; stack_params = 4; ret_bytes = "0x10");
/// Full Rust replacement for DrawSpriteGlobal (0x541FE0).////// Enqueues a render command (type 4, world-space) to the drawing queue.unsafe extern "cdecl" fn draw_sprite_global_impl( y_pos: u32, this: u32, layer: u32, x_pos: u32, sprite_id: u32, frame: u32,) { let q = &mut *(this as *mut RenderQueue);
if let Some(entry) = q.alloc_entry() { *entry = RenderCommand { command_type: command_type::DRAW_SPRITE_GLOBAL, layer, x_pos: x_pos & 0xFFFF0000, y_pos: y_pos & 0xFFFF0000, sprite_id, frame, }; }}
// ---------------------------------------------------------------------------// DrawSpriteLocal — full Rust replacement// ---------------------------------------------------------------------------
usercall_trampoline!(fn trampoline_draw_sprite_local; impl_fn = draw_sprite_local_impl; regs = [eax, ecx]; stack_params = 4; ret_bytes = "0x10");
/// Full Rust replacement for DrawSpriteLocal (0x542060).////// Enqueues a render command (type 5, screen-space) to the drawing queue.unsafe extern "cdecl" fn draw_sprite_local_impl( y_pos: u32, this: u32, layer: u32, x_pos: u32, sprite_id: u32, frame: u32,) { let q = &mut *(this as *mut RenderQueue);
if let Some(entry) = q.alloc_entry() { *entry = RenderCommand { command_type: command_type::DRAW_SPRITE_LOCAL, layer, x_pos: x_pos & 0xFFFF0000, y_pos: y_pos & 0xFFFF0000, sprite_id, frame, }; }}
// ---------------------------------------------------------------------------// Installation// ---------------------------------------------------------------------------
pub unsafe fn install() -> Result<(), String> { let _ = hook::install( "DrawSpriteGlobal", va::DRAW_SPRITE_GLOBAL, trampoline_draw_sprite_global as *const (), )?;
let _ = hook::install( "DrawSpriteLocal", va::DRAW_SPRITE_LOCAL, trampoline_draw_sprite_local as *const (), )?;
Ok(())}crates/openwa-types/src/lib.rs+1
crates/openwa-types/src/render.rsAdded+68
crates/openwa-wormkit/src/hook.rs+22
crates/openwa-wormkit/src/replacements/mod.rs+2
crates/openwa-wormkit/src/replacements/sprite.rsAdded+95