@@ -40,9 +40,8 @@ use std::ptr;
4040use crate :: core_types:: { GodotString , ToVariant , Variant } ;
4141use crate :: export:: method:: * ;
4242use crate :: export:: property:: * ;
43- use crate :: export:: { class_registry, emplace} ;
44- use crate :: export:: { user_data:: UserData , NativeClass , NativeClassMethods } ;
45- use crate :: object:: { GodotObject , NewRef , RawObject , TRef } ;
43+ use crate :: export:: NativeClass ;
44+ use crate :: object:: NewRef ;
4645use crate :: private:: get_api;
4746
4847//pub use self::method::{
@@ -65,180 +64,11 @@ pub trait Export: ToVariant {
6564 fn export_info ( hint : Option < Self :: Hint > ) -> ExportInfo ;
6665}
6766
68- /// A handle that can register new classes to the engine during initialization.
69- ///
70- /// See [`godot_nativescript_init`](macro.godot_nativescript_init.html) and
71- /// [`godot_init`](macro.godot_init.html).
72- #[ derive( Copy , Clone ) ]
73- pub struct InitHandle {
74- #[ doc( hidden) ]
75- handle : * mut libc:: c_void ,
76- }
77-
78- impl InitHandle {
79- #[ doc( hidden) ]
80- #[ inline]
81- pub unsafe fn new ( handle : * mut libc:: c_void ) -> Self {
82- InitHandle { handle }
83- }
84-
85- /// Registers a new class to the engine.
86- #[ inline]
87- pub fn add_class < C > ( self )
88- where
89- C : NativeClassMethods ,
90- {
91- self . add_maybe_tool_class :: < C > ( false )
92- }
93-
94- /// Registers a new tool class to the engine.
95- #[ inline]
96- pub fn add_tool_class < C > ( self )
97- where
98- C : NativeClassMethods ,
99- {
100- self . add_maybe_tool_class :: < C > ( true )
101- }
102-
103- #[ inline]
104- fn add_maybe_tool_class < C > ( self , is_tool : bool )
105- where
106- C : NativeClassMethods ,
107- {
108- if !class_registry:: register_class :: < C > ( ) {
109- panic ! (
110- "`{type_name}` has already been registered" ,
111- type_name = std:: any:: type_name:: <C >( )
112- ) ;
113- }
114- unsafe {
115- let class_name = CString :: new ( C :: class_name ( ) ) . unwrap ( ) ;
116- let base_name = CString :: new ( C :: Base :: class_name ( ) ) . unwrap ( ) ;
117-
118- let create = {
119- unsafe extern "C" fn constructor < C : NativeClass > (
120- this : * mut sys:: godot_object ,
121- _method_data : * mut libc:: c_void ,
122- ) -> * mut libc:: c_void {
123- use std:: panic:: { self , AssertUnwindSafe } ;
124-
125- let this = match ptr:: NonNull :: new ( this) {
126- Some ( this) => this,
127- None => {
128- godot_error ! (
129- "gdnative-core: error constructing {}: owner pointer is null" ,
130- C :: class_name( ) ,
131- ) ;
132-
133- return ptr:: null_mut ( ) ;
134- }
135- } ;
136-
137- let owner = match RawObject :: < C :: Base > :: try_from_sys_ref ( this) {
138- Some ( owner) => owner,
139- None => {
140- godot_error ! (
141- "gdnative-core: error constructing {}: incompatible owner type, expecting {}" ,
142- C :: class_name( ) ,
143- C :: Base :: class_name( ) ,
144- ) ;
145- return ptr:: null_mut ( ) ;
146- }
147- } ;
148-
149- let val = match panic:: catch_unwind ( AssertUnwindSafe ( || {
150- emplace:: take ( )
151- . unwrap_or_else ( || C :: init ( TRef :: new ( C :: Base :: cast_ref ( owner) ) ) )
152- } ) ) {
153- Ok ( val) => val,
154- Err ( _) => {
155- godot_error ! (
156- "gdnative-core: error constructing {}: constructor panicked" ,
157- C :: class_name( ) ,
158- ) ;
159- return ptr:: null_mut ( ) ;
160- }
161- } ;
162-
163- let wrapper = C :: UserData :: new ( val) ;
164- C :: UserData :: into_user_data ( wrapper) as * mut _
165- }
166-
167- sys:: godot_instance_create_func {
168- create_func : Some ( constructor :: < C > ) ,
169- method_data : ptr:: null_mut ( ) ,
170- free_func : None ,
171- }
172- } ;
173-
174- let destroy = {
175- unsafe extern "C" fn destructor < C : NativeClass > (
176- _this : * mut sys:: godot_object ,
177- _method_data : * mut libc:: c_void ,
178- user_data : * mut libc:: c_void ,
179- ) {
180- if user_data. is_null ( ) {
181- godot_error ! (
182- "gdnative-core: user data pointer for {} is null (did the constructor fail?)" ,
183- C :: class_name( ) ,
184- ) ;
185- return ;
186- }
187-
188- let wrapper = C :: UserData :: consume_user_data_unchecked ( user_data) ;
189- drop ( wrapper)
190- }
191-
192- sys:: godot_instance_destroy_func {
193- destroy_func : Some ( destructor :: < C > ) ,
194- method_data : ptr:: null_mut ( ) ,
195- free_func : None ,
196- }
197- } ;
198-
199- if is_tool {
200- ( get_api ( ) . godot_nativescript_register_tool_class ) (
201- self . handle as * mut _ ,
202- class_name. as_ptr ( ) as * const _ ,
203- base_name. as_ptr ( ) as * const _ ,
204- create,
205- destroy,
206- ) ;
207- } else {
208- ( get_api ( ) . godot_nativescript_register_class ) (
209- self . handle as * mut _ ,
210- class_name. as_ptr ( ) as * const _ ,
211- base_name. as_ptr ( ) as * const _ ,
212- create,
213- destroy,
214- ) ;
215- }
216-
217- ( get_api ( ) . godot_nativescript_set_type_tag ) (
218- self . handle as * mut _ ,
219- class_name. as_ptr ( ) as * const _ ,
220- crate :: export:: type_tag:: create :: < C > ( ) ,
221- ) ;
222-
223- let builder = ClassBuilder {
224- init_handle : self . handle ,
225- class_name,
226- _marker : PhantomData ,
227- } ;
228-
229- C :: register_properties ( & builder) ;
230-
231- // register methods
232- C :: register ( & builder) ;
233- }
234- }
235- }
236-
23767#[ derive( Debug ) ]
23868pub struct ClassBuilder < C > {
239- pub ( super ) init_handle : * mut libc:: c_void ,
240- pub ( super ) class_name : CString ,
241- _marker : PhantomData < C > ,
69+ pub ( crate ) init_handle : * mut libc:: c_void ,
70+ pub ( crate ) class_name : CString ,
71+ pub ( crate ) _marker : PhantomData < C > ,
24272}
24373
24474impl < C : NativeClass > ClassBuilder < C > {
0 commit comments