Skip to main content

steel_core/
bootstrap.rs

1//! Global registry and behavior initialization.
2
3use std::time::Instant;
4
5use steel_registry::init_vanilla_registry;
6
7use crate::behavior::init_behaviors;
8use crate::block_entity::init_block_entities;
9use crate::entity::init_entities;
10
11fn fill_behavior_registries() {
12    init_behaviors();
13    init_block_entities();
14    init_entities();
15    log::info!("Behavior registries initialized");
16}
17
18/// # Errors
19/// Returns an error if the global registry has already been initialized.
20pub(crate) fn init_globals() -> Result<(), String> {
21    let start = Instant::now();
22    let published = init_vanilla_registry();
23    log::info!("Vanilla registry loaded in {:?}", start.elapsed());
24
25    if !published {
26        return Err("global registry has already been initialized".to_owned());
27    }
28
29    fill_behavior_registries();
30    Ok(())
31}
32
33/// Idempotent [`init_globals`] for tests and benchmarks, which bootstrap
34/// repeatedly in one process.
35#[cfg(any(test, feature = "benchmark-support"))]
36pub fn init_globals_once() {
37    use std::sync::Once;
38
39    static INIT: Once = Once::new();
40    INIT.call_once(|| {
41        init_vanilla_registry();
42        fill_behavior_registries();
43    });
44}