rusteal-codegen 0.8.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
500
501
502
pub mod config;
pub mod context;
pub mod cpp_gen;
pub mod defaults;
pub mod filter;
pub mod naming;
pub mod rust_gen;
pub mod schema;
pub mod type_map;

use std::path::Path;

use crate::config::{
    CodegenConfig, LibraryKind, LibraryOutput, PluginConfig, PluginLayout, ProjectConfig,
    ProjectLayout,
};
use crate::schema::{ClassesFile, EnumsFile, StructsFile};

pub(crate) fn write_if_changed(path: &Path, contents: &str) -> std::io::Result<()> {
    if std::fs::read(path).is_ok_and(|old| old == contents.as_bytes()) {
        return Ok(());
    }

    std::fs::write(path, contents)
}

pub fn run_generate(project_root: &Path) {
    let config = ProjectConfig::load(project_root).unwrap_or_else(|e| {
        eprintln!("rusteal-codegen: {e}");
        std::process::exit(1);
    });

    let layout = ProjectLayout::new(project_root);
    generate_library(&layout.uht_json(), &config.codegen, &layout.game_output());
}

pub fn run_generate_plugin(project_root: &Path, plugin: &PluginLayout) {
    let config = PluginConfig::load(&plugin.dir).unwrap_or_else(|e| {
        eprintln!("rusteal-codegen: {e}");
        std::process::exit(1);
    });

    let layout = ProjectLayout::new(project_root);
    eprintln!("rusteal-codegen: plugin {}", plugin.name);

    generate_library(
        &layout.uht_json(),
        &config.codegen,
        &plugin.output(config.plugin.kind),
    );
}

pub fn generate_library(uht_input: &Path, codegen: &CodegenConfig, output: &LibraryOutput) {
    let codegen = &codegen.for_library(output.kind);
    let classes_path = uht_input.join("rusteal_classes.json");
    let structs_path = uht_input.join("rusteal_structs.json");
    let enums_path = uht_input.join("rusteal_enums.json");

    let rust_out = output.bindings_crate.clone();
    let rust_src = rust_out.join("src");
    let cpp_out = output.cpp_generated.clone();

    eprintln!("rusteal-codegen: loading JSON...");

    let classes_json: ClassesFile = {
        let data = std::fs::read_to_string(&classes_path)
            .unwrap_or_else(|e| panic!("Failed to read {}: {e}", classes_path.display()));
        serde_json::from_str(&data)
            .unwrap_or_else(|e| panic!("Failed to parse {}: {e}", classes_path.display()))
    };

    let structs_json: StructsFile = {
        let data = std::fs::read_to_string(&structs_path)
            .unwrap_or_else(|e| panic!("Failed to read {}: {e}", structs_path.display()));
        serde_json::from_str(&data)
            .unwrap_or_else(|e| panic!("Failed to parse {}: {e}", structs_path.display()))
    };

    let enums_json: EnumsFile = {
        let data = std::fs::read_to_string(&enums_path)
            .unwrap_or_else(|e| panic!("Failed to read {}: {e}", enums_path.display()));
        serde_json::from_str(&data)
            .unwrap_or_else(|e| panic!("Failed to parse {}: {e}", enums_path.display()))
    };

    eprintln!(
        "  Loaded {} classes, {} structs, {} enums",
        classes_json.classes.len(),
        structs_json.structs.len(),
        enums_json.enums.len()
    );

    let mut ctx = context::CodegenContext::new(
        classes_json.classes,
        structs_json.structs,
        enums_json.enums,
        codegen,
    );

    ctx.cpp_prefix = output.cpp_prefix.clone();

    eprintln!(
        "  Enabled modules: {:?}",
        ctx.enabled_modules.iter().collect::<Vec<_>>()
    );

    for (module, classes) in &ctx.module_classes {
        eprintln!("    {}: {} classes", module, classes.len());
    }

    for (module, structs) in &ctx.module_structs {
        eprintln!("    {}: {} structs", module, structs.len());
    }

    for (module, enums) in &ctx.module_enums {
        eprintln!("    {}: {} enums", module, enums.len());
    }

    eprintln!("rusteal-codegen: filtering...");
    let blocklist = with_unlisted_classes(&ctx, codegen);
    filter::apply_filters(&mut ctx, &blocklist, output.kind == LibraryKind::Editor);

    eprintln!("rusteal-codegen: building function table...");
    build_func_table(&mut ctx);
    eprintln!("  {} functions in func_table", ctx.func_table.len());

    eprintln!("rusteal-codegen: generating Rust code...");
    rust_gen::generate(&ctx, &rust_src);
    rust_gen::manual::write_manual_module(&rust_src);
    rust_gen::prelude::write_prelude(&rust_src, &ctx);
    rust_gen::cargo_toml::write_crate_files(&rust_out, &ctx, codegen);

    eprintln!("rusteal-codegen: generating C++ code...");
    cpp_gen::generate(&ctx, &cpp_out);

    generate_module_deps(codegen, &output.host_module, &cpp_out);
    update_plugin_dependencies(codegen, &output.plugin_descriptor);

    eprintln!("rusteal-codegen: verifying output...");
    verify_output(&ctx, &rust_src, &cpp_out);

    eprintln!("rusteal-codegen: done!");
}

