multiplayer_replicator.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************/
  2. /* multiplayer_replicator.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_REPLICATOR_H
  31. #define MULTIPLAYER_REPLICATOR_H
  32. #include "core/multiplayer/multiplayer_api.h"
  33. #include "core/io/resource_uid.h"
  34. #include "core/templates/hash_map.h"
  35. #include "core/variant/typed_array.h"
  36. class MultiplayerReplicator : public Object {
  37. GDCLASS(MultiplayerReplicator, Object);
  38. public:
  39. enum {
  40. SPAWN_CMD_OFFSET = 9,
  41. SYNC_CMD_OFFSET = 9,
  42. };
  43. enum ReplicationMode {
  44. REPLICATION_MODE_NONE,
  45. REPLICATION_MODE_SERVER,
  46. REPLICATION_MODE_CUSTOM,
  47. };
  48. struct SceneConfig {
  49. ReplicationMode mode;
  50. uint64_t sync_interval = 0;
  51. uint64_t sync_last = 0;
  52. uint8_t sync_recv = 0;
  53. List<StringName> properties;
  54. List<StringName> sync_properties;
  55. Callable on_spawn_despawn_send;
  56. Callable on_spawn_despawn_receive;
  57. Callable on_sync_send;
  58. Callable on_sync_receive;
  59. };
  60. protected:
  61. static void _bind_methods();
  62. private:
  63. enum {
  64. BYTE_OR_ZERO_SHIFT = MultiplayerAPI::CMD_FLAG_0_SHIFT,
  65. };
  66. enum {
  67. BYTE_OR_ZERO_FLAG = 1 << BYTE_OR_ZERO_SHIFT,
  68. };
  69. MultiplayerAPI *multiplayer = nullptr;
  70. Vector<uint8_t> packet_cache;
  71. Map<ResourceUID::ID, SceneConfig> replications;
  72. Map<ObjectID, ResourceUID::ID> replicated_nodes;
  73. HashMap<ResourceUID::ID, List<ObjectID>> tracked_objects;
  74. // Encoding
  75. Error _get_state(const List<StringName> &p_properties, const Object *p_obj, List<Variant> &r_variant);
  76. Error _encode_state(const List<Variant> &p_variants, uint8_t *p_buffer, int &r_len, bool *r_raw = nullptr);
  77. Error _decode_state(const List<StringName> &p_cfg, Object *p_obj, const uint8_t *p_buffer, int p_len, int &r_len, bool p_raw = false);
  78. // Spawn
  79. Error _spawn_despawn(ResourceUID::ID p_scene_id, Object *p_obj, int p_peer, bool p_spawn);
  80. Error _send_spawn_despawn(int p_peer_id, const ResourceUID::ID &p_scene_id, const Variant &p_data, bool p_spawn);
  81. void _process_default_spawn_despawn(int p_from, const ResourceUID::ID &p_scene_id, const uint8_t *p_packet, int p_packet_len, bool p_spawn);
  82. Error _send_default_spawn_despawn(int p_peer_id, const ResourceUID::ID &p_scene_id, Object *p_obj, const NodePath &p_path, bool p_spawn);
  83. // Sync
  84. void _process_default_sync(const ResourceUID::ID &p_id, const uint8_t *p_packet, int p_packet_len);
  85. Error _sync_all_default(const ResourceUID::ID &p_scene_id, int p_peer);
  86. void _track(const ResourceUID::ID &p_scene_id, Object *p_object);
  87. void _untrack(const ResourceUID::ID &p_scene_id, Object *p_object);
  88. public:
  89. void clear();
  90. // Encoding
  91. PackedByteArray encode_state(const ResourceUID::ID &p_scene_id, const Object *p_node, bool p_initial);
  92. Error decode_state(const ResourceUID::ID &p_scene_id, Object *p_node, PackedByteArray p_data, bool p_initial);
  93. // Spawn
  94. Error spawn_config(const ResourceUID::ID &p_id, ReplicationMode p_mode, const TypedArray<StringName> &p_props = TypedArray<StringName>(), const Callable &p_on_send = Callable(), const Callable &p_on_recv = Callable());
  95. Error spawn(ResourceUID::ID p_scene_id, Object *p_obj, int p_peer = 0);
  96. Error despawn(ResourceUID::ID p_scene_id, Object *p_obj, int p_peer = 0);
  97. Error send_despawn(int p_peer_id, const ResourceUID::ID &p_scene_id, const Variant &p_data = Variant(), const NodePath &p_path = NodePath());
  98. Error send_spawn(int p_peer_id, const ResourceUID::ID &p_scene_id, const Variant &p_data = Variant(), const NodePath &p_path = NodePath());
  99. // Sync
  100. Error sync_config(const ResourceUID::ID &p_id, uint64_t p_interval, const TypedArray<StringName> &p_props = TypedArray<StringName>(), const Callable &p_on_send = Callable(), const Callable &p_on_recv = Callable());
  101. Error sync_all(const ResourceUID::ID &p_scene_id, int p_peer);
  102. Error send_sync(int p_peer_id, const ResourceUID::ID &p_scene_id, PackedByteArray p_data, Multiplayer::TransferMode p_mode, int p_channel);
  103. void track(const ResourceUID::ID &p_scene_id, Object *p_object);
  104. void untrack(const ResourceUID::ID &p_scene_id, Object *p_object);
  105. // Used by MultiplayerAPI
  106. void spawn_all(int p_peer);
  107. void process_spawn_despawn(int p_from, const uint8_t *p_packet, int p_packet_len, bool p_spawn);
  108. void process_sync(int p_from, const uint8_t *p_packet, int p_packet_len);
  109. void scene_enter_exit_notify(const String &p_scene, Node *p_node, bool p_enter);
  110. void poll();
  111. MultiplayerReplicator(MultiplayerAPI *p_multiplayer) {
  112. multiplayer = p_multiplayer;
  113. }
  114. };
  115. VARIANT_ENUM_CAST(MultiplayerReplicator::ReplicationMode);
  116. #endif // MULTIPLAYER_REPLICATOR_H