rusteal-codegen 0.3.0

Code generator: UHT JSON to Rust bindings and C++ wrappers for rusteal
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Delegate codegen: generates typed delegate accessor methods and wrapper structs.
//
// For each delegate property on a class, we generate:
// 1. A trait method returning a typed delegate handle struct
// 2. The delegate handle struct with bind()/add() methods that accept typed closures
//
// The handle struct wraps the UObject owner + FPropertyHandle, and the bind/add
// methods register a closure in the Rust delegate_registry, then call the C++ API.

use crate::context::CodegenContext;
use crate::naming::to_snake_case;
use crate::schema::PropertyInfo;
use crate::type_map::{self, ConversionKind};

/// Information about a delegate property to generate code for.
pub struct DelegateInfo<'a> {
    pub prop: &'a PropertyInfo,
    pub class_name: &'a str,
    /// Rust name for the accessor method (snake_case).
    pub rust_name: String,
    /// Struct name for the delegate wrapper (PascalCase).
    pub struct_name: String,
    /// Whether this is a multicast delegate.
    pub is_multicast: bool,
    /// Parsed delegate parameters.
    pub params: Vec<DelegateParam>,
}

/// A single parameter in a delegate signature.
pub struct DelegateParam {
    pub name: String,
    pub rust_type: String,
    pub conversion: ParamConversion,
}

/// How to read a delegate param from the raw params buffer.
pub enum ParamConversion {
    /// Primitive: read directly as the type (bool, i32, f32, etc.)
    Primitive(String),
    /// Object reference: read UObjectHandle, wrap in UObjectRef<T>.
    ObjectRef(String),
    /// Enum: read underlying repr, convert via from_value.
    Enum { rust_type: String, repr: String },
    /// FName: use read_param FFI to properly pack FName (Editor-safe).
    FName,
    /// String/FText: use read_param FFI to read UTF-8 string.
    String,
    /// Struct: use read_param FFI to read struct bytes, wrap in OwnedStruct<T>.
    Struct { struct_name: String, cpp_name: String },
}

/// Collect delegate properties from a class and resolve their param types.
pub fn collect_delegate_props<'a>(
    props: &'a [PropertyInfo],
    class_name: &'a str,
    ctx: &CodegenContext,
) -> Vec<DelegateInfo<'a>> {
    let mut result = Vec::new();

    for prop in props {
        let mapped = type_map::map_property_type(
            &prop.prop_type,
            prop.class_name.as_deref(),
            prop.struct_name.as_deref(),
            prop.enum_name.as_deref(),
            prop.enum_underlying_type.as_deref(),
            prop.meta_class_name.as_deref(),
            prop.interface_name.as_deref(),
        );
        if !matches!(
            mapped.rust_to_ffi,
            ConversionKind::Delegate | ConversionKind::MulticastDelegate
        ) {
            continue;
        }

        let is_multicast = matches!(mapped.rust_to_ffi, ConversionKind::MulticastDelegate);

        // Parse func_info params
        let func_info = match &prop.func_info {
            Some(fi) => fi,
            None => continue,
        };
        let params_json = match func_info.get("params").and_then(|p| p.as_array()) {
            Some(params) => params,
            None => &Vec::new() as &Vec<serde_json::Value>,
        };

        let mut params = Vec::new();
        let mut all_supported = true;

        for param_value in params_json {
            let param_name = param_value
                .get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("unknown");
            let param_type = match param_value.get("type").and_then(|t| t.as_str()) {
                Some(t) => t,
                None => {
                    all_supported = false;
                    break;
                }
            };

            match resolve_delegate_param(param_name, param_type, param_value, ctx) {
                Some(dp) => params.push(dp),
                None => {
                    all_supported = false;
                    break;
                }
            }
        }

        if !all_supported {
            continue;
        }

        let rust_name = to_snake_case(&prop.name);
        let struct_name = format!("{}{}Delegate", class_name, prop.name);

        result.push(DelegateInfo {
            prop,
            class_name,
            rust_name,
            struct_name,
            is_multicast,
            params,
        });
    }

    result
}

