scene_replication_config.cpp 11 KB

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