Skip to content

Commit

Permalink
Merge init.rs -> private.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Oct 11, 2021
1 parent 056b689 commit 72f645d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 106 deletions.
93 changes: 0 additions & 93 deletions gdnative-core/src/init.rs

This file was deleted.

7 changes: 0 additions & 7 deletions gdnative-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ extern crate approx;
mod macros;

pub mod core_types;
mod init;

#[cfg(feature = "nativescript")]
pub mod nativescript;
Expand All @@ -49,9 +48,3 @@ pub mod object;
/// Internal low-level API for use by macros and generated bindings. Not a part of the public API.
#[doc(hidden)]
pub mod private;

//
// Re-exports
//

pub use init::{InitializeInfo, TerminateInfo};
12 changes: 6 additions & 6 deletions gdnative-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
#[macro_export]
macro_rules! godot_gdnative_init {
() => {
fn godot_gdnative_init_empty(_options: &$crate::InitializeInfo) {}
fn godot_gdnative_init_empty(_options: &$crate::private::InitializeInfo) {}
$crate::godot_gdnative_init!(godot_gdnative_init_empty);
};
(_ as $fn_name:ident) => {
fn godot_gdnative_init_empty(_options: &$crate::InitializeInfo) {}
fn godot_gdnative_init_empty(_options: &$crate::private::InitializeInfo) {}
$crate::godot_gdnative_init!(godot_gdnative_init_empty as $fn_name);
};
($callback:ident) => {
Expand All @@ -38,7 +38,7 @@ macro_rules! godot_gdnative_init {
}

let __result = ::std::panic::catch_unwind(|| {
let callback_options = $crate::InitializeInfo::new(options);
let callback_options = $crate::private::InitializeInfo::new(options);
$callback(&callback_options)
});
if __result.is_err() {
Expand All @@ -64,14 +64,14 @@ macro_rules! godot_gdnative_init {
#[macro_export]
macro_rules! godot_gdnative_terminate {
() => {
fn godot_gdnative_terminate_empty(_term_info: &$crate::TerminateInfo) {}
fn godot_gdnative_terminate_empty(_term_info: &$crate::private::TerminateInfo) {}
$crate::godot_gdnative_terminate!(godot_gdnative_terminate_empty);
};
($callback:ident) => {
$crate::godot_gdnative_terminate!($callback as godot_gdnative_terminate);
};
(_ as $fn_name:ident) => {
fn godot_gdnative_terminate_empty(_term_info: &$crate::TerminateInfo) {}
fn godot_gdnative_terminate_empty(_term_info: &$crate::private::TerminateInfo) {}
$crate::godot_gdnative_terminate!(godot_gdnative_terminate_empty as $fn_name);
};
($callback:ident as $fn_name:ident) => {
Expand All @@ -86,7 +86,7 @@ macro_rules! godot_gdnative_terminate {
}

let __result = ::std::panic::catch_unwind(|| {
let term_info = $crate::TerminateInfo::new(options);
let term_info = $crate::private::TerminateInfo::new(options);
$callback(&term_info)
});
if __result.is_err() {
Expand Down
99 changes: 99 additions & 0 deletions gdnative-core/src/private.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::ffi::CString;

use crate::core_types::GodotString;
use crate::sys;

// ----------------------------------------------------------------------------------------------------------------------------------------------
// Unsafe helpers for sys

static mut GODOT_API: Option<sys::GodotApi> = None;
static mut GDNATIVE_LIBRARY_SYS: Option<*mut sys::godot_object> = None;

Expand Down Expand Up @@ -229,3 +233,98 @@ make_method_table!(struct NativeScriptMethodTable for NativeScript {
set_library,
new,
});

// ----------------------------------------------------------------------------------------------------------------------------------------------
// Helper structs for init/terminate

pub struct TerminateInfo {
in_editor: bool,
}

impl TerminateInfo {
#[inline]
#[doc(hidden)] // avoids clippy warning: unsafe function's docs miss `# Safety` section
pub unsafe fn new(options: *mut crate::sys::godot_gdnative_terminate_options) -> Self {
assert!(!options.is_null(), "options were NULL");

let crate::sys::godot_gdnative_terminate_options { in_editor } = *options;

Self { in_editor }
}

/// Returns `true` if the library is loaded in the Godot Editor.
#[inline]
pub fn in_editor(&self) -> bool {
self.in_editor
}
}

pub struct InitializeInfo {
in_editor: bool,
active_library_path: GodotString,
options: *mut crate::sys::godot_gdnative_init_options,
}

impl InitializeInfo {
/// Returns true if the library is loaded in the Godot Editor.
#[inline]
pub fn in_editor(&self) -> bool {
self.in_editor
}

/// Returns a path to the library relative to the project.
///
/// Example: `res://../../target/debug/libhello_world.dylib`
#[inline]
pub fn active_library_path(&self) -> &GodotString {
&self.active_library_path
}

/// # Safety
///
/// Will `panic!()` if options is NULL or invalid.
#[inline]
#[doc(hidden)]
pub unsafe fn new(options: *mut crate::sys::godot_gdnative_init_options) -> Self {
assert!(!options.is_null(), "options were NULL");
let crate::sys::godot_gdnative_init_options {
in_editor,
active_library_path,
..
} = *options;

let active_library_path =
crate::core_types::GodotString::clone_from_sys(*active_library_path);

Self {
in_editor,
active_library_path,
options,
}
}

#[inline]
pub fn report_loading_error<T>(&self, message: T)
where
T: std::fmt::Display,
{
let crate::sys::godot_gdnative_init_options {
report_loading_error,
gd_native_library,
..
} = unsafe { *self.options };

if let Some(report_loading_error_fn) = report_loading_error {
// Add the trailing zero and convert Display => String
let message = format!("{}\0", message);

// Convert to FFI compatible string
let message = std::ffi::CStr::from_bytes_with_nul(message.as_bytes())
.expect("message should not have a NULL");

unsafe {
report_loading_error_fn(gd_native_library, message.as_ptr());
}
}
}
}

0 comments on commit 72f645d

Please sign in to comment.