multiplayer_api.h 5.7 KB

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