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