fn resolve_delegate_param(
    name: &str,
    prop_type: &str,
    value: &serde_json::Value,
    ctx: &CodegenContext,
) -> Option<DelegateParam> {
    let param_name = to_snake_case(name);

    match prop_type {
        "BoolProperty" => Some(DelegateParam {
            name: param_name,
            rust_type: "bool".into(),
            conversion: ParamConversion::Primitive("bool".into()),
        }),
        "Int8Property" => Some(DelegateParam {
            name: param_name,
            rust_type: "i8".into(),
            conversion: ParamConversion::Primitive("i8".into()),
        }),
        "ByteProperty" => {
            if let Some(en) = value.get("enum_name").and_then(|v| v.as_str()) {
                if ctx.enums.contains_key(en) {
                    Some(DelegateParam {
                        name: param_name,
                        rust_type: en.to_string(),
                        conversion: ParamConversion::Enum {
                            rust_type: en.to_string(),
                            repr: ctx
                                .enum_actual_repr(en)
                                .unwrap_or("u8")
                                .to_string(),
                        },
                    })
                } else {
                    None
                }
            } else {
                Some(DelegateParam {
                    name: param_name,
                    rust_type: "u8".into(),
                    conversion: ParamConversion::Primitive("u8".into()),
                })
            }
        }
        "Int16Property" => prim_param(param_name, "i16"),
        "UInt16Property" => prim_param(param_name, "u16"),
        "IntProperty" => prim_param(param_name, "i32"),
        "UInt32Property" => prim_param(param_name, "u32"),
        "Int64Property" => prim_param(param_name, "i64"),
        "UInt64Property" => prim_param(param_name, "u64"),
        "FloatProperty" => prim_param(param_name, "f32"),
        "DoubleProperty" => prim_param(param_name, "f64"),
        "NameProperty" => Some(DelegateParam {
            name: param_name,
            rust_type: "rusteal_core::FNameHandle".into(),
            conversion: ParamConversion::FName,
        }),
        "StrProperty" | "TextProperty" => Some(DelegateParam {
            name: param_name,
            rust_type: "String".into(),
            conversion: ParamConversion::String,
        }),
        "ObjectProperty" | "ClassProperty" => {
            let cls = value.get("class_name").and_then(|v| v.as_str());
            if let Some(cls) = cls {
                if ctx.classes.contains_key(cls) {
                    Some(DelegateParam {
                        name: param_name,
                        rust_type: format!("rusteal_core::UObjectRef<{cls}>"),
                        conversion: ParamConversion::ObjectRef(cls.to_string()),
                    })
                } else {
                    None
                }
            } else {
                Some(DelegateParam {
                    name: param_name,
                    rust_type: "rusteal_core::UObjectHandle".into(),
                    conversion: ParamConversion::Primitive("rusteal_core::UObjectHandle".into()),
                })
            }
        }
        "EnumProperty" => {
            let en = value.get("enum_name").and_then(|v| v.as_str())?;
            if !ctx.enums.contains_key(en) {
                return None;
            }
            Some(DelegateParam {
                name: param_name,
                rust_type: en.to_string(),
                conversion: ParamConversion::Enum {
                    rust_type: en.to_string(),
                    repr: ctx.enum_actual_repr(en).unwrap_or("u8").to_string(),
                },
            })
        }
        "StructProperty" => {
            let sn = value.get("struct_name").and_then(|v| v.as_str())?;
            let si = ctx.structs.get(sn)?;
            if !si.has_static_struct {
                return None;
            }
            Some(DelegateParam {
                name: param_name,
                rust_type: format!("rusteal_core::OwnedStruct<{}>", si.cpp_name),
                conversion: ParamConversion::Struct {
                    struct_name: sn.to_string(),
                    cpp_name: si.cpp_name.clone(),
                },
            })
        }
        _ => None,
    }
}

