Skip to content

Commit d841973

Browse files
committed
Extract InitLevel + InitStage to separate file
1 parent 3a6d28b commit d841973

File tree

2 files changed

+122
-99
lines changed

2 files changed

+122
-99
lines changed

godot-ffi/src/init_level.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) godot-rust; Bromeon and contributors.
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
*/
7+
8+
/// Stage of the Godot initialization process.
9+
///
10+
/// Godot's initialization and deinitialization processes are split into multiple stages, like a stack. At each level,
11+
/// a different amount of engine functionality is available. Deinitialization happens in reverse order.
12+
///
13+
/// See also:
14+
// Explicit HTML links because this is re-exported in godot::init, and we can't document a `use` statement.
15+
/// - [`InitStage`](enum.InitStage.html): all levels + main loop.
16+
/// - [`ExtensionLibrary::on_stage_init()`](trait.ExtensionLibrary.html#method.on_stage_init)
17+
/// - [`ExtensionLibrary::on_stage_deinit()`](trait.ExtensionLibrary.html#method.on_stage_deinit)
18+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
19+
pub enum InitLevel {
20+
/// First level loaded by Godot. Builtin types are available, classes are not.
21+
Core,
22+
23+
/// Second level loaded by Godot. Only server classes and builtins are available.
24+
Servers,
25+
26+
/// Third level loaded by Godot. Most classes are available.
27+
Scene,
28+
29+
/// Fourth level loaded by Godot, only in the editor. All classes are available.
30+
Editor,
31+
}
32+
33+
impl InitLevel {
34+
#[doc(hidden)]
35+
pub fn from_sys(level: crate::GDExtensionInitializationLevel) -> Self {
36+
match level {
37+
crate::GDEXTENSION_INITIALIZATION_CORE => Self::Core,
38+
crate::GDEXTENSION_INITIALIZATION_SERVERS => Self::Servers,
39+
crate::GDEXTENSION_INITIALIZATION_SCENE => Self::Scene,
40+
crate::GDEXTENSION_INITIALIZATION_EDITOR => Self::Editor,
41+
_ => {
42+
eprintln!("WARNING: unknown initialization level {level}");
43+
Self::Scene
44+
}
45+
}
46+
}
47+
48+
#[doc(hidden)]
49+
pub fn to_sys(self) -> crate::GDExtensionInitializationLevel {
50+
match self {
51+
Self::Core => crate::GDEXTENSION_INITIALIZATION_CORE,
52+
Self::Servers => crate::GDEXTENSION_INITIALIZATION_SERVERS,
53+
Self::Scene => crate::GDEXTENSION_INITIALIZATION_SCENE,
54+
Self::Editor => crate::GDEXTENSION_INITIALIZATION_EDITOR,
55+
}
56+
}
57+
58+
/// Convert this initialization level to an initialization stage.
59+
pub fn to_stage(self) -> InitStage {
60+
match self {
61+
Self::Core => InitStage::Core,
62+
Self::Servers => InitStage::Servers,
63+
Self::Scene => InitStage::Scene,
64+
Self::Editor => InitStage::Editor,
65+
}
66+
}
67+
}
68+
69+
// ----------------------------------------------------------------------------------------------------------------------------------------------
70+
71+
/// Extended initialization stage that includes both initialization levels and the main loop.
72+
///
73+
/// This enum extends [`InitLevel`] with a `MainLoop` variant, representing the fully initialized state of Godot
74+
/// after all initialization levels have been loaded and before any deinitialization begins.
75+
///
76+
/// During initialization, stages are loaded in order: `Core` → `Servers` → `Scene` → `Editor` (if in editor) → `MainLoop`. \
77+
/// During deinitialization, stages are unloaded in reverse order.
78+
///
79+
/// See also:
80+
/// - [`InitLevel`](enum.InitLevel.html): only levels, without `MainLoop`.
81+
/// - [`ExtensionLibrary::on_stage_init()`](trait.ExtensionLibrary.html#method.on_stage_init)
82+
/// - [`ExtensionLibrary::on_stage_deinit()`](trait.ExtensionLibrary.html#method.on_stage_deinit)
83+
/// - [`ExtensionLibrary::on_main_loop_frame()`](trait.ExtensionLibrary.html#method.on_main_loop_frame)
84+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
85+
#[non_exhaustive]
86+
pub enum InitStage {
87+
/// First level loaded by Godot. Builtin types are available, classes are not.
88+
Core,
89+
90+
/// Second level loaded by Godot. Only server classes and builtins are available.
91+
Servers,
92+
93+
/// Third level loaded by Godot. Most classes are available.
94+
Scene,
95+
96+
/// Fourth level loaded by Godot, only in the editor. All classes are available.
97+
Editor,
98+
99+
/// The main loop stage, representing the fully initialized state of Godot.
100+
///
101+
/// This variant is only available in Godot 4.5+. In earlier versions, it will never be passed to callbacks.
102+
#[cfg(since_api = "4.5")]
103+
MainLoop,
104+
}
105+
106+
impl InitStage {
107+
/// Try to convert this initialization stage to an initialization level.
108+
///
109+
/// Returns `None` for [`InitStage::MainLoop`], as it doesn't correspond to a Godot initialization level.
110+
pub fn try_to_level(self) -> Option<InitLevel> {
111+
match self {
112+
Self::Core => Some(InitLevel::Core),
113+
Self::Servers => Some(InitLevel::Servers),
114+
Self::Scene => Some(InitLevel::Scene),
115+
Self::Editor => Some(InitLevel::Editor),
116+
#[cfg(since_api = "4.5")]
117+
Self::MainLoop => None,
118+
}
119+
}
120+
}

