scene_replication_config.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. TypedArray<NodePath> SceneReplicationConfig::get_properties() const {
  98. TypedArray<NodePath> paths;
  99. for (const ReplicationProperty &prop : properties) {
  100. paths.push_back(prop.name);
  101. }
  102. return paths;
  103. }
  104. void SceneReplicationConfig::add_property(const NodePath &p_path, int p_index) {
  105. ERR_FAIL_COND(properties.find(p_path));
  106. ERR_FAIL_COND(p_path == NodePath());
  107. if (p_index < 0 || p_index == properties.size()) {
  108. properties.push_back(ReplicationProperty(p_path));
  109. dirty = true;
  110. return;
  111. }
  112. ERR_FAIL_INDEX(p_index, properties.size());
  113. List<ReplicationProperty>::Element *I = properties.front();
  114. int c = 0;
  115. while (c < p_index) {
  116. I = I->next();
  117. c++;
  118. }
  119. properties.insert_before(I, ReplicationProperty(p_path));
  120. dirty = true;
  121. }
  122. void SceneReplicationConfig::remove_property(const NodePath &p_path) {
  123. properties.erase(p_path);
  124. dirty = true;
  125. }
  126. bool SceneReplicationConfig::has_property(const NodePath &p_path) const {
  127. for (int i = 0; i < properties.size(); i++) {
  128. if (properties[i].name == p_path) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. int SceneReplicationConfig::property_get_index(const NodePath &p_path) const {
  135. for (int i = 0; i < properties.size(); i++) {
  136. if (properties[i].name == p_path) {
  137. return i;
  138. }
  139. }
  140. ERR_FAIL_V(-1);
  141. }
  142. bool SceneReplicationConfig::property_get_spawn(const NodePath &p_path) {
  143. List<ReplicationProperty>::Element *E = properties.find(p_path);
  144. ERR_FAIL_COND_V(!E, false);
  145. return E->get().spawn;
  146. }
  147. void SceneReplicationConfig::property_set_spawn(const NodePath &p_path, bool p_enabled) {
  148. List<ReplicationProperty>::Element *E = properties.find(p_path);
  149. ERR_FAIL_COND(!E);
  150. if (E->get().spawn == p_enabled) {
  151. return;
  152. }
  153. E->get().spawn = p_enabled;
  154. dirty = true;
  155. }
  156. bool SceneReplicationConfig::property_get_sync(const NodePath &p_path) {
  157. List<ReplicationProperty>::Element *E = properties.find(p_path);
  158. ERR_FAIL_COND_V(!E, false);
  159. return E->get().mode == REPLICATION_MODE_ALWAYS;
  160. }
  161. void SceneReplicationConfig::property_set_sync(const NodePath &p_path, bool p_enabled) {
  162. if (p_enabled) {
  163. property_set_replication_mode(p_path, REPLICATION_MODE_ALWAYS);
  164. } else if (property_get_replication_mode(p_path) == REPLICATION_MODE_ALWAYS) {
  165. property_set_replication_mode(p_path, REPLICATION_MODE_NEVER);
  166. }
  167. }
  168. bool SceneReplicationConfig::property_get_watch(const NodePath &p_path) {
  169. List<ReplicationProperty>::Element *E = properties.find(p_path);
  170. ERR_FAIL_COND_V(!E, false);
  171. return E->get().mode == REPLICATION_MODE_ON_CHANGE;
  172. }
  173. void SceneReplicationConfig::property_set_watch(const NodePath &p_path, bool p_enabled) {
  174. if (p_enabled) {
  175. property_set_replication_mode(p_path, REPLICATION_MODE_ON_CHANGE);
  176. } else if (property_get_replication_mode(p_path) == REPLICATION_MODE_ON_CHANGE) {
  177. property_set_replication_mode(p_path, REPLICATION_MODE_NEVER);
  178. }
  179. }
  180. SceneReplicationConfig::ReplicationMode SceneReplicationConfig::property_get_replication_mode(const NodePath &p_path) {
  181. List<ReplicationProperty>::Element *E = properties.find(p_path);
  182. ERR_FAIL_COND_V(!E, REPLICATION_MODE_NEVER);
  183. return E->get().mode;
  184. }
  185. void SceneReplicationConfig::property_set_replication_mode(const NodePath &p_path, ReplicationMode p_mode) {
  186. List<ReplicationProperty>::Element *E = properties.find(p_path);
  187. ERR_FAIL_COND(!E);
  188. if (E->get().mode == p_mode) {
  189. return;
  190. }
  191. E->get().mode = p_mode;
  192. dirty = true;
  193. }
  194. void SceneReplicationConfig::_update() {
  195. if (!dirty) {
  196. return;
  197. }
  198. dirty = false;
  199. sync_props.clear();
  200. spawn_props.clear();
  201. watch_props.clear();
  202. for (const ReplicationProperty &prop : properties) {
  203. if (prop.spawn) {
  204. spawn_props.push_back(prop.name);
  205. }
  206. switch (prop.mode) {
  207. case REPLICATION_MODE_ALWAYS:
  208. sync_props.push_back(prop.name);
  209. break;
  210. case REPLICATION_MODE_ON_CHANGE:
  211. watch_props.push_back(prop.name);
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. }
  218. const List<NodePath> &SceneReplicationConfig::get_spawn_properties() {
  219. if (dirty) {
  220. _update();
  221. }
  222. return spawn_props;
  223. }
  224. const List<NodePath> &SceneReplicationConfig::get_sync_properties() {
  225. if (dirty) {
  226. _update();
  227. }
  228. return sync_props;
  229. }
  230. const List<NodePath> &SceneReplicationConfig::get_watch_properties() {
  231. if (dirty) {
  232. _update();
  233. }
  234. return watch_props;
  235. }
  236. void SceneReplicationConfig::_bind_methods() {
  237. ClassDB::bind_method(D_METHOD("get_properties"), &SceneReplicationConfig::get_properties);
  238. ClassDB::bind_method(D_METHOD("add_property", "path", "index"), &SceneReplicationConfig::add_property, DEFVAL(-1));
  239. ClassDB::bind_method(D_METHOD("has_property", "path"), &SceneReplicationConfig::has_property);
  240. ClassDB::bind_method(D_METHOD("remove_property", "path"), &SceneReplicationConfig::remove_property);
  241. ClassDB::bind_method(D_METHOD("property_get_index", "path"), &SceneReplicationConfig::property_get_index);
  242. ClassDB::bind_method(D_METHOD("property_get_spawn", "path"), &SceneReplicationConfig::property_get_spawn);
  243. ClassDB::bind_method(D_METHOD("property_set_spawn", "path", "enabled"), &SceneReplicationConfig::property_set_spawn);
  244. ClassDB::bind_method(D_METHOD("property_get_replication_mode", "path"), &SceneReplicationConfig::property_get_replication_mode);
  245. ClassDB::bind_method(D_METHOD("property_set_replication_mode", "path", "mode"), &SceneReplicationConfig::property_set_replication_mode);
  246. BIND_ENUM_CONSTANT(REPLICATION_MODE_NEVER);
  247. BIND_ENUM_CONSTANT(REPLICATION_MODE_ALWAYS);
  248. BIND_ENUM_CONSTANT(REPLICATION_MODE_ON_CHANGE);
  249. // Deprecated.
  250. ClassDB::bind_method(D_METHOD("property_get_sync", "path"), &SceneReplicationConfig::property_get_sync);
  251. ClassDB::bind_method(D_METHOD("property_set_sync", "path", "enabled"), &SceneReplicationConfig::property_set_sync);
  252. ClassDB::bind_method(D_METHOD("property_get_watch", "path"), &SceneReplicationConfig::property_get_watch);
  253. ClassDB::bind_method(D_METHOD("property_set_watch", "path", "enabled"), &SceneReplicationConfig::property_set_watch);
  254. }