gdnative_core/init/mod.rs
1//! Global initialization and termination of the library.
2//!
3//! This module provides all the plumbing required for global initialization and shutdown of godot-rust.
4//!
5//! ## Init and exit hooks
6//!
7//! Three endpoints are automatically invoked by the engine during startup and shutdown:
8//!
9//! * [`godot_gdnative_init`],
10//! * [`godot_nativescript_init`],
11//! * [`godot_gdnative_terminate`],
12//!
13//! All three must be present. To quickly define all three endpoints using the default names,
14//! use [`godot_init`].
15//!
16//! ## Registering script classes
17//!
18//! [`InitHandle`] is the registry of all your exported symbols.
19//! To register script classes, call [`InitHandle::add_class()`] or [`InitHandle::add_tool_class()`]
20//! in your `godot_nativescript_init` or `godot_init` callback:
21//!
22//! ```no_run
23//! use gdnative::prelude::*;
24//!
25//! #[derive(NativeClass)]
26//! # #[no_constructor]
27//! struct HelloWorld { /* ... */ }
28//!
29//! #[methods]
30//! impl HelloWorld { /* ... */ }
31//!
32//! fn init(handle: InitHandle) {
33//! handle.add_class::<HelloWorld>();
34//! }
35//!
36//! godot_init!(init);
37//! ```
38
39mod info;
40mod init_handle;
41mod macros;
42
43pub mod diagnostics;
44
45pub use info::*;
46pub use init_handle::*;
47
48bitflags::bitflags! {
49 /// Initialization level used to distinguish the source of init actions, such as class registration.
50 /// Internal API.
51 #[doc(hidden)]
52 pub struct InitLevel: u8 {
53 /// Init level for automatic registration
54 const AUTO = 1;
55 /// Init level for user code
56 const USER = 2;
57 }
58}
59
60#[doc(hidden)]
61#[cfg(feature = "inventory")]
62#[inline]
63pub fn auto_register(init_handle: InitHandle) {
64 for plugin in inventory::iter::<crate::private::AutoInitPlugin> {
65 (plugin.f)(init_handle);
66 }
67}
68
69#[doc(hidden)]
70#[cfg(not(feature = "inventory"))]
71#[inline]
72pub fn auto_register(_init_handle: InitHandle) {
73 // Nothing to do here.
74}
75
76pub use crate::{
77 godot_gdnative_init, godot_gdnative_terminate, godot_init, godot_nativescript_init,
78};