fn with_unlisted_classes(
    ctx: &context::CodegenContext,
    codegen: &CodegenConfig,
) -> config::Blocklist {
    use std::collections::HashSet;

    let mut blocklist = codegen.blocklist.clone();

    for (package, mapping) in &codegen.modules {
        let Some(listed) = &mapping.classes else {
            continue;
        };

        let mut wanted: HashSet<&str> = HashSet::new();

        for name in listed {
            let mut current = Some(name.as_str());

            while let Some(class) = current.and_then(|n| ctx.classes.get(n)) {
                if &class.package != package || !wanted.insert(class.name.as_str()) {
                    break;
                }

                current = class.super_class.as_deref();
            }
        }

        for class in ctx
            .module_classes
            .get(&mapping.module)
            .into_iter()
            .flatten()
        {
            if !wanted.contains(class.name.as_str()) {
                blocklist.classes.push(class.name.clone());
            }
        }
    }

    blocklist
}

fn generate_module_deps(config: &CodegenConfig, host_module: &str, cpp_out: &Path) {
    use std::collections::BTreeSet;

    let enabled_features: std::collections::HashSet<&str> =
        config.features.iter().map(|s| s.as_str()).collect();

    let mut ue_modules: BTreeSet<&str> = BTreeSet::new();
    ue_modules.insert("Core");

    for (pkg, mapping) in &config.modules {
        if enabled_features.contains(mapping.feature.as_str()) {
            ue_modules.insert(pkg.as_str());
        }
    }

    ue_modules.remove(host_module);

    let content = ue_modules.iter().copied().collect::<Vec<_>>().join("\n");

    let path = cpp_out.join("module_deps.txt");

    write_if_changed(&path, &content)
        .unwrap_or_else(|e| panic!("Failed to write {}: {e}", path.display()));

    eprintln!(
        "  module_deps.txt: {:?}",
        ue_modules.iter().collect::<Vec<_>>()
    );
}

fn update_plugin_dependencies(config: &CodegenConfig, descriptor: &Path) {
    let Ok(text) = std::fs::read_to_string(descriptor) else {
        return;
    };

    let mut json: serde_json::Value = serde_json::from_str(&text)
        .unwrap_or_else(|e| panic!("Failed to parse {}: {e}", descriptor.display()));

    let enabled: std::collections::HashSet<&str> =
        config.features.iter().map(|s| s.as_str()).collect();

    let mut wanted: Vec<&str> = config
        .modules
        .values()
        .filter(|m| enabled.contains(m.feature.as_str()))
        .filter_map(|m| m.plugin.as_deref())
        .collect();

    wanted.sort();
    wanted.dedup();

    let plugins = json
        .as_object_mut()
        .expect("a plugin descriptor is a JSON object")
        .entry("Plugins")
        .or_insert_with(|| serde_json::Value::Array(Vec::new()))
        .as_array_mut()
        .expect("a plugin descriptor's Plugins is an array");

    let mut added = Vec::new();

    for name in wanted {
        if plugins.iter().any(|p| p["Name"] == name) {
            continue;
        }

        plugins.push(serde_json::json!({ "Name": name, "Enabled": true }));
        added.push(name);
    }

    if added.is_empty() {
        return;
    }

    let mut out = serde_json::to_string_pretty(&json).expect("serializable descriptor");
    out.push('\n');

    std::fs::write(descriptor, out)
        .unwrap_or_else(|e| panic!("Failed to write {}: {e}", descriptor.display()));

    let name = descriptor.file_name().unwrap_or_default().to_string_lossy();
    eprintln!("  {name}: now depends on {added:?}");
}

