rusteal 0.5.0

Rust bindings for Unreal Engine 5.8.2
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
// Build command: the 5-step build pipeline, for every Rust library of the
// project: the game's and each Rusteal plugin's (`Plugins/<Name>/rusteal.toml`).

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Instant;

use rusteal_codegen::config::{
    find_plugins, PluginConfig, PluginLayout, ProjectConfig, ProjectLayout,
};

/// Canonicalize a path, stripping the `\\?\` extended-length prefix that
/// Windows adds. UBT's .NET XML parser chokes on that prefix.
pub fn canonical_no_prefix(path: &Path) -> PathBuf {
    let abs = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
    #[cfg(windows)]
    {
        // `canonicalize()` returns `\\?\C:\...` on Windows; strip the prefix.
        let s = abs.to_string_lossy();
        if let Some(stripped) = s.strip_prefix(r"\\?\") {
            return PathBuf::from(stripped);
        }
    }
    abs
}

/// The first step that only builds Rust: `rusteal build` without `--all`
/// starts there.
pub const RUST_STEP: u8 = 4;

/// Run the build pipeline.
///
/// `project_root` holds the .uproject and `rusteal.toml`; `engine_path` is the
/// UE root. `step` runs only that step (1-5), `from` starts from it; the two
/// are mutually exclusive. `release` builds the libraries with Cargo's
/// release profile, for shipping; otherwise with the dev profile, for
/// iterating. `plugin` limits steps 2, 4 and 5 to that Rusteal plugin's
/// library; the UE steps always build the whole project.
pub fn run_build(
    project_root: &Path,
    engine_path: &Path,
    step: Option<u8>,
    from: u8,
    release: bool,
    plugin: Option<&str>,
) {
    // Validate step/from
    if step.is_some() && from != 1 {
        eprintln!("Error: --step and --from are mutually exclusive.");
        std::process::exit(1);
    }
    if let Some(s) = step
        && !(1..=5).contains(&s)
    {
        eprintln!("Error: --step must be 1-5, got {s}");
        std::process::exit(1);
    }
    if !(1..=5).contains(&from) {
        eprintln!("Error: --from must be 1-5, got {from}");
        std::process::exit(1);
    }

    let config = ProjectConfig::load(project_root).unwrap_or_else(|e| {
        eprintln!("Error: {e}");
        std::process::exit(1);
    });
    let ctx = BuildContext::new(&config, project_root, engine_path, release, plugin);

    // Determine which steps to run
    let steps: Vec<u8> = if let Some(s) = step {
        vec![s]
    } else {
        (from..=5).collect()
    };

    let cargo_step = format!("cargo build, {} profile (cdylib)", ctx.profile());
    let libraries = ctx
        .libraries
        .iter()
        .map(|library| library.label.as_str())
        .collect::<Vec<_>>()
        .join(", ");
    let step_descs: &[&str] = &[
        "",
        "UE build (UHT export to JSON)",
        "rusteal-codegen (JSON -> Rust + C++)",
        "UE rebuild (compile C++ wrappers)",
        &cargo_step,
        "Copy the shared library to plugin Binaries",
    ];

    let total = steps.len();
    let overall_start = Instant::now();

    eprintln!("\n{}", "=".repeat(60));
    eprintln!("  Rusteal build pipeline");
    if step.is_some() {
        eprintln!("  Running step {} only", steps[0]);
    } else if from == RUST_STEP {
        eprintln!("  Running steps {RUST_STEP}-5: the Rust libraries (--all runs every step)");
    } else if from > 1 {
        eprintln!("  Running steps {from}-5");
    } else {
        eprintln!("  Running all {total} steps");
    }
    eprintln!("  Libraries: {libraries}");
    eprintln!("{}\n", "=".repeat(60));

    for (i, &step_num) in steps.iter().enumerate() {
        let step_start = Instant::now();
        eprintln!(
            "[{}/{}] Step {}: {}",
            i + 1,
            total,
            step_num,
            step_descs[step_num as usize]
        );
        eprintln!("{}", "-".repeat(60));

        match step_num {
            1 => ctx.step1_ue_build(),
            2 => ctx.step2_codegen(),
            3 => ctx.step3_ue_rebuild(),
            4 => ctx.step4_cargo_build(),
            5 => ctx.step5_copy_dll(),
            _ => unreachable!(),
        }

        let elapsed = step_start.elapsed().as_secs_f64();
        eprintln!("  Step {step_num} completed in {elapsed:.1}s\n");
    }

    let overall = overall_start.elapsed().as_secs_f64();
    eprintln!("{}", "=".repeat(60));
    eprintln!("  All steps completed in {overall:.1}s");
    eprintln!("{}", "=".repeat(60));
}