godot-ffi/src/lib.rs

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub use gen::table_scene_classes::*;
8787
pub use gen::table_servers_classes::*;
8888
pub use gen::table_utilities::*;
8989
pub use global::*;
90+
pub use init_level::*;
9091
pub use string_cache::StringCache;
9192
pub use toolbox::*;
9293

@@ -98,6 +99,7 @@ pub use crate::godot_ffi::{
9899
// API to access Godot via FFI
99100

100101
mod binding;
102+
mod init_level;
101103

102104
pub use binding::*;
103105
use binding::{
@@ -109,105 +111,6 @@ use binding::{
109111
#[cfg(not(wasm_nothreads))]
110112
static MAIN_THREAD_ID: ManualInitCell<std::thread::ThreadId> = ManualInitCell::new();
111113

112-
/// Stage of the Godot initialization process.
113-
///
114-
/// Godot's initialization and deinitialization processes are split into multiple stages, like a stack. At each level,
115-
/// a different amount of engine functionality is available. Deinitialization happens in reverse order.
116-
///
117-
/// See also:
118-
// Explicit HTML links because this is re-exported in godot::init, and we can't document a `use` statement.
119-
/// - [`InitStage`](enum.InitStage.html): all levels + main loop.
120-
/// - [`ExtensionLibrary::on_stage_init()`](trait.ExtensionLibrary.html#method.on_stage_init)
121-
/// - [`ExtensionLibrary::on_stage_deinit()`](trait.ExtensionLibrary.html#method.on_stage_deinit)
122-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
123-
pub enum InitLevel {
124-
/// First level loaded by Godot. Builtin types are available, classes are not.
125-
Core,
126-
127-
/// Second level loaded by Godot. Only server classes and builtins are available.
128-
Servers,
129-
130-
/// Third level loaded by Godot. Most classes are available.
131-
Scene,
132-
133-
/// Fourth level loaded by Godot, only in the editor. All classes are available.
134-
Editor,
135-
}
136-
137-
impl InitLevel {
138-
#[doc(hidden)]
139-
pub fn from_sys(level: crate::GDExtensionInitializationLevel) -> Self {
140-
match level {
141-
crate::GDEXTENSION_INITIALIZATION_CORE => Self::Core,
142-
crate::GDEXTENSION_INITIALIZATION_SERVERS => Self::Servers,
143-
crate::GDEXTENSION_INITIALIZATION_SCENE => Self::Scene,
144-
crate::GDEXTENSION_INITIALIZATION_EDITOR => Self::Editor,
145-
_ => {
146-
eprintln!("WARNING: unknown initialization level {level}");
147-
Self::Scene
148-
}
149-
}
150-
}
151-
#[doc(hidden)]
152-
pub fn to_sys(self) -> crate::GDExtensionInitializationLevel {
153-
match self {
154-
Self::Core => crate::GDEXTENSION_INITIALIZATION_CORE,
155-
Self::Servers => crate::GDEXTENSION_INITIALIZATION_SERVERS,
156-
Self::Scene => crate::GDEXTENSION_INITIALIZATION_SCENE,
157-
Self::Editor => crate::GDEXTENSION_INITIALIZATION_EDITOR,
158-
}
159-
}
160-
161-
/// Convert this initialization level to an initialization stage.
162-
pub fn to_stage(self) -> InitStage {
163-
match self {
164-
Self::Core => InitStage::Core,
165-
Self::Servers => InitStage::Servers,
166-
Self::Scene => InitStage::Scene,
167-
Self::Editor => InitStage::Editor,
168-
}
169-
}
170-
}
171-
172-
// ----------------------------------------------------------------------------------------------------------------------------------------------
173-
174-
/// Extended initialization stage that includes both initialization levels and the main loop.
175-
///
176-
/// This enum extends [`InitLevel`] with a `MainLoop` variant, representing the fully initialized state of Godot
177-
/// after all initialization levels have been loaded and before any deinitialization begins.
178-
///
179-
/// During initialization, stages are loaded in order: `Core` → `Servers` → `Scene` → `Editor` (if in editor) → `MainLoop`. \
180-
/// During deinitialization, stages are unloaded in reverse order.
181-
///
182-
/// See also:
183-
/// - [`InitLevel`](enum.InitLevel.html): only levels, without `MainLoop`.
184-
/// - [`ExtensionLibrary::on_stage_init()`](trait.ExtensionLibrary.html#method.on_stage_init)
185-
/// - [`ExtensionLibrary::on_stage_deinit()`](trait.ExtensionLibrary.html#method.on_stage_deinit)
186-
/// - [`ExtensionLibrary::on_main_loop_frame()`](trait.ExtensionLibrary.html#method.on_main_loop_frame)
187-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
188-
#[non_exhaustive]
189-
pub enum InitStage {
190-
Core,
191-
Servers,
192-
Scene,
193-
Editor,
194-
#[cfg(since_api = "4.5")]
195-
MainLoop,
196-
}
197-
198-
impl InitStage {
199-
pub fn try_to_level(self) -> Option<InitLevel> {
200-
match self {
201-
Self::Core => Some(InitLevel::Core),
202-
Self::Servers => Some(InitLevel::Servers),
203-
Self::Scene => Some(InitLevel::Scene),
204-
Self::Editor => Some(InitLevel::Editor),
205-
#[cfg(since_api = "4.5")]
206-
Self::MainLoop => None,
207-
}
208-
}
209-
}
210-
211114
// ----------------------------------------------------------------------------------------------------------------------------------------------
212115

213116
pub struct GdextRuntimeMetadata {

0 commit comments

Comments
 (0)