Skip to content

Commit 3c1531d

Browse files
committed
Used correct enums for rpc frome headers,
add_method_with_rpc mode added, removed multiple parsings of rpc mode fro m attribute, various syntax updates
1 parent a3f5437 commit 3c1531d

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

gdnative-core/src/nativescript/init.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ impl<C: NativeClass> ClassBuilder<C> {
255255
let rpc = match method.attributes.rpc_mode {
256256
RpcMode::Master => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_MASTER,
257257
RpcMode::Remote => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_REMOTE,
258-
RpcMode::Puppet => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_SLAVE,
259-
RpcMode::RemoteSync => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_SYNC,
258+
RpcMode::Puppet => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_PUPPET,
259+
RpcMode::RemoteSync => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_REMOTESYNC,
260260
RpcMode::Disabled => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_DISABLED,
261261
RpcMode::MasterSync => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_MASTERSYNC,
262262
RpcMode::PuppetSync => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_PUPPETSYNC,
@@ -282,21 +282,24 @@ impl<C: NativeClass> ClassBuilder<C> {
282282
}
283283

284284
#[inline]
285-
pub fn add_method(&self, name: &str, method: ScriptMethodFn, rpc: &str) {
286-
let rpc_mode = match rpc {
287-
"remote" => RpcMode::Remote,
288-
"remotesync" => RpcMode::RemoteSync,
289-
"master" => RpcMode::Master,
290-
"puppet" => RpcMode::Puppet,
291-
"puppetsync" => RpcMode::PuppetSync,
292-
"mastersync" => RpcMode::MasterSync,
293-
_ => RpcMode::Disabled,
294-
};
285+
pub fn add_method_with_rpc_mode(&self, name: &str, method: ScriptMethodFn, rpc_mode: RpcMode) {
286+
self.add_method_advanced(ScriptMethod {
287+
name,
288+
method_ptr: Some(method),
289+
attributes: ScriptMethodAttributes { rpc_mode },
290+
method_data: ptr::null_mut(),
291+
free_func: None,
292+
});
293+
}
295294

295+
#[inline]
296+
pub fn add_method(&self, name: &str, method: ScriptMethodFn) {
296297
self.add_method_advanced(ScriptMethod {
297298
name,
298299
method_ptr: Some(method),
299-
attributes: ScriptMethodAttributes { rpc_mode: rpc_mode },
300+
attributes: ScriptMethodAttributes {
301+
rpc_mode: RpcMode::Disabled,
302+
},
300303
method_data: ptr::null_mut(),
301304
free_func: None,
302305
});

gdnative-derive/src/methods.rs

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
use syn::{spanned::Spanned, FnArg, ImplItem, ItemImpl, Pat, PatIdent, Signature, Type};
22

33
use proc_macro::TokenStream;
4+
use quote::{quote, ToTokens};
45
use std::boxed::Box;
56

7+
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
8+
pub enum RpcMode {
9+
Disabled,
10+
Remote,
11+
RemoteSync,
12+
Master,
13+
Puppet,
14+
MasterSync,
15+
PuppetSync,
16+
}
17+
18+
impl Default for RpcMode {
19+
fn default() -> Self {
20+
RpcMode::Disabled
21+
}
22+
}
23+
24+
impl ToTokens for RpcMode {
25+
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
26+
match self {
27+
RpcMode::Disabled => tokens.extend(quote!(RpcMode::Disabled)),
28+
RpcMode::Remote => tokens.extend(quote!(RpcMode::Remote)),
29+
RpcMode::RemoteSync => tokens.extend(quote!(RpcMode::RemoteSync)),
30+
RpcMode::Master => tokens.extend(quote!(RpcMode::Master)),
31+
RpcMode::Puppet => tokens.extend(quote!(RpcMode::Puppet)),
32+
RpcMode::MasterSync => tokens.extend(quote!(RpcMode::MasterSync)),
33+
RpcMode::PuppetSync => tokens.extend(quote!(RpcMode::PuppetSync)),
34+
}
35+
}
36+
}
37+
638
pub(crate) struct ClassMethodExport {
739
pub(crate) class_ty: Box<Type>,
840
pub(crate) methods: Vec<ExportMethod>,
@@ -12,12 +44,12 @@ pub(crate) struct ClassMethodExport {
1244
pub(crate) struct ExportMethod {
1345
pub(crate) sig: Signature,
1446
pub(crate) args: ExportArgs,
15-
pub(crate) rpc: String,
1647
}
1748

1849
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
1950
pub(crate) struct ExportArgs {
2051
pub(crate) optional_args: Option<usize>,
52+
pub(crate) rpc_mode: RpcMode,
2153
}
2254

2355
pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStream {
@@ -34,7 +66,7 @@ pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStre
3466
let methods = export
3567
.methods
3668
.into_iter()
37-
.map(|ExportMethod { sig, args, rpc }| {
69+
.map(|ExportMethod { sig, args }| {
3870
let sig_span = sig.ident.span();
3971

4072
let name = sig.ident;
@@ -70,6 +102,8 @@ pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStre
70102
None => 0,
71103
};
72104

105+
let rpc = args.rpc_mode;
106+
73107
let args = sig.inputs.iter().enumerate().map(|(n, arg)| {
74108
let span = arg.span();
75109
if n < arg_count - optional_args {
@@ -86,7 +120,7 @@ pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStre
86120
fn #name ( #( #args )* ) -> #ret_ty
87121
);
88122

89-
#builder.add_method(#name_string, method, #rpc);
123+
#builder.add_method_with_rpc_mode(#name_string, method, #rpc);
90124
}
91125
)
92126
})
@@ -154,7 +188,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
154188
let items = match func {
155189
ImplItem::Method(mut method) => {
156190
let mut export_args = None;
157-
let mut rpc = "disabled";
191+
let mut rpc = RpcMode::Disabled;
158192

159193
let mut errors = vec![];
160194

@@ -176,7 +210,6 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
176210
if let Some("export") = last_seg.as_deref() {
177211
let _export_args = export_args.get_or_insert_with(ExportArgs::default);
178212
if !attr.tokens.is_empty() {
179-
use quote::ToTokens;
180213
use syn::{Meta, MetaNameValue, NestedMeta};
181214

182215
let meta = match attr.parse_meta() {
@@ -253,31 +286,31 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
253286

254287
match value.as_str() {
255288
"remote" => {
256-
rpc = "remote";
289+
rpc = RpcMode::Remote;
257290
return false;
258291
}
259-
"remotesync" => {
260-
rpc = "remotesync";
292+
"remote_sync" => {
293+
rpc = RpcMode::RemoteSync;
261294
return false;
262295
}
263296
"master" => {
264-
rpc = "master";
297+
rpc = RpcMode::Master;
265298
return false;
266299
}
267300
"puppet" => {
268-
rpc = "puppet";
301+
rpc = RpcMode::Puppet;
269302
return false;
270303
}
271304
"disabled" => {
272-
rpc = "disabled";
305+
rpc = RpcMode::Disabled;
273306
return false;
274307
}
275-
"mastersync" => {
276-
rpc = "mastersync";
308+
"master_sync" => {
309+
rpc = RpcMode::MasterSync;
277310
return false;
278311
}
279-
"puppetsync" => {
280-
rpc = "puppetsync";
312+
"puppet_sync" => {
313+
rpc = RpcMode::PuppetSync;
281314
return false;
282315
}
283316
_ => {
@@ -347,11 +380,11 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
347380
}
348381

349382
export_args.optional_args = optional_args;
383+
export_args.rpc_mode = rpc;
350384

351385
methods_to_export.push(ExportMethod {
352386
sig: method.sig.clone(),
353387
args: export_args,
354-
rpc: rpc.to_string(),
355388
});
356389
}
357390

0 commit comments

Comments
 (0)