multiplayer_api.h 5.6 KB

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