feat: generic CTask<V> vtable parameter + FieldRegistry generics support
Browse filesMake CTask generic over its vtable pointer type:
CTask<V: 'static = *const c_void>
This allows subclasses to specify their typed vtable:
CTaskTeam { base: CTask<*const CTaskTeamVTable>, ... }
Changes:
FieldRegistry derive macro (openwa-derive):
- Handle generic type params by substituting defaults in size_of
expressions. offset_of uses the bare struct name (Rust applies
defaults). Builds a generic_defaults map from TypeParam::default.
bind macro (openwa-derive):
- Accept dotted field paths ($($vtable_field:ident).+) instead of
single idents, enabling bind_VTable!(Struct, base.vtable) for
inherited vtables.
CTask (openwa-core):
- Add generic parameter V with default *const c_void. All existing
code using bare CTask continues to work unchanged.
CTaskTeam (openwa-core):
- Use CTask<*const CTaskTeamVTable> as base type
- Replace manual vtable() and handle_message() with bind macro
- bind_CTaskTeamVTable!(CTaskTeam, base.vtable) generates typed
method wrappers automatically
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@@ -105,9 +105,9 @@ crate::define_addresses! {
|
|
| 105 |
/// 0x1C: 0x563210 ProcessFrame
|
| 106 |
#[derive(FieldRegistry)]
|
| 107 |
#[repr(C)]
|
| 108 |
-
pub struct CTask {
|
| 109 |
/// 0x00: Pointer to virtual method table
|
| 110 |
-
pub vtable:
|
| 111 |
/// 0x04: Parent task in the hierarchy
|
| 112 |
pub parent: *mut u8,
|
| 113 |
/// 0x08: Children array capacity — starts at 0x10, doubles via realloc when full.
|
|
|
|
| 105 |
/// 0x1C: 0x563210 ProcessFrame
|
| 106 |
#[derive(FieldRegistry)]
|
| 107 |
#[repr(C)]
|
| 108 |
+
pub struct CTask<V: 'static = *const core::ffi::c_void> {
|
| 109 |
/// 0x00: Pointer to virtual method table
|
| 110 |
+
pub vtable: V,
|
| 111 |
/// 0x04: Parent task in the hierarchy
|
| 112 |
pub parent: *mut u8,
|
| 113 |
/// 0x08: Children array capacity — starts at 0x10, doubles via realloc when full.
|
|
@@ -59,8 +59,8 @@ pub struct CTaskTeamVTable {
|
|
| 59 |
#[derive(FieldRegistry)]
|
| 60 |
#[repr(C)]
|
| 61 |
pub struct CTaskTeam {
|
| 62 |
-
/// 0x00–0x2F: CTask base
|
| 63 |
-
pub base: CTask,
|
| 64 |
/// 0x30: Secondary interface vtable pointer (Ghidra 0x00669F00)
|
| 65 |
pub _secondary_vtable: u32,
|
| 66 |
/// 0x34: Unknown — observed to hold the same value as `team_index` in all runs.
|
|
@@ -112,27 +112,5 @@ pub struct CTaskTeam {
|
|
| 112 |
|
| 113 |
const _: () = assert!(core::mem::size_of::<CTaskTeam>() == 0x460);
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
pub fn vtable(&self) -> &CTaskTeamVTable {
|
| 118 |
-
unsafe { &*(self.base.vtable as *const CTaskTeamVTable) }
|
| 119 |
-
}
|
| 120 |
-
|
| 121 |
-
/// Call HandleMessage (vtable slot 2) on this team task.
|
| 122 |
-
///
|
| 123 |
-
/// This is the main team message dispatcher — handles weapon fire results,
|
| 124 |
-
/// surrender, worm selection, napalm strike, and many other team-level events.
|
| 125 |
-
///
|
| 126 |
-
/// # Safety
|
| 127 |
-
/// `sender` must be a valid CTask pointer. `data` must point to `size` bytes.
|
| 128 |
-
pub unsafe fn handle_message(
|
| 129 |
-
&mut self,
|
| 130 |
-
sender: *mut CTask,
|
| 131 |
-
msg_type: u32,
|
| 132 |
-
size: u32,
|
| 133 |
-
data: *const u8,
|
| 134 |
-
) {
|
| 135 |
-
let vt = self.vtable();
|
| 136 |
-
(vt.handle_message)(self as *mut Self, sender, msg_type, size, data);
|
| 137 |
-
}
|
| 138 |
-
}
|
|
|
|
| 59 |
#[derive(FieldRegistry)]
|
| 60 |
#[repr(C)]
|
| 61 |
pub struct CTaskTeam {
|
| 62 |
+
/// 0x00–0x2F: CTask base with typed CTaskTeamVTable vtable pointer.
|
| 63 |
+
pub base: CTask<*const CTaskTeamVTable>,
|
| 64 |
/// 0x30: Secondary interface vtable pointer (Ghidra 0x00669F00)
|
| 65 |
pub _secondary_vtable: u32,
|
| 66 |
/// 0x34: Unknown — observed to hold the same value as `team_index` in all runs.
|
|
|
|
| 112 |
|
| 113 |
const _: () = assert!(core::mem::size_of::<CTaskTeam>() == 0x460);
|
| 114 |
|
| 115 |
+
// Generate typed vtable method wrappers: handle_message(), write_replay_state(), etc.
|
| 116 |
+
bind_CTaskTeamVTable!(CTaskTeam, base.vtable);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -316,7 +316,7 @@ fn vtable_impl(
|
|
| 316 |
/// Usage: `bind_VtableName!(ClassName, vtable_field_name);`
|
| 317 |
#[macro_export]
|
| 318 |
macro_rules! #bind_macro_name {
|
| 319 |
-
($class:ty, $vtable_field:ident) => {
|
| 320 |
impl $class {
|
| 321 |
#bind_methods
|
| 322 |
}
|
|
@@ -406,7 +406,7 @@ fn generate_bind_methods(slots: &[SlotInfo]) -> syn::Result<proc_macro2::TokenSt
|
|
| 406 |
methods.push(quote! {
|
| 407 |
#[doc = #doc]
|
| 408 |
pub unsafe fn #ident(#self_param #(, #param_decls)*) #ret_ty {
|
| 409 |
-
((*self.$vtable_field).#ident)(#self_cast #(, #param_names)*)
|
| 410 |
}
|
| 411 |
});
|
| 412 |
}
|
|
@@ -539,6 +539,19 @@ pub fn derive_field_registry(input: TokenStream) -> TokenStream {
|
|
| 539 |
let struct_name = &input.ident;
|
| 540 |
let struct_name_str = struct_name.to_string();
|
| 541 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
let fields = match &input.data {
|
| 543 |
Data::Struct(data) => match &data.fields {
|
| 544 |
Fields::Named(named) => &named.named,
|
|
@@ -573,6 +586,25 @@ pub fn derive_field_registry(input: TokenStream) -> TokenStream {
|
|
| 573 |
let doc = extract_doc_comment(&field.attrs);
|
| 574 |
let field_ty = &field.ty;
|
| 575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
// Determine ValueKind: check for #[field(kind = "...")] override, else infer
|
| 577 |
let kind = parse_field_kind_attr(&field.attrs)
|
| 578 |
.unwrap_or_else(|| infer_value_kind(field_ty));
|
|
@@ -581,7 +613,7 @@ pub fn derive_field_registry(input: TokenStream) -> TokenStream {
|
|
| 581 |
openwa_core::registry::FieldEntry {
|
| 582 |
offset: core::mem::offset_of!(#struct_name, #field_ident) as u32,
|
| 583 |
name: #field_name,
|
| 584 |
-
size: core::mem::size_of::<#
|
| 585 |
kind: #kind,
|
| 586 |
doc: #doc,
|
| 587 |
}
|
|
|
|
| 316 |
/// Usage: `bind_VtableName!(ClassName, vtable_field_name);`
|
| 317 |
#[macro_export]
|
| 318 |
macro_rules! #bind_macro_name {
|
| 319 |
+
($class:ty, $($vtable_field:ident).+) => {
|
| 320 |
impl $class {
|
| 321 |
#bind_methods
|
| 322 |
}
|
|
|
|
| 406 |
methods.push(quote! {
|
| 407 |
#[doc = #doc]
|
| 408 |
pub unsafe fn #ident(#self_param #(, #param_decls)*) #ret_ty {
|
| 409 |
+
((*self.$($vtable_field).+).#ident)(#self_cast #(, #param_names)*)
|
| 410 |
}
|
| 411 |
});
|
| 412 |
}
|
|
|
|
| 539 |
let struct_name = &input.ident;
|
| 540 |
let struct_name_str = struct_name.to_string();
|
| 541 |
|
| 542 |
+
// Build a map of generic type params → their defaults (e.g., V → *const c_void).
|
| 543 |
+
// This allows the derive to work on generic structs like CTask<V = *const c_void>
|
| 544 |
+
// by substituting generic params with concrete defaults in size_of expressions.
|
| 545 |
+
let generic_defaults: std::collections::HashMap<String, syn::Type> = input
|
| 546 |
+
.generics
|
| 547 |
+
.type_params()
|
| 548 |
+
.filter_map(|tp| {
|
| 549 |
+
tp.default
|
| 550 |
+
.as_ref()
|
| 551 |
+
.map(|def| (tp.ident.to_string(), def.clone()))
|
| 552 |
+
})
|
| 553 |
+
.collect();
|
| 554 |
+
|
| 555 |
let fields = match &input.data {
|
| 556 |
Data::Struct(data) => match &data.fields {
|
| 557 |
Fields::Named(named) => &named.named,
|
|
|
|
| 586 |
let doc = extract_doc_comment(&field.attrs);
|
| 587 |
let field_ty = &field.ty;
|
| 588 |
|
| 589 |
+
// If the field type is a generic param (e.g., `V`), substitute with its
|
| 590 |
+
// default type for size_of. offset_of uses the bare struct name which
|
| 591 |
+
// Rust resolves with defaults applied.
|
| 592 |
+
let size_ty: Box<dyn quote::ToTokens> =
|
| 593 |
+
if let syn::Type::Path(tp) = field_ty {
|
| 594 |
+
if tp.path.segments.len() == 1 {
|
| 595 |
+
let seg = &tp.path.segments[0];
|
| 596 |
+
if let Some(default) = generic_defaults.get(&seg.ident.to_string()) {
|
| 597 |
+
Box::new(default.clone())
|
| 598 |
+
} else {
|
| 599 |
+
Box::new(field_ty.clone())
|
| 600 |
+
}
|
| 601 |
+
} else {
|
| 602 |
+
Box::new(field_ty.clone())
|
| 603 |
+
}
|
| 604 |
+
} else {
|
| 605 |
+
Box::new(field_ty.clone())
|
| 606 |
+
};
|
| 607 |
+
|
| 608 |
// Determine ValueKind: check for #[field(kind = "...")] override, else infer
|
| 609 |
let kind = parse_field_kind_attr(&field.attrs)
|
| 610 |
.unwrap_or_else(|| infer_value_kind(field_ty));
|
|
|
|
| 613 |
openwa_core::registry::FieldEntry {
|
| 614 |
offset: core::mem::offset_of!(#struct_name, #field_ident) as u32,
|
| 615 |
name: #field_name,
|
| 616 |
+
size: core::mem::size_of::<#size_ty>() as u32,
|
| 617 |
kind: #kind,
|
| 618 |
doc: #doc,
|
| 619 |
}
|