scene_replication_config.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. bool SceneReplicationConfig::_set(const StringName &p_name, const Variant &p_value) {
  32. String prop_name = p_name;
  33. if (prop_name.begins_with("properties/")) {
  34. int idx = prop_name.get_slicec('/', 1).to_int();
  35. String what = prop_name.get_slicec('/', 2);
  36. if (properties.size() == idx && what == "path") {
  37. ERR_FAIL_COND_V(p_value.get_type() != Variant::NODE_PATH, false);
  38. NodePath path = p_value;
  39. ERR_FAIL_COND_V(path.is_empty() || path.get_subname_count() == 0, false);
  40. add_property(path);
  41. return true;
  42. }
  43. ERR_FAIL_INDEX_V(idx, properties.size(), false);
  44. const ReplicationProperty &prop = properties.get(idx);
  45. if (what == "replication_mode") {
  46. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  47. ReplicationMode mode = (ReplicationMode)p_value.operator int();
  48. ERR_FAIL_COND_V(mode < REPLICATION_MODE_NEVER || mode > REPLICATION_MODE_ON_CHANGE, false);
  49. property_set_replication_mode(prop.name, mode);
  50. return true;
  51. }
  52. ERR_FAIL_COND_V(p_value.get_type() != Variant::BOOL, false);
  53. if (what == "spawn") {
  54. property_set_spawn(prop.name, p_value);
  55. return true;
  56. } else if (what == "sync") {
  57. // Deprecated.
  58. property_set_sync(prop.name, p_value);
  59. return true;
  60. } else if (what == "watch") {
  61. // Deprecated.
  62. property_set_watch(prop.name, p_value);
  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.get(idx);
  75. if (what == "path") {
  76. r_ret = prop.name;
  77. return true;
  78. } else if (what == "spawn") {
  79. r_ret = prop.spawn;
  80. return true;
  81. } else if (what == "replication_mode") {
  82. r_ret = prop.mode;
  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::INT, "properties/" + itos(i) + "/replication_mode", PROPERTY_HINT_ENUM, "Never,Always,On Change", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  93. }
  94. }
  95. void SceneReplicationConfig::reset_state() {
  96. dirty = false;
  97. properties.clear();
  98. sync_props.clear();
  99. spawn_props.clear();
  100. watch_props.clear();
  101. }
  102. TypedArray<NodePath> SceneReplicationConfig::get_properties() const {
  103. TypedArray<NodePath> paths;
  104. for (const ReplicationProperty &prop : properties) {
  105. paths.push_back(prop.name);
  106. }
  107. return paths;
  108. }
  109. void SceneReplicationConfig::add_property(const NodePath &p_path, int p_index) {
  110. ERR_FAIL_COND(properties.find(p_path));
  111. ERR_FAIL_COND(p_path == NodePath());
  112. if (p_index < 0 || p_index == properties.size()) {
  113. properties.push_back(ReplicationProperty(p_path));
  114. dirty = true;
  115. return;
  116. }
  117. ERR_FAIL_INDEX(p_index, properties.size());
  118. List<ReplicationProperty>::Element *I = properties.front();
  119. int c = 0;
  120. while (c < p_index) {
  121. I = I->next();
  122. c++;
  123. }
  124. properties.insert_before(I, ReplicationProperty(p_path));
  125. dirty = true;
  126. }
  127. void SceneReplicationConfig::remove_property(const NodePath &p_path) {
  128. properties.erase(p_path);
  129. dirty = true;
  130. }
  131. bool SceneReplicationConfig::has_property(const NodePath &p_path) const {
  132. for (const ReplicationProperty &property : properties) {
  133. if (property.name == p_path) {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. int SceneReplicationConfig::property_get_index(const NodePath &p_path) const {
  140. int i = 0;
  141. for (List<ReplicationProperty>::ConstIterator itr = properties.begin(); itr != properties.end(); ++itr, ++i) {
  142. if (itr->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. dirty = true;
  161. }
  162. bool SceneReplicationConfig::property_get_sync(const NodePath &p_path) {
  163. List<ReplicationProperty>::Element *E = properties.find(p_path);
  164. ERR_FAIL_COND_V(!E, false);
  165. return E->get().mode == REPLICATION_MODE_ALWAYS;
  166. }
  167. void SceneReplicationConfig::property_set_sync(const NodePath &p_path, bool p_enabled) {
  168. if (p_enabled) {
  169. property_set_replication_mode(p_path, REPLICATION_MODE_ALWAYS);
  170. } else if (property_get_replication_mode(p_path) == REPLICATION_MODE_ALWAYS) {
  171. property_set_replication_mode(p_path, REPLICATION_MODE_NEVER);
  172. }
  173. }
  174. bool SceneReplicationConfig::property_get_watch(const NodePath &p_path) {
  175. List<ReplicationProperty>::Element *E = properties.find(p_path);
  176. ERR_FAIL_COND_V(!E, false);
  177. return E->get().mode == REPLICATION_MODE_ON_CHANGE;
  178. }
  179. void SceneReplicationConfig::property_set_watch(const NodePath &p_path, bool p_enabled) {
  180. if (p_enabled) {
  181. property_set_replication_mode(p_path, REPLICATION_MODE_ON_CHANGE);
  182. } else if (property_get_replication_mode(p_path) == REPLICATION_MODE_ON_CHANGE) {
  183. property_set_replication_mode(p_path, REPLICATION_MODE_NEVER);
  184. }
  185. }
  186. SceneReplicationConfig::ReplicationMode SceneReplicationConfig::property_get_replication_mode(const NodePath &p_path) {
  187. List<ReplicationProperty>::Element *E = properties.find(p_path);
  188. ERR_FAIL_COND_V(!E, REPLICATION_MODE_NEVER);
  189. return E->get().mode;
  190. }
  191. void SceneReplicationConfig::property_set_replication_mode(const NodePath &p_path, ReplicationMode p_mode) {
  192. List<ReplicationProperty>::Element *E = properties.find(p_path);
  193. ERR_FAIL_COND(!E);
  194. if (E->get().mode == p_mode) {
  195. return;
  196. }
  197. E->get().mode = p_mode;
  198. dirty = true;
  199. }
  200. void SceneReplicationConfig::_update() {
  201. if (!dirty) {
  202. return;
  203. }
  204. dirty = false;
  205. sync_props.clear();
  206. spawn_props.clear();
  207. watch_props.clear();
  208. for (const ReplicationProperty &prop : properties) {
  209. if (prop.spawn) {
  210. spawn_props.push_back(prop.name);
  211. }
  212. switch (prop.mode) {
  213. case REPLICATION_MODE_ALWAYS:
  214. sync_props.push_back(prop.name);
  215. break;
  216. case REPLICATION_MODE_ON_CHANGE:
  217. watch_props.push_back(prop.name);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. }
  224. const List<NodePath> &SceneReplicationConfig::get_spawn_properties() {
  225. if (dirty) {
  226. _update();
  227. }
  228. return spawn_props;
  229. }
  230. const List<NodePath> &SceneReplicationConfig::get_sync_properties() {
  231. if (dirty) {
  232. _update();
  233. }
  234. return sync_props;
  235. }
  236. const List<NodePath> &SceneReplicationConfig::get_watch_properties() {
  237. if (dirty) {
  238. _update();
  239. }
  240. return watch_props;
  241. }
  242. void SceneReplicationConfig::_bind_methods() {
  243. ClassDB::bind_method(D_METHOD("get_properties"), &SceneReplicationConfig::get_properties);
  244. ClassDB::bind_method(D_METHOD("add_property", "path", "index"), &SceneReplicationConfig::add_property, DEFVAL(-1));
  245. ClassDB::bind_method(D_METHOD("has_property", "path"), &SceneReplicationConfig::has_property);
  246. ClassDB::bind_method(D_METHOD("remove_property", "path"), &SceneReplicationConfig::remove_property);
  247. ClassDB::bind_method(D_METHOD("property_get_index", "path"), &SceneReplicationConfig::property_get_index);
  248. ClassDB::bind_method(D_METHOD("property_get_spawn", "path"), &SceneReplicationConfig::property_get_spawn);
  249. ClassDB::bind_method(D_METHOD("property_set_spawn", "path", "enabled"), &SceneReplicationConfig::property_set_spawn);
  250. ClassDB::bind_method(D_METHOD("property_get_replication_mode", "path"), &SceneReplicationConfig::property_get_replication_mode);
  251. ClassDB::bind_method(D_METHOD("property_set_replication_mode", "path", "mode"), &SceneReplicationConfig::property_set_replication_mode);
  252. BIND_ENUM_CONSTANT(REPLICATION_MODE_NEVER);
  253. BIND_ENUM_CONSTANT(REPLICATION_MODE_ALWAYS);
  254. BIND_ENUM_CONSTANT(REPLICATION_MODE_ON_CHANGE);
  255. // Deprecated.
  256. ClassDB::bind_method(D_METHOD("property_get_sync", "path"), &SceneReplicationConfig::property_get_sync);
  257. ClassDB::bind_method(D_METHOD("property_set_sync", "path", "enabled"), &SceneReplicationConfig::property_set_sync);
  258. ClassDB::bind_method(D_METHOD("property_get_watch", "path"), &SceneReplicationConfig::property_get_watch);
  259. ClassDB::bind_method(D_METHOD("property_set_watch", "path", "enabled"), &SceneReplicationConfig::property_set_watch);
  260. }