55 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
66 */
77
8- //! Godot engine classes and methods.
8+ #! [ deprecated = "Module has been split into `godot:: classes`, `godot::global` and `godot::extras`." ]
99
10- use crate :: builtin:: GString ;
11- use crate :: obj:: { bounds, Bounds , Gd , GodotClass , InstanceId } ;
10+ use crate :: obj:: { Bounds , GodotClass } ;
1211
13- // Re-exports of generated symbols
14-
15- pub use crate :: gen:: classes:: * ;
16-
17- use crate :: builtin:: meta:: CallContext ;
18- use crate :: sys;
19-
20- mod manual_extensions;
21-
22- #[ cfg( debug_assertions) ]
23- use crate :: builtin:: meta:: ClassName ;
24-
25- /// Support for Godot _native structures_.
26- ///
27- /// Native structures are a niche API in Godot. These are low-level data types that are passed as pointers to/from the engine.
28- /// In Rust, they are represented as `#[repr(C)]` structs.
29- ///
30- /// There is unfortunately not much official documentation available; you may need to look at Godot source code.
31- /// Most users will not need native structures, as they are very specialized.
32- pub mod native {
33- pub use crate :: gen:: native:: * ;
34- }
35-
36- // ----------------------------------------------------------------------------------------------------------------------------------------------
37- // Deprecations
12+ #[ deprecated = "Classes have been moved to `godot::classes`." ]
13+ pub use crate :: classes:: * ;
3814
3915#[ deprecated = "Enums have been moved to `godot::global`." ]
4016pub mod global {
@@ -47,6 +23,11 @@ pub mod utilities {
4723 pub use crate :: global:: * ;
4824}
4925
26+ #[ deprecated = "Native structures have been moved to `godot::classes::native`." ]
27+ pub mod native {
28+ pub use crate :: gen:: native:: * ;
29+ }
30+
5031#[ deprecated = "`godot::engine::translate` has been moved to `godot::extras`." ]
5132pub mod translate {
5233 pub use crate :: extras:: { tr, tr_n} ;
@@ -78,110 +59,3 @@ pub use crate::global::load;
7859
7960#[ deprecated = "`try_load` has been moved to `godot::global`." ]
8061pub use crate :: global:: try_load;
81-
82- // ----------------------------------------------------------------------------------------------------------------------------------------------
83- // Utilities for crate
84-
85- pub ( crate ) fn debug_string < T : GodotClass > (
86- obj : & Gd < T > ,
87- f : & mut std:: fmt:: Formatter < ' _ > ,
88- ty : & str ,
89- ) -> std:: fmt:: Result {
90- if let Some ( id) = obj. instance_id_or_none ( ) {
91- let class: GString = obj. raw . as_object ( ) . get_class ( ) ;
92- write ! ( f, "{ty} {{ id: {id}, class: {class} }}" )
93- } else {
94- write ! ( f, "{ty} {{ freed obj }}" )
95- }
96- }
97-
98- pub ( crate ) fn display_string < T : GodotClass > (
99- obj : & Gd < T > ,
100- f : & mut std:: fmt:: Formatter < ' _ > ,
101- ) -> std:: fmt:: Result {
102- let string: GString = obj. raw . as_object ( ) . to_string ( ) ;
103- <GString as std:: fmt:: Display >:: fmt ( & string, f)
104- }
105-
106- pub ( crate ) fn object_ptr_from_id ( instance_id : InstanceId ) -> sys:: GDExtensionObjectPtr {
107- // SAFETY: Godot looks up ID in ObjectDB and returns null if not found.
108- unsafe { sys:: interface_fn!( object_get_instance_from_id) ( instance_id. to_u64 ( ) ) }
109- }
110-
111- pub ( crate ) fn construct_engine_object < T > ( ) -> Gd < T >
112- where
113- T : GodotClass + Bounds < Declarer = bounds:: DeclEngine > ,
114- {
115- // SAFETY: adhere to Godot API; valid class name and returned pointer is an object.
116- unsafe {
117- let object_ptr = sys:: interface_fn!( classdb_construct_object) ( T :: class_name ( ) . string_sys ( ) ) ;
118- Gd :: from_obj_sys ( object_ptr)
119- }
120- }
121-
122- pub ( crate ) fn ensure_object_alive (
123- instance_id : InstanceId ,
124- old_object_ptr : sys:: GDExtensionObjectPtr ,
125- call_ctx : & CallContext ,
126- ) {
127- let new_object_ptr = object_ptr_from_id ( instance_id) ;
128-
129- assert ! (
130- !new_object_ptr. is_null( ) ,
131- "{call_ctx}: access to instance with ID {instance_id} after it has been freed"
132- ) ;
133-
134- // This should not happen, as reuse of instance IDs was fixed according to https://github.com/godotengine/godot/issues/32383,
135- // namely in PR https://github.com/godotengine/godot/pull/36189. Double-check to make sure.
136- assert_eq ! (
137- new_object_ptr, old_object_ptr,
138- "{call_ctx}: instance ID {instance_id} points to a stale, reused object. Please report this to gdext maintainers."
139- ) ;
140- }
141-
142- #[ cfg( debug_assertions) ]
143- pub ( crate ) fn ensure_object_inherits (
144- derived : ClassName ,
145- base : ClassName ,
146- instance_id : InstanceId ,
147- ) -> bool {
148- if derived == base
149- || base == Object :: class_name ( ) // for Object base, anything inherits by definition
150- || is_derived_base_cached ( derived, base)
151- {
152- return true ;
153- }
154-
155- panic ! (
156- "Instance of ID {instance_id} has type {derived} but is incorrectly stored in a Gd<{base}>.\n \
157- This may happen if you change an object's identity through DerefMut."
158- )
159- }
160-
161- // ----------------------------------------------------------------------------------------------------------------------------------------------
162- // Implementation of this file
163-
164- /// Checks if `derived` inherits from `base`, using a cache for _successful_ queries.
165- #[ cfg( debug_assertions) ]
166- fn is_derived_base_cached ( derived : ClassName , base : ClassName ) -> bool {
167- use std:: collections:: HashSet ;
168- use sys:: Global ;
169- static CACHE : Global < HashSet < ( ClassName , ClassName ) > > = Global :: default ( ) ;
170-
171- let mut cache = CACHE . lock ( ) ;
172- let key = ( derived, base) ;
173- if cache. contains ( & key) {
174- return true ;
175- }
176-
177- // Query Godot API (takes linear time in depth of inheritance tree).
178- let is_parent_class =
179- ClassDb :: singleton ( ) . is_parent_class ( derived. to_string_name ( ) , base. to_string_name ( ) ) ;
180-
181- // Insert only successful queries. Those that fail are on the error path already and don't need to be fast.
182- if is_parent_class {
183- cache. insert ( key) ;
184- }
185-
186- is_parent_class
187- }
0 commit comments