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::{WorldSpawnExt, find_object, load_object}"),
41    ("input", "crate::input_core::FKey"),
42    ("input", "crate::manual::fkey::FKeyExt"),
43];
44
45/// Write `src/prelude.rs`, keeping only the entries whose feature exists in
46/// this generation (a feature no module maps to would never compile).
47pub fn write_prelude(src_dir: &Path, ctx: &CodegenContext) {
48    let available: HashSet<&str> = ctx
49        .enabled_modules
50        .iter()
51        .filter_map(|module| ctx.feature_for_module(module))
52        .collect();
53
54    let mut out = String::from(
55        "// Auto-generated by rusteal-codegen. Do not edit.\n\
56         //\n\
57         // One-import access to the engine types used most often:\n\
58         //\n\
59         //     use rusteal_runtime::prelude::*;\n\
60         //     use bindings::prelude::*;\n\n",
61    );
62    for (feature, path) in ENTRIES {
63        if !available.contains(feature) {
64            continue;
65        }
66        out.push_str(&format!("#[cfg(feature = \"{feature}\")]\npub use {path};\n"));
67    }
68
69    let path = src_dir.join("prelude.rs");
70    std::fs::write(&path, out)
71        .unwrap_or_else(|e| panic!("Failed to write {}: {e}", path.display()));
72}