scene_replication_config.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**************************************************************************/
  2. /* scene_replication_config.cpp */
  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. #include "scene_replication_config.h"
  31. #include "scene/main/multiplayer_api.h"
  32. #include "scene/main/node.h"
  33. bool SceneReplicationConfig::_set(const StringName &p_name, const Variant &p_value) {
  34. String prop_name = p_name;
  35. if (prop_name.begins_with("properties/")) {
  36. int idx = prop_name.get_slicec('/', 1).to_int();
  37. String what = prop_name.get_slicec('/', 2);
  38. if (properties.size() == idx && what == "path") {
  39. ERR_FAIL_COND_V(p_value.get_type() != Variant::NODE_PATH, false);
  40. NodePath path = p_value;
  41. ERR_FAIL_COND_V(path.is_empty() || path.get_subname_count() == 0, false);
  42. add_property(path);
  43. return true;
  44. }
  45. ERR_FAIL_COND_V(p_value.get_type() != Variant::BOOL, false);
  46. ERR_FAIL_INDEX_V(idx, properties.size(), false);
  47. ReplicationProperty &prop = properties[idx];
  48. if (what == "sync") {
  49. prop.sync = p_value;
  50. if (prop.sync) {
  51. sync_props.push_back(prop.name);
  52. } else {
  53. sync_props.erase(prop.name);
  54. }
  55. return true;
  56. } else if (what == "spawn") {
  57. prop.spawn = p_value;
  58. if (prop.spawn) {
  59. spawn_props.push_back(prop.name);
  60. } else {
  61. spawn_props.erase(prop.name);
  62. }
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. bool SceneReplicationConfig::_get(const StringName &p_name, Variant &r_ret) const {
  69. String prop_name = p_name;
  70. if (prop_name.begins_with("properties/")) {
  71. int idx = prop_name.get_slicec('/', 1).to_int();
  72. String what = prop_name.get_slicec('/', 2);
  73. ERR_FAIL_INDEX_V(idx, properties.size(), false);
  74. const ReplicationProperty &prop = properties[idx];
  75. if (what == "path") {
  76. r_ret = prop.name;
  77. return true;
  78. } else if (what == "sync") {
  79. r_ret = prop.sync;
  80. return true;
  81. } else if (what == "spawn") {
  82. r_ret = prop.spawn;
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. void SceneReplicationConfig::_get_property_list(List<PropertyInfo> *p_list) const {
  89. for (int i = 0; i < properties.size(); i++) {
  90. p_list->push_back(PropertyInfo(Variant::STRING, "properties/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  91. p_list->push_back(PropertyInfo(Variant::STRING, "properties/" + itos(i) + "/spawn", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  92. p_list->push_back(PropertyInfo(Variant::STRING, "properties/" + itos(i) + "/sync", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  93. }
  94. }
  95. TypedArray<NodePath> SceneReplicationConfig::get_properties() const {
  96. TypedArray<NodePath> paths;
  97. for (const ReplicationProperty &prop : properties) {
  98. paths.push_back(prop.name);
  99. }
  100. return paths;
  101. }
  102. void SceneReplicationConfig::add_property(const NodePath &p_path, int p_index) {
  103. ERR_FAIL_COND(properties.find(p_path));
  104. if (p_index < 0 || p_index == properties.size()) {
  105. properties.push_back(ReplicationProperty(p_path));
  106. sync_props.push_back(p_path);
  107. spawn_props.push_back(p_path);
  108. return;
  109. }
  110. ERR_FAIL_INDEX(p_index, properties.size());
  111. List<ReplicationProperty>::Element *I = properties.front();
  112. int c = 0;
  113. while (c < p_index) {
  114. I = I->next();
  115. c++;
  116. }
  117. properties.insert_before(I, ReplicationProperty(p_path));
  118. sync_props.clear();
  119. spawn_props.clear();
  120. for (const ReplicationProperty &prop : properties) {
  121. if (prop.sync) {
  122. sync_props.push_back(p_path);
  123. }
  124. if (prop.spawn) {
  125. spawn_props.push_back(p_path);
  126. }
  127. }
  128. }
  129. void SceneReplicationConfig::remove_property(const NodePath &p_path) {
  130. properties.erase(p_path);
  131. }
  132. bool SceneReplicationConfig::has_property(const NodePath &p_path) const {
  133. for (int i = 0; i < properties.size(); i++) {
  134. if (properties[i].name == p_path) {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. int SceneReplicationConfig::property_get_index(const NodePath &p_path) const {
  141. for (int i = 0; i < properties.size(); i++) {
  142. if (properties[i].name == p_path) {
  143. return i;
  144. }
  145. }
  146. ERR_FAIL_V(-1);
  147. }
  148. bool SceneReplicationConfig::property_get_spawn(const NodePath &p_path) {
  149. List<ReplicationProperty>::Element *E = properties.find(p_path);
  150. ERR_FAIL_COND_V(!E, false);
  151. return E->get().spawn;
  152. }
  153. void SceneReplicationConfig::property_set_spawn(const NodePath &p_path, bool p_enabled) {
  154. List<ReplicationProperty>::Element *E = properties.find(p_path);
  155. ERR_FAIL_COND(!E);
  156. if (E->get().spawn == p_enabled) {
  157. return;
  158. }
  159. E->get().spawn = p_enabled;
  160. spawn_props.clear();
  161. for (const ReplicationProperty &prop : properties) {
  162. if (prop.spawn) {
  163. spawn_props.push_back(p_path);
  164. }
  165. }
  166. }
  167. bool SceneReplicationConfig::property_get_sync(const NodePath &p_path) {
  168. List<ReplicationProperty>::Element *E = properties.find(p_path);
  169. ERR_FAIL_COND_V(!E, false);
  170. return E->get().sync;
  171. }
  172. void SceneReplicationConfig::property_set_sync(const NodePath &p_path, bool p_enabled) {
  173. List<ReplicationProperty>::Element *E = properties.find(p_path);
  174. ERR_FAIL_COND(!E);
  175. if (E->get().sync == p_enabled) {
  176. return;
  177. }
  178. E->get().sync = p_enabled;
  179. sync_props.clear();
  180. for (const ReplicationProperty &prop : properties) {
  181. if (prop.sync) {
  182. sync_props.push_back(p_path);
  183. }
  184. }
  185. }
  186. void SceneReplicationConfig::_bind_methods() {
  187. ClassDB::bind_method(D_METHOD("get_properties"), &SceneReplicationConfig::get_properties);
  188. ClassDB::bind_method(D_METHOD("add_property", "path", "index"), &SceneReplicationConfig::add_property, DEFVAL(-1));
  189. ClassDB::bind_method(D_METHOD("has_property", "path"), &SceneReplicationConfig::has_property);
  190. ClassDB::bind_method(D_METHOD("remove_property", "path"), &SceneReplicationConfig::remove_property);
  191. ClassDB::bind_method(D_METHOD("property_get_index", "path"), &SceneReplicationConfig::property_get_index);
  192. ClassDB::bind_method(D_METHOD("property_get_spawn", "path"), &SceneReplicationConfig::property_get_spawn);
  193. ClassDB::bind_method(D_METHOD("property_set_spawn", "path", "enabled"), &SceneReplicationConfig::property_set_spawn);
  194. ClassDB::bind_method(D_METHOD("property_get_sync", "path"), &SceneReplicationConfig::property_get_sync);
  195. ClassDB::bind_method(D_METHOD("property_set_sync", "path", "enabled"), &SceneReplicationConfig::property_set_sync);
  196. }