/// What differs between the host platforms the pipeline runs on. Chosen at compile time from
/// the platform `rusteal-cli` itself was built for, which is also the platform UE builds the
/// editor for.
pub struct HostPlatform {
    /// UBT entry script, relative to the engine root.
    ubt_script: &'static str,
    /// UAT entry script, relative to the engine root.
    pub uat_script: &'static str,
    /// Platform name as UBT expects it (`Build.bat <Target> Win64 ...`).
    pub ubt_platform: &'static str,
    /// Prefix and extension of a shared library (`libx.so`, `x.dll`, `libx.dylib`).
    lib_prefix: &'static str,
    lib_extension: &'static str,
}

impl HostPlatform {
    #[cfg(target_os = "windows")]
    pub const CURRENT: HostPlatform = HostPlatform {
        ubt_script: "Engine/Build/BatchFiles/Build.bat",
        uat_script: "Engine/Build/BatchFiles/RunUAT.bat",
        ubt_platform: "Win64",
        lib_prefix: "",
        lib_extension: "dll",
    };

    #[cfg(target_os = "linux")]
    pub const CURRENT: HostPlatform = HostPlatform {
        ubt_script: "Engine/Build/BatchFiles/Linux/Build.sh",
        uat_script: "Engine/Build/BatchFiles/RunUAT.sh",
        ubt_platform: "Linux",
        lib_prefix: "lib",
        lib_extension: "so",
    };

    #[cfg(target_os = "macos")]
    pub const CURRENT: HostPlatform = HostPlatform {
        ubt_script: "Engine/Build/BatchFiles/Mac/Build.sh",
        uat_script: "Engine/Build/BatchFiles/RunUAT.sh",
        ubt_platform: "Mac",
        lib_prefix: "lib",
        lib_extension: "dylib",
    };

    /// File name of a shared library built from `crate_name`, as Cargo names it.
    fn lib_filename(&self, crate_name: &str) -> String {
        // Cargo converts hyphens to underscores in output filenames.
        format!(
            "{}{}.{}",
            self.lib_prefix,
            crate_name.replace('-', "_"),
            self.lib_extension
        )
    }

    /// File name a library is deployed as, from its stem: `rusteal` gives
    /// `rusteal.dll`, `librusteal.so`, `librusteal.dylib`; must match
    /// `RustealLibraryFileName` in the C++ plugin.
    pub fn deployed_lib_filename(&self, stem: &str) -> String {
        format!("{}{stem}.{}", self.lib_prefix, self.lib_extension)
    }
}

/// One Rust library the pipeline builds and deploys.
struct Library {
    /// For the log: `game` or `plugin <Name>`.
    label: String,
    /// The Cargo workspace holding the crate.
    rust_workspace: PathBuf,
    crate_name: String,
    /// Extra features for `cargo build`.
    features: Vec<String>,
    /// Where it is deployed, and the stem of the deployed file's name.
    deploy_dir: PathBuf,
    deploy_stem: String,
    /// The plugin it belongs to; `None` for the game's.
    plugin: Option<PluginLayout>,
}

/// Resolved build context with all paths pre-computed.
struct BuildContext {
    engine_path: PathBuf,
    layout: ProjectLayout,
    libraries: Vec<Library>,
    /// Cargo's release profile instead of dev for the libraries.
    release: bool,
}