fn prim_param(name: String, ty: &str) -> Option<DelegateParam> {
    Some(DelegateParam {
        name,
        rust_type: ty.into(),
        conversion: ParamConversion::Primitive(ty.into()),
    })
}

// ---------------------------------------------------------------------------
// Code generation
// ---------------------------------------------------------------------------

/// Generate trait method implementations for delegate properties (used as default impls).
pub fn generate_delegate_impls(
    out: &mut String,
    delegates: &[DelegateInfo],
) {
    for d in delegates {
        let rust_name = &d.rust_name;
        let struct_name = &d.struct_name;
        let class_name = d.class_name;
        let prop_name = &d.prop.name;
        let prop_name_len = prop_name.len();
        let byte_lit = format!("b\"{}\\0\"", prop_name);

        out.push_str(&format!(
            "    fn {rust_name}(&self) -> {struct_name} {{\n\
             \x20       static PROP: std::sync::OnceLock<rusteal_core::FPropertyHandle> = std::sync::OnceLock::new();\n\
             \x20       let prop = *PROP.get_or_init(|| unsafe {{\n\
             \x20           rusteal_core::ffi_dispatch::reflection_find_property(\n\
             \x20               {class_name}::static_class(), {byte_lit}.as_ptr(), {prop_name_len}\n\
             \x20           )\n\
             \x20       }});\n\
             \x20       {struct_name} {{ owner: self.handle(), prop }}\n\
             \x20   }}\n\n"
        ));
    }
}

