Skip to main content

rusteal_codegen/rust_gen/
prelude.rs

1// The generated crate's `prelude`: the engine types a game reaches for first.
2//
3// A curated list, feature-gated exactly like the modules it draws from — never
4// a glob of the generated modules, which would bring thousands of names and
5// collide across extension traits.
6
7use std::collections::HashSet;
8use std::path::Path;
9
10use crate::context::CodegenContext;
11
12/// One prelude entry: the feature that must be on, and the `use` path.
13const ENTRIES: &[(&str, &str)] = &[
14    (
15        "core",
16        "crate::core_ue::{\n    \
17             FVector, FVectorExt,\n    \
18             FVector2D, FVector2DExt,\n    \
19             FVector4, FVector4Ext,\n    \
20             FQuat, FQuatExt,\n    \
21             FRotator, FRotatorExt,\n    \
22             FTransform, FTransformExt,\n    \
23             FLinearColor, FLinearColorExt,\n    \
24             FColor, FColorExt,\n    \
25             FPlane, FPlaneExt,\n    \
26             FBox2D, FBox2DExt,\n\
27         }",
28    ),
29    ("core", "crate::manual::vector::OwnedFVectorExt"),
30    ("core", "crate::manual::vector2d::OwnedFVector2DExt"),
31    ("core", "crate::manual::vector4::OwnedFVector4Ext"),
32    ("core", "crate::manual::quat::OwnedFQuatExt"),
33    ("core", "crate::manual::rotator::OwnedFRotatorExt"),
34    ("core", "crate::manual::transform::OwnedFTransformExt"),
35    ("core", "crate::manual::linear_color::OwnedFLinearColorExt"),
36    ("core", "crate::manual::color::OwnedFColorExt"),
37    ("core", "crate::manual::plane::OwnedFPlaneExt"),
38    ("core", "crate::manual::ue_box2d::OwnedFBox2DExt"),
39    ("engine", "crate::engine::{Actor, ActorExt, World, WorldExt}"),
40    ("engine", "crate::manual::world_ext::{ObjectWorldExt, WorldSpawnExt, find_object, load_object}"),
41    ("engine", "crate::manual::collision::object_type_query"),
42    ("engine", "crate::manual::data_table::{DataTableRowHandleRowExt, find_data_table_row}"),
43    ("umg", "crate::manual::widget_ext::{create_widget, create_widget_of_class}"),
44    ("input", "crate::input_core::FKey"),
45    ("input", "crate::manual::fkey::FKeyExt"),
46    (
47        "enhanced-input",
48        "crate::manual::input_ext::{InputActionValueExt, bind_action, enhanced_input_subsystem}",
49    ),
50];
51
52/// Write `src/prelude.rs`, keeping only the entries whose feature exists in
53/// this generation (a feature no module maps to would never compile).
54pub fn write_prelude(src_dir: &Path, ctx: &CodegenContext) {
55    let available: HashSet<&str> = ctx
56        .enabled_modules
57        .iter()
58        .filter_map(|module| ctx.feature_for_module(module))
59        .collect();
60
61    let mut out = String::from(
62        "// Auto-generated by rusteal-codegen. Do not edit.\n\
63         //\n\
64         // One-import access to the engine types used most often:\n\
65         //\n\
66         //     use rusteal_runtime::prelude::*;\n\
67         //     use bindings::prelude::*;\n\n",
68    );
69    for (feature, path) in ENTRIES {
70        if !available.contains(feature) {
71            continue;
72        }
73        out.push_str(&format!("#[cfg(feature = \"{feature}\")]\npub use {path};\n"));
74    }
75
76    let path = src_dir.join("prelude.rs");
77    crate::write_if_changed(&path, &out)
78        .unwrap_or_else(|e| panic!("Failed to write {}: {e}", path.display()));
79}