| 1 | /**************************************************************************/ |
| 2 | /* multiplayer_api.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 MULTIPLAYER_API_H |
| 32 | #define MULTIPLAYER_API_H |
| 33 | |
| 34 | #include "core/object/ref_counted.h" |
| 35 | #include "scene/main/multiplayer_peer.h" |
| 36 | |
| 37 | class MultiplayerAPI : public RefCounted { |
| 38 | GDCLASS(MultiplayerAPI, RefCounted); |
| 39 | |
| 40 | private: |
| 41 | static StringName default_interface; |
| 42 | |
| 43 | protected: |
| 44 | static void _bind_methods(); |
| 45 | Error _rpc_bind(int p_peer, Object *p_obj, const StringName &p_method, Array args = Array()); |
| 46 | |
| 47 | public: |
| 48 | enum RPCMode { |
| 49 | RPC_MODE_DISABLED, // No rpc for this method, calls to this will be blocked (default) |
| 50 | RPC_MODE_ANY_PEER, // Any peer can call this RPC |
| 51 | RPC_MODE_AUTHORITY, // Only the node's multiplayer authority (server by default) can call this RPC |
| 52 | }; |
| 53 | |
| 54 | static Ref<MultiplayerAPI> create_default_interface(); |
| 55 | static void set_default_interface(const StringName &p_interface); |
| 56 | static StringName get_default_interface(); |
| 57 | |
| 58 | static Error encode_and_compress_variant(const Variant &p_variant, uint8_t *p_buffer, int &r_len, bool p_allow_object_decoding); |
| 59 | static Error decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_object_decoding); |
| 60 | static Error encode_and_compress_variants(const Variant **p_variants, int p_count, uint8_t *p_buffer, int &r_len, bool *r_raw = nullptr, bool p_allow_object_decoding = false); |
| 61 | static Error decode_and_decompress_variants(Vector<Variant> &r_variants, const uint8_t *p_buffer, int p_len, int &r_len, bool p_raw = false, bool p_allow_object_decoding = false); |
| 62 | |
| 63 | virtual Error poll() = 0; |
| 64 | virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) = 0; |
| 65 | virtual Ref<MultiplayerPeer> get_multiplayer_peer() = 0; |
| 66 | virtual int get_unique_id() = 0; |
| 67 | virtual Vector<int> get_peer_ids() = 0; |
| 68 | |
| 69 | virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) = 0; |
| 70 | virtual int get_remote_sender_id() = 0; |
| 71 | |
| 72 | virtual Error object_configuration_add(Object *p_object, Variant p_config) = 0; |
| 73 | virtual Error object_configuration_remove(Object *p_object, Variant p_config) = 0; |
| 74 | |
| 75 | bool has_multiplayer_peer() { return get_multiplayer_peer().is_valid(); } |
| 76 | bool is_server() { return get_unique_id() == MultiplayerPeer::TARGET_PEER_SERVER; } |
| 77 | |
| 78 | MultiplayerAPI() {} |
| 79 | virtual ~MultiplayerAPI() {} |
| 80 | }; |
| 81 | |
| 82 | VARIANT_ENUM_CAST(MultiplayerAPI::RPCMode); |
| 83 | |
| 84 | class MultiplayerAPIExtension : public MultiplayerAPI { |
| 85 | GDCLASS(MultiplayerAPIExtension, MultiplayerAPI); |
| 86 | |
| 87 | protected: |
| 88 | static void _bind_methods(); |
| 89 | |
| 90 | public: |
| 91 | virtual Error poll() override; |
| 92 | virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) override; |
| 93 | virtual Ref<MultiplayerPeer> get_multiplayer_peer() override; |
| 94 | virtual int get_unique_id() override; |
| 95 | virtual Vector<int> get_peer_ids() override; |
| 96 | |
| 97 | virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) override; |
| 98 | virtual int get_remote_sender_id() override; |
| 99 | |
| 100 | virtual Error object_configuration_add(Object *p_object, Variant p_config) override; |
| 101 | virtual Error object_configuration_remove(Object *p_object, Variant p_config) override; |
| 102 | |
| 103 | // Extensions |
| 104 | GDVIRTUAL0R(Error, _poll); |
| 105 | GDVIRTUAL1(_set_multiplayer_peer, Ref<MultiplayerPeer>); |
| 106 | GDVIRTUAL0R(Ref<MultiplayerPeer>, _get_multiplayer_peer); |
| 107 | GDVIRTUAL0RC(int, _get_unique_id); |
| 108 | GDVIRTUAL0RC(PackedInt32Array, _get_peer_ids); |
| 109 | GDVIRTUAL4R(Error, _rpc, int, Object *, StringName, Array); |
| 110 | GDVIRTUAL0RC(int, _get_remote_sender_id); |
| 111 | GDVIRTUAL2R(Error, _object_configuration_add, Object *, Variant); |
| 112 | GDVIRTUAL2R(Error, _object_configuration_remove, Object *, Variant); |
| 113 | }; |
| 114 | |
| 115 | #endif // MULTIPLAYER_API_H |
| 116 | |