refactor: add CStringRef wrapper, make InitFromData and ScanDirectory fully standalone
Browse filesAdd CStringRef type to openwa-lib::wa::mfc for safe interaction with
MFC ATL::CSimpleStringT<char,0> objects. Includes:
- CStringRef: wraps CString field address, supports assign_from() and
assign_resource() via WA's operator= and string resource lookup
- cstring_release(): standalone refcount release with atomic decrement
and conditional deallocation via pStringMgr vtable
InitFromData and ScanDirectory no longer delegate to their originals.
InitFromData handles CString name assignment directly (operator= for
non-empty, string resource #14 for empty). ScanDirectory releases
the CString parameter refcount after completing the Rust dir scan.
New address constants: CSTRING_OPERATOR_ASSIGN (0x401D20),
CSTRING_ASSIGN_RESOURCE (0x4A39F0), CSTRING_SET_STRING (0x401EA0).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@@ -1,6 +1,8 @@
|
|
| 1 |
//! MFC library function wrappers.
|
| 2 |
|
|
|
|
| 3 |
use crate::wa_call;
|
|
|
|
| 4 |
|
| 5 |
/// Zero-cost handle to a CWnd-derived MFC window (raw pointer as u32).
|
| 6 |
#[derive(Clone, Copy)]
|
|
@@ -31,3 +33,89 @@ impl From<CDialogHandle> for CWndHandle {
|
|
| 31 |
CWndHandle(d.0)
|
| 32 |
}
|
| 33 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
//! MFC library function wrappers.
|
| 2 |
|
| 3 |
+
use crate::rebase::rb;
|
| 4 |
use crate::wa_call;
|
| 5 |
+
use openwa_types::address::va;
|
| 6 |
|
| 7 |
/// Zero-cost handle to a CWnd-derived MFC window (raw pointer as u32).
|
| 8 |
#[derive(Clone, Copy)]
|
|
|
|
| 33 |
CWndHandle(d.0)
|
| 34 |
}
|
| 35 |
}
|
| 36 |
+
|
| 37 |
+
/// Handle to an MFC `ATL::CSimpleStringT<char,0>` field in a WA struct.
|
| 38 |
+
///
|
| 39 |
+
/// A CSimpleStringT "object" is just a `char*` data pointer (4 bytes on x86).
|
| 40 |
+
/// Metadata lives at negative offsets from the data pointer:
|
| 41 |
+
///
|
| 42 |
+
/// ```text
|
| 43 |
+
/// data - 0x10: pStringMgr* (IAtlStringMgr vtable pointer)
|
| 44 |
+
/// data - 0x0C: nDataLength (i32)
|
| 45 |
+
/// data - 0x08: nAllocLength (i32)
|
| 46 |
+
/// data - 0x04: nRefs (i32, atomically managed)
|
| 47 |
+
/// data + 0x00: char data[]
|
| 48 |
+
/// ```
|
| 49 |
+
///
|
| 50 |
+
/// `CStringRef` wraps the address of the CSimpleStringT object (i.e., the
|
| 51 |
+
/// address of the `char*` pointer itself), NOT the char data directly.
|
| 52 |
+
/// This matches how thiscall methods like operator= expect `this`.
|
| 53 |
+
pub struct CStringRef {
|
| 54 |
+
/// Address of the CSimpleStringT object (pointer to the char* data pointer).
|
| 55 |
+
pub ptr: u32,
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
impl CStringRef {
|
| 59 |
+
/// Create a CStringRef from the address of a CString field in a struct.
|
| 60 |
+
/// For example, `CStringRef::new(dest + 0x0C)` for the name field.
|
| 61 |
+
pub fn new(object_addr: u32) -> Self {
|
| 62 |
+
Self { ptr: object_addr }
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
/// Read the char* data pointer from the CString object.
|
| 66 |
+
unsafe fn data_ptr(&self) -> u32 {
|
| 67 |
+
*(self.ptr as *const u32)
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/// Get string length from CStringData header (nDataLength at data - 0x0C).
|
| 71 |
+
pub unsafe fn len(&self) -> i32 {
|
| 72 |
+
let data = self.data_ptr();
|
| 73 |
+
*((data - 0x0C) as *const i32)
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
/// Assign from another CString via `ATL::CSimpleStringT::operator=` (0x401D20).
|
| 77 |
+
/// thiscall(ECX=this, stack=&src) where both are CSimpleStringT* pointers.
|
| 78 |
+
pub unsafe fn assign_from(&mut self, src: &CStringRef) {
|
| 79 |
+
wa_call::thiscall_1(va::CSTRING_OPERATOR_ASSIGN, self.ptr, src.ptr);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
/// Assign a localized string resource by ID.
|
| 83 |
+
/// Used when source CString is empty (e.g., resource 0x0E = default scheme name).
|
| 84 |
+
/// FUN_004A39F0: EDX=resource_id, stack param=dest CString object pointer.
|
| 85 |
+
pub unsafe fn assign_resource(&mut self, resource_id: u32) {
|
| 86 |
+
let addr = rb(va::CSTRING_ASSIGN_RESOURCE);
|
| 87 |
+
let f: unsafe extern "fastcall" fn(u32, u32, u32) =
|
| 88 |
+
core::mem::transmute(addr);
|
| 89 |
+
// ECX is overwritten by the function (loads from global), so any value works.
|
| 90 |
+
// EDX = resource_id, stack = self.ptr
|
| 91 |
+
f(0, resource_id, self.ptr);
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
/// Release a CString data pointer's refcount.
|
| 96 |
+
///
|
| 97 |
+
/// Takes the raw `char*` data pointer (NOT the CString object address).
|
| 98 |
+
/// Atomically decrements nRefs; if old_ref <= 1, calls the deallocator
|
| 99 |
+
/// via `pStringMgr->vtable[1](pCStringData)`.
|
| 100 |
+
///
|
| 101 |
+
/// This is the standalone cleanup used when a CString data pointer is
|
| 102 |
+
/// passed by value (e.g., ScanDirectory's parameter).
|
| 103 |
+
pub unsafe fn cstring_release(data_ptr: u32) {
|
| 104 |
+
let refcount_ptr = (data_ptr - 4) as *mut i32;
|
| 105 |
+
|
| 106 |
+
// LOCK XADD [refcount], -1 → returns old value
|
| 107 |
+
let old_ref = core::sync::atomic::AtomicI32::from_ptr(refcount_ptr)
|
| 108 |
+
.fetch_sub(1, core::sync::atomic::Ordering::AcqRel);
|
| 109 |
+
|
| 110 |
+
if old_ref <= 1 {
|
| 111 |
+
// pCStringData is at data - 0x10
|
| 112 |
+
let cstring_data = data_ptr - 0x10;
|
| 113 |
+
// pStringMgr is the first field of CStringData
|
| 114 |
+
let string_mgr = *(cstring_data as *const u32);
|
| 115 |
+
// vtable[1] = Free method (thiscall)
|
| 116 |
+
let vtable = *(string_mgr as *const u32);
|
| 117 |
+
let free_fn: unsafe extern "fastcall" fn(u32, u32, u32) =
|
| 118 |
+
core::mem::transmute(*((vtable + 4) as *const u32));
|
| 119 |
+
free_fn(string_mgr, 0, cstring_data);
|
| 120 |
+
}
|
| 121 |
+
}
|
|
@@ -256,6 +256,18 @@ pub mod va {
|
|
| 256 |
/// Guard global for gameplay modifier application in ReadFile (nonzero = apply modifiers)
|
| 257 |
pub const SCHEME_MODIFIER_GUARD: u32 = 0x0088_E460;
|
| 258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
// === Lobby / network ===
|
| 260 |
|
| 261 |
pub const LOBBY_HOST_COMMANDS: u32 = 0x004B_9B00;
|
|
|
|
| 256 |
/// Guard global for gameplay modifier application in ReadFile (nonzero = apply modifiers)
|
| 257 |
pub const SCHEME_MODIFIER_GUARD: u32 = 0x0088_E460;
|
| 258 |
|
| 259 |
+
// === MFC / ATL library functions ===
|
| 260 |
+
|
| 261 |
+
/// ATL::CSimpleStringT<char,0>::operator= — thiscall(this, &src)
|
| 262 |
+
/// Assigns one CString to another with refcount management.
|
| 263 |
+
pub const CSTRING_OPERATOR_ASSIGN: u32 = 0x0040_1D20;
|
| 264 |
+
/// String resource lookup + assign — stdcall(dest_cstring_ptr) with EDX=resource_id
|
| 265 |
+
/// Looks up localized string by resource ID, assigns to dest CString.
|
| 266 |
+
pub const CSTRING_ASSIGN_RESOURCE: u32 = 0x004A_39F0;
|
| 267 |
+
/// CSimpleStringT::SetString — thiscall(this, str_ptr, str_len)
|
| 268 |
+
/// Low-level string buffer assignment (called by operator= and assign_resource).
|
| 269 |
+
pub const CSTRING_SET_STRING: u32 = 0x0040_1EA0;
|
| 270 |
+
|
| 271 |
// === Lobby / network ===
|
| 272 |
|
| 273 |
pub const LOBBY_HOST_COMMANDS: u32 = 0x004B_9B00;
|
|
@@ -333,21 +333,19 @@ unsafe fn call_original_save_file(this: u32, name: u32, flag: u32) -> u32 {
|
|
| 333 |
// Scheme__InitFromData replacement (0x4D5020)
|
| 334 |
// ============================================================
|
| 335 |
|
| 336 |
-
/// Trampoline to original Scheme__InitFromData (for CString name assignment).
|
| 337 |
-
static ORIG_SCHEME_INIT_FROM_DATA: AtomicU32 = AtomicU32::new(0);
|
| 338 |
-
|
| 339 |
/// Rust replacement for Scheme__InitFromData (0x4D5020).
|
| 340 |
/// fastcall(ECX=unused, EDX=src_data, dest, name_cstring), RET 0x8
|
| 341 |
///
|
| 342 |
/// Copies V1 payload data into scheme struct, zeroes super weapons,
|
| 343 |
-
/// applies V3 defaults from ROM, sets flag/index fields.
|
| 344 |
-
/// Delegates to original for CString name assignment (MFC refcounting).
|
| 345 |
unsafe extern "fastcall" fn hook_init_from_data(
|
| 346 |
-
|
| 347 |
src_data: u32,
|
| 348 |
dest: u32,
|
| 349 |
name_cstring: u32,
|
| 350 |
) {
|
|
|
|
|
|
|
| 351 |
// Step 1: Copy 0xD8 bytes (V1 payload) from src_data to dest+0x14
|
| 352 |
core::ptr::copy_nonoverlapping(
|
| 353 |
src_data as *const u8,
|
|
@@ -374,12 +372,25 @@ unsafe extern "fastcall" fn hook_init_from_data(
|
|
| 374 |
*((dest + 0x04) as *mut u8) = 0;
|
| 375 |
*((dest + 0x08) as *mut u32) = 0xFFFF_FFFF;
|
| 376 |
|
| 377 |
-
// Step 5: CString name assignment
|
| 378 |
-
//
|
| 379 |
-
//
|
| 380 |
-
let
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
}
|
| 384 |
|
| 385 |
// ============================================================
|
|
@@ -406,25 +417,20 @@ unsafe extern "stdcall" fn hook_check_weapon_limits() -> u32 {
|
|
| 406 |
// Scheme__ScanDirectory replacement (0x4D54E0)
|
| 407 |
// ============================================================
|
| 408 |
|
| 409 |
-
/// Trampoline to original Scheme__ScanDirectory (for CString param cleanup).
|
| 410 |
-
static ORIG_SCHEME_SCAN_DIRECTORY: AtomicU32 = AtomicU32::new(0);
|
| 411 |
-
|
| 412 |
/// Rust replacement for Scheme__ScanDirectory (0x4D54E0).
|
| 413 |
/// stdcall(cstring_param), RET 0x4
|
| 414 |
///
|
| 415 |
/// Recursively scans for {{DD}} name.wsc files and marks slot flags.
|
| 416 |
-
///
|
|
|
|
| 417 |
unsafe extern "stdcall" fn hook_scan_directory(cstring_param: u32) {
|
| 418 |
-
//
|
| 419 |
scan_directory_recursive("User\\Schemes");
|
| 420 |
|
| 421 |
let _ = log_line("[Scheme] ScanDirectory completed (Rust)");
|
| 422 |
|
| 423 |
-
//
|
| 424 |
-
|
| 425 |
-
let orig: unsafe extern "stdcall" fn(u32) =
|
| 426 |
-
core::mem::transmute(ORIG_SCHEME_SCAN_DIRECTORY.load(Ordering::Relaxed));
|
| 427 |
-
orig(cstring_param);
|
| 428 |
}
|
| 429 |
|
| 430 |
/// Parse a scheme filename matching the pattern `{{DD}} name.wsc`.
|
|
@@ -527,19 +533,17 @@ pub fn install() -> Result<(), String> {
|
|
| 527 |
)?;
|
| 528 |
ORIG_SCHEME_SAVE_FILE.store(trampoline_save as u32, Ordering::Relaxed);
|
| 529 |
|
| 530 |
-
let
|
| 531 |
"Scheme__InitFromData",
|
| 532 |
va::SCHEME_INIT_FROM_DATA,
|
| 533 |
hook_init_from_data as *const (),
|
| 534 |
)?;
|
| 535 |
-
ORIG_SCHEME_INIT_FROM_DATA.store(trampoline_init as u32, Ordering::Relaxed);
|
| 536 |
|
| 537 |
-
let
|
| 538 |
"Scheme__ScanDirectory",
|
| 539 |
va::SCHEME_SCAN_DIRECTORY,
|
| 540 |
hook_scan_directory as *const (),
|
| 541 |
)?;
|
| 542 |
-
ORIG_SCHEME_SCAN_DIRECTORY.store(trampoline_scan as u32, Ordering::Relaxed);
|
| 543 |
}
|
| 544 |
|
| 545 |
Ok(())
|
|
|
|
| 333 |
// Scheme__InitFromData replacement (0x4D5020)
|
| 334 |
// ============================================================
|
| 335 |
|
|
|
|
|
|
|
|
|
|
| 336 |
/// Rust replacement for Scheme__InitFromData (0x4D5020).
|
| 337 |
/// fastcall(ECX=unused, EDX=src_data, dest, name_cstring), RET 0x8
|
| 338 |
///
|
| 339 |
/// Copies V1 payload data into scheme struct, zeroes super weapons,
|
| 340 |
+
/// applies V3 defaults from ROM, sets flag/index fields, assigns name.
|
|
|
|
| 341 |
unsafe extern "fastcall" fn hook_init_from_data(
|
| 342 |
+
_ecx: u32,
|
| 343 |
src_data: u32,
|
| 344 |
dest: u32,
|
| 345 |
name_cstring: u32,
|
| 346 |
) {
|
| 347 |
+
use openwa_lib::wa::mfc::{cstring_release, CStringRef};
|
| 348 |
+
|
| 349 |
// Step 1: Copy 0xD8 bytes (V1 payload) from src_data to dest+0x14
|
| 350 |
core::ptr::copy_nonoverlapping(
|
| 351 |
src_data as *const u8,
|
|
|
|
| 372 |
*((dest + 0x04) as *mut u8) = 0;
|
| 373 |
*((dest + 0x08) as *mut u32) = 0xFFFF_FFFF;
|
| 374 |
|
| 375 |
+
// Step 5: CString name assignment.
|
| 376 |
+
// name_cstring is the char* data pointer of the source CString.
|
| 377 |
+
// dest+0x0C is the CString field in the dest scheme struct.
|
| 378 |
+
let src_len = *((name_cstring - 0x0C) as *const i32);
|
| 379 |
+
let mut dest_name = CStringRef::new(dest + 0x0C);
|
| 380 |
+
if src_len == 0 {
|
| 381 |
+
// Empty source: assign default name from string resource #14 (0x0E)
|
| 382 |
+
dest_name.assign_resource(0x0E);
|
| 383 |
+
} else {
|
| 384 |
+
// Non-empty: copy via CString operator=.
|
| 385 |
+
// operator= expects &CSimpleStringT (pointer to the char* pointer).
|
| 386 |
+
// name_cstring is the char* itself, passed on the stack — its stack
|
| 387 |
+
// address IS the CSimpleStringT object pointer.
|
| 388 |
+
let src_name = CStringRef::new(&name_cstring as *const u32 as u32);
|
| 389 |
+
dest_name.assign_from(&src_name);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
// Step 6: Release the source CString (original decrements refcount at end).
|
| 393 |
+
cstring_release(name_cstring);
|
| 394 |
}
|
| 395 |
|
| 396 |
// ============================================================
|
|
|
|
| 417 |
// Scheme__ScanDirectory replacement (0x4D54E0)
|
| 418 |
// ============================================================
|
| 419 |
|
|
|
|
|
|
|
|
|
|
| 420 |
/// Rust replacement for Scheme__ScanDirectory (0x4D54E0).
|
| 421 |
/// stdcall(cstring_param), RET 0x4
|
| 422 |
///
|
| 423 |
/// Recursively scans for {{DD}} name.wsc files and marks slot flags.
|
| 424 |
+
/// The cstring_param is a char* data pointer from a CString passed by the caller
|
| 425 |
+
/// (ExtractBuiltins). We must release its refcount when done.
|
| 426 |
unsafe extern "stdcall" fn hook_scan_directory(cstring_param: u32) {
|
| 427 |
+
// Perform the Rust directory scan
|
| 428 |
scan_directory_recursive("User\\Schemes");
|
| 429 |
|
| 430 |
let _ = log_line("[Scheme] ScanDirectory completed (Rust)");
|
| 431 |
|
| 432 |
+
// Release the CString parameter's refcount (caller transferred ownership)
|
| 433 |
+
openwa_lib::wa::mfc::cstring_release(cstring_param);
|
|
|
|
|
|
|
|
|
|
| 434 |
}
|
| 435 |
|
| 436 |
/// Parse a scheme filename matching the pattern `{{DD}} name.wsc`.
|
|
|
|
| 533 |
)?;
|
| 534 |
ORIG_SCHEME_SAVE_FILE.store(trampoline_save as u32, Ordering::Relaxed);
|
| 535 |
|
| 536 |
+
let _ = crate::hook::install(
|
| 537 |
"Scheme__InitFromData",
|
| 538 |
va::SCHEME_INIT_FROM_DATA,
|
| 539 |
hook_init_from_data as *const (),
|
| 540 |
)?;
|
|
|
|
| 541 |
|
| 542 |
+
let _ = crate::hook::install(
|
| 543 |
"Scheme__ScanDirectory",
|
| 544 |
va::SCHEME_SCAN_DIRECTORY,
|
| 545 |
hook_scan_directory as *const (),
|
| 546 |
)?;
|
|
|
|
| 547 |
}
|
| 548 |
|
| 549 |
Ok(())
|