| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/object/ref_counted.h" |
| 34 |
#include "scene/main/multiplayer_peer.h" |
| 35 |
|
| 36 |
class MultiplayerAPI : public RefCounted { |
| 37 |
GDCLASS(MultiplayerAPI, RefCounted); |
| 38 |
|
| 39 |
private: |
| 40 |
static StringName default_interface; |
| 41 |
|
| 42 |
protected: |
| 43 |
static void _bind_methods(); |
| 44 |
Error _rpc_bind(int p_peer, Object *p_obj, const StringName &p_method, Array args = Array()); |
| 45 |
|
| 46 |
public: |
| 47 |
enum RPCMode { |
| 48 |
RPC_MODE_DISABLED, |
| 49 |
RPC_MODE_ANY_PEER, |
| 50 |
RPC_MODE_AUTHORITY, |
| 51 |
}; |
| 52 |
|
| 53 |
static Ref<MultiplayerAPI> create_default_interface(); |
| 54 |
static void set_default_interface(const StringName &p_interface); |
| 55 |
static StringName get_default_interface(); |
| 56 |
|
| 57 |
static Error encode_and_compress_variant(const Variant &p_variant, uint8_t *p_buffer, int &r_len, bool p_allow_object_decoding); |
| 58 |
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); |
| 59 |
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); |
| 60 |
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); |
| 61 |
|
| 62 |
virtual Error poll() = 0; |
| 63 |
virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) = 0; |
| 64 |
virtual Ref<MultiplayerPeer> get_multiplayer_peer() = 0; |
| 65 |
virtual int get_unique_id() = 0; |
| 66 |
virtual Vector<int> get_peer_ids() = 0; |
| 67 |
|
| 68 |
virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) = 0; |
| 69 |
virtual int get_remote_sender_id() = 0; |
| 70 |
|
| 71 |
virtual Error object_configuration_add(Object *p_object, Variant p_config) = 0; |
| 72 |
virtual Error object_configuration_remove(Object *p_object, Variant p_config) = 0; |
| 73 |
|
| 74 |
bool has_multiplayer_peer() { return get_multiplayer_peer().is_valid(); } |
| 75 |
bool is_server() { return get_unique_id() == MultiplayerPeer::TARGET_PEER_SERVER; } |
| 76 |
|
| 77 |
virtual ~MultiplayerAPI() {} |
| 78 |
}; |
| 79 |
|
| 80 |
VARIANT_ENUM_CAST(MultiplayerAPI::RPCMode); |
| 81 |
|
| 82 |
class MultiplayerAPIExtension : public MultiplayerAPI { |
| 83 |
GDCLASS(MultiplayerAPIExtension, MultiplayerAPI); |
| 84 |
|
| 85 |
protected: |
| 86 |
static void _bind_methods(); |
| 87 |
|
| 88 |
public: |
| 89 |
virtual Error poll() override; |
| 90 |
virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) override; |
| 91 |
virtual Ref<MultiplayerPeer> get_multiplayer_peer() override; |
| 92 |
virtual int get_unique_id() override; |
| 93 |
virtual Vector<int> get_peer_ids() override; |
| 94 |
|
| 95 |
virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) override; |
| 96 |
virtual int get_remote_sender_id() override; |
| 97 |
|
| 98 |
virtual Error object_configuration_add(Object *p_object, Variant p_config) override; |
| 99 |
virtual Error object_configuration_remove(Object *p_object, Variant p_config) override; |
| 100 |
|
| 101 |
|
| 102 |
GDVIRTUAL0R(Error, _poll); |
| 103 |
GDVIRTUAL1(_set_multiplayer_peer, Ref<MultiplayerPeer>); |
| 104 |
GDVIRTUAL0R(Ref<MultiplayerPeer>, _get_multiplayer_peer); |
| 105 |
GDVIRTUAL0RC(int, _get_unique_id); |
| 106 |
GDVIRTUAL0RC(PackedInt32Array, _get_peer_ids); |
| 107 |
GDVIRTUAL4R(Error, _rpc, int, Object *, StringName, Array); |
| 108 |
GDVIRTUAL0RC(int, _get_remote_sender_id); |
| 109 |
GDVIRTUAL2R(Error, _object_configuration_add, Object *, Variant); |
| 110 |
GDVIRTUAL2R(Error, _object_configuration_remove, Object *, Variant); |
| 111 |
}; |