1/**************************************************************************/
2/* scene_rpc_interface.h */
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#ifndef SCENE_RPC_INTERFACE_H
32#define SCENE_RPC_INTERFACE_H
33
34#include "core/object/ref_counted.h"
35#include "scene/main/multiplayer_api.h"
36
37class SceneMultiplayer;
38class Node;
39
40class SceneRPCInterface : public RefCounted {
41 GDCLASS(SceneRPCInterface, RefCounted);
42
43private:
44 struct RPCConfig {
45 StringName name;
46 MultiplayerAPI::RPCMode rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
47 bool call_local = false;
48 MultiplayerPeer::TransferMode transfer_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
49 int channel = 0;
50
51 bool operator==(RPCConfig const &p_other) const {
52 return name == p_other.name;
53 }
54 };
55
56 struct RPCConfigCache {
57 HashMap<uint16_t, RPCConfig> configs;
58 HashMap<StringName, uint16_t> ids;
59 };
60
61 struct SortRPCConfig {
62 StringName::AlphCompare compare;
63 bool operator()(const RPCConfig &p_a, const RPCConfig &p_b) const {
64 return compare(p_a.name, p_b.name);
65 }
66 };
67
68 enum NetworkNodeIdCompression {
69 NETWORK_NODE_ID_COMPRESSION_8 = 0,
70 NETWORK_NODE_ID_COMPRESSION_16,
71 NETWORK_NODE_ID_COMPRESSION_32,
72 };
73
74 enum NetworkNameIdCompression {
75 NETWORK_NAME_ID_COMPRESSION_8 = 0,
76 NETWORK_NAME_ID_COMPRESSION_16,
77 };
78
79 SceneMultiplayer *multiplayer = nullptr;
80 Vector<uint8_t> packet_cache;
81
82 HashMap<ObjectID, RPCConfigCache> rpc_cache;
83
84#ifdef DEBUG_ENABLED
85 _FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);
86#endif
87
88protected:
89 void _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);
90
91 void _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);
92 Node *_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len);
93
94 void _parse_rpc_config(const Variant &p_config, bool p_for_node, RPCConfigCache &r_cache);
95 const RPCConfigCache &_get_node_config(const Node *p_node);
96
97public:
98 Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
99 void process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len);
100 String get_rpc_md5(const Object *p_obj);
101
102 SceneRPCInterface(SceneMultiplayer *p_multiplayer) { multiplayer = p_multiplayer; }
103};
104
105#endif // SCENE_RPC_INTERFACE_H
106