gdnative_core/init/
info.rs

1use crate::core_types::GodotString;
2
3/// Context for the [`godot_gdnative_init`][crate::init::godot_gdnative_init] callback.
4pub struct InitializeInfo {
5    in_editor: bool,
6    active_library_path: GodotString,
7    options: *mut crate::sys::godot_gdnative_init_options,
8}
9
10impl InitializeInfo {
11    /// Returns true if the library is loaded in the Godot Editor.
12    #[inline]
13    pub fn in_editor(&self) -> bool {
14        self.in_editor
15    }
16
17    /// Returns a path to the library relative to the project.
18    ///
19    /// Example: `res://../../target/debug/libhello_world.dylib`
20    #[inline]
21    pub fn active_library_path(&self) -> &GodotString {
22        &self.active_library_path
23    }
24
25    /// Internal interface.
26    ///
27    /// # Safety
28    ///
29    /// Will `panic!()` if options is NULL, UB if invalid.
30    #[inline]
31    #[doc(hidden)]
32    pub unsafe fn new(options: *mut crate::sys::godot_gdnative_init_options) -> Self {
33        assert!(!options.is_null(), "options were NULL");
34        let crate::sys::godot_gdnative_init_options {
35            in_editor,
36            active_library_path,
37            ..
38        } = *options;
39
40        let active_library_path = GodotString::clone_from_sys(*active_library_path);
41
42        Self {
43            in_editor,
44            active_library_path,
45            options,
46        }
47    }
48
49    #[inline]
50    pub fn report_loading_error<T>(&self, message: T)
51    where
52        T: std::fmt::Display,
53    {
54        let crate::sys::godot_gdnative_init_options {
55            report_loading_error,
56            gd_native_library,
57            ..
58        } = unsafe { *self.options };
59
60        if let Some(report_loading_error_fn) = report_loading_error {
61            // Add the trailing zero and convert Display => String
62            let message = format!("{message}\0");
63
64            // Convert to FFI compatible string
65            let message = std::ffi::CStr::from_bytes_with_nul(message.as_bytes())
66                .expect("message should not have a NULL");
67
68            unsafe {
69                report_loading_error_fn(gd_native_library, message.as_ptr());
70            }
71        }
72    }
73}
74
75/// Context for the [`godot_gdnative_terminate`][crate::init::godot_gdnative_terminate] callback.
76pub struct TerminateInfo {
77    in_editor: bool,
78}
79
80impl TerminateInfo {
81    #[inline]
82    #[doc(hidden)] // avoids clippy warning: unsafe function's docs miss `# Safety` section
83    pub unsafe fn new(options: *mut crate::sys::godot_gdnative_terminate_options) -> Self {
84        assert!(!options.is_null(), "options were NULL");
85
86        let crate::sys::godot_gdnative_terminate_options { in_editor } = *options;
87
88        Self { in_editor }
89    }
90
91    /// Returns `true` if the library is loaded in the Godot Editor.
92    #[inline]
93    pub fn in_editor(&self) -> bool {
94        self.in_editor
95    }
96}