Skip to content

Commit 1bcb302

Browse files
committed
Implemented rpc modes to be user editable and updated names to follow godot official documentation
1 parent 3883d75 commit 1bcb302

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

gdnative-core/src/nativescript/init.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ pub type ScriptMethodFn = unsafe extern "C" fn(
220220
pub enum RpcMode {
221221
Disabled,
222222
Remote,
223-
Sync,
224-
Mater,
225-
Slave,
223+
RemoteSync,
224+
Master,
225+
Puppet,
226226
}
227227

228228
pub struct ScriptMethodAttributes {
@@ -249,10 +249,17 @@ impl<C: NativeClass> ClassBuilder<C> {
249249
#[inline]
250250
pub fn add_method_advanced(&self, method: ScriptMethod) {
251251
let method_name = CString::new(method.name).unwrap();
252-
let attr = sys::godot_method_attributes {
253-
rpc_type: sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_DISABLED,
252+
253+
let rpc = match method.attributes.rpc_mode {
254+
RpcMode::Master => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_MASTER,
255+
RpcMode::Remote => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_REMOTE,
256+
RpcMode::Puppet => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_SLAVE,
257+
RpcMode::RemoteSync => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_SYNC,
258+
RpcMode::Disabled => sys::godot_method_rpc_mode_GODOT_METHOD_RPC_MODE_DISABLED,
254259
};
255260

261+
let attr = sys::godot_method_attributes { rpc_type: rpc };
262+
256263
let method_desc = sys::godot_instance_method {
257264
method: method.method_ptr,
258265
method_data: method.method_data,
@@ -271,13 +278,19 @@ impl<C: NativeClass> ClassBuilder<C> {
271278
}
272279

273280
#[inline]
274-
pub fn add_method(&self, name: &str, method: ScriptMethodFn) {
281+
pub fn add_method(&self, name: &str, method: ScriptMethodFn, rpc: &str) {
282+
let rpc_mode = match rpc {
283+
"remote" => RpcMode::Remote,
284+
"remotesync" => RpcMode::RemoteSync,
285+
"master" => RpcMode::Master,
286+
"puppet" => RpcMode::Puppet,
287+
_ => RpcMode::Disabled,
288+
};
289+
275290
self.add_method_advanced(ScriptMethod {
276291
name,
277292
method_ptr: Some(method),
278-
attributes: ScriptMethodAttributes {
279-
rpc_mode: RpcMode::Disabled,
280-
},
293+
attributes: ScriptMethodAttributes { rpc_mode: rpc_mode },
281294
method_data: ptr::null_mut(),
282295
free_func: None,
283296
});

gdnative-derive/src/methods.rs

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub(crate) struct ClassMethodExport {
1212
pub(crate) struct ExportMethod {
1313
pub(crate) sig: Signature,
1414
pub(crate) args: ExportArgs,
15+
pub(crate) rpc: String,
1516
}
1617

1718
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
@@ -33,7 +34,7 @@ pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStre
3334
let methods = export
3435
.methods
3536
.into_iter()
36-
.map(|ExportMethod { sig, args }| {
37+
.map(|ExportMethod { sig, args, rpc }| {
3738
let sig_span = sig.ident.span();
3839

3940
let name = sig.ident;
@@ -85,7 +86,7 @@ pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStre
8586
fn #name ( #( #args )* ) -> #ret_ty
8687
);
8788

88-
#builder.add_method(#name_string, method);
89+
#builder.add_method(#name_string, method, #rpc);
8990
}
9091
)
9192
})
@@ -153,6 +154,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
153154
let items = match func {
154155
ImplItem::Method(mut method) => {
155156
let mut export_args = None;
157+
let mut rpc = "disabled";
156158

157159
let mut errors = vec![];
158160

@@ -218,7 +220,12 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
218220
}
219221
};
220222

221-
for MetaNameValue { path, .. } in pairs {
223+
for MetaNameValue {
224+
path,
225+
eq_token: _,
226+
lit,
227+
} in pairs
228+
{
222229
let last = match path.segments.last() {
223230
Some(val) => val,
224231
None => {
@@ -229,9 +236,58 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
229236
return false;
230237
}
231238
};
232-
let unexpected = last.ident.to_string();
233-
let msg =
234-
format!("unknown option for export: `{}`", unexpected);
239+
let path = last.ident.to_string();
240+
241+
// Match rpc mode
242+
match path.as_str() {
243+
"rpc" => {
244+
let value = if let syn::Lit::Str(lit_str) = lit {
245+
lit_str.value()
246+
} else {
247+
errors.push(syn::Error::new(
248+
last.span(),
249+
"unexpected type for rpc value, expected Str",
250+
));
251+
return false;
252+
};
253+
254+
match value.as_str() {
255+
"remote" => {
256+
rpc = "remote";
257+
return false;
258+
}
259+
"remotesync" => {
260+
rpc = "remotesync";
261+
return false;
262+
}
263+
"master" => {
264+
rpc = "master";
265+
return false;
266+
}
267+
"puppet" => {
268+
rpc = "puppet";
269+
return false;
270+
}
271+
"disabled" => {
272+
rpc = "disabled";
273+
return false;
274+
}
275+
_ => {
276+
errors.push(syn::Error::new(
277+
last.span(),
278+
format!(
279+
"unexpected value for rpc: {}",
280+
value
281+
),
282+
));
283+
return false;
284+
}
285+
}
286+
}
287+
_ => (),
288+
}
289+
290+
let msg = format!("unknown option for export: `{}`", path);
235291
errors.push(syn::Error::new(last.span(), msg));
236292
}
237293
}
@@ -287,6 +343,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
287343
methods_to_export.push(ExportMethod {
288344
sig: method.sig.clone(),
289345
args: export_args,
346+
rpc: rpc.to_string(),
290347
});
291348
}
292349

0 commit comments

Comments
 (0)