Skip to content

Commit

Permalink
Merge #1026
Browse files Browse the repository at this point in the history
1026: Implement trait-based entry point declaration r=chitoyuu a=chitoyuu

Adds the `#[gdnative::init::callbacks]` attribute macro and `GDNativeCallbacks` trait for entry point declaration, replacing the old system of init macros. `godot_init!` is kept as a shim and deprecated.

This also adds support for the following callbacks:

- `gdnative_singleton`
- `nativescript_terminate`
- `nativescript_frame`
- `nativescript_thread_enter`
- `nativescript_thread_exit`

As a side effect, most of the init boilerplate are no longer emitted into the user crate, instead staying in `gdnative-core`. Some further internal refactoring might be necessary to improve code organization.

Close #985, Close #338

Co-authored-by: Chitose Yuuzaki <[email protected]>
  • Loading branch information
bors[bot] and chitoyuu authored Feb 6, 2023
2 parents 24b98be + bdd6ecd commit 25f108f
Show file tree
Hide file tree
Showing 19 changed files with 586 additions and 238 deletions.
11 changes: 7 additions & 4 deletions examples/builder-export/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ impl ExportsArrays {
}
}

fn init(handle: InitHandle) {
handle.add_class::<ExportsArrays>();
}
struct BuilderExportLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for BuilderExportLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<ExportsArrays>();
}
}
17 changes: 10 additions & 7 deletions examples/dodge-the-creeps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ mod main_scene;
mod mob;
mod player;

fn init(handle: InitHandle) {
handle.add_class::<player::Player>();
handle.add_class::<mob::Mob>();
handle.add_class::<main_scene::Main>();
handle.add_class::<hud::Hud>();
}
struct DtcLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for DtcLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<player::Player>();
handle.add_class::<mob::Mob>();
handle.add_class::<main_scene::Main>();
handle.add_class::<hud::Hud>();
}
}
11 changes: 7 additions & 4 deletions examples/godot_tps_controller_port/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use gdnative::prelude::*;

mod player;

fn init(handle: InitHandle) {
handle.add_class::<player::Player>();
}
struct TpsLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for TpsLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<player::Player>();
}
}
11 changes: 7 additions & 4 deletions examples/hello-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ impl HelloWorld {
}
}

fn init(handle: InitHandle) {
handle.add_class::<HelloWorld>();
}
struct HelloWorldLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for HelloWorldLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<HelloWorld>();
}
}
13 changes: 8 additions & 5 deletions examples/native-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ impl MyButton {
}
}

fn init(handle: InitHandle) {
handle.add_tool_class::<CustomNode>();
handle.add_tool_class::<MyButton>();
}
struct PluginLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for PluginLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_tool_class::<CustomNode>();
handle.add_tool_class::<MyButton>();
}
}
11 changes: 7 additions & 4 deletions examples/property-export/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ impl PropertyExport {
}
}

fn init(handle: InitHandle) {
handle.add_class::<PropertyExport>();
}
struct PropertyExportLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for PropertyExportLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<PropertyExport>();
}
}
13 changes: 8 additions & 5 deletions examples/resource/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ impl Greeter {
}
}

fn init(handle: InitHandle) {
handle.add_class::<Greeter>();
handle.add_class::<GreetingResource>();
}
struct ResourceLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for ResourceLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<Greeter>();
handle.add_class::<GreetingResource>();
}
}
13 changes: 8 additions & 5 deletions examples/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use gdnative::prelude::*;
mod client;
mod server;

fn init(handle: InitHandle) {
handle.add_class::<client::ServerPuppet>();
handle.add_class::<server::Server>();
}
struct RpcLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for RpcLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<client::ServerPuppet>();
handle.add_class::<server::Server>();
}
}
13 changes: 8 additions & 5 deletions examples/scene-create/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ impl SceneCreate {
}
}

fn init(handle: InitHandle) {
handle.add_class::<SceneCreate>();
}

pub fn load_scene(path: &str) -> Option<Ref<PackedScene, ThreadLocal>> {
let scene = load::<PackedScene>(path)?;
let scene = unsafe { scene.assume_thread_local() };
Expand Down Expand Up @@ -151,4 +147,11 @@ fn update_panel(owner: &Spatial, num_children: i64) {
}
}

godot_init!(init);
struct SceneCreateLibrary;

#[gdnative::init::callbacks]
impl GDNativeCallbacks for SceneCreateLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<SceneCreate>();
}
}
13 changes: 8 additions & 5 deletions examples/signals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ impl SignalSubscriber {
}
}

fn init(handle: InitHandle) {
handle.add_class::<SignalEmitter>();
handle.add_class::<SignalSubscriber>();
}
struct SignalLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for SignalLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<SignalEmitter>();
handle.add_class::<SignalSubscriber>();
}
}
11 changes: 7 additions & 4 deletions examples/spinning-cube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ impl RustTest {
}
}

fn init(handle: InitHandle) {
handle.add_class::<RustTest>();
}
struct CubeLibrary;

godot_init!(init);
#[gdnative::init::callbacks]
impl GDNativeCallbacks for CubeLibrary {
fn nativescript_init(handle: InitHandle) {
handle.add_class::<RustTest>();
}
}
Loading

0 comments on commit 25f108f

Please sign in to comment.