impl BuildContext {
    fn new(
        config: &ProjectConfig,
        project_root: &Path,
        engine_path: &Path,
        release: bool,
        only_plugin: Option<&str>,
    ) -> Self {
        let layout = ProjectLayout::new(project_root);
        let platform = HostPlatform::CURRENT.ubt_platform;
        let mut libraries = Vec::new();
        if only_plugin.is_none() {
            libraries.push(Library {
                label: "game".to_string(),
                rust_workspace: layout.rust_workspace(),
                crate_name: config.project.crate_name.clone(),
                features: config.project.features.clone(),
                deploy_dir: project_root.join("Plugins/Rusteal/Binaries").join(platform),
                deploy_stem: "rusteal".to_string(),
                plugin: None,
            });
        }
        let plugins = find_plugins(project_root);
        if let Some(name) = only_plugin
            && !plugins.iter().any(|p| p.name.eq_ignore_ascii_case(name))
        {
            let names: Vec<&str> = plugins.iter().map(|p| p.name.as_str()).collect();
            eprintln!(
                "Error: no Rusteal plugin '{name}' in {}/Plugins (Rusteal plugins: {}).",
                project_root.display(),
                if names.is_empty() { "none".to_string() } else { names.join(", ") }
            );
            std::process::exit(1);
        }
        for plugin in plugins {
            if only_plugin.is_some_and(|name| !plugin.name.eq_ignore_ascii_case(name)) {
                continue;
            }
            let plugin_config = PluginConfig::load(&plugin.dir).unwrap_or_else(|e| {
                eprintln!("Error: {e}");
                std::process::exit(1);
            });
            libraries.push(Library {
                label: format!("plugin {}", plugin.name),
                rust_workspace: plugin.rust_workspace(),
                crate_name: plugin_config.plugin.crate_name,
                features: plugin_config.plugin.features,
                deploy_dir: plugin.binaries(platform),
                deploy_stem: plugin.library_stem(),
                plugin: Some(plugin),
            });
        }
        BuildContext { engine_path: engine_path.to_path_buf(), layout, libraries, release }
    }

