scene_replication_config.cpp 8.3 KB

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