Skip to content

Commit 25f108f

Browse files
bors[bot]chitoyuu
andauthored
Merge #1026
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 <chitoyuu@potatoes.gay>
2 parents 24b98be + bdd6ecd commit 25f108f

File tree

19 files changed

+586
-238
lines changed

19 files changed

+586
-238
lines changed

examples/builder-export/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ impl ExportsArrays {
5757
}
5858
}
5959

60-
fn init(handle: InitHandle) {
61-
handle.add_class::<ExportsArrays>();
62-
}
60+
struct BuilderExportLibrary;
6361

64-
godot_init!(init);
62+
#[gdnative::init::callbacks]
63+
impl GDNativeCallbacks for BuilderExportLibrary {
64+
fn nativescript_init(handle: InitHandle) {
65+
handle.add_class::<ExportsArrays>();
66+
}
67+
}

examples/dodge-the-creeps/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ mod main_scene;
55
mod mob;
66
mod player;
77

8-
fn init(handle: InitHandle) {
9-
handle.add_class::<player::Player>();
10-
handle.add_class::<mob::Mob>();
11-
handle.add_class::<main_scene::Main>();
12-
handle.add_class::<hud::Hud>();
13-
}
8+
struct DtcLibrary;
149

15-
godot_init!(init);
10+
#[gdnative::init::callbacks]
11+
impl GDNativeCallbacks for DtcLibrary {
12+
fn nativescript_init(handle: InitHandle) {
13+
handle.add_class::<player::Player>();
14+
handle.add_class::<mob::Mob>();
15+
handle.add_class::<main_scene::Main>();
16+
handle.add_class::<hud::Hud>();
17+
}
18+
}

examples/godot_tps_controller_port/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ use gdnative::prelude::*;
22

33
mod player;
44

5-
fn init(handle: InitHandle) {
6-
handle.add_class::<player::Player>();
7-
}
5+
struct TpsLibrary;
86

9-
godot_init!(init);
7+
#[gdnative::init::callbacks]
8+
impl GDNativeCallbacks for TpsLibrary {
9+
fn nativescript_init(handle: InitHandle) {
10+
handle.add_class::<player::Player>();
11+
}
12+
}

examples/hello-world/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ impl HelloWorld {
1616
}
1717
}
1818

19-
fn init(handle: InitHandle) {
20-
handle.add_class::<HelloWorld>();
21-
}
19+
struct HelloWorldLibrary;
2220

23-
godot_init!(init);
21+
#[gdnative::init::callbacks]
22+
impl GDNativeCallbacks for HelloWorldLibrary {
23+
fn nativescript_init(handle: InitHandle) {
24+
handle.add_class::<HelloWorld>();
25+
}
26+
}

examples/native-plugin/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ impl MyButton {
5454
}
5555
}
5656

57-
fn init(handle: InitHandle) {
58-
handle.add_tool_class::<CustomNode>();
59-
handle.add_tool_class::<MyButton>();
60-
}
57+
struct PluginLibrary;
6158

62-
godot_init!(init);
59+
#[gdnative::init::callbacks]
60+
impl GDNativeCallbacks for PluginLibrary {
61+
fn nativescript_init(handle: InitHandle) {
62+
handle.add_tool_class::<CustomNode>();
63+
handle.add_tool_class::<MyButton>();
64+
}
65+
}

examples/property-export/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ impl PropertyExport {
4141
}
4242
}
4343

44-
fn init(handle: InitHandle) {
45-
handle.add_class::<PropertyExport>();
46-
}
44+
struct PropertyExportLibrary;
4745

48-
godot_init!(init);
46+
#[gdnative::init::callbacks]
47+
impl GDNativeCallbacks for PropertyExportLibrary {
48+
fn nativescript_init(handle: InitHandle) {
49+
handle.add_class::<PropertyExport>();
50+
}
51+
}

examples/resource/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ impl Greeter {
4646
}
4747
}
4848

49-
fn init(handle: InitHandle) {
50-
handle.add_class::<Greeter>();
51-
handle.add_class::<GreetingResource>();
52-
}
49+
struct ResourceLibrary;
5350

54-
godot_init!(init);
51+
#[gdnative::init::callbacks]
52+
impl GDNativeCallbacks for ResourceLibrary {
53+
fn nativescript_init(handle: InitHandle) {
54+
handle.add_class::<Greeter>();
55+
handle.add_class::<GreetingResource>();
56+
}
57+
}

examples/rpc/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ use gdnative::prelude::*;
33
mod client;
44
mod server;
55

6-
fn init(handle: InitHandle) {
7-
handle.add_class::<client::ServerPuppet>();
8-
handle.add_class::<server::Server>();
9-
}
6+
struct RpcLibrary;
107

11-
godot_init!(init);
8+
#[gdnative::init::callbacks]
9+
impl GDNativeCallbacks for RpcLibrary {
10+
fn nativescript_init(handle: InitHandle) {
11+
handle.add_class::<client::ServerPuppet>();
12+
handle.add_class::<server::Server>();
13+
}
14+
}

examples/scene-create/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ impl SceneCreate {
100100
}
101101
}
102102

103-
fn init(handle: InitHandle) {
104-
handle.add_class::<SceneCreate>();
105-
}
106-
107103
pub fn load_scene(path: &str) -> Option<Ref<PackedScene, ThreadLocal>> {
108104
let scene = load::<PackedScene>(path)?;
109105
let scene = unsafe { scene.assume_thread_local() };
@@ -151,4 +147,11 @@ fn update_panel(owner: &Spatial, num_children: i64) {
151147
}
152148
}
153149

154-
godot_init!(init);
150+
struct SceneCreateLibrary;
151+
152+
#[gdnative::init::callbacks]
153+
impl GDNativeCallbacks for SceneCreateLibrary {
154+
fn nativescript_init(handle: InitHandle) {
155+
handle.add_class::<SceneCreate>();
156+
}
157+
}

examples/signals/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ impl SignalSubscriber {
9595
}
9696
}
9797

98-
fn init(handle: InitHandle) {
99-
handle.add_class::<SignalEmitter>();
100-
handle.add_class::<SignalSubscriber>();
101-
}
98+
struct SignalLibrary;
10299

103-
godot_init!(init);
100+
#[gdnative::init::callbacks]
101+
impl GDNativeCallbacks for SignalLibrary {
102+
fn nativescript_init(handle: InitHandle) {
103+
handle.add_class::<SignalEmitter>();
104+
handle.add_class::<SignalSubscriber>();
105+
}
106+
}

0 commit comments

Comments
 (0)