    /// The Cargo profile the game library is built with, as named in
    /// `target/<dir>`.
    fn profile(&self) -> &'static str {
        if self.release { "release" } else { "dev" }
    }

    fn target_dir(&self) -> &'static str {
        if self.release { "release" } else { "debug" }
    }

    /// The directory holding the .uproject.
    fn project_path(&self) -> &Path {
        &self.layout.root
    }

    /// Find the .uproject file and derive the Editor target name.
    fn editor_target(&self) -> String {
        let uproject = self.uproject_path();
        let stem = uproject
            .file_stem()
            .unwrap()
            .to_str()
            .unwrap();
        format!("{stem}Editor")
    }

    /// Full path to the .uproject file.
    fn uproject_path(&self) -> PathBuf {
        self.layout.uproject().unwrap_or_else(|| {
            eprintln!("Error: no .uproject found in {}", self.project_path().display());
            std::process::exit(1);
        })
    }

    /// Build the project's Editor target with UBT for the host platform.
    fn run_ubt(&self) {
        let platform = &HostPlatform::CURRENT;
        let script = self.engine_path.join(platform.ubt_script);
        if !script.exists() {
            eprintln!("Error: UBT script not found at {}", script.display());
            std::process::exit(1);
        }

        let target = self.editor_target();
        let uproject = self.uproject_path();
        let uproject_abs = canonical_no_prefix(&uproject);

        run_cmd(&[
            script.to_str().unwrap(),
            &target,
            platform.ubt_platform,
            "Development",
            &format!("-Project={}", uproject_abs.display()),
        ]);
    }

    /// Step 1: UE build — triggers UHT export, then copies JSON.
    fn step1_ue_build(&self) {
        self.run_ubt();

        // Copy UHT JSON files
        let uht_input = self.layout.uht_json();
        let uht_intermediate = self.project_path().join(format!(
            "Plugins/RustealGenerator/Intermediate/Build/{}/UnrealEditor/Inc/RustealGenerator/UHT",
            HostPlatform::CURRENT.ubt_platform
        ));

        fs::create_dir_all(&uht_input)
            .unwrap_or_else(|e| panic!("Failed to create {}: {e}", uht_input.display()));

        let json_files = [
            "rusteal_classes.json",
            "rusteal_structs.json",
            "rusteal_enums.json",
        ];
        let mut copied = 0;
        for name in &json_files {
            let src = uht_intermediate.join(name);
            if src.exists() {
                let size_kb = fs::metadata(&src).map(|m| m.len() / 1024).unwrap_or(0);
                fs::copy(&src, uht_input.join(name)).unwrap_or_else(|e| {
                    panic!("Failed to copy {}: {e}", src.display())
                });
                eprintln!("  Copied {name} ({size_kb} KB)");
                copied += 1;
            } else {
                eprintln!("  Warning: {name} not found in {}", uht_intermediate.display());
            }
        }
        eprintln!(
            "  {copied}/{} JSON files copied to {}",
            json_files.len(),
            uht_input.display()
        );
    }

    /// Step 2: Run codegen (in-process), for each library.
    fn step2_codegen(&self) {
        for library in &self.libraries {
            match &library.plugin {
                None => rusteal_codegen::run_generate(&self.layout.root),
                Some(plugin) => rusteal_codegen::run_generate_plugin(&self.layout.root, plugin),
            }
        }
    }

    /// Step 3: UE rebuild — compiles generated C++ wrappers.
    fn step3_ue_rebuild(&self) {
        self.run_ubt();
    }

    /// Step 4: cargo build of each library's crate in its Rust workspace, with
    /// the profile the workspace's `Cargo.toml` sets.
    fn step4_cargo_build(&self) {
        for library in &self.libraries {
            // Until codegen runs, `bindings` is the placeholder `rusteal new`
            // writes, and the crate cannot compile against it.
            if !library.rust_workspace.join("bindings/src/func_ids.rs").is_file() {
                eprintln!(
                    "Error: the {} library's bindings have not been generated yet: \
                     run `rusteal build --all` once.",
                    library.label
                );
                std::process::exit(1);
            }
            eprintln!("  {}: {}", library.label, library.crate_name);
            // The crate by its own manifest, as the templates lay it out
            // (Rust/<crate>/): `-p <crate>` is ambiguous when a dependency
            // has the same name (a plugin named Inventory, a game named Glam).
            let member = library.rust_workspace.join(&library.crate_name).join("Cargo.toml");
            let (manifest, package) = if member.is_file() {
                (member, None)
            } else {
                (library.rust_workspace.join("Cargo.toml"), Some(library.crate_name.as_str()))
            };
            let manifest_str = manifest.to_string_lossy().into_owned();
            let mut args = vec!["cargo", "build", "--manifest-path", &manifest_str];
            if let Some(package) = package {
                args.extend(["-p", package]);
            }
            if self.release {
                args.push("--release");
            }
            let features_str = library.features.join(",");
            if !features_str.is_empty() {
                args.push("--features");
                args.push(&features_str);
            }
            run_cmd(&args);
        }
    }

    /// Step 5: Copy each built library to where its UE module loads it from.
    fn step5_copy_dll(&self) {
        let platform = &HostPlatform::CURRENT;
        for library in &self.libraries {
            let src = library
                .rust_workspace
                .join("target")
                .join(self.target_dir())
                .join(platform.lib_filename(&library.crate_name));
            let deployed_name = platform.deployed_lib_filename(&library.deploy_stem);
            let dest = library.deploy_dir.join(&deployed_name);

            if !src.exists() {
                eprintln!("Error: library not found at {}", src.display());
                eprintln!("  Did step 4 (cargo build) succeed?");
                std::process::exit(1);
            }

            fs::create_dir_all(&library.deploy_dir).unwrap_or_else(|e| {
                panic!("Failed to create {}: {e}", library.deploy_dir.display())
            });
            fs::copy(&src, &dest).unwrap_or_else(|e| panic!("Failed to copy DLL: {e}"));

            eprintln!("  {}: copied {}", library.label, src.display());
            eprintln!("      -> {} (renamed to {deployed_name})", dest.display());
        }
    }
}

/// Run an external command, printing it and exiting on failure.
pub fn run_cmd(args: &[&str]) {
    let display: String = args.to_vec().join(" ");
    let truncated = if display.len() > 200 {
        format!("{}...", &display[..197])
    } else {
        display
    };
    eprintln!("  $ {truncated}");

    let status = Command::new(args[0])
        .args(&args[1..])
        .status()
        .unwrap_or_else(|e| panic!("Failed to run {}: {e}", args[0]));

    if !status.success() {
        let code = status.code().unwrap_or(1);
        eprintln!("\n  Command failed with exit code {code}");
        std::process::exit(code);
    }
}