| 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 | // Default values should match GDScript `@rpc` annotation registration and `rpc_annotation()`. |
| 88 | RPCConfig cfg; |
| 89 | cfg.name = name; |
| 90 | cfg.rpc_mode = ((MultiplayerAPI::RPCMode)dict.get("rpc_mode", MultiplayerAPI::RPC_MODE_AUTHORITY).operator int()); |
| 91 | cfg.transfer_mode = ((MultiplayerPeer::TransferMode)dict.get("transfer_mode", MultiplayerPeer::TRANSFER_MODE_RELIABLE).operator int()); |
| 92 | cfg.call_local = dict.get("call_local", false).operator bool(); |
| 93 | cfg.channel = dict.get("channel", 0).operator int(); |
| 94 | uint16_t id = ((uint16_t)i); |
| 95 | if (p_for_node) { |
| 96 | id |= (1 << 15); |
| 97 | } |
| 98 | r_cache.configs[id] = cfg; |
| 99 | r_cache.ids[name] = id; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | const SceneRPCInterface::RPCConfigCache &SceneRPCInterface::_get_node_config(const Node *p_node) { |
| 104 | const ObjectID oid = p_node->get_instance_id(); |
| 105 | if (rpc_cache.has(oid)) { |
| 106 | return rpc_cache[oid]; |
| 107 | } |
| 108 | RPCConfigCache cache; |
| 109 | _parse_rpc_config(p_node->get_node_rpc_config(), true, cache); |
| 110 | if (p_node->get_script_instance()) { |
| 111 | _parse_rpc_config(p_node->get_script_instance()->get_rpc_config(), false, cache); |
| 112 | } |
| 113 | rpc_cache[oid] = cache; |
| 114 | return rpc_cache[oid]; |
| 115 | } |
| 116 | |
| 117 | String SceneRPCInterface::get_rpc_md5(const Object *p_obj) { |
| 118 | const Node *node = Object::cast_to<Node>(p_obj); |
| 119 | ERR_FAIL_NULL_V(node, ""); |
| 120 | const RPCConfigCache cache = _get_node_config(node); |
| 121 | String rpc_list; |
| 122 | for (const KeyValue<uint16_t, RPCConfig> &config : cache.configs) { |
| 123 | rpc_list += String(config.value.name); |
| 124 | } |
| 125 | return rpc_list.md5_text(); |
| 126 | } |
| 127 | |
| 128 | Node *SceneRPCInterface::_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len) { |
| 129 | Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path()); |
| 130 | ERR_FAIL_NULL_V(root_node, nullptr); |
| 131 | Node *node = nullptr; |
| 132 | |
| 133 | if (p_node_target & 0x80000000) { |
| 134 | // Use full path (not cached yet). |
| 135 | int ofs = p_node_target & 0x7FFFFFFF; |
| 136 | |
| 137 | ERR_FAIL_COND_V_MSG(ofs >= p_packet_len, nullptr, "Invalid packet received. Size smaller than declared."); |
| 138 | |
| 139 | String paths = String::utf8((const char *)&p_packet[ofs], p_packet_len - ofs); |
| 140 | |
| 141 | NodePath np = paths; |
| 142 | |
| 143 | node = root_node->get_node(np); |
| 144 | |
| 145 | if (!node) { |
| 146 | ERR_PRINT("Failed to get path from RPC: " + String(np) + "."); |
| 147 | } |
| 148 | return node; |
| 149 | } else { |
| 150 | // Use cached path. |
| 151 | return Object::cast_to<Node>(multiplayer_cache->get_cached_object(p_from, p_node_target)); |
| 152 | } |
| 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 | |
| 164 | switch (node_id_compression) { |
| 165 | case NETWORK_NODE_ID_COMPRESSION_8: |
| 166 | packet_min_size += 1; |
| 167 | name_id_offset += 1; |
| 168 | break; |
| 169 | case NETWORK_NODE_ID_COMPRESSION_16: |
| 170 | packet_min_size += 2; |
| 171 | name_id_offset += 2; |
| 172 | break; |
| 173 | case NETWORK_NODE_ID_COMPRESSION_32: |
| 174 | packet_min_size += 4; |
| 175 | name_id_offset += 4; |
| 176 | break; |
| 177 | default: |
| 178 | ERR_FAIL_MSG("Was not possible to extract the node id compression mode."); |
| 179 | } |
| 180 | switch (name_id_compression) { |
| 181 | case NETWORK_NAME_ID_COMPRESSION_8: |
| 182 | packet_min_size += 1; |
| 183 | break; |
| 184 | case NETWORK_NAME_ID_COMPRESSION_16: |
| 185 | packet_min_size += 2; |
| 186 | break; |
| 187 | default: |
| 188 | ERR_FAIL_MSG("Was not possible to extract the name id compression mode."); |
| 189 | } |
| 190 | ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small."); |
| 191 | |
| 192 | uint32_t node_target = 0; |
| 193 | switch (node_id_compression) { |
| 194 | case NETWORK_NODE_ID_COMPRESSION_8: |
| 195 | node_target = p_packet[1]; |
| 196 | break; |
| 197 | case NETWORK_NODE_ID_COMPRESSION_16: |
| 198 | node_target = decode_uint16(p_packet + 1); |
| 199 | break; |
| 200 | case NETWORK_NODE_ID_COMPRESSION_32: |
| 201 | node_target = decode_uint32(p_packet + 1); |
| 202 | break; |
| 203 | default: |
| 204 | // Unreachable, checked before. |
| 205 | CRASH_NOW(); |
| 206 | } |
| 207 | |
| 208 | Node *node = _process_get_node(p_from, p_packet, node_target, p_packet_len); |
| 209 | ERR_FAIL_NULL_MSG(node, "Invalid packet received. Requested node was not found."); |
| 210 | |
| 211 | uint16_t name_id = 0; |
| 212 | switch (name_id_compression) { |
| 213 | case NETWORK_NAME_ID_COMPRESSION_8: |
| 214 | name_id = p_packet[name_id_offset]; |
| 215 | break; |
| 216 | case NETWORK_NAME_ID_COMPRESSION_16: |
| 217 | name_id = decode_uint16(p_packet + name_id_offset); |
| 218 | break; |
| 219 | default: |
| 220 | // Unreachable, checked before. |
| 221 | CRASH_NOW(); |
| 222 | } |
| 223 | |
| 224 | const int packet_len = get_packet_len(node_target, p_packet_len); |
| 225 | _process_rpc(node, name_id, p_from, p_packet, packet_len, packet_min_size); |
| 226 | } |
| 227 | |
| 228 | static String _get_rpc_mode_string(MultiplayerAPI::RPCMode p_mode) { |
| 229 | switch (p_mode) { |
| 230 | case MultiplayerAPI::RPC_MODE_DISABLED: |
| 231 | return "disabled"; |
| 232 | case MultiplayerAPI::RPC_MODE_ANY_PEER: |
| 233 | return "any_peer"; |
| 234 | case MultiplayerAPI::RPC_MODE_AUTHORITY: |
| 235 | return "authority"; |
| 236 | } |
| 237 | ERR_FAIL_V_MSG(String(), "Invalid RPC mode."); |
| 238 | } |
| 239 | |
| 240 | 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) { |
| 241 | ERR_FAIL_COND_MSG(p_offset > p_packet_len, "Invalid packet received. Size too small."); |
| 242 | |
| 243 | // Check that remote can call the RPC on this node. |
| 244 | const RPCConfigCache &cache_config = _get_node_config(p_node); |
| 245 | ERR_FAIL_COND(!cache_config.configs.has(p_rpc_method_id)); |
| 246 | const RPCConfig &config = cache_config.configs[p_rpc_method_id]; |
| 247 | |
| 248 | bool can_call = false; |
| 249 | switch (config.rpc_mode) { |
| 250 | case MultiplayerAPI::RPC_MODE_DISABLED: { |
| 251 | can_call = false; |
| 252 | } break; |
| 253 | case MultiplayerAPI::RPC_MODE_ANY_PEER: { |
| 254 | can_call = true; |
| 255 | } break; |
| 256 | case MultiplayerAPI::RPC_MODE_AUTHORITY: { |
| 257 | can_call = p_from == p_node->get_multiplayer_authority(); |
| 258 | } break; |
| 259 | } |
| 260 | |
| 261 | 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()) + "."); |
| 262 | |
| 263 | int argc = 0; |
| 264 | |
| 265 | const bool byte_only_or_no_args = p_packet[0] & BYTE_ONLY_OR_NO_ARGS_FLAG; |
| 266 | if (byte_only_or_no_args) { |
| 267 | if (p_offset < p_packet_len) { |
| 268 | // This packet contains only bytes. |
| 269 | argc = 1; |
| 270 | } |
| 271 | } else { |
| 272 | // Normal variant, takes the argument count from the packet. |
| 273 | ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small."); |
| 274 | argc = p_packet[p_offset]; |
| 275 | p_offset += 1; |
| 276 | } |
| 277 | |
| 278 | Vector<Variant> args; |
| 279 | Vector<const Variant *> argp; |
| 280 | args.resize(argc); |
| 281 | argp.resize(argc); |
| 282 | |
| 283 | #ifdef DEBUG_ENABLED |
| 284 | _profile_node_data("rpc_in", p_node->get_instance_id(), p_packet_len); |
| 285 | #endif |
| 286 | |
| 287 | int out; |
| 288 | 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()); |
| 289 | for (int i = 0; i < argc; i++) { |
| 290 | argp.write[i] = &args[i]; |
| 291 | } |
| 292 | |
| 293 | Callable::CallError ce; |
| 294 | |
| 295 | p_node->callp(config.name, (const Variant **)argp.ptr(), argc, ce); |
| 296 | if (ce.error != Callable::CallError::CALL_OK) { |
| 297 | String error = Variant::get_call_error_text(p_node, config.name, (const Variant **)argp.ptr(), argc, ce); |
| 298 | error = "RPC - " + error; |
| 299 | ERR_PRINT(error); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | 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) { |
| 304 | Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer(); |
| 305 | ERR_FAIL_COND_MSG(peer.is_null(), "Attempt to call RPC without active multiplayer peer."); |
| 306 | |
| 307 | ERR_FAIL_COND_MSG(peer->get_connection_status() == MultiplayerPeer::CONNECTION_CONNECTING, "Attempt to call RPC while multiplayer peer is not connected yet."); |
| 308 | |
| 309 | ERR_FAIL_COND_MSG(peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED, "Attempt to call RPC while multiplayer peer is disconnected."); |
| 310 | |
| 311 | ERR_FAIL_COND_MSG(p_argcount > 255, "Too many arguments (>255)."); |
| 312 | |
| 313 | if (p_to != 0 && !multiplayer->get_connected_peers().has(Math::abs(p_to))) { |
| 314 | ERR_FAIL_COND_MSG(p_to == multiplayer->get_unique_id(), "Attempt to call RPC on yourself! Peer unique ID: " + itos(multiplayer->get_unique_id()) + "."); |
| 315 | |
| 316 | ERR_FAIL_MSG("Attempt to call RPC with unknown peer ID: " + itos(p_to) + "."); |
| 317 | } |
| 318 | |
| 319 | // See if all peers have cached path (if so, call can be fast) while building the RPC target list. |
| 320 | HashSet<int> targets; |
| 321 | int psc_id = -1; |
| 322 | bool has_all_peers = true; |
| 323 | const ObjectID oid = p_node->get_instance_id(); |
| 324 | if (p_to > 0) { |
| 325 | 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)); |
| 326 | targets.insert(p_to); |
| 327 | has_all_peers = multiplayer_cache->send_object_cache(p_node, p_to, psc_id); |
| 328 | } else { |
| 329 | bool restricted = !multiplayer_replicator->is_rpc_visible(oid, 0); |
| 330 | for (const int &P : multiplayer->get_connected_peers()) { |
| 331 | if (p_to < 0 && P == -p_to) { |
| 332 | continue; // Excluded peer. |
| 333 | } |
| 334 | if (restricted && !multiplayer_replicator->is_rpc_visible(oid, P)) { |
| 335 | continue; // Not visible to this peer. |
| 336 | } |
| 337 | targets.insert(P); |
| 338 | bool has_peer = multiplayer_cache->send_object_cache(p_node, P, psc_id); |
| 339 | has_all_peers = has_all_peers && has_peer; |
| 340 | } |
| 341 | } |
| 342 | if (targets.is_empty()) { |
| 343 | return; // No one in sight. |
| 344 | } |
| 345 | |
| 346 | // Create base packet, lots of hardcode because it must be tight. |
| 347 | int ofs = 0; |
| 348 | |
| 349 | #define MAKE_ROOM(m_amount) \ |
| 350 | if (packet_cache.size() < m_amount) \ |
| 351 | packet_cache.resize(m_amount); |
| 352 | |
| 353 | // Encode meta. |
| 354 | uint8_t command_type = SceneMultiplayer::NETWORK_COMMAND_REMOTE_CALL; |
| 355 | uint8_t node_id_compression = UINT8_MAX; |
| 356 | uint8_t name_id_compression = UINT8_MAX; |
| 357 | bool byte_only_or_no_args = false; |
| 358 | |
| 359 | MAKE_ROOM(1); |
| 360 | // The meta is composed along the way, so just set 0 for now. |
| 361 | packet_cache.write[0] = 0; |
| 362 | ofs += 1; |
| 363 | |
| 364 | // Encode Node ID. |
| 365 | if (has_all_peers) { |
| 366 | // Compress the node ID only if all the target peers already know it. |
| 367 | if (psc_id >= 0 && psc_id <= 255) { |
| 368 | // We can encode the id in 1 byte |
| 369 | node_id_compression = NETWORK_NODE_ID_COMPRESSION_8; |
| 370 | MAKE_ROOM(ofs + 1); |
| 371 | packet_cache.write[ofs] = static_cast<uint8_t>(psc_id); |
| 372 | ofs += 1; |
| 373 | } else if (psc_id >= 0 && psc_id <= 65535) { |
| 374 | // We can encode the id in 2 bytes |
| 375 | node_id_compression = NETWORK_NODE_ID_COMPRESSION_16; |
| 376 | MAKE_ROOM(ofs + 2); |
| 377 | encode_uint16(static_cast<uint16_t>(psc_id), &(packet_cache.write[ofs])); |
| 378 | ofs += 2; |
| 379 | } else { |
| 380 | // Too big, let's use 4 bytes. |
| 381 | node_id_compression = NETWORK_NODE_ID_COMPRESSION_32; |
| 382 | MAKE_ROOM(ofs + 4); |
| 383 | encode_uint32(psc_id, &(packet_cache.write[ofs])); |
| 384 | ofs += 4; |
| 385 | } |
| 386 | } else { |
| 387 | // The targets don't know the node yet, so we need to use 32 bits int. |
| 388 | node_id_compression = NETWORK_NODE_ID_COMPRESSION_32; |
| 389 | MAKE_ROOM(ofs + 4); |
| 390 | encode_uint32(psc_id, &(packet_cache.write[ofs])); |
| 391 | ofs += 4; |
| 392 | } |
| 393 | |
| 394 | // Encode method ID |
| 395 | if (p_rpc_id <= UINT8_MAX) { |
| 396 | // The ID fits in 1 byte |
| 397 | name_id_compression = NETWORK_NAME_ID_COMPRESSION_8; |
| 398 | MAKE_ROOM(ofs + 1); |
| 399 | packet_cache.write[ofs] = static_cast<uint8_t>(p_rpc_id); |
| 400 | ofs += 1; |
| 401 | } else { |
| 402 | // The ID is larger, let's use 2 bytes |
| 403 | name_id_compression = NETWORK_NAME_ID_COMPRESSION_16; |
| 404 | MAKE_ROOM(ofs + 2); |
| 405 | encode_uint16(p_rpc_id, &(packet_cache.write[ofs])); |
| 406 | ofs += 2; |
| 407 | } |
| 408 | |
| 409 | int len; |
| 410 | Error err = MultiplayerAPI::encode_and_compress_variants(p_arg, p_argcount, nullptr, len, &byte_only_or_no_args, multiplayer->is_object_decoding_allowed()); |
| 411 | ERR_FAIL_COND_MSG(err != OK, "Unable to encode RPC arguments. THIS IS LIKELY A BUG IN THE ENGINE!"); |
| 412 | if (byte_only_or_no_args) { |
| 413 | MAKE_ROOM(ofs + len); |
| 414 | } else { |
| 415 | MAKE_ROOM(ofs + 1 + len); |
| 416 | packet_cache.write[ofs] = p_argcount; |
| 417 | ofs += 1; |
| 418 | } |
| 419 | if (len) { |
| 420 | MultiplayerAPI::encode_and_compress_variants(p_arg, p_argcount, &packet_cache.write[ofs], len, &byte_only_or_no_args, multiplayer->is_object_decoding_allowed()); |
| 421 | ofs += len; |
| 422 | } |
| 423 | |
| 424 | ERR_FAIL_COND(command_type > 7); |
| 425 | ERR_FAIL_COND(node_id_compression > 3); |
| 426 | ERR_FAIL_COND(name_id_compression > 1); |
| 427 | |
| 428 | #ifdef DEBUG_ENABLED |
| 429 | _profile_node_data("rpc_out", p_node->get_instance_id(), ofs); |
| 430 | #endif |
| 431 | |
| 432 | // We can now set the meta |
| 433 | 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); |
| 434 | |
| 435 | // Take chance and set transfer mode, since all send methods will use it. |
| 436 | peer->set_transfer_channel(p_config.channel); |
| 437 | peer->set_transfer_mode(p_config.transfer_mode); |
| 438 | |
| 439 | if (has_all_peers) { |
| 440 | for (const int P : targets) { |
| 441 | multiplayer->send_command(P, packet_cache.ptr(), ofs); |
| 442 | } |
| 443 | } else { |
| 444 | // Unreachable because the node ID is never compressed if the peers doesn't know it. |
| 445 | CRASH_COND(node_id_compression != NETWORK_NODE_ID_COMPRESSION_32); |
| 446 | |
| 447 | // Not all verified path, so send one by one. |
| 448 | |
| 449 | // Append path at the end, since we will need it for some packets. |
| 450 | CharString pname = String(multiplayer->get_root_path().rel_path_to(p_node->get_path())).utf8(); |
| 451 | int path_len = encode_cstring(pname.get_data(), nullptr); |
| 452 | MAKE_ROOM(ofs + path_len); |
| 453 | encode_cstring(pname.get_data(), &(packet_cache.write[ofs])); |
| 454 | |
| 455 | // Not all verified path, so check which needs the longer packet. |
| 456 | for (const int P : targets) { |
| 457 | bool confirmed = multiplayer_cache->is_cache_confirmed(p_node, P); |
| 458 | if (confirmed) { |
| 459 | // This one confirmed path, so use id. |
| 460 | encode_uint32(psc_id, &(packet_cache.write[1])); |
| 461 | multiplayer->send_command(P, packet_cache.ptr(), ofs); |
| 462 | } else { |
| 463 | // This one did not confirm path yet, so use entire path (sorry!). |
| 464 | encode_uint32(0x80000000 | ofs, &(packet_cache.write[1])); // Offset to path and flag. |
| 465 | multiplayer->send_command(P, packet_cache.ptr(), ofs + path_len); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | Error SceneRPCInterface::rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) { |
| 472 | Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer(); |
| 473 | ERR_FAIL_COND_V_MSG(peer.is_null(), ERR_UNCONFIGURED, "Trying to call an RPC while no multiplayer peer is active."); |
| 474 | Node *node = Object::cast_to<Node>(p_obj); |
| 475 | ERR_FAIL_COND_V_MSG(!node || !node->is_inside_tree(), ERR_INVALID_PARAMETER, "The object must be a valid Node inside the SceneTree"); |
| 476 | 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."); |
| 477 | |
| 478 | int caller_id = multiplayer->get_unique_id(); |
| 479 | bool call_local_native = false; |
| 480 | bool call_local_script = false; |
| 481 | const RPCConfigCache &config_cache = _get_node_config(node); |
| 482 | uint16_t rpc_id = config_cache.ids.has(p_method) ? config_cache.ids[p_method] : UINT16_MAX; |
| 483 | ERR_FAIL_COND_V_MSG(rpc_id == UINT16_MAX, ERR_INVALID_PARAMETER, |
| 484 | 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())); |
| 485 | const RPCConfig &config = config_cache.configs[rpc_id]; |
| 486 | |
| 487 | 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."); |
| 488 | |
| 489 | if (p_peer_id == 0 || p_peer_id == caller_id || (p_peer_id < 0 && p_peer_id != -caller_id)) { |
| 490 | if (rpc_id & (1 << 15)) { |
| 491 | call_local_native = config.call_local; |
| 492 | } else { |
| 493 | call_local_script = config.call_local; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (p_peer_id != caller_id) { |
| 498 | _send_rpc(node, p_peer_id, rpc_id, config, p_method, p_arg, p_argcount); |
| 499 | } |
| 500 | |
| 501 | if (call_local_native) { |
| 502 | Callable::CallError ce; |
| 503 | |
| 504 | multiplayer->set_remote_sender_override(multiplayer->get_unique_id()); |
| 505 | node->callp(p_method, p_arg, p_argcount, ce); |
| 506 | multiplayer->set_remote_sender_override(0); |
| 507 | |
| 508 | if (ce.error != Callable::CallError::CALL_OK) { |
| 509 | String error = Variant::get_call_error_text(node, p_method, p_arg, p_argcount, ce); |
| 510 | error = "rpc() aborted in local call: - " + error + "."; |
| 511 | ERR_PRINT(error); |
| 512 | return FAILED; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (call_local_script) { |
| 517 | Callable::CallError ce; |
| 518 | ce.error = Callable::CallError::CALL_OK; |
| 519 | |
| 520 | multiplayer->set_remote_sender_override(multiplayer->get_unique_id()); |
| 521 | node->get_script_instance()->callp(p_method, p_arg, p_argcount, ce); |
| 522 | multiplayer->set_remote_sender_override(0); |
| 523 | |
| 524 | if (ce.error != Callable::CallError::CALL_OK) { |
| 525 | String error = Variant::get_call_error_text(node, p_method, p_arg, p_argcount, ce); |
| 526 | error = "rpc() aborted in script local call: - " + error + "."; |
| 527 | ERR_PRINT(error); |
| 528 | return FAILED; |
| 529 | } |
| 530 | } |
| 531 | return OK; |
| 532 | } |