/// Generate delegate wrapper structs with typed bind/add methods.
/// These are emitted at the top of the class file (before the trait).
pub fn generate_delegate_structs(
    out: &mut String,
    delegates: &[DelegateInfo],
    class_name: &str,
) {
    for d in delegates {
        let struct_name = &d.struct_name;
        let is_multicast = d.is_multicast;

        out.push_str(&format!(
            "pub struct {struct_name} {{\n\
             \x20   pub owner: rusteal_core::UObjectHandle,\n\
             \x20   pub prop: rusteal_core::FPropertyHandle,\n\
             }}\n\n"
        ));

        // Build the closure parameter types and extraction code.
        // UHT exports the stripped function name (e.g., "OnEditableTextBoxCommittedEvent"),
        // but UE stores the signature UFunction with "__DelegateSignature" suffix.
        let sig_name_base = d.prop.func_info.as_ref()
            .and_then(|fi| fi.get("name"))
            .and_then(|n| n.as_str())
            .unwrap_or(&d.prop.name);
        let sig_name = format!("{sig_name_base}__DelegateSignature");
        let sig_name_len = sig_name.len();
        let sig_byte_lit = format!("b\"{}\\0\"", sig_name);

        // Build closure param types for the user-facing closure signature
        let callback_params: Vec<String> = d.params.iter().map(|p| p.rust_type.clone()).collect();
        let callback_sig = callback_params.join(", ");

        let method_name = if is_multicast { "add" } else { "bind" };
        let api_fn = if is_multicast { "bind_multicast" } else { "bind_unicast" };

        out.push_str(&format!(
            "impl {struct_name} {{\n"
        ));

        // Generate the bind/add method
        out.push_str(&format!(
            "    pub fn {method_name}(&self, mut callback: impl FnMut({callback_sig}) + Send + 'static) -> rusteal_core::RustealResult<rusteal_core::DelegateBinding> {{\n"
        ));

        // If there are params, we need to resolve offsets + property handles via OnceLock
        if !d.params.is_empty() {
            let n_params = d.params.len();
            out.push_str(&format!(
                "        static PARAM_INFO: std::sync::OnceLock<[(u32, rusteal_core::FPropertyHandle); {n_params}]> = std::sync::OnceLock::new();\n\
                 \x20       let param_info = PARAM_INFO.get_or_init(|| unsafe {{\n\
                 \x20           let sig_func = rusteal_core::ffi_dispatch::reflection_find_function_by_class(\n\
                 \x20               {class_name}::static_class(),\n\
                 \x20               {sig_byte_lit}.as_ptr(), {sig_name_len});\n\
                 \x20           [\n"
            ));

            for p in &d.params {
                let param_ue_name = &d.prop.func_info.as_ref()
                    .and_then(|fi| fi.get("params"))
                    .and_then(|ps| ps.as_array())
                    .and_then(|arr| arr.iter().find(|v| {
                        v.get("name").and_then(|n| n.as_str()).map(to_snake_case) == Some(p.name.clone())
                    }))
                    .and_then(|v| v.get("name"))
                    .and_then(|n| n.as_str())
                    .unwrap_or(&p.name);
                let pname_len = param_ue_name.len();
                let pname_lit = format!("b\"{}\\0\"", param_ue_name);
                out.push_str(&format!(
                    "                {{\n\
                     \x20                   let param_prop = rusteal_core::ffi_dispatch::reflection_get_function_param(\n\
                     \x20                       sig_func, {pname_lit}.as_ptr(), {pname_len});\n\
                     \x20                   (rusteal_core::ffi_dispatch::reflection_get_property_offset(param_prop), param_prop)\n\
                     \x20               }},\n"
                ));
            }

            out.push_str(
                "            ]\n\
                 \x20       });\n\
                 \x20       #[allow(unused_variables)] let param_info = param_info;\n"
            );
        }

        // Build the closure wrapper that extracts typed params from NativePtr
        if d.params.is_empty() {
            out.push_str(&format!(
                "        let owner = self.owner;\n\
                 \x20       let prop = self.prop;\n\
                 \x20       rusteal_core::delegate_registry::{api_fn}(owner, prop, move |_params: rusteal_core::ffi_dispatch::NativePtr| {{\n\
                 \x20           callback();\n\
                 \x20       }})\n\
                 \x20   }}\n"
            ));
            out.push_str("}\n\n");
            continue;
        }

        out.push_str(&format!(
            "        let owner = self.owner;\n\
             \x20       let prop = self.prop;\n\
             \x20       rusteal_core::delegate_registry::{api_fn}(owner, prop, move |params: rusteal_core::ffi_dispatch::NativePtr| {{\n\
             \x20           unsafe {{\n"
        ));

        // Extract each parameter
        for (i, p) in d.params.iter().enumerate() {
            let var_name = &p.name;
            match &p.conversion {
                ParamConversion::Primitive(ty) => {
                    out.push_str(&format!(
                        "                let {var_name} = rusteal_core::ffi_dispatch::native_mem_read::<{ty}>(params, param_info[{i}].0 as usize);\n"
                    ));
                }
                ParamConversion::ObjectRef(_cls) => {
                    out.push_str(&format!(
                        "                let {var_name} = rusteal_core::UObjectRef::from_raw(\n\
                         \x20                   rusteal_core::ffi_dispatch::native_mem_read::<rusteal_core::UObjectHandle>(params, param_info[{i}].0 as usize)\n\
                         \x20               );\n"
                    ));
                }
                ParamConversion::Enum { rust_type, repr } => {
                    out.push_str(&format!(
                        "                let __raw_{var_name} = rusteal_core::ffi_dispatch::native_mem_read::<{repr}>(params, param_info[{i}].0 as usize);\n\
                         \x20               let {var_name} = {rust_type}::from_value(__raw_{var_name}).unwrap_or_else(|| std::mem::transmute(__raw_{var_name}));\n"
                    ));
                }
                ParamConversion::FName => {
                    out.push_str(&format!(
                        "                let {var_name} = {{\n\
                         \x20                   let mut __buf = [0u8; 8];\n\
                         \x20                   let mut __written: u32 = 0;\n\
                         \x20                   rusteal_core::ffi_infallible(rusteal_core::ffi_dispatch::delegate_read_param(\n\
                         \x20                       param_info[{i}].1,\n\
                         \x20                       params,\n\
                         \x20                       param_info[{i}].0,\n\
                         \x20                       __buf.as_mut_ptr(),\n\
                         \x20                       8,\n\
                         \x20                       &mut __written,\n\
                         \x20                   ));\n\
                         \x20                   rusteal_core::FNameHandle(u64::from_ne_bytes(__buf))\n\
                         \x20               }};\n"
                    ));
                }
                ParamConversion::String => {
                    out.push_str(&format!(
                        "                let {var_name} = {{\n\
                         \x20                   let mut __buf = vec![0u8; 260];\n\
                         \x20                   let mut __written: u32 = 0;\n\
                         \x20                   let __err = rusteal_core::ffi_dispatch::delegate_read_param(\n\
                         \x20                       param_info[{i}].1,\n\
                         \x20                       params,\n\
                         \x20                       param_info[{i}].0,\n\
                         \x20                       __buf.as_mut_ptr(),\n\
                         \x20                       __buf.len() as u32,\n\
                         \x20                       &mut __written,\n\
                         \x20                   );\n\
                         \x20                   if __err == rusteal_core::RustealErrorCode::BufferTooSmall && __written > 0 {{\n\
                         \x20                       __buf.resize(__written as usize, 0);\n\
                         \x20                       rusteal_core::ffi_infallible(rusteal_core::ffi_dispatch::delegate_read_param(\n\
                         \x20                           param_info[{i}].1,\n\
                         \x20                           params,\n\
                         \x20                           param_info[{i}].0,\n\
                         \x20                           __buf.as_mut_ptr(),\n\
                         \x20                           __buf.len() as u32,\n\
                         \x20                           &mut __written,\n\
                         \x20                       ));\n\
                         \x20                   }}\n\
                         \x20                   if __written >= 4 {{\n\
                         \x20                       let __slen = u32::from_ne_bytes([__buf[0], __buf[1], __buf[2], __buf[3]]) as usize;\n\
                         \x20                       String::from_utf8_lossy(&__buf[4..4 + __slen]).into_owned()\n\
                         \x20                   }} else {{\n\
                         \x20                       String::new()\n\
                         \x20                   }}\n\
                         \x20               }};\n"
                    ));
                }
                ParamConversion::Struct { cpp_name, .. } => {
                    out.push_str(&format!(
                        "                let {var_name} = {{\n\
                         \x20                   let __size = rusteal_core::ffi_dispatch::reflection_get_property_size(param_info[{i}].1) as usize;\n\
                         \x20                   let mut __buf = vec![0u8; __size];\n\
                         \x20                   let mut __written: u32 = 0;\n\
                         \x20                   rusteal_core::ffi_infallible(rusteal_core::ffi_dispatch::delegate_read_param(\n\
                         \x20                       param_info[{i}].1,\n\
                         \x20                       params,\n\
                         \x20                       param_info[{i}].0,\n\
                         \x20                       __buf.as_mut_ptr(),\n\
                         \x20                       __size as u32,\n\
                         \x20                       &mut __written,\n\
                         \x20                   ));\n\
                         \x20                   rusteal_core::OwnedStruct::<{cpp_name}>::from_bytes(__buf)\n\
                         \x20               }};\n"
                    ));
                }
            }
        }

        // Call the user's callback with extracted params
        let param_names: Vec<&str> = d.params.iter().map(|p| p.name.as_str()).collect();
        let call_args = param_names.join(", ");
        out.push_str(&format!(
            "                callback({call_args});\n\
             \x20           }}\n\
             \x20       }})\n\
             \x20   }}\n"
        ));

        out.push_str("}\n\n");
    }
}