-
Notifications
You must be signed in to change notification settings - Fork 210
/
init_handle.rs
203 lines (179 loc) · 7.01 KB
/
init_handle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use crate::export::user_data::UserData;
use crate::export::{
class_registry, emplace, ClassBuilder, NativeClass, NativeClassMethods, StaticallyNamed,
};
use crate::object::{GodotObject, RawObject, TRef};
use crate::private::get_api;
use std::borrow::Cow;
use std::ffi::CString;
use std::ptr;
/// A handle that can register new classes to the engine during initialization.
///
/// See [`godot_nativescript_init`](macro.godot_nativescript_init.html) and
/// [`godot_init`](macro.godot_init.html).
#[derive(Copy, Clone)]
pub struct InitHandle {
#[doc(hidden)]
handle: *mut libc::c_void,
}
#[allow(deprecated)] // Remove once init(), register_properties() and register() have been renamed
impl InitHandle {
#[doc(hidden)]
#[inline]
pub unsafe fn new(handle: *mut libc::c_void) -> Self {
InitHandle { handle }
}
/// Registers a new class to the engine.
#[inline]
pub fn add_class<C>(self)
where
C: NativeClassMethods + StaticallyNamed,
{
self.add_maybe_tool_class_as::<C>(Cow::Borrowed(C::CLASS_NAME), false)
}
/// Registers a new tool class to the engine.
#[inline]
pub fn add_tool_class<C>(self)
where
C: NativeClassMethods + StaticallyNamed,
{
self.add_maybe_tool_class_as::<C>(Cow::Borrowed(C::CLASS_NAME), true)
}
/// Registers a new class to the engine
///
/// If the type implements [`StaticallyTyped`], that name is ignored in favor of the
/// name provided at registration.
#[inline]
pub fn add_class_as<C>(self, name: String)
where
C: NativeClassMethods,
{
self.add_maybe_tool_class_as::<C>(Cow::Owned(name), false)
}
/// Registers a new tool class to the engine
///
/// If the type implements [`StaticallyTyped`], that name is ignored in favor of the
/// name provided at registration.
#[inline]
pub fn add_tool_class_as<C>(self, name: String)
where
C: NativeClassMethods,
{
self.add_maybe_tool_class_as::<C>(Cow::Owned(name), true)
}
#[inline]
fn add_maybe_tool_class_as<C>(self, name: Cow<'static, str>, is_tool: bool)
where
C: NativeClassMethods,
{
let c_class_name = CString::new(&*name).unwrap();
if let Some(class_info) = class_registry::register_class_as::<C>(name) {
panic!(
"`{type_name}` has already been registered as `{old_name}`",
type_name = std::any::type_name::<C>(),
old_name = class_info.name,
);
}
unsafe {
let base_name = CString::new(C::Base::class_name()).unwrap();
let create = {
unsafe extern "C" fn constructor<C: NativeClass>(
this: *mut sys::godot_object,
_method_data: *mut libc::c_void,
) -> *mut libc::c_void {
use std::panic::{self, AssertUnwindSafe};
let this = match ptr::NonNull::new(this) {
Some(this) => this,
None => {
godot_error!(
"gdnative-core: error constructing {}: owner pointer is null",
class_registry::class_name_or_default::<C>(),
);
return ptr::null_mut();
}
};
let owner = match RawObject::<C::Base>::try_from_sys_ref(this) {
Some(owner) => owner,
None => {
godot_error!(
"gdnative-core: error constructing {}: incompatible owner type, expecting {}",
class_registry::class_name_or_default::<C>(),
C::Base::class_name(),
);
return ptr::null_mut();
}
};
let val = match panic::catch_unwind(AssertUnwindSafe(|| {
emplace::take().unwrap_or_else(|| {
C::nativeclass_init(TRef::new(C::Base::cast_ref(owner)))
})
})) {
Ok(val) => val,
Err(_) => {
godot_error!(
"gdnative-core: error constructing {}: constructor panicked",
class_registry::class_name_or_default::<C>(),
);
return ptr::null_mut();
}
};
let wrapper = C::UserData::new(val);
C::UserData::into_user_data(wrapper) as *mut _
}
sys::godot_instance_create_func {
create_func: Some(constructor::<C>),
method_data: ptr::null_mut(),
free_func: None,
}
};
let destroy = {
unsafe extern "C" fn destructor<C: NativeClass>(
_this: *mut sys::godot_object,
_method_data: *mut libc::c_void,
user_data: *mut libc::c_void,
) {
if user_data.is_null() {
godot_error!(
"gdnative-core: user data pointer for {} is null (did the constructor fail?)",
class_registry::class_name_or_default::<C>(),
);
return;
}
let wrapper = C::UserData::consume_user_data_unchecked(user_data);
drop(wrapper)
}
sys::godot_instance_destroy_func {
destroy_func: Some(destructor::<C>),
method_data: ptr::null_mut(),
free_func: None,
}
};
if is_tool {
(get_api().godot_nativescript_register_tool_class)(
self.handle as *mut _,
c_class_name.as_ptr() as *const _,
base_name.as_ptr() as *const _,
create,
destroy,
);
} else {
(get_api().godot_nativescript_register_class)(
self.handle as *mut _,
c_class_name.as_ptr() as *const _,
base_name.as_ptr() as *const _,
create,
destroy,
);
}
(get_api().godot_nativescript_set_type_tag)(
self.handle as *mut _,
c_class_name.as_ptr() as *const _,
crate::export::type_tag::create::<C>(),
);
let builder = ClassBuilder::new(self.handle, c_class_name);
C::nativeclass_register_properties(&builder);
// register methods
C::nativeclass_register(&builder);
}
}
}