multiplayer_synchronizer.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*************************************************************************/
  2. /* multiplayer_synchronizer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "multiplayer_synchronizer.h"
  31. #include "core/config/engine.h"
  32. #include "core/multiplayer/multiplayer_api.h"
  33. Object *MultiplayerSynchronizer::_get_prop_target(Object *p_obj, const NodePath &p_path) {
  34. if (p_path.get_name_count() == 0) {
  35. return p_obj;
  36. }
  37. Node *node = Object::cast_to<Node>(p_obj);
  38. ERR_FAIL_COND_V_MSG(!node || !node->has_node(p_path), nullptr, vformat("Node '%s' not found.", p_path));
  39. return node->get_node(p_path);
  40. }
  41. void MultiplayerSynchronizer::_stop() {
  42. Node *node = is_inside_tree() ? get_node_or_null(root_path) : nullptr;
  43. if (node) {
  44. get_multiplayer()->replication_stop(node, this);
  45. }
  46. }
  47. void MultiplayerSynchronizer::_start() {
  48. Node *node = is_inside_tree() ? get_node_or_null(root_path) : nullptr;
  49. if (node) {
  50. get_multiplayer()->replication_start(node, this);
  51. }
  52. }
  53. Error MultiplayerSynchronizer::get_state(const List<NodePath> &p_properties, Object *p_obj, Vector<Variant> &r_variant, Vector<const Variant *> &r_variant_ptrs) {
  54. ERR_FAIL_COND_V(!p_obj, ERR_INVALID_PARAMETER);
  55. r_variant.resize(p_properties.size());
  56. r_variant_ptrs.resize(r_variant.size());
  57. int i = 0;
  58. for (const NodePath &prop : p_properties) {
  59. bool valid = false;
  60. const Object *obj = _get_prop_target(p_obj, prop);
  61. ERR_FAIL_COND_V(!obj, FAILED);
  62. r_variant.write[i] = obj->get(prop.get_concatenated_subnames(), &valid);
  63. r_variant_ptrs.write[i] = &r_variant[i];
  64. ERR_FAIL_COND_V_MSG(!valid, ERR_INVALID_DATA, vformat("Property '%s' not found.", prop));
  65. i++;
  66. }
  67. return OK;
  68. }
  69. Error MultiplayerSynchronizer::set_state(const List<NodePath> &p_properties, Object *p_obj, const Vector<Variant> &p_state) {
  70. ERR_FAIL_COND_V(!p_obj, ERR_INVALID_PARAMETER);
  71. int i = 0;
  72. for (const NodePath &prop : p_properties) {
  73. Object *obj = _get_prop_target(p_obj, prop);
  74. ERR_FAIL_COND_V(!obj, FAILED);
  75. obj->set(prop.get_concatenated_subnames(), p_state[i]);
  76. i += 1;
  77. }
  78. return OK;
  79. }
  80. void MultiplayerSynchronizer::_bind_methods() {
  81. ClassDB::bind_method(D_METHOD("set_root_path", "path"), &MultiplayerSynchronizer::set_root_path);
  82. ClassDB::bind_method(D_METHOD("get_root_path"), &MultiplayerSynchronizer::get_root_path);
  83. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_path"), "set_root_path", "get_root_path");
  84. ClassDB::bind_method(D_METHOD("set_replication_interval", "milliseconds"), &MultiplayerSynchronizer::set_replication_interval);
  85. ClassDB::bind_method(D_METHOD("get_replication_interval"), &MultiplayerSynchronizer::get_replication_interval);
  86. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "replication_interval", PROPERTY_HINT_RANGE, "0,5,0.001"), "set_replication_interval", "get_replication_interval");
  87. ClassDB::bind_method(D_METHOD("set_replication_config", "config"), &MultiplayerSynchronizer::set_replication_config);
  88. ClassDB::bind_method(D_METHOD("get_replication_config"), &MultiplayerSynchronizer::get_replication_config);
  89. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "SceneReplicationConfig"), "set_replication_config", "get_replication_config");
  90. }
  91. void MultiplayerSynchronizer::_notification(int p_what) {
  92. #ifdef TOOLS_ENABLED
  93. if (Engine::get_singleton()->is_editor_hint()) {
  94. return;
  95. }
  96. #endif
  97. if (root_path.is_empty()) {
  98. return;
  99. }
  100. if (p_what == NOTIFICATION_ENTER_TREE) {
  101. _start();
  102. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  103. _stop();
  104. }
  105. }
  106. void MultiplayerSynchronizer::set_replication_interval(double p_interval) {
  107. ERR_FAIL_COND_MSG(p_interval < 0, "Interval must be greater or equal to 0 (where 0 means default)");
  108. interval_msec = uint64_t(p_interval * 1000);
  109. }
  110. double MultiplayerSynchronizer::get_replication_interval() const {
  111. return double(interval_msec) / 1000.0;
  112. }
  113. uint64_t MultiplayerSynchronizer::get_replication_interval_msec() const {
  114. return interval_msec;
  115. }
  116. void MultiplayerSynchronizer::set_replication_config(Ref<SceneReplicationConfig> p_config) {
  117. replication_config = p_config;
  118. }
  119. Ref<SceneReplicationConfig> MultiplayerSynchronizer::get_replication_config() {
  120. return replication_config;
  121. }
  122. void MultiplayerSynchronizer::set_root_path(const NodePath &p_path) {
  123. _stop();
  124. root_path = p_path;
  125. _start();
  126. }
  127. NodePath MultiplayerSynchronizer::get_root_path() const {
  128. return root_path;
  129. }
  130. void MultiplayerSynchronizer::set_multiplayer_authority(int p_peer_id, bool p_recursive) {
  131. Node *node = is_inside_tree() ? get_node_or_null(root_path) : nullptr;
  132. if (!node) {
  133. Node::set_multiplayer_authority(p_peer_id, p_recursive);
  134. return;
  135. }
  136. get_multiplayer()->replication_stop(node, this);
  137. Node::set_multiplayer_authority(p_peer_id, p_recursive);
  138. get_multiplayer()->replication_start(node, this);
  139. }