scene_rpc_interface.cpp 20 KB

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