use std::sync::OnceLock;
use super::GodotBinding;
use crate::ManualInitCell;
pub(super) struct BindingStorage {
binding: ManualInitCell<GodotBinding>,
}
impl BindingStorage {
#[inline(always)]
fn storage() -> &'static Self {
static BINDING: BindingStorage = BindingStorage {
binding: ManualInitCell::new(),
};
&BINDING
}
pub unsafe fn initialize(binding: GodotBinding) {
let storage = Self::storage();
assert!(
!storage.binding.is_initialized(),
"initialize must only be called at startup or after deinitialize"
);
unsafe { storage.binding.set(binding) }
}
pub unsafe fn deinitialize() {
let storage = Self::storage();
assert!(
storage.binding.is_initialized(),
"deinitialize must only be called after initialize"
);
unsafe { storage.binding.clear() };
}
#[inline(always)]
pub unsafe fn get_binding_unchecked() -> &'static GodotBinding {
let storage = Self::storage();
crate::strict_assert!(
storage.binding.is_initialized(),
"Godot engine not available; make sure you are not calling it from unit/doc tests"
);
unsafe { storage.binding.get_unchecked() }
}
pub fn is_initialized() -> bool {
let storage = Self::storage();
storage.binding.is_initialized()
}
}
pub struct GdextConfig {
pub tool_only_in_editor: bool,
is_editor: OnceLock<bool>,
}
impl GdextConfig {
pub fn new(tool_only_in_editor: bool) -> Self {
Self {
tool_only_in_editor,
is_editor: OnceLock::new(),
}
}
pub fn is_editor_or_init(&self, is_editor: impl FnOnce() -> bool) -> bool {
*self.is_editor.get_or_init(is_editor)
}
}