Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/multiplayer/scene_rpc_interface.cpp
11351 views
1
/**************************************************************************/
2
/* scene_rpc_interface.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "scene_rpc_interface.h"
32
33
#include "scene_multiplayer.h"
34
35
#include "core/debugger/engine_debugger.h"
36
#include "core/io/marshalls.h"
37
#include "scene/main/multiplayer_api.h"
38
#include "scene/main/node.h"
39
#include "scene/main/window.h"
40
41
// The RPC meta is composed by a single byte that contains (starting from the least significant bit):
42
// - `NetworkCommands` in the first four bits.
43
// - `NetworkNodeIdCompression` in the next 2 bits.
44
// - `NetworkNameIdCompression` in the next 1 bit.
45
// - `byte_only_or_no_args` in the next 1 bit.
46
#define NODE_ID_COMPRESSION_SHIFT SceneMultiplayer::CMD_FLAG_0_SHIFT
47
#define NAME_ID_COMPRESSION_SHIFT SceneMultiplayer::CMD_FLAG_2_SHIFT
48
#define BYTE_ONLY_OR_NO_ARGS_SHIFT SceneMultiplayer::CMD_FLAG_3_SHIFT
49
50
#define NODE_ID_COMPRESSION_FLAG ((1 << NODE_ID_COMPRESSION_SHIFT) | (1 << (NODE_ID_COMPRESSION_SHIFT + 1)))
51
#define NAME_ID_COMPRESSION_FLAG (1 << NAME_ID_COMPRESSION_SHIFT)
52
#define BYTE_ONLY_OR_NO_ARGS_FLAG (1 << BYTE_ONLY_OR_NO_ARGS_SHIFT)
53
54
#ifdef DEBUG_ENABLED
55
_FORCE_INLINE_ void SceneRPCInterface::_profile_node_data(const String &p_what, ObjectID p_id, int p_size) {
56
if (EngineDebugger::is_profiling("multiplayer:rpc")) {
57
Array values = { p_what, p_id, p_size };
58
EngineDebugger::profiler_add_frame_data("multiplayer:rpc", values);
59
}
60
}
61
#endif
62
63
// Returns the packet size stripping the node path added when the node is not yet cached.
64
int get_packet_len(uint32_t p_node_target, int p_packet_len) {
65
if (p_node_target & 0x80000000) {
66
int ofs = p_node_target & 0x7FFFFFFF;
67
return p_packet_len - (p_packet_len - ofs);
68
} else {
69
return p_packet_len;
70
}
71
}
72
73
void SceneRPCInterface::_parse_rpc_config(const Variant &p_config, bool p_for_node, RPCConfigCache &r_cache) {
74
if (p_config.get_type() == Variant::NIL) {
75
return;
76
}
77
ERR_FAIL_COND(p_config.get_type() != Variant::DICTIONARY);
78
const Dictionary config = p_config;
79
Array names = config.keys();
80
names.sort_custom(callable_mp_static(&StringLikeVariantOrder::compare)); // Ensure ID order
81
for (int i = 0; i < names.size(); i++) {
82
ERR_CONTINUE(!names[i].is_string());
83
String name = names[i].operator String();
84
ERR_CONTINUE(config[name].get_type() != Variant::DICTIONARY);
85
ERR_CONTINUE(!config[name].operator Dictionary().has("rpc_mode"));
86
Dictionary dict = config[name];
87
RPCConfig cfg;
88
cfg.name = name;
89
cfg.rpc_mode = ((MultiplayerAPI::RPCMode)dict.get("rpc_mode", MultiplayerAPI::RPC_MODE_AUTHORITY).operator int());
90
cfg.transfer_mode = ((MultiplayerPeer::TransferMode)dict.get("transfer_mode", MultiplayerPeer::TRANSFER_MODE_RELIABLE).operator int());
91
cfg.call_local = dict.get("call_local", false).operator bool();
92
cfg.channel = dict.get("channel", 0).operator int();
93
uint16_t id = ((uint16_t)i);
94
if (p_for_node) {
95
id |= (1 << 15);
96
}
97
r_cache.configs[id] = cfg;
98
r_cache.ids[name] = id;
99
}
100
}
101
102
const SceneRPCInterface::RPCConfigCache &SceneRPCInterface::_get_node_config(const Node *p_node) {
103
const ObjectID oid = p_node->get_instance_id();
104
if (rpc_cache.has(oid)) {
105
return rpc_cache[oid];
106
}
107
RPCConfigCache cache;
108
_parse_rpc_config(p_node->get_node_rpc_config(), true, cache);
109
if (p_node->get_script_instance()) {
110
_parse_rpc_config(p_node->get_script_instance()->get_rpc_config(), false, cache);
111
}
112
rpc_cache[oid] = cache;
113
return rpc_cache[oid];
114
}
115
116
String SceneRPCInterface::get_rpc_md5(const Object *p_obj) {
117
const Node *node = Object::cast_to<Node>(p_obj);
118
ERR_FAIL_NULL_V(node, "");
119
const RPCConfigCache cache = _get_node_config(node);
120
String rpc_list;
121
for (const KeyValue<uint16_t, RPCConfig> &config : cache.configs) {
122
rpc_list += String(config.value.name);
123
}
124
return rpc_list.md5_text();
125
}
126
127
Node *SceneRPCInterface::_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len) {
128
Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
129
ERR_FAIL_NULL_V(root_node, nullptr);
130
Node *node = nullptr;
131
132
if (p_node_target & 0x80000000) {
133
// Use full path (not cached yet).
134
int ofs = p_node_target & 0x7FFFFFFF;
135
136
ERR_FAIL_COND_V_MSG(ofs >= p_packet_len, nullptr, "Invalid packet received. Size smaller than declared.");
137
138
String paths = String::utf8((const char *)&p_packet[ofs], p_packet_len - ofs);
139
140
NodePath np = paths;
141
142
node = root_node->get_node(np);
143
144
if (!node) {
145
ERR_PRINT("Failed to get path from RPC: " + String(np) + ".");
146
}
147
return node;
148
} else {
149
// Use cached path.
150
return Object::cast_to<Node>(multiplayer_cache->get_cached_object(p_from, p_node_target));
151
}
152
}
153
154
void SceneRPCInterface::process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len) {
155
// Extract packet meta
156
int packet_min_size = 1;
157
int name_id_offset = 1;
158
ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small.");
159
// Compute the meta size, which depends on the compression level.
160
int node_id_compression = (p_packet[0] & NODE_ID_COMPRESSION_FLAG) >> NODE_ID_COMPRESSION_SHIFT;
161
int name_id_compression = (p_packet[0] & NAME_ID_COMPRESSION_FLAG) >> NAME_ID_COMPRESSION_SHIFT;
162
163
switch (node_id_compression) {
164
case NETWORK_NODE_ID_COMPRESSION_8:
165
packet_min_size += 1;
166
name_id_offset += 1;
167
break;
168
case NETWORK_NODE_ID_COMPRESSION_16:
169
packet_min_size += 2;
170
name_id_offset += 2;
171
break;
172
case NETWORK_NODE_ID_COMPRESSION_32:
173
packet_min_size += 4;
174
name_id_offset += 4;
175
break;
176
default:
177
ERR_FAIL_MSG("Was not possible to extract the node id compression mode.");
178
}
179
switch (name_id_compression) {
180
case NETWORK_NAME_ID_COMPRESSION_8:
181
packet_min_size += 1;
182
break;
183
case NETWORK_NAME_ID_COMPRESSION_16:
184
packet_min_size += 2;
185
break;
186
default:
187
ERR_FAIL_MSG("Was not possible to extract the name id compression mode.");
188
}
189
ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small.");
190
191
uint32_t node_target = 0;
192
switch (node_id_compression) {
193
case NETWORK_NODE_ID_COMPRESSION_8:
194
node_target = p_packet[1];
195
break;
196
case NETWORK_NODE_ID_COMPRESSION_16:
197
node_target = decode_uint16(p_packet + 1);
198
break;
199
case NETWORK_NODE_ID_COMPRESSION_32:
200
node_target = decode_uint32(p_packet + 1);
201
break;
202
default:
203
// Unreachable, checked before.
204
CRASH_NOW();
205
}
206
207
Node *node = _process_get_node(p_from, p_packet, node_target, p_packet_len);
208
ERR_FAIL_NULL_MSG(node, "Invalid packet received. Requested node was not found.");
209
210
uint16_t name_id = 0;
211
switch (name_id_compression) {
212
case NETWORK_NAME_ID_COMPRESSION_8:
213
name_id = p_packet[name_id_offset];
214
break;
215
case NETWORK_NAME_ID_COMPRESSION_16:
216
name_id = decode_uint16(p_packet + name_id_offset);
217
break;
218
default:
219
// Unreachable, checked before.
220
CRASH_NOW();
221
}
222
223
const int packet_len = get_packet_len(node_target, p_packet_len);
224
_process_rpc(node, name_id, p_from, p_packet, packet_len, packet_min_size);
225
}
226
227
static String _get_rpc_mode_string(MultiplayerAPI::RPCMode p_mode) {
228
switch (p_mode) {
229
case MultiplayerAPI::RPC_MODE_DISABLED:
230
return "disabled";
231
case MultiplayerAPI::RPC_MODE_ANY_PEER:
232
return "any_peer";
233
case MultiplayerAPI::RPC_MODE_AUTHORITY:
234
return "authority";
235
}
236
ERR_FAIL_V_MSG(String(), "Invalid RPC mode.");
237
}
238
239
void SceneRPCInterface::_process_rpc(Node *p_node, const uint16_t p_rpc_method_id, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) {
240
ERR_FAIL_COND_MSG(p_offset > p_packet_len, "Invalid packet received. Size too small.");
241
242
// Check that remote can call the RPC on this node.
243
const RPCConfigCache &cache_config = _get_node_config(p_node);
244
ERR_FAIL_COND(!cache_config.configs.has(p_rpc_method_id));
245
const RPCConfig &config = cache_config.configs[p_rpc_method_id];
246
247
bool can_call = false;
248
switch (config.rpc_mode) {
249
case MultiplayerAPI::RPC_MODE_DISABLED: {
250
can_call = false;
251
} break;
252
case MultiplayerAPI::RPC_MODE_ANY_PEER: {
253
can_call = true;
254
} break;
255
case MultiplayerAPI::RPC_MODE_AUTHORITY: {
256
can_call = p_from == p_node->get_multiplayer_authority();
257
} break;
258
}
259
260
ERR_FAIL_COND_MSG(!can_call, "RPC '" + String(config.name) + "' is not allowed on node " + String(p_node->get_path()) + " from: " + itos(p_from) + ". Mode is \"" + _get_rpc_mode_string(config.rpc_mode) + "\", authority is " + itos(p_node->get_multiplayer_authority()) + ".");
261
262
int argc = 0;
263
264
const bool byte_only_or_no_args = p_packet[0] & BYTE_ONLY_OR_NO_ARGS_FLAG;
265
if (byte_only_or_no_args) {
266
if (p_offset < p_packet_len) {
267
// This packet contains only bytes.
268
argc = 1;
269
}
270
} else {
271
// Normal variant, takes the argument count from the packet.
272
ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small.");
273
argc = p_packet[p_offset];
274
p_offset += 1;
275
}
276
277
Vector<Variant> args;
278
Vector<const Variant *> argp;
279
args.resize(argc);
280
argp.resize(argc);
281
282
#ifdef DEBUG_ENABLED
283
_profile_node_data("rpc_in", p_node->get_instance_id(), p_packet_len);
284
#endif
285
286
int out;
287
MultiplayerAPI::decode_and_decompress_variants(args, &p_packet[p_offset], p_packet_len - p_offset, out, byte_only_or_no_args, multiplayer->is_object_decoding_allowed());
288
for (int i = 0; i < argc; i++) {
289
argp.write[i] = &args[i];
290
}
291
292
Callable::CallError ce;
293
294
p_node->callp(config.name, (const Variant **)argp.ptr(), argc, ce);
295
if (ce.error != Callable::CallError::CALL_OK) {
296
String error = Variant::get_call_error_text(p_node, config.name, (const Variant **)argp.ptr(), argc, ce);
297
error = "RPC - " + error;
298
ERR_PRINT(error);
299
}
300
}
301
302
void SceneRPCInterface::_send_rpc(Node *p_node, int p_to, uint16_t p_rpc_id, const RPCConfig &p_config, const StringName &p_name, const Variant **p_arg, int p_argcount) {
303
Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer();
304
ERR_FAIL_COND_MSG(peer.is_null(), "Attempt to call RPC without active multiplayer peer.");
305
306
ERR_FAIL_COND_MSG(peer->get_connection_status() == MultiplayerPeer::CONNECTION_CONNECTING, "Attempt to call RPC while multiplayer peer is not connected yet.");
307
308
ERR_FAIL_COND_MSG(peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED, "Attempt to call RPC while multiplayer peer is disconnected.");
309
310
ERR_FAIL_COND_MSG(p_argcount > 255, "Too many arguments (>255).");
311
312
if (p_to != 0 && !multiplayer->get_connected_peers().has(Math::abs(p_to))) {
313
ERR_FAIL_COND_MSG(p_to == multiplayer->get_unique_id(), "Attempt to call RPC on yourself! Peer unique ID: " + itos(multiplayer->get_unique_id()) + ".");
314
315
ERR_FAIL_MSG("Attempt to call RPC with unknown peer ID: " + itos(p_to) + ".");
316
}
317
318
// See if all peers have cached path (if so, call can be fast) while building the RPC target list.
319
HashSet<int> targets;
320
int psc_id = -1;
321
bool has_all_peers = true;
322
const ObjectID oid = p_node->get_instance_id();
323
if (p_to > 0) {
324
ERR_FAIL_COND_MSG(!multiplayer_replicator->is_rpc_visible(oid, p_to), "Attempt to call an RPC to a peer that cannot see this node. Peer ID: " + itos(p_to));
325
targets.insert(p_to);
326
has_all_peers = multiplayer_cache->send_object_cache(p_node, p_to, psc_id);
327
} else {
328
bool restricted = !multiplayer_replicator->is_rpc_visible(oid, 0);
329
for (const int &P : multiplayer->get_connected_peers()) {
330
if (p_to < 0 && P == -p_to) {
331
continue; // Excluded peer.
332
}
333
if (restricted && !multiplayer_replicator->is_rpc_visible(oid, P)) {
334
continue; // Not visible to this peer.
335
}
336
targets.insert(P);
337
bool has_peer = multiplayer_cache->send_object_cache(p_node, P, psc_id);
338
has_all_peers = has_all_peers && has_peer;
339
}
340
}
341
if (targets.is_empty()) {
342
return; // No one in sight.
343
}
344
345
// Create base packet, lots of hardcode because it must be tight.
346
int ofs = 0;
347
348
#define MAKE_ROOM(m_amount) \
349
if (packet_cache.size() < m_amount) \
350
packet_cache.resize(m_amount);
351
352
// Encode meta.
353
uint8_t command_type = SceneMultiplayer::NETWORK_COMMAND_REMOTE_CALL;
354
uint8_t node_id_compression = UINT8_MAX;
355
uint8_t name_id_compression = UINT8_MAX;
356
bool byte_only_or_no_args = false;
357
358
MAKE_ROOM(1);
359
// The meta is composed along the way, so just set 0 for now.
360
packet_cache.write[0] = 0;
361
ofs += 1;
362
363
// Encode Node ID.
364
if (has_all_peers) {
365
// Compress the node ID only if all the target peers already know it.
366
if (psc_id >= 0 && psc_id <= 255) {
367
// We can encode the id in 1 byte
368
node_id_compression = NETWORK_NODE_ID_COMPRESSION_8;
369
MAKE_ROOM(ofs + 1);
370
packet_cache.write[ofs] = static_cast<uint8_t>(psc_id);
371
ofs += 1;
372
} else if (psc_id >= 0 && psc_id <= 65535) {
373
// We can encode the id in 2 bytes
374
node_id_compression = NETWORK_NODE_ID_COMPRESSION_16;
375
MAKE_ROOM(ofs + 2);
376
encode_uint16(static_cast<uint16_t>(psc_id), &(packet_cache.write[ofs]));
377
ofs += 2;
378
} else {
379
// Too big, let's use 4 bytes.
380
node_id_compression = NETWORK_NODE_ID_COMPRESSION_32;
381
MAKE_ROOM(ofs + 4);
382
encode_uint32(psc_id, &(packet_cache.write[ofs]));
383
ofs += 4;
384
}
385
} else {
386
// The targets don't know the node yet, so we need to use 32 bits int.
387
node_id_compression = NETWORK_NODE_ID_COMPRESSION_32;
388
MAKE_ROOM(ofs + 4);
389
encode_uint32(psc_id, &(packet_cache.write[ofs]));
390
ofs += 4;
391
}
392
393
// Encode method ID
394
if (p_rpc_id <= UINT8_MAX) {
395
// The ID fits in 1 byte
396
name_id_compression = NETWORK_NAME_ID_COMPRESSION_8;
397
MAKE_ROOM(ofs + 1);
398
packet_cache.write[ofs] = static_cast<uint8_t>(p_rpc_id);
399
ofs += 1;
400
} else {
401
// The ID is larger, let's use 2 bytes
402
name_id_compression = NETWORK_NAME_ID_COMPRESSION_16;
403
MAKE_ROOM(ofs + 2);
404
encode_uint16(p_rpc_id, &(packet_cache.write[ofs]));
405
ofs += 2;
406
}
407
408
int len;
409
Error err = MultiplayerAPI::encode_and_compress_variants(p_arg, p_argcount, nullptr, len, &byte_only_or_no_args, multiplayer->is_object_decoding_allowed());
410
ERR_FAIL_COND_MSG(err != OK, "Unable to encode RPC arguments. THIS IS LIKELY A BUG IN THE ENGINE!");
411
if (byte_only_or_no_args) {
412
MAKE_ROOM(ofs + len);
413
} else {
414
MAKE_ROOM(ofs + 1 + len);
415
packet_cache.write[ofs] = p_argcount;
416
ofs += 1;
417
}
418
if (len) {
419
MultiplayerAPI::encode_and_compress_variants(p_arg, p_argcount, &packet_cache.write[ofs], len, &byte_only_or_no_args, multiplayer->is_object_decoding_allowed());
420
ofs += len;
421
}
422
423
ERR_FAIL_COND(command_type > 7);
424
ERR_FAIL_COND(node_id_compression > 3);
425
ERR_FAIL_COND(name_id_compression > 1);
426
427
#ifdef DEBUG_ENABLED
428
_profile_node_data("rpc_out", p_node->get_instance_id(), ofs);
429
#endif
430
431
// We can now set the meta
432
packet_cache.write[0] = command_type + (node_id_compression << NODE_ID_COMPRESSION_SHIFT) + (name_id_compression << NAME_ID_COMPRESSION_SHIFT) + (byte_only_or_no_args ? BYTE_ONLY_OR_NO_ARGS_FLAG : 0);
433
434
// Take chance and set transfer mode, since all send methods will use it.
435
peer->set_transfer_channel(p_config.channel);
436
peer->set_transfer_mode(p_config.transfer_mode);
437
438
if (has_all_peers) {
439
for (const int P : targets) {
440
multiplayer->send_command(P, packet_cache.ptr(), ofs);
441
}
442
} else {
443
// Unreachable because the node ID is never compressed if the peers doesn't know it.
444
CRASH_COND(node_id_compression != NETWORK_NODE_ID_COMPRESSION_32);
445
446
// Not all verified path, so send one by one.
447
448
// Append path at the end, since we will need it for some packets.
449
CharString pname = String(multiplayer->get_root_path().rel_path_to(p_node->get_path())).utf8();
450
int path_len = encode_cstring(pname.get_data(), nullptr);
451
MAKE_ROOM(ofs + path_len);
452
encode_cstring(pname.get_data(), &(packet_cache.write[ofs]));
453
454
// Not all verified path, so check which needs the longer packet.
455
for (const int P : targets) {
456
bool confirmed = multiplayer_cache->is_cache_confirmed(p_node, P);
457
if (confirmed) {
458
// This one confirmed path, so use id.
459
encode_uint32(psc_id, &(packet_cache.write[1]));
460
multiplayer->send_command(P, packet_cache.ptr(), ofs);
461
} else {
462
// This one did not confirm path yet, so use entire path (sorry!).
463
encode_uint32(0x80000000 | ofs, &(packet_cache.write[1])); // Offset to path and flag.
464
multiplayer->send_command(P, packet_cache.ptr(), ofs + path_len);
465
}
466
}
467
}
468
}
469
470
Error SceneRPCInterface::rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
471
Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer();
472
ERR_FAIL_COND_V_MSG(peer.is_null(), ERR_UNCONFIGURED, "Trying to call an RPC while no multiplayer peer is active.");
473
Node *node = Object::cast_to<Node>(p_obj);
474
ERR_FAIL_COND_V_MSG(!node || !node->is_inside_tree(), ERR_INVALID_PARAMETER, "The object must be a valid Node inside the SceneTree");
475
ERR_FAIL_COND_V_MSG(peer->get_connection_status() != MultiplayerPeer::CONNECTION_CONNECTED, ERR_CONNECTION_ERROR, "Trying to call an RPC via a multiplayer peer which is not connected.");
476
477
int caller_id = multiplayer->get_unique_id();
478
bool call_local_native = false;
479
bool call_local_script = false;
480
const RPCConfigCache &config_cache = _get_node_config(node);
481
uint16_t rpc_id = config_cache.ids.has(p_method) ? config_cache.ids[p_method] : UINT16_MAX;
482
ERR_FAIL_COND_V_MSG(rpc_id == UINT16_MAX, ERR_INVALID_PARAMETER,
483
vformat("Unable to get the RPC configuration for the function \"%s\" at path: \"%s\". This happens when the method is missing or not marked for RPCs in the local script.", p_method, node->get_path()));
484
const RPCConfig &config = config_cache.configs[rpc_id];
485
486
ERR_FAIL_COND_V_MSG(p_peer_id == caller_id && !config.call_local, ERR_INVALID_PARAMETER, "RPC '" + p_method + "' on yourself is not allowed by selected mode.");
487
488
if (p_peer_id == 0 || p_peer_id == caller_id || (p_peer_id < 0 && p_peer_id != -caller_id)) {
489
if (rpc_id & (1 << 15)) {
490
call_local_native = config.call_local;
491
} else {
492
call_local_script = config.call_local;
493
}
494
}
495
496
if (p_peer_id != caller_id) {
497
_send_rpc(node, p_peer_id, rpc_id, config, p_method, p_arg, p_argcount);
498
}
499
500
if (call_local_native) {
501
Callable::CallError ce;
502
503
multiplayer->set_remote_sender_override(multiplayer->get_unique_id());
504
node->callp(p_method, p_arg, p_argcount, ce);
505
multiplayer->set_remote_sender_override(0);
506
507
if (ce.error != Callable::CallError::CALL_OK) {
508
String error = Variant::get_call_error_text(node, p_method, p_arg, p_argcount, ce);
509
error = "rpc() aborted in local call: - " + error + ".";
510
ERR_PRINT(error);
511
return FAILED;
512
}
513
}
514
515
if (call_local_script) {
516
Callable::CallError ce;
517
ce.error = Callable::CallError::CALL_OK;
518
519
multiplayer->set_remote_sender_override(multiplayer->get_unique_id());
520
node->get_script_instance()->callp(p_method, p_arg, p_argcount, ce);
521
multiplayer->set_remote_sender_override(0);
522
523
if (ce.error != Callable::CallError::CALL_OK) {
524
String error = Variant::get_call_error_text(node, p_method, p_arg, p_argcount, ce);
525
error = "rpc() aborted in script local call: - " + error + ".";
526
ERR_PRINT(error);
527
return FAILED;
528
}
529
}
530
return OK;
531
}
532
533