fn verify_output(ctx: &context::CodegenContext, rust_out: &Path, cpp_out: &Path) {
    let mut errors: Vec<String> = Vec::new();

    for (i, entry) in ctx.func_table.iter().enumerate() {
        if entry.func_id != i as u32 {
            errors.push(format!(
                "FuncId gap: expected {} for module '{}' {}.{}, got {} (total functions: {})",
                i,
                entry.module_name,
                entry.class_name,
                entry.func_name,
                entry.func_id,
                ctx.func_table.len()
            ));

            break;
        }
    }

    let rust_required = ["lib.rs", "func_ids.rs"];

    for name in &rust_required {
        let path = rust_out.join(name);

        match std::fs::metadata(&path) {
            Ok(m) if m.len() == 0 => errors.push(format!("Rust output empty: {}", path.display())),
            Err(_) => errors.push(format!("Rust output missing: {}", path.display())),
            _ => {}
        }
    }

    for module_name in ctx.enabled_modules.iter() {
        let mod_path = rust_out.join(module_name).join("mod.rs");

        if !mod_path.exists() {
            errors.push(format!("Module mod.rs missing: {}", mod_path.display()));
        }
    }

    let cpp_required = ["RustealFuncIds.h", "RustealFillFuncTable.cpp"];

    for name in &cpp_required {
        let path = cpp_out.join(name);

        match std::fs::metadata(&path) {
            Ok(m) if m.len() == 0 => errors.push(format!("C++ output empty: {}", path.display())),
            Err(_) => errors.push(format!("C++ output missing: {}", path.display())),
            _ => {}
        }
    }

    let func_count = ctx.func_table.len();
    let module_count = ctx.enabled_modules.len();
    let class_count: usize = ctx.module_classes.values().map(|v| v.len()).sum();
    let struct_count: usize = ctx.module_structs.values().map(|v| v.len()).sum();
    let enum_count: usize = ctx.module_enums.values().map(|v| v.len()).sum();

    if errors.is_empty() {
        eprintln!(
            "  OK: {module_count} modules, {class_count} classes, {struct_count} structs, {enum_count} enums, {func_count} functions"
        );
    } else {
        eprintln!("  Verification FAILED:");

        for e in &errors {
            eprintln!("    - {e}");
        }

        std::process::exit(1);
    }
}

