scene_replication_config.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. ReplicationProperty &prop = properties[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[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 (int i = 0; i < properties.size(); i++) {
  135. if (properties[i].name == p_path) {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. int SceneReplicationConfig::property_get_index(const NodePath &p_path) const {
  142. for (int i = 0; i < properties.size(); i++) {
  143. if (properties[i].name == p_path) {
  144. return i;
  145. }
  146. }
  147. ERR_FAIL_V(-1);
  148. }
  149. bool SceneReplicationConfig::property_get_spawn(const NodePath &p_path) {
  150. List<ReplicationProperty>::Element *E = properties.find(p_path);
  151. ERR_FAIL_COND_V(!E, false);
  152. return E->get().spawn;
  153. }
  154. void SceneReplicationConfig::property_set_spawn(const NodePath &p_path, bool p_enabled) {
  155. List<ReplicationProperty>::Element *E = properties.find(p_path);
  156. ERR_FAIL_COND(!E);
  157. if (E->get().spawn == p_enabled) {
  158. return;
  159. }
  160. E->get().spawn = p_enabled;
  161. dirty = true;
  162. }
  163. bool SceneReplicationConfig::property_get_sync(const NodePath &p_path) {
  164. List<ReplicationProperty>::Element *E = properties.find(p_path);
  165. ERR_FAIL_COND_V(!E, false);
  166. return E->get().mode == REPLICATION_MODE_ALWAYS;
  167. }
  168. void SceneReplicationConfig::property_set_sync(const NodePath &p_path, bool p_enabled) {
  169. if (p_enabled) {
  170. property_set_replication_mode(p_path, REPLICATION_MODE_ALWAYS);
  171. } else if (property_get_replication_mode(p_path) == REPLICATION_MODE_ALWAYS) {
  172. property_set_replication_mode(p_path, REPLICATION_MODE_NEVER);
  173. }
  174. }
  175. bool SceneReplicationConfig::property_get_watch(const NodePath &p_path) {
  176. List<ReplicationProperty>::Element *E = properties.find(p_path);
  177. ERR_FAIL_COND_V(!E, false);
  178. return E->get().mode == REPLICATION_MODE_ON_CHANGE;
  179. }
  180. void SceneReplicationConfig::property_set_watch(const NodePath &p_path, bool p_enabled) {
  181. if (p_enabled) {
  182. property_set_replication_mode(p_path, REPLICATION_MODE_ON_CHANGE);
  183. } else if (property_get_replication_mode(p_path) == REPLICATION_MODE_ON_CHANGE) {
  184. property_set_replication_mode(p_path, REPLICATION_MODE_NEVER);
  185. }
  186. }
  187. SceneReplicationConfig::ReplicationMode SceneReplicationConfig::property_get_replication_mode(const NodePath &p_path) {
  188. List<ReplicationProperty>::Element *E = properties.find(p_path);
  189. ERR_FAIL_COND_V(!E, REPLICATION_MODE_NEVER);
  190. return E->get().mode;
  191. }
  192. void SceneReplicationConfig::property_set_replication_mode(const NodePath &p_path, ReplicationMode p_mode) {
  193. List<ReplicationProperty>::Element *E = properties.find(p_path);
  194. ERR_FAIL_COND(!E);
  195. if (E->get().mode == p_mode) {
  196. return;
  197. }
  198. E->get().mode = p_mode;
  199. dirty = true;
  200. }
  201. void SceneReplicationConfig::_update() {
  202. if (!dirty) {
  203. return;
  204. }
  205. dirty = false;
  206. sync_props.clear();
  207. spawn_props.clear();
  208. watch_props.clear();
  209. for (const ReplicationProperty &prop : properties) {
  210. if (prop.spawn) {
  211. spawn_props.push_back(prop.name);
  212. }
  213. switch (prop.mode) {
  214. case REPLICATION_MODE_ALWAYS:
  215. sync_props.push_back(prop.name);
  216. break;
  217. case REPLICATION_MODE_ON_CHANGE:
  218. watch_props.push_back(prop.name);
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. }
  225. const List<NodePath> &SceneReplicationConfig::get_spawn_properties() {
  226. if (dirty) {
  227. _update();
  228. }
  229. return spawn_props;
  230. }
  231. const List<NodePath> &SceneReplicationConfig::get_sync_properties() {
  232. if (dirty) {
  233. _update();
  234. }
  235. return sync_props;
  236. }
  237. const List<NodePath> &SceneReplicationConfig::get_watch_properties() {
  238. if (dirty) {
  239. _update();
  240. }
  241. return watch_props;
  242. }
  243. void SceneReplicationConfig::_bind_methods() {
  244. ClassDB::bind_method(D_METHOD("get_properties"), &SceneReplicationConfig::get_properties);
  245. ClassDB::bind_method(D_METHOD("add_property", "path", "index"), &SceneReplicationConfig::add_property, DEFVAL(-1));
  246. ClassDB::bind_method(D_METHOD("has_property", "path"), &SceneReplicationConfig::has_property);
  247. ClassDB::bind_method(D_METHOD("remove_property", "path"), &SceneReplicationConfig::remove_property);
  248. ClassDB::bind_method(D_METHOD("property_get_index", "path"), &SceneReplicationConfig::property_get_index);
  249. ClassDB::bind_method(D_METHOD("property_get_spawn", "path"), &SceneReplicationConfig::property_get_spawn);
  250. ClassDB::bind_method(D_METHOD("property_set_spawn", "path", "enabled"), &SceneReplicationConfig::property_set_spawn);
  251. ClassDB::bind_method(D_METHOD("property_get_replication_mode", "path"), &SceneReplicationConfig::property_get_replication_mode);
  252. ClassDB::bind_method(D_METHOD("property_set_replication_mode", "path", "mode"), &SceneReplicationConfig::property_set_replication_mode);
  253. BIND_ENUM_CONSTANT(REPLICATION_MODE_NEVER);
  254. BIND_ENUM_CONSTANT(REPLICATION_MODE_ALWAYS);
  255. BIND_ENUM_CONSTANT(REPLICATION_MODE_ON_CHANGE);
  256. // Deprecated.
  257. ClassDB::bind_method(D_METHOD("property_get_sync", "path"), &SceneReplicationConfig::property_get_sync);
  258. ClassDB::bind_method(D_METHOD("property_set_sync", "path", "enabled"), &SceneReplicationConfig::property_set_sync);
  259. ClassDB::bind_method(D_METHOD("property_get_watch", "path"), &SceneReplicationConfig::property_get_watch);
  260. ClassDB::bind_method(D_METHOD("property_set_watch", "path", "enabled"), &SceneReplicationConfig::property_set_watch);
  261. }