scene_rpc_interface.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. #include "scene_rpc_interface.h"
  31. #include "scene_multiplayer.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/io/marshalls.h"
  34. #include "scene/main/multiplayer_api.h"
  35. #include "scene/main/node.h"
  36. #include "scene/main/window.h"
  37. // The RPC meta is composed by a single byte that contains (starting from the least significant bit):
  38. // - `NetworkCommands` in the first four bits.
  39. // - `NetworkNodeIdCompression` in the next 2 bits.
  40. // - `NetworkNameIdCompression` in the next 1 bit.
  41. // - `byte_only_or_no_args` in the next 1 bit.
  42. #define NODE_ID_COMPRESSION_SHIFT SceneMultiplayer::CMD_FLAG_0_SHIFT
  43. #define NAME_ID_COMPRESSION_SHIFT SceneMultiplayer::CMD_FLAG_2_SHIFT
  44. #define BYTE_ONLY_OR_NO_ARGS_SHIFT SceneMultiplayer::CMD_FLAG_3_SHIFT
  45. #define NODE_ID_COMPRESSION_FLAG ((1 << NODE_ID_COMPRESSION_SHIFT) | (1 << (NODE_ID_COMPRESSION_SHIFT + 1)))
  46. #define NAME_ID_COMPRESSION_FLAG (1 << NAME_ID_COMPRESSION_SHIFT)
  47. #define BYTE_ONLY_OR_NO_ARGS_FLAG (1 << BYTE_ONLY_OR_NO_ARGS_SHIFT)
  48. #ifdef DEBUG_ENABLED
  49. _FORCE_INLINE_ void SceneRPCInterface::_profile_node_data(const String &p_what, ObjectID p_id, int p_size) {
  50. if (EngineDebugger::is_profiling("multiplayer:rpc")) {
  51. Array values = { p_what, p_id, p_size };
  52. EngineDebugger::profiler_add_frame_data("multiplayer:rpc", values);
  53. }
  54. }
  55. #endif
  56. // Returns the packet size stripping the node path added when the node is not yet cached.
  57. int get_packet_len(uint32_t p_node_target, int p_packet_len) {
  58. if (p_node_target & 0x80000000) {
  59. int ofs = p_node_target & 0x7FFFFFFF;
  60. return p_packet_len - (p_packet_len - ofs);
  61. } else {
  62. return p_packet_len;
  63. }
  64. }
  65. void SceneRPCInterface::_parse_rpc_config(const Variant &p_config, bool p_for_node, RPCConfigCache &r_cache) {
  66. if (p_config.get_type() == Variant::NIL) {
  67. return;
  68. }
  69. ERR_FAIL_COND(p_config.get_type() != Variant::DICTIONARY);
  70. const Dictionary config = p_config;
  71. Array names = config.keys();
  72. names.sort_custom(callable_mp_static(&StringLikeVariantOrder::compare)); // Ensure ID order
  73. for (int i = 0; i < names.size(); i++) {
  74. ERR_CONTINUE(!names[i].is_string());
  75. String name = names[i].operator String();
  76. ERR_CONTINUE(config[name].get_type() != Variant::DICTIONARY);
  77. ERR_CONTINUE(!config[name].operator Dictionary().has("rpc_mode"));
  78. Dictionary dict = config[name];
  79. RPCConfig cfg;
  80. cfg.name = name;
  81. cfg.rpc_mode = ((MultiplayerAPI::RPCMode)dict.get("rpc_mode", MultiplayerAPI::RPC_MODE_AUTHORITY).operator int());
  82. cfg.transfer_mode = ((MultiplayerPeer::TransferMode)dict.get("transfer_mode", MultiplayerPeer::TRANSFER_MODE_RELIABLE).operator int());
  83. cfg.call_local = dict.get("call_local", false).operator bool();
  84. cfg.channel = dict.get("channel", 0).operator int();
  85. uint16_t id = ((uint16_t)i);
  86. if (p_for_node) {
  87. id |= (1 << 15);
  88. }
  89. r_cache.configs[id] = cfg;
  90. r_cache.ids[name] = id;
  91. }
  92. }
  93. const SceneRPCInterface::RPCConfigCache &SceneRPCInterface::_get_node_config(const Node *p_node) {
  94. const ObjectID oid = p_node->get_instance_id();
  95. if (rpc_cache.has(oid)) {
  96. return rpc_cache[oid];
  97. }
  98. RPCConfigCache cache;
  99. _parse_rpc_config(p_node->get_rpc_config(), true, cache);
  100. if (p_node->get_script_instance()) {
  101. _parse_rpc_config(p_node->get_script_instance()->get_rpc_config(), false, cache);
  102. }
  103. rpc_cache[oid] = cache;
  104. return rpc_cache[oid];
  105. }
  106. String SceneRPCInterface::get_rpc_md5(const Object *p_obj) {
  107. const Node *node = Object::cast_to<Node>(p_obj);
  108. ERR_FAIL_NULL_V(node, "");
  109. const RPCConfigCache cache = _get_node_config(node);
  110. String rpc_list;
  111. for (const KeyValue<uint16_t, RPCConfig> &config : cache.configs) {
  112. rpc_list += String(config.value.name);
  113. }
  114. return rpc_list.md5_text();
  115. }
  116. Node *SceneRPCInterface::_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len) {
  117. Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
  118. ERR_FAIL_NULL_V(root_node, nullptr);
  119. Node *node = nullptr;
  120. if (p_node_target & 0x80000000) {
  121. // Use full path (not cached yet).
  122. int ofs = p_node_target & 0x7FFFFFFF;
  123. ERR_FAIL_COND_V_MSG(ofs >= p_packet_len, nullptr, "Invalid packet received. Size smaller than declared.");
  124. String paths = String::utf8((const char *)&p_packet[ofs], p_packet_len - ofs);
  125. NodePath np = paths;
  126. node = root_node->get_node(np);
  127. if (!node) {
  128. ERR_PRINT("Failed to get path from RPC: " + String(np) + ".");
  129. }
  130. return node;
  131. } else {
  132. // Use cached path.
  133. return Object::cast_to<Node>(multiplayer_cache->get_cached_object(p_from, p_node_target));
  134. }
  135. }
  136. void SceneRPCInterface::process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len) {
  137. // Extract packet meta
  138. int packet_min_size = 1;
  139. int name_id_offset = 1;
  140. ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small.");
  141. // Compute the meta size, which depends on the compression level.
  142. int node_id_compression = (p_packet[0] & NODE_ID_COMPRESSION_FLAG) >> NODE_ID_COMPRESSION_SHIFT;
  143. int name_id_compression = (p_packet[0] & NAME_ID_COMPRESSION_FLAG) >> NAME_ID_COMPRESSION_SHIFT;
  144. switch (node_id_compression) {
  145. case NETWORK_NODE_ID_COMPRESSION_8:
  146. packet_min_size += 1;
  147. name_id_offset += 1;
  148. break;
  149. case NETWORK_NODE_ID_COMPRESSION_16:
  150. packet_min_size += 2;
  151. name_id_offset += 2;
  152. break;
  153. case NETWORK_NODE_ID_COMPRESSION_32:
  154. packet_min_size += 4;
  155. name_id_offset += 4;
  156. break;
  157. default:
  158. ERR_FAIL_MSG("Was not possible to extract the node id compression mode.");
  159. }
  160. switch (name_id_compression) {
  161. case NETWORK_NAME_ID_COMPRESSION_8:
  162. packet_min_size += 1;
  163. break;
  164. case NETWORK_NAME_ID_COMPRESSION_16:
  165. packet_min_size += 2;
  166. break;
  167. default:
  168. ERR_FAIL_MSG("Was not possible to extract the name id compression mode.");
  169. }
  170. ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small.");
  171. uint32_t node_target = 0;
  172. switch (node_id_compression) {
  173. case NETWORK_NODE_ID_COMPRESSION_8:
  174. node_target = p_packet[1];
  175. break;
  176. case NETWORK_NODE_ID_COMPRESSION_16:
  177. node_target = decode_uint16(p_packet + 1);
  178. break;
  179. case NETWORK_NODE_ID_COMPRESSION_32:
  180. node_target = decode_uint32(p_packet + 1);
  181. break;
  182. default:
  183. // Unreachable, checked before.
  184. CRASH_NOW();
  185. }
  186. Node *node = _process_get_node(p_from, p_packet, node_target, p_packet_len);
  187. ERR_FAIL_NULL_MSG(node, "Invalid packet received. Requested node was not found.");
  188. uint16_t name_id = 0;
  189. switch (name_id_compression) {
  190. case NETWORK_NAME_ID_COMPRESSION_8:
  191. name_id = p_packet[name_id_offset];
  192. break;
  193. case NETWORK_NAME_ID_COMPRESSION_16:
  194. name_id = decode_uint16(p_packet + name_id_offset);
  195. break;
  196. default:
  197. // Unreachable, checked before.
  198. CRASH_NOW();
  199. }
  200. const int packet_len = get_packet_len(node_target, p_packet_len);
  201. _process_rpc(node, name_id, p_from, p_packet, packet_len, packet_min_size);
  202. }
  203. 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) {
  204. ERR_FAIL_COND_MSG(p_offset > p_packet_len, "Invalid packet received. Size too small.");
  205. // Check that remote can call the RPC on this node.
  206. const RPCConfigCache &cache_config = _get_node_config(p_node);
  207. ERR_FAIL_COND(!cache_config.configs.has(p_rpc_method_id));
  208. const RPCConfig &config = cache_config.configs[p_rpc_method_id];
  209. bool can_call = false;
  210. switch (config.rpc_mode) {
  211. case MultiplayerAPI::RPC_MODE_DISABLED: {
  212. can_call = false;
  213. } break;
  214. case MultiplayerAPI::RPC_MODE_ANY_PEER: {
  215. can_call = true;
  216. } break;
  217. case MultiplayerAPI::RPC_MODE_AUTHORITY: {
  218. can_call = p_from == p_node->get_multiplayer_authority();
  219. } break;
  220. }
  221. ERR_FAIL_COND_MSG(!can_call, "RPC '" + String(config.name) + "' is not allowed on node " + p_node->get_path() + " from: " + itos(p_from) + ". Mode is " + itos((int)config.rpc_mode) + ", authority is " + itos(p_node->get_multiplayer_authority()) + ".");
  222. int argc = 0;
  223. const bool byte_only_or_no_args = p_packet[0] & BYTE_ONLY_OR_NO_ARGS_FLAG;
  224. if (byte_only_or_no_args) {
  225. if (p_offset < p_packet_len) {
  226. // This packet contains only bytes.
  227. argc = 1;
  228. }
  229. } else {
  230. // Normal variant, takes the argument count from the packet.
  231. ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small.");
  232. argc = p_packet[p_offset];
  233. p_offset += 1;
  234. }
  235. Vector<Variant> args;
  236. Vector<const Variant *> argp;
  237. args.resize(argc);
  238. argp.resize(argc);
  239. #ifdef DEBUG_ENABLED
  240. _profile_node_data("rpc_in", p_node->get_instance_id(), p_packet_len);
  241. #endif
  242. int out;
  243. 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());
  244. for (int i = 0; i < argc; i++) {
  245. argp.write[i] = &args[i];
  246. }
  247. Callable::CallError ce;
  248. p_node->callp(config.name, (const Variant **)argp.ptr(), argc, ce);
  249. if (ce.error != Callable::CallError::CALL_OK) {
  250. String error = Variant::get_call_error_text(p_node, config.name, (const Variant **)argp.ptr(), argc, ce);
  251. error = "RPC - " + error;
  252. ERR_PRINT(error);
  253. }
  254. }
  255. 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) {
  256. Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer();
  257. ERR_FAIL_COND_MSG(peer.is_null(), "Attempt to call RPC without active multiplayer peer.");
  258. ERR_FAIL_COND_MSG(peer->get_connection_status() == MultiplayerPeer::CONNECTION_CONNECTING, "Attempt to call RPC while multiplayer peer is not connected yet.");
  259. ERR_FAIL_COND_MSG(peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED, "Attempt to call RPC while multiplayer peer is disconnected.");
  260. ERR_FAIL_COND_MSG(p_argcount > 255, "Too many arguments (>255).");
  261. if (p_to != 0 && !multiplayer->get_connected_peers().has(Math::abs(p_to))) {
  262. ERR_FAIL_COND_MSG(p_to == multiplayer->get_unique_id(), "Attempt to call RPC on yourself! Peer unique ID: " + itos(multiplayer->get_unique_id()) + ".");
  263. ERR_FAIL_MSG("Attempt to call RPC with unknown peer ID: " + itos(p_to) + ".");
  264. }
  265. // See if all peers have cached path (if so, call can be fast) while building the RPC target list.
  266. HashSet<int> targets;
  267. int psc_id = -1;
  268. bool has_all_peers = true;
  269. const ObjectID oid = p_node->get_instance_id();
  270. if (p_to > 0) {
  271. 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));
  272. targets.insert(p_to);
  273. has_all_peers = multiplayer_cache->send_object_cache(p_node, p_to, psc_id);
  274. } else {
  275. bool restricted = !multiplayer_replicator->is_rpc_visible(oid, 0);
  276. for (const int &P : multiplayer->get_connected_peers()) {
  277. if (p_to < 0 && P == -p_to) {
  278. continue; // Excluded peer.
  279. }
  280. if (restricted && !multiplayer_replicator->is_rpc_visible(oid, P)) {
  281. continue; // Not visible to this peer.
  282. }
  283. targets.insert(P);
  284. bool has_peer = multiplayer_cache->send_object_cache(p_node, P, psc_id);
  285. has_all_peers = has_all_peers && has_peer;
  286. }
  287. }
  288. if (targets.is_empty()) {
  289. return; // No one in sight.
  290. }
  291. // Create base packet, lots of hardcode because it must be tight.
  292. int ofs = 0;
  293. #define MAKE_ROOM(m_amount) \
  294. if (packet_cache.size() < m_amount) \
  295. packet_cache.resize(m_amount);
  296. // Encode meta.
  297. uint8_t command_type = SceneMultiplayer::NETWORK_COMMAND_REMOTE_CALL;
  298. uint8_t node_id_compression = UINT8_MAX;
  299. uint8_t name_id_compression = UINT8_MAX;
  300. bool byte_only_or_no_args = false;
  301. MAKE_ROOM(1);
  302. // The meta is composed along the way, so just set 0 for now.
  303. packet_cache.write[0] = 0;
  304. ofs += 1;
  305. // Encode Node ID.
  306. if (has_all_peers) {
  307. // Compress the node ID only if all the target peers already know it.
  308. if (psc_id >= 0 && psc_id <= 255) {
  309. // We can encode the id in 1 byte
  310. node_id_compression = NETWORK_NODE_ID_COMPRESSION_8;
  311. MAKE_ROOM(ofs + 1);
  312. packet_cache.write[ofs] = static_cast<uint8_t>(psc_id);
  313. ofs += 1;
  314. } else if (psc_id >= 0 && psc_id <= 65535) {
  315. // We can encode the id in 2 bytes
  316. node_id_compression = NETWORK_NODE_ID_COMPRESSION_16;
  317. MAKE_ROOM(ofs + 2);
  318. encode_uint16(static_cast<uint16_t>(psc_id), &(packet_cache.write[ofs]));
  319. ofs += 2;
  320. } else {
  321. // Too big, let's use 4 bytes.
  322. node_id_compression = NETWORK_NODE_ID_COMPRESSION_32;
  323. MAKE_ROOM(ofs + 4);
  324. encode_uint32(psc_id, &(packet_cache.write[ofs]));
  325. ofs += 4;
  326. }
  327. } else {
  328. // The targets don't know the node yet, so we need to use 32 bits int.
  329. node_id_compression = NETWORK_NODE_ID_COMPRESSION_32;
  330. MAKE_ROOM(ofs + 4);
  331. encode_uint32(psc_id, &(packet_cache.write[ofs]));
  332. ofs += 4;
  333. }
  334. // Encode method ID
  335. if (p_rpc_id <= UINT8_MAX) {
  336. // The ID fits in 1 byte
  337. name_id_compression = NETWORK_NAME_ID_COMPRESSION_8;
  338. MAKE_ROOM(ofs + 1);
  339. packet_cache.write[ofs] = static_cast<uint8_t>(p_rpc_id);
  340. ofs += 1;
  341. } else {
  342. // The ID is larger, let's use 2 bytes
  343. name_id_compression = NETWORK_NAME_ID_COMPRESSION_16;
  344. MAKE_ROOM(ofs + 2);
  345. encode_uint16(p_rpc_id, &(packet_cache.write[ofs]));
  346. ofs += 2;
  347. }
  348. int len;
  349. Error err = MultiplayerAPI::encode_and_compress_variants(p_arg, p_argcount, nullptr, len, &byte_only_or_no_args, multiplayer->is_object_decoding_allowed());
  350. ERR_FAIL_COND_MSG(err != OK, "Unable to encode RPC arguments. THIS IS LIKELY A BUG IN THE ENGINE!");
  351. if (byte_only_or_no_args) {
  352. MAKE_ROOM(ofs + len);
  353. } else {
  354. MAKE_ROOM(ofs + 1 + len);
  355. packet_cache.write[ofs] = p_argcount;
  356. ofs += 1;
  357. }
  358. if (len) {
  359. MultiplayerAPI::encode_and_compress_variants(p_arg, p_argcount, &packet_cache.write[ofs], len, &byte_only_or_no_args, multiplayer->is_object_decoding_allowed());
  360. ofs += len;
  361. }
  362. ERR_FAIL_COND(command_type > 7);
  363. ERR_FAIL_COND(node_id_compression > 3);
  364. ERR_FAIL_COND(name_id_compression > 1);
  365. #ifdef DEBUG_ENABLED
  366. _profile_node_data("rpc_out", p_node->get_instance_id(), ofs);
  367. #endif
  368. // We can now set the meta
  369. 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);
  370. // Take chance and set transfer mode, since all send methods will use it.
  371. peer->set_transfer_channel(p_config.channel);
  372. peer->set_transfer_mode(p_config.transfer_mode);
  373. if (has_all_peers) {
  374. for (const int P : targets) {
  375. multiplayer->send_command(P, packet_cache.ptr(), ofs);
  376. }
  377. } else {
  378. // Unreachable because the node ID is never compressed if the peers doesn't know it.
  379. CRASH_COND(node_id_compression != NETWORK_NODE_ID_COMPRESSION_32);
  380. // Not all verified path, so send one by one.
  381. // Append path at the end, since we will need it for some packets.
  382. CharString pname = String(multiplayer->get_root_path().rel_path_to(p_node->get_path())).utf8();
  383. int path_len = encode_cstring(pname.get_data(), nullptr);
  384. MAKE_ROOM(ofs + path_len);
  385. encode_cstring(pname.get_data(), &(packet_cache.write[ofs]));
  386. // Not all verified path, so check which needs the longer packet.
  387. for (const int P : targets) {
  388. bool confirmed = multiplayer_cache->is_cache_confirmed(p_node, P);
  389. if (confirmed) {
  390. // This one confirmed path, so use id.
  391. encode_uint32(psc_id, &(packet_cache.write[1]));
  392. multiplayer->send_command(P, packet_cache.ptr(), ofs);
  393. } else {
  394. // This one did not confirm path yet, so use entire path (sorry!).
  395. encode_uint32(0x80000000 | ofs, &(packet_cache.write[1])); // Offset to path and flag.
  396. multiplayer->send_command(P, packet_cache.ptr(), ofs + path_len);
  397. }
  398. }
  399. }
  400. }
  401. Error SceneRPCInterface::rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
  402. Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer();
  403. ERR_FAIL_COND_V_MSG(peer.is_null(), ERR_UNCONFIGURED, "Trying to call an RPC while no multiplayer peer is active.");
  404. Node *node = Object::cast_to<Node>(p_obj);
  405. ERR_FAIL_COND_V_MSG(!node || !node->is_inside_tree(), ERR_INVALID_PARAMETER, "The object must be a valid Node inside the SceneTree");
  406. 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.");
  407. int caller_id = multiplayer->get_unique_id();
  408. bool call_local_native = false;
  409. bool call_local_script = false;
  410. const RPCConfigCache &config_cache = _get_node_config(node);
  411. uint16_t rpc_id = config_cache.ids.has(p_method) ? config_cache.ids[p_method] : UINT16_MAX;
  412. ERR_FAIL_COND_V_MSG(rpc_id == UINT16_MAX, ERR_INVALID_PARAMETER,
  413. 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()));
  414. const RPCConfig &config = config_cache.configs[rpc_id];
  415. 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.");
  416. if (p_peer_id == 0 || p_peer_id == caller_id || (p_peer_id < 0 && p_peer_id != -caller_id)) {
  417. if (rpc_id & (1 << 15)) {
  418. call_local_native = config.call_local;
  419. } else {
  420. call_local_script = config.call_local;
  421. }
  422. }
  423. if (p_peer_id != caller_id) {
  424. _send_rpc(node, p_peer_id, rpc_id, config, p_method, p_arg, p_argcount);
  425. }
  426. if (call_local_native) {
  427. Callable::CallError ce;
  428. multiplayer->set_remote_sender_override(multiplayer->get_unique_id());
  429. node->callp(p_method, p_arg, p_argcount, ce);
  430. multiplayer->set_remote_sender_override(0);
  431. if (ce.error != Callable::CallError::CALL_OK) {
  432. String error = Variant::get_call_error_text(node, p_method, p_arg, p_argcount, ce);
  433. error = "rpc() aborted in local call: - " + error + ".";
  434. ERR_PRINT(error);
  435. return FAILED;
  436. }
  437. }
  438. if (call_local_script) {
  439. Callable::CallError ce;
  440. ce.error = Callable::CallError::CALL_OK;
  441. multiplayer->set_remote_sender_override(multiplayer->get_unique_id());
  442. node->get_script_instance()->callp(p_method, p_arg, p_argcount, ce);
  443. multiplayer->set_remote_sender_override(0);
  444. if (ce.error != Callable::CallError::CALL_OK) {
  445. String error = Variant::get_call_error_text(node, p_method, p_arg, p_argcount, ce);
  446. error = "rpc() aborted in script local call: - " + error + ".";
  447. ERR_PRINT(error);
  448. return FAILED;
  449. }
  450. }
  451. return OK;
  452. }