Skip to content

Commit 72f645d

Browse files
committed
Merge init.rs -> private.rs
1 parent 056b689 commit 72f645d

File tree

4 files changed

+105
-106
lines changed

4 files changed

+105
-106
lines changed

gdnative-core/src/init.rs

Lines changed: 0 additions & 93 deletions
This file was deleted.

gdnative-core/src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ extern crate approx;
3737
mod macros;
3838

3939
pub mod core_types;
40-
mod init;
4140

4241
#[cfg(feature = "nativescript")]
4342
pub mod nativescript;
@@ -49,9 +48,3 @@ pub mod object;
4948
/// Internal low-level API for use by macros and generated bindings. Not a part of the public API.
5049
#[doc(hidden)]
5150
pub mod private;
52-
53-
//
54-
// Re-exports
55-
//
56-
57-
pub use init::{InitializeInfo, TerminateInfo};

gdnative-core/src/macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#[macro_export]
1717
macro_rules! godot_gdnative_init {
1818
() => {
19-
fn godot_gdnative_init_empty(_options: &$crate::InitializeInfo) {}
19+
fn godot_gdnative_init_empty(_options: &$crate::private::InitializeInfo) {}
2020
$crate::godot_gdnative_init!(godot_gdnative_init_empty);
2121
};
2222
(_ as $fn_name:ident) => {
23-
fn godot_gdnative_init_empty(_options: &$crate::InitializeInfo) {}
23+
fn godot_gdnative_init_empty(_options: &$crate::private::InitializeInfo) {}
2424
$crate::godot_gdnative_init!(godot_gdnative_init_empty as $fn_name);
2525
};
2626
($callback:ident) => {
@@ -38,7 +38,7 @@ macro_rules! godot_gdnative_init {
3838
}
3939

4040
let __result = ::std::panic::catch_unwind(|| {
41-
let callback_options = $crate::InitializeInfo::new(options);
41+
let callback_options = $crate::private::InitializeInfo::new(options);
4242
$callback(&callback_options)
4343
});
4444
if __result.is_err() {
@@ -64,14 +64,14 @@ macro_rules! godot_gdnative_init {
6464
#[macro_export]
6565
macro_rules! godot_gdnative_terminate {
6666
() => {
67-
fn godot_gdnative_terminate_empty(_term_info: &$crate::TerminateInfo) {}
67+
fn godot_gdnative_terminate_empty(_term_info: &$crate::private::TerminateInfo) {}
6868
$crate::godot_gdnative_terminate!(godot_gdnative_terminate_empty);
6969
};
7070
($callback:ident) => {
7171
$crate::godot_gdnative_terminate!($callback as godot_gdnative_terminate);
7272
};
7373
(_ as $fn_name:ident) => {
74-
fn godot_gdnative_terminate_empty(_term_info: &$crate::TerminateInfo) {}
74+
fn godot_gdnative_terminate_empty(_term_info: &$crate::private::TerminateInfo) {}
7575
$crate::godot_gdnative_terminate!(godot_gdnative_terminate_empty as $fn_name);
7676
};
7777
($callback:ident as $fn_name:ident) => {
@@ -86,7 +86,7 @@ macro_rules! godot_gdnative_terminate {
8686
}
8787

8888
let __result = ::std::panic::catch_unwind(|| {
89-
let term_info = $crate::TerminateInfo::new(options);
89+
let term_info = $crate::private::TerminateInfo::new(options);
9090
$callback(&term_info)
9191
});
9292
if __result.is_err() {

gdnative-core/src/private.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use std::ffi::CString;
22

3+
use crate::core_types::GodotString;
34
use crate::sys;
45

6+
// ----------------------------------------------------------------------------------------------------------------------------------------------
7+
// Unsafe helpers for sys
8+
59
static mut GODOT_API: Option<sys::GodotApi> = None;
610
static mut GDNATIVE_LIBRARY_SYS: Option<*mut sys::godot_object> = None;
711

@@ -229,3 +233,98 @@ make_method_table!(struct NativeScriptMethodTable for NativeScript {
229233
set_library,
230234
new,
231235
});
236+
237+
// ----------------------------------------------------------------------------------------------------------------------------------------------
238+
// Helper structs for init/terminate
239+
240+
pub struct TerminateInfo {
241+
in_editor: bool,
242+
}
243+
244+
impl TerminateInfo {
245+
#[inline]
246+
#[doc(hidden)] // avoids clippy warning: unsafe function's docs miss `# Safety` section
247+
pub unsafe fn new(options: *mut crate::sys::godot_gdnative_terminate_options) -> Self {
248+
assert!(!options.is_null(), "options were NULL");
249+
250+
let crate::sys::godot_gdnative_terminate_options { in_editor } = *options;
251+
252+
Self { in_editor }
253+
}
254+
255+
/// Returns `true` if the library is loaded in the Godot Editor.
256+
#[inline]
257+
pub fn in_editor(&self) -> bool {
258+
self.in_editor
259+
}
260+
}
261+
262+
pub struct InitializeInfo {
263+
in_editor: bool,
264+
active_library_path: GodotString,
265+
options: *mut crate::sys::godot_gdnative_init_options,
266+
}
267+
268+
impl InitializeInfo {
269+
/// Returns true if the library is loaded in the Godot Editor.
270+
#[inline]
271+
pub fn in_editor(&self) -> bool {
272+
self.in_editor
273+
}
274+
275+
/// Returns a path to the library relative to the project.
276+
///
277+
/// Example: `res://../../target/debug/libhello_world.dylib`
278+
#[inline]
279+
pub fn active_library_path(&self) -> &GodotString {
280+
&self.active_library_path
281+
}
282+
283+
/// # Safety
284+
///
285+
/// Will `panic!()` if options is NULL or invalid.
286+
#[inline]
287+
#[doc(hidden)]
288+
pub unsafe fn new(options: *mut crate::sys::godot_gdnative_init_options) -> Self {
289+
assert!(!options.is_null(), "options were NULL");
290+
let crate::sys::godot_gdnative_init_options {
291+
in_editor,
292+
active_library_path,
293+
..
294+
} = *options;
295+
296+
let active_library_path =
297+
crate::core_types::GodotString::clone_from_sys(*active_library_path);
298+
299+
Self {
300+
in_editor,
301+
active_library_path,
302+
options,
303+
}
304+
}
305+
306+
#[inline]
307+
pub fn report_loading_error<T>(&self, message: T)
308+
where
309+
T: std::fmt::Display,
310+
{
311+
let crate::sys::godot_gdnative_init_options {
312+
report_loading_error,
313+
gd_native_library,
314+
..
315+
} = unsafe { *self.options };
316+
317+
if let Some(report_loading_error_fn) = report_loading_error {
318+
// Add the trailing zero and convert Display => String
319+
let message = format!("{}\0", message);
320+
321+
// Convert to FFI compatible string
322+
let message = std::ffi::CStr::from_bytes_with_nul(message.as_bytes())
323+
.expect("message should not have a NULL");
324+
325+
unsafe {
326+
report_loading_error_fn(gd_native_library, message.as_ptr());
327+
}
328+
}
329+
}
330+
}

0 commit comments

Comments
 (0)