fn build_func_table(ctx: &mut context::CodegenContext) {
    let mut entries = Vec::new();

    for (module_name, classes) in &ctx.module_classes {
        for class in classes {
            if class.super_class.as_deref() == Some("Interface") {
                continue;
            }

            for func in &class.funcs {
                let all_supported = func.params.iter().all(|p| {
                    type_map::map_property_type(
                        &p.prop_type,
                        p.class_name.as_deref(),
                        p.struct_name.as_deref(),
                        p.enum_name.as_deref(),
                        p.enum_underlying_type.as_deref(),
                        p.meta_class_name.as_deref(),
                        p.interface_name.as_deref(),
                    )
                    .supported
                });

                if !all_supported {
                    continue;
                }

                entries.push(context::FuncEntry {
                    func_id: 0,
                    module_name: module_name.clone(),
                    class_name: class.name.clone(),
                    func_name: func.name.clone(),
                    rust_func_name: naming::to_snake_case(&func.name),
                    func: func.clone(),
                    cpp_class_name: class.cpp_name.clone(),
                    header: class.header.clone(),
                });
            }
        }
    }

    entries.sort_by(|a, b| {
        a.module_name
            .cmp(&b.module_name)
            .then_with(|| a.class_name.cmp(&b.class_name))
            .then_with(|| a.func_name.cmp(&b.func_name))
    });

    for (i, entry) in entries.iter_mut().enumerate() {
        entry.func_id = i as u32;
    }

    ctx.func_table = entries;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn listed_classes_keep_their_ancestors_only() {
        let class = |name: &str, package: &str, parent: &str| -> schema::ClassInfo {
            serde_json::from_value(serde_json::json!({
                "name": name, "cpp_name": format!("U{name}"), "package": package,
                "header": "", "class_flags": 0, "super": parent,
            }))
            .unwrap()
        };

        let codegen: CodegenConfig = toml::from_str(
            r#"
            features = ["core", "editor"]
            [modules]
            CoreUObject = { module = "core_ue", feature = "core" }
            UnrealEd = { module = "unreal_ed", feature = "editor", classes = ["EditorActorSubsystem"] }
            "#,
        )
        .unwrap();

        let ctx = context::CodegenContext::new(
            vec![
                class("Object", "CoreUObject", ""),
                class("EditorSubsystemBase", "UnrealEd", "Object"),
                class("EditorActorSubsystem", "UnrealEd", "EditorSubsystemBase"),
                class("Factory", "UnrealEd", "Object"),
            ],
            vec![],
            vec![],
            &codegen,
        );

        let blocked = with_unlisted_classes(&ctx, &codegen).classes;
        assert_eq!(blocked, ["Factory"]);
    }

    #[test]
    fn unchanged_output_keeps_its_timestamp() {
        let dir = std::env::temp_dir().join(format!("rusteal-write-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("out.rs");
        write_if_changed(&path, "a").unwrap();
        let old = std::time::SystemTime::now() - std::time::Duration::from_secs(3600);

        std::fs::File::options()
            .write(true)
            .open(&path)
            .unwrap()
            .set_modified(old)
            .unwrap();

        write_if_changed(&path, "a").unwrap();
        assert_eq!(std::fs::metadata(&path).unwrap().modified().unwrap(), old);
        write_if_changed(&path, "b").unwrap();
        assert_ne!(std::fs::metadata(&path).unwrap().modified().unwrap(), old);
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "b");
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn plugin_dependencies_follow_enabled_modules() {
        let config: crate::config::CodegenConfig = toml::from_str(
            r#"
            features = ["engine", "state-tree"]
            [modules]
            Engine = { module = "engine", feature = "engine" }
            StateTreeModule = { module = "state_tree", feature = "state-tree", plugin = "StateTree" }
            GameplayStateTreeModule = { module = "gameplay_state_tree", feature = "state-tree", plugin = "GameplayStateTree" }
            Niagara = { module = "niagara", feature = "niagara", plugin = "Niagara" }
            "#,
        )
        .unwrap();

        let dir = std::env::temp_dir().join(format!("rusteal-uplugin-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let descriptor = dir.join("Rusteal.uplugin");

        std::fs::write(
            &descriptor,
            r#"{ "VersionName": "0.0.0", "Plugins": [ { "Name": "EnhancedInput", "Enabled": true } ] }"#,
        )
        .unwrap();

        update_plugin_dependencies(&config, &descriptor);
        update_plugin_dependencies(&config, &descriptor);

        let json: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&descriptor).unwrap()).unwrap();

        let names: Vec<&str> = json["Plugins"]
            .as_array()
            .unwrap()
            .iter()
            .map(|p| p["Name"].as_str().unwrap())
            .collect();

        assert_eq!(names, ["EnhancedInput", "GameplayStateTree", "StateTree"]);
        assert_eq!(json["VersionName"], "0.0.0");
        std::fs::remove_dir_all(&dir).unwrap();
    }
}