multiplayer_replicator.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*************************************************************************/
  2. /* multiplayer_replicator.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/multiplayer/multiplayer_replicator.h"
  31. #include "core/io/marshalls.h"
  32. #include "scene/main/node.h"
  33. #include "scene/resources/packed_scene.h"
  34. #define MAKE_ROOM(m_amount) \
  35. if (packet_cache.size() < m_amount) \
  36. packet_cache.resize(m_amount);
  37. Error MultiplayerReplicator::_sync_all_default(const ResourceUID::ID &p_scene_id, int p_peer) {
  38. ERR_FAIL_COND_V(!replications.has(p_scene_id), ERR_INVALID_PARAMETER);
  39. SceneConfig &cfg = replications[p_scene_id];
  40. int full_size = 0;
  41. bool same_size = true;
  42. int last_size = 0;
  43. bool all_raw = true;
  44. struct EncodeInfo {
  45. int size = 0;
  46. bool raw = false;
  47. List<Variant> state;
  48. };
  49. Map<ObjectID, struct EncodeInfo> state;
  50. if (tracked_objects.has(p_scene_id)) {
  51. for (const ObjectID &obj_id : tracked_objects[p_scene_id]) {
  52. Object *obj = ObjectDB::get_instance(obj_id);
  53. if (obj) {
  54. struct EncodeInfo info;
  55. Error err = _get_state(cfg.sync_properties, obj, info.state);
  56. ERR_CONTINUE(err);
  57. err = _encode_state(info.state, nullptr, info.size, &info.raw);
  58. ERR_CONTINUE(err);
  59. state[obj_id] = info;
  60. full_size += info.size;
  61. if (last_size && info.size != last_size) {
  62. same_size = false;
  63. }
  64. all_raw = all_raw && info.raw;
  65. last_size = info.size;
  66. }
  67. }
  68. }
  69. // Default implementation do not send empty updates.
  70. if (!full_size) {
  71. return OK;
  72. }
  73. #ifdef DEBUG_ENABLED
  74. if (full_size > 4096 && cfg.sync_interval) {
  75. WARN_PRINT_ONCE(vformat("The timed state update for scene %d is big (%d bytes) consider optimizing it", p_scene_id));
  76. }
  77. #endif
  78. if (same_size) {
  79. // This is fast and small. Should we allow more than 256 objects per type?
  80. // This costs us 1 byte.
  81. MAKE_ROOM(SYNC_CMD_OFFSET + 1 + 2 + 2 + full_size);
  82. } else {
  83. MAKE_ROOM(SYNC_CMD_OFFSET + 1 + 2 + state.size() * 2 + full_size);
  84. }
  85. int ofs = 0;
  86. uint8_t *ptr = packet_cache.ptrw();
  87. ptr[0] = MultiplayerAPI::NETWORK_COMMAND_SYNC | (same_size ? BYTE_OR_ZERO_FLAG : 0);
  88. ofs = 1;
  89. ofs += encode_uint64(p_scene_id, &ptr[ofs]);
  90. ptr[ofs] = cfg.sync_recv++;
  91. ofs += 1;
  92. ofs += encode_uint16(state.size(), &ptr[ofs]);
  93. if (same_size) {
  94. ofs += encode_uint16(last_size + (all_raw ? 1 << 15 : 0), &ptr[ofs]);
  95. }
  96. for (const ObjectID &obj_id : tracked_objects[p_scene_id]) {
  97. if (!state.has(obj_id)) {
  98. continue;
  99. }
  100. struct EncodeInfo &info = state[obj_id];
  101. Object *obj = ObjectDB::get_instance(obj_id);
  102. ERR_CONTINUE(!obj);
  103. int size = 0;
  104. if (!same_size) {
  105. // We need to encode the size of every object.
  106. ofs += encode_uint16(info.size + (info.raw ? 1 << 15 : 0), &ptr[ofs]);
  107. }
  108. Error err = _encode_state(info.state, &ptr[ofs], size, &info.raw);
  109. ERR_CONTINUE(err);
  110. ofs += size;
  111. }
  112. Ref<MultiplayerPeer> network_peer = multiplayer->get_network_peer();
  113. network_peer->set_target_peer(p_peer);
  114. network_peer->set_transfer_channel(0);
  115. network_peer->set_transfer_mode(Multiplayer::TRANSFER_MODE_UNRELIABLE);
  116. return network_peer->put_packet(ptr, ofs);
  117. }
  118. void MultiplayerReplicator::_process_default_sync(const ResourceUID::ID &p_id, const uint8_t *p_packet, int p_packet_len) {
  119. ERR_FAIL_COND_MSG(p_packet_len < SYNC_CMD_OFFSET + 5, "Invalid spawn packet received");
  120. ERR_FAIL_COND_MSG(!replications.has(p_id), "Invalid spawn ID received " + itos(p_id));
  121. SceneConfig &cfg = replications[p_id];
  122. ERR_FAIL_COND_MSG(cfg.mode != REPLICATION_MODE_SERVER || multiplayer->is_network_server(), "The defualt implementation only allows sync packets from the server");
  123. const bool same_size = p_packet[0] & BYTE_OR_ZERO_FLAG;
  124. int ofs = SYNC_CMD_OFFSET;
  125. int time = p_packet[ofs];
  126. // Skip old update.
  127. if (time < cfg.sync_recv && cfg.sync_recv - time < 127) {
  128. return;
  129. }
  130. cfg.sync_recv = time;
  131. ofs += 1;
  132. int count = decode_uint16(&p_packet[ofs]);
  133. ofs += 2;
  134. #ifdef DEBUG_ENABLED
  135. ERR_FAIL_COND(!tracked_objects.has(p_id) || tracked_objects[p_id].size() != count);
  136. #else
  137. if (!tracked_objects.has(p_id) || tracked_objects[p_id].size() != count) {
  138. return;
  139. }
  140. #endif
  141. int data_size = 0;
  142. bool raw = false;
  143. if (same_size) {
  144. // This is fast and optimized.
  145. data_size = decode_uint16(&p_packet[ofs]);
  146. raw = (data_size & (1 << 15)) != 0;
  147. data_size = data_size & ~(1 << 15);
  148. ofs += 2;
  149. ERR_FAIL_COND(p_packet_len - ofs < data_size * count);
  150. }
  151. for (const ObjectID &obj_id : tracked_objects[p_id]) {
  152. Object *obj = ObjectDB::get_instance(obj_id);
  153. ERR_CONTINUE(!obj);
  154. if (!same_size) {
  155. // This is slow and wasteful.
  156. data_size = decode_uint16(&p_packet[ofs]);
  157. raw = (data_size & (1 << 15)) != 0;
  158. data_size = data_size & ~(1 << 15);
  159. ofs += 2;
  160. ERR_FAIL_COND(p_packet_len - ofs < data_size);
  161. }
  162. int size = 0;
  163. Error err = _decode_state(cfg.sync_properties, obj, &p_packet[ofs], data_size, size, raw);
  164. ofs += data_size;
  165. ERR_CONTINUE(err);
  166. ERR_CONTINUE(size != data_size);
  167. }
  168. }
  169. Error MultiplayerReplicator::_send_default_spawn_despawn(int p_peer_id, const ResourceUID::ID &p_scene_id, Object *p_obj, const NodePath &p_path, bool p_spawn) {
  170. ERR_FAIL_COND_V(p_spawn && !p_obj, ERR_INVALID_PARAMETER);
  171. ERR_FAIL_COND_V(!replications.has(p_scene_id), ERR_INVALID_PARAMETER);
  172. Error err;
  173. // Prepare state
  174. List<Variant> state_variants;
  175. int state_len = 0;
  176. const SceneConfig &cfg = replications[p_scene_id];
  177. if (p_spawn) {
  178. if ((err = _get_state(cfg.properties, p_obj, state_variants)) != OK) {
  179. return err;
  180. }
  181. }
  182. bool is_raw = false;
  183. if (state_variants.size() == 1 && state_variants[0].get_type() == Variant::PACKED_BYTE_ARRAY) {
  184. is_raw = true;
  185. const PackedByteArray pba = state_variants[0];
  186. state_len = pba.size();
  187. } else if (state_variants.size()) {
  188. err = _encode_state(state_variants, nullptr, state_len);
  189. ERR_FAIL_COND_V(err, err);
  190. } else {
  191. is_raw = true;
  192. }
  193. int ofs = 0;
  194. // Prepare simplified path
  195. const Node *root_node = multiplayer->get_root_node();
  196. ERR_FAIL_COND_V(!root_node, ERR_UNCONFIGURED);
  197. NodePath rel_path = (root_node->get_path()).rel_path_to(p_path);
  198. const Vector<StringName> names = rel_path.get_names();
  199. ERR_FAIL_COND_V(names.size() < 2, ERR_INVALID_PARAMETER);
  200. NodePath parent = NodePath(names.subarray(0, names.size() - 2), false);
  201. ERR_FAIL_COND_V_MSG(!root_node->has_node(parent), ERR_INVALID_PARAMETER, "Path not found: " + parent);
  202. int path_id = 0;
  203. multiplayer->send_confirm_path(root_node->get_node(parent), parent, p_peer_id, path_id);
  204. // Encode name and parent ID.
  205. CharString cname = String(names[names.size() - 1]).utf8();
  206. int nlen = encode_cstring(cname.get_data(), nullptr);
  207. MAKE_ROOM(SPAWN_CMD_OFFSET + 4 + 4 + nlen + state_len);
  208. uint8_t *ptr = packet_cache.ptrw();
  209. ptr[0] = (p_spawn ? MultiplayerAPI::NETWORK_COMMAND_SPAWN : MultiplayerAPI::NETWORK_COMMAND_DESPAWN) | (is_raw ? BYTE_OR_ZERO_FLAG : 0);
  210. ofs = 1;
  211. ofs += encode_uint64(p_scene_id, &ptr[ofs]);
  212. ofs += encode_uint32(path_id, &ptr[ofs]);
  213. ofs += encode_uint32(nlen, &ptr[ofs]);
  214. ofs += encode_cstring(cname.get_data(), &ptr[ofs]);
  215. // Encode state.
  216. if (!is_raw) {
  217. _encode_state(state_variants, &ptr[ofs], state_len);
  218. } else if (state_len) {
  219. PackedByteArray pba = state_variants[0];
  220. memcpy(&ptr[ofs], pba.ptr(), state_len);
  221. }
  222. Ref<MultiplayerPeer> network_peer = multiplayer->get_network_peer();
  223. network_peer->set_target_peer(p_peer_id);
  224. network_peer->set_transfer_channel(0);
  225. network_peer->set_transfer_mode(Multiplayer::TRANSFER_MODE_RELIABLE);
  226. return network_peer->put_packet(ptr, ofs + state_len);
  227. }
  228. void MultiplayerReplicator::_process_default_spawn_despawn(int p_from, const ResourceUID::ID &p_scene_id, const uint8_t *p_packet, int p_packet_len, bool p_spawn) {
  229. ERR_FAIL_COND_MSG(p_packet_len < SPAWN_CMD_OFFSET + 9, "Invalid spawn packet received");
  230. int ofs = SPAWN_CMD_OFFSET;
  231. uint32_t node_target = decode_uint32(&p_packet[ofs]);
  232. Node *parent = multiplayer->get_cached_node(p_from, node_target);
  233. ofs += 4;
  234. ERR_FAIL_COND_MSG(parent == nullptr, "Invalid packet received. Requested node was not found.");
  235. uint32_t name_len = decode_uint32(&p_packet[ofs]);
  236. ofs += 4;
  237. ERR_FAIL_COND_MSG(name_len > uint32_t(p_packet_len - ofs), vformat("Invalid spawn packet size: %d, wants: %d", p_packet_len, ofs + name_len));
  238. ERR_FAIL_COND_MSG(name_len < 1, "Zero spawn name size.");
  239. const String name = String::utf8((const char *)&p_packet[ofs], name_len);
  240. // We need to make sure no trickery happens here (e.g. despawning a subpath), but we want to allow autogenerated ("@") node names.
  241. ERR_FAIL_COND_MSG(name.validate_node_name() != name.replace("@", ""), vformat("Invalid node name received: '%s'", name));
  242. ofs += name_len;
  243. const SceneConfig &cfg = replications[p_scene_id];
  244. if (cfg.mode == REPLICATION_MODE_SERVER && p_from == 1) {
  245. String scene_path = ResourceUID::get_singleton()->get_id_path(p_scene_id);
  246. if (p_spawn) {
  247. const bool is_raw = ((p_packet[0] & BYTE_OR_ZERO_FLAG) >> BYTE_OR_ZERO_SHIFT) == 1;
  248. ERR_FAIL_COND_MSG(parent->has_node(name), vformat("Unable to spawn node. Node already exists: %s/%s", parent->get_path(), name));
  249. RES res = ResourceLoader::load(scene_path);
  250. ERR_FAIL_COND_MSG(!res.is_valid(), "Unable to load scene to spawn at path: " + scene_path);
  251. PackedScene *scene = Object::cast_to<PackedScene>(res.ptr());
  252. ERR_FAIL_COND(!scene);
  253. Node *node = scene->instantiate();
  254. ERR_FAIL_COND(!node);
  255. replicated_nodes[node->get_instance_id()] = p_scene_id;
  256. _track(p_scene_id, node);
  257. int size;
  258. _decode_state(cfg.properties, node, &p_packet[ofs], p_packet_len - ofs, size, is_raw);
  259. parent->_add_child_nocheck(node, name);
  260. emit_signal(SNAME("spawned"), p_scene_id, node);
  261. } else {
  262. ERR_FAIL_COND_MSG(!parent->has_node(name), vformat("Path not found: %s/%s", parent->get_path(), name));
  263. Node *node = parent->get_node(name);
  264. ERR_FAIL_COND_MSG(!replicated_nodes.has(node->get_instance_id()), vformat("Trying to despawn a Node that was not replicated: %s/%s", parent->get_path(), name));
  265. emit_signal(SNAME("despawned"), p_scene_id, node);
  266. _untrack(p_scene_id, node);
  267. replicated_nodes.erase(node->get_instance_id());
  268. node->queue_delete();
  269. }
  270. } else {
  271. PackedByteArray data;
  272. if (p_packet_len > ofs) {
  273. data.resize(p_packet_len - ofs);
  274. memcpy(data.ptrw(), &p_packet[ofs], data.size());
  275. }
  276. if (p_spawn) {
  277. emit_signal(SNAME("spawn_requested"), p_from, p_scene_id, parent, name, data);
  278. } else {
  279. emit_signal(SNAME("despawn_requested"), p_from, p_scene_id, parent, name, data);
  280. }
  281. }
  282. }
  283. void MultiplayerReplicator::process_spawn_despawn(int p_from, const uint8_t *p_packet, int p_packet_len, bool p_spawn) {
  284. ERR_FAIL_COND_MSG(p_packet_len < SPAWN_CMD_OFFSET, "Invalid spawn packet received");
  285. ResourceUID::ID id = decode_uint64(&p_packet[1]);
  286. ERR_FAIL_COND_MSG(!replications.has(id), "Invalid spawn ID received " + itos(id));
  287. const SceneConfig &cfg = replications[id];
  288. if (cfg.on_spawn_despawn_receive.is_valid()) {
  289. int ofs = SPAWN_CMD_OFFSET;
  290. bool is_raw = ((p_packet[0] & BYTE_OR_ZERO_FLAG) >> BYTE_OR_ZERO_SHIFT) == 1;
  291. Variant data;
  292. int left = p_packet_len - ofs;
  293. if (is_raw && left) {
  294. PackedByteArray pba;
  295. pba.resize(left);
  296. memcpy(pba.ptrw(), &p_packet[ofs], pba.size());
  297. data = pba;
  298. } else if (left) {
  299. ERR_FAIL_COND(decode_variant(data, &p_packet[ofs], left) != OK);
  300. }
  301. Variant args[4];
  302. args[0] = p_from;
  303. args[1] = id;
  304. args[2] = data;
  305. args[3] = p_spawn;
  306. const Variant *argp[] = { &args[0], &args[1], &args[2], &args[3] };
  307. Callable::CallError ce;
  308. Variant ret;
  309. cfg.on_spawn_despawn_receive.call(argp, 4, ret, ce);
  310. ERR_FAIL_COND_MSG(ce.error != Callable::CallError::CALL_OK, "Custom receive function failed");
  311. } else {
  312. _process_default_spawn_despawn(p_from, id, p_packet, p_packet_len, p_spawn);
  313. }
  314. }
  315. void MultiplayerReplicator::process_sync(int p_from, const uint8_t *p_packet, int p_packet_len) {
  316. ERR_FAIL_COND_MSG(p_packet_len < SPAWN_CMD_OFFSET, "Invalid spawn packet received");
  317. ResourceUID::ID id = decode_uint64(&p_packet[1]);
  318. ERR_FAIL_COND_MSG(!replications.has(id), "Invalid spawn ID received " + itos(id));
  319. const SceneConfig &cfg = replications[id];
  320. if (cfg.on_sync_receive.is_valid()) {
  321. Array objs;
  322. if (tracked_objects.has(id)) {
  323. objs.resize(tracked_objects[id].size());
  324. int idx = 0;
  325. for (const ObjectID &obj_id : tracked_objects[id]) {
  326. objs[idx++] = ObjectDB::get_instance(obj_id);
  327. }
  328. }
  329. PackedByteArray pba;
  330. pba.resize(p_packet_len - SPAWN_CMD_OFFSET);
  331. if (pba.size()) {
  332. memcpy(pba.ptrw(), p_packet, p_packet_len - SPAWN_CMD_OFFSET);
  333. }
  334. Variant args[4] = { p_from, id, objs, pba };
  335. Variant *argp[4] = { args, &args[1], &args[2], &args[3] };
  336. Callable::CallError ce;
  337. Variant ret;
  338. cfg.on_sync_receive.call((const Variant **)argp, 4, ret, ce);
  339. ERR_FAIL_COND_MSG(ce.error != Callable::CallError::CALL_OK, "Custom sync function failed");
  340. } else {
  341. ERR_FAIL_COND_MSG(p_from != 1, "Default sync implementation only allow syncing from server to client");
  342. _process_default_sync(id, p_packet, p_packet_len);
  343. }
  344. }
  345. Error MultiplayerReplicator::_get_state(const List<StringName> &p_properties, const Object *p_obj, List<Variant> &r_variant) {
  346. ERR_FAIL_COND_V_MSG(!p_obj, ERR_INVALID_PARAMETER, "Cannot encode null object");
  347. for (const StringName &prop : p_properties) {
  348. bool valid = false;
  349. const Variant v = p_obj->get(prop, &valid);
  350. ERR_FAIL_COND_V_MSG(!valid, ERR_INVALID_DATA, vformat("Property '%s' not found.", prop));
  351. r_variant.push_back(v);
  352. }
  353. return OK;
  354. }
  355. Error MultiplayerReplicator::_encode_state(const List<Variant> &p_variants, uint8_t *p_buffer, int &r_len, bool *r_raw) {
  356. r_len = 0;
  357. int size = 0;
  358. // Try raw encoding optimization.
  359. if (r_raw && p_variants.size() == 1) {
  360. *r_raw = false;
  361. const Variant v = p_variants[0];
  362. if (v.get_type() == Variant::PACKED_BYTE_ARRAY) {
  363. *r_raw = true;
  364. const PackedByteArray pba = v;
  365. if (p_buffer) {
  366. memcpy(p_buffer, pba.ptr(), pba.size());
  367. }
  368. r_len += pba.size();
  369. } else {
  370. multiplayer->encode_and_compress_variant(v, p_buffer, size);
  371. r_len += size;
  372. }
  373. return OK;
  374. }
  375. // Regular encoding.
  376. for (const Variant &v : p_variants) {
  377. multiplayer->encode_and_compress_variant(v, p_buffer ? p_buffer + r_len : nullptr, size);
  378. r_len += size;
  379. }
  380. return OK;
  381. }
  382. Error MultiplayerReplicator::_decode_state(const List<StringName> &p_properties, Object *p_obj, const uint8_t *p_buffer, int p_len, int &r_len, bool p_raw) {
  383. r_len = 0;
  384. int argc = p_properties.size();
  385. if (argc == 0 && p_raw) {
  386. ERR_FAIL_COND_V_MSG(p_len != 0, ERR_INVALID_DATA, "Buffer has trailing bytes.");
  387. return OK;
  388. }
  389. ERR_FAIL_COND_V(p_raw && argc != 1, ERR_INVALID_DATA);
  390. if (p_raw) {
  391. r_len = p_len;
  392. PackedByteArray pba;
  393. pba.resize(p_len);
  394. memcpy(pba.ptrw(), p_buffer, p_len);
  395. p_obj->set(p_properties[0], pba);
  396. return OK;
  397. }
  398. Vector<Variant> args;
  399. Vector<const Variant *> argp;
  400. args.resize(argc);
  401. for (int i = 0; i < argc; i++) {
  402. ERR_FAIL_COND_V_MSG(r_len >= p_len, ERR_INVALID_DATA, "Invalid packet received. Size too small.");
  403. int vlen;
  404. Error err = multiplayer->decode_and_decompress_variant(args.write[i], &p_buffer[r_len], p_len - r_len, &vlen);
  405. ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid packet received. Unable to decode state variable.");
  406. r_len += vlen;
  407. }
  408. ERR_FAIL_COND_V_MSG(p_len - r_len != 0, ERR_INVALID_DATA, "Buffer has trailing bytes.");
  409. int i = 0;
  410. for (const StringName &prop : p_properties) {
  411. p_obj->set(prop, args[i]);
  412. i += 1;
  413. }
  414. return OK;
  415. }
  416. Error MultiplayerReplicator::spawn_config(const ResourceUID::ID &p_id, ReplicationMode p_mode, const TypedArray<StringName> &p_props, const Callable &p_on_send, const Callable &p_on_recv) {
  417. ERR_FAIL_COND_V(p_mode < REPLICATION_MODE_NONE || p_mode > REPLICATION_MODE_CUSTOM, ERR_INVALID_PARAMETER);
  418. ERR_FAIL_COND_V(!ResourceUID::get_singleton()->has_id(p_id), ERR_INVALID_PARAMETER);
  419. ERR_FAIL_COND_V_MSG(p_on_send.is_valid() != p_on_recv.is_valid(), ERR_INVALID_PARAMETER, "Send and receive custom callables must be both valid or both empty");
  420. #ifdef TOOLS_ENABLED
  421. if (!p_on_send.is_valid()) {
  422. // We allow non scene spawning with custom callables.
  423. String path = ResourceUID::get_singleton()->get_id_path(p_id);
  424. RES res = ResourceLoader::load(path);
  425. ERR_FAIL_COND_V(!res->is_class("PackedScene"), ERR_INVALID_PARAMETER);
  426. }
  427. #endif
  428. if (p_mode == REPLICATION_MODE_NONE) {
  429. if (replications.has(p_id)) {
  430. replications.erase(p_id);
  431. }
  432. } else {
  433. SceneConfig cfg;
  434. cfg.mode = p_mode;
  435. for (int i = 0; i < p_props.size(); i++) {
  436. cfg.properties.push_back(p_props[i]);
  437. }
  438. cfg.on_spawn_despawn_send = p_on_send;
  439. cfg.on_spawn_despawn_receive = p_on_recv;
  440. replications[p_id] = cfg;
  441. }
  442. return OK;
  443. }
  444. Error MultiplayerReplicator::sync_config(const ResourceUID::ID &p_id, uint64_t p_interval, const TypedArray<StringName> &p_props, const Callable &p_on_send, const Callable &p_on_recv) {
  445. ERR_FAIL_COND_V(!ResourceUID::get_singleton()->has_id(p_id), ERR_INVALID_PARAMETER);
  446. ERR_FAIL_COND_V_MSG(p_on_send.is_valid() != p_on_recv.is_valid(), ERR_INVALID_PARAMETER, "Send and receive custom callables must be both valid or both empty");
  447. ERR_FAIL_COND_V(!replications.has(p_id), ERR_UNCONFIGURED);
  448. SceneConfig &cfg = replications[p_id];
  449. ERR_FAIL_COND_V_MSG(p_interval && cfg.mode != REPLICATION_MODE_SERVER && !p_on_send.is_valid(), ERR_INVALID_PARAMETER, "Timed updates in custom mode are only allowed if custom callbacks are also specified");
  450. for (int i = 0; i < p_props.size(); i++) {
  451. cfg.sync_properties.push_back(p_props[i]);
  452. }
  453. cfg.on_sync_send = p_on_send;
  454. cfg.on_sync_receive = p_on_recv;
  455. cfg.sync_interval = p_interval * 1000;
  456. return OK;
  457. }
  458. Error MultiplayerReplicator::_send_spawn_despawn(int p_peer_id, const ResourceUID::ID &p_scene_id, const Variant &p_data, bool p_spawn) {
  459. int data_size = 0;
  460. int is_raw = false;
  461. if (p_data.get_type() == Variant::PACKED_BYTE_ARRAY) {
  462. const PackedByteArray pba = p_data;
  463. is_raw = true;
  464. data_size = p_data.operator PackedByteArray().size();
  465. } else if (p_data.get_type() == Variant::NIL) {
  466. is_raw = true;
  467. } else {
  468. Error err = encode_variant(p_data, nullptr, data_size);
  469. ERR_FAIL_COND_V(err, err);
  470. }
  471. MAKE_ROOM(SPAWN_CMD_OFFSET + data_size);
  472. uint8_t *ptr = packet_cache.ptrw();
  473. ptr[0] = (p_spawn ? MultiplayerAPI::NETWORK_COMMAND_SPAWN : MultiplayerAPI::NETWORK_COMMAND_DESPAWN) + ((is_raw ? 1 : 0) << BYTE_OR_ZERO_SHIFT);
  474. encode_uint64(p_scene_id, &ptr[1]);
  475. if (p_data.get_type() == Variant::PACKED_BYTE_ARRAY) {
  476. const PackedByteArray pba = p_data;
  477. memcpy(&ptr[SPAWN_CMD_OFFSET], pba.ptr(), pba.size());
  478. } else if (data_size) {
  479. encode_variant(p_data, &ptr[SPAWN_CMD_OFFSET], data_size);
  480. }
  481. Ref<MultiplayerPeer> network_peer = multiplayer->get_network_peer();
  482. network_peer->set_target_peer(p_peer_id);
  483. network_peer->set_transfer_channel(0);
  484. network_peer->set_transfer_mode(Multiplayer::TRANSFER_MODE_RELIABLE);
  485. return network_peer->put_packet(ptr, SPAWN_CMD_OFFSET + data_size);
  486. }
  487. Error MultiplayerReplicator::send_despawn(int p_peer_id, const ResourceUID::ID &p_scene_id, const Variant &p_data, const NodePath &p_path) {
  488. ERR_FAIL_COND_V(!multiplayer->has_network_peer(), ERR_UNCONFIGURED);
  489. ERR_FAIL_COND_V_MSG(!replications.has(p_scene_id), ERR_INVALID_PARAMETER, vformat("Spawnable not found: %d", p_scene_id));
  490. const SceneConfig &cfg = replications[p_scene_id];
  491. if (cfg.on_spawn_despawn_send.is_valid()) {
  492. return _send_spawn_despawn(p_peer_id, p_scene_id, p_data, true);
  493. } else {
  494. ERR_FAIL_COND_V_MSG(cfg.mode == REPLICATION_MODE_SERVER && multiplayer->is_network_server(), ERR_UNAVAILABLE, "Manual despawn is restricted in default server mode implementation. Use custom mode if you desire control over server spawn requests.");
  495. NodePath path = p_path;
  496. Object *obj = p_data.get_type() == Variant::OBJECT ? p_data.get_validated_object() : nullptr;
  497. if (path.is_empty() && obj) {
  498. Node *node = Object::cast_to<Node>(obj);
  499. if (node && node->is_inside_tree()) {
  500. path = node->get_path();
  501. }
  502. }
  503. ERR_FAIL_COND_V_MSG(path.is_empty(), ERR_INVALID_PARAMETER, "Despawn default implementation requires a despawn path, or the data to be a node inside the SceneTree");
  504. return _send_default_spawn_despawn(p_peer_id, p_scene_id, obj, path, false);
  505. }
  506. }
  507. Error MultiplayerReplicator::send_spawn(int p_peer_id, const ResourceUID::ID &p_scene_id, const Variant &p_data, const NodePath &p_path) {
  508. ERR_FAIL_COND_V(!multiplayer->has_network_peer(), ERR_UNCONFIGURED);
  509. ERR_FAIL_COND_V_MSG(!replications.has(p_scene_id), ERR_INVALID_PARAMETER, vformat("Spawnable not found: %d", p_scene_id));
  510. const SceneConfig &cfg = replications[p_scene_id];
  511. if (cfg.on_spawn_despawn_send.is_valid()) {
  512. return _send_spawn_despawn(p_peer_id, p_scene_id, p_data, false);
  513. } else {
  514. ERR_FAIL_COND_V_MSG(cfg.mode == REPLICATION_MODE_SERVER && multiplayer->is_network_server(), ERR_UNAVAILABLE, "Manual spawn is restricted in default server mode implementation. Use custom mode if you desire control over server spawn requests.");
  515. NodePath path = p_path;
  516. Object *obj = p_data.get_type() == Variant::OBJECT ? p_data.get_validated_object() : nullptr;
  517. ERR_FAIL_COND_V_MSG(!obj, ERR_INVALID_PARAMETER, "Spawn default implementation requires the data to be an object.");
  518. if (path.is_empty()) {
  519. Node *node = Object::cast_to<Node>(obj);
  520. if (node && node->is_inside_tree()) {
  521. path = node->get_path();
  522. }
  523. }
  524. ERR_FAIL_COND_V_MSG(path.is_empty(), ERR_INVALID_PARAMETER, "Spawn default implementation requires a spawn path, or the data to be a node inside the SceneTree");
  525. return _send_default_spawn_despawn(p_peer_id, p_scene_id, obj, path, true);
  526. }
  527. }
  528. Error MultiplayerReplicator::_spawn_despawn(ResourceUID::ID p_scene_id, Object *p_obj, int p_peer, bool p_spawn) {
  529. ERR_FAIL_COND_V_MSG(!replications.has(p_scene_id), ERR_INVALID_PARAMETER, vformat("Spawnable not found: %d", p_scene_id));
  530. const SceneConfig &cfg = replications[p_scene_id];
  531. if (cfg.on_spawn_despawn_send.is_valid()) {
  532. Variant args[4];
  533. args[0] = p_peer;
  534. args[1] = p_scene_id;
  535. args[2] = p_obj;
  536. args[3] = true;
  537. const Variant *argp[] = { &args[0], &args[1], &args[2], &args[3] };
  538. Callable::CallError ce;
  539. Variant ret;
  540. cfg.on_spawn_despawn_send.call(argp, 4, ret, ce);
  541. ERR_FAIL_COND_V_MSG(ce.error != Callable::CallError::CALL_OK, FAILED, "Custom send function failed");
  542. return OK;
  543. } else {
  544. Node *node = Object::cast_to<Node>(p_obj);
  545. ERR_FAIL_COND_V_MSG(!p_obj, ERR_INVALID_PARAMETER, "Only nodes can be replicated by the default implementation");
  546. return _send_default_spawn_despawn(p_peer, p_scene_id, node, node->get_path(), p_spawn);
  547. }
  548. }
  549. Error MultiplayerReplicator::spawn(ResourceUID::ID p_scene_id, Object *p_obj, int p_peer) {
  550. return _spawn_despawn(p_scene_id, p_obj, p_peer, true);
  551. }
  552. Error MultiplayerReplicator::despawn(ResourceUID::ID p_scene_id, Object *p_obj, int p_peer) {
  553. return _spawn_despawn(p_scene_id, p_obj, p_peer, false);
  554. }
  555. PackedByteArray MultiplayerReplicator::encode_state(const ResourceUID::ID &p_scene_id, const Object *p_obj, bool p_initial) {
  556. PackedByteArray state;
  557. ERR_FAIL_COND_V_MSG(!replications.has(p_scene_id), state, vformat("Spawnable not found: %d", p_scene_id));
  558. const SceneConfig &cfg = replications[p_scene_id];
  559. int len = 0;
  560. List<Variant> state_vars;
  561. const List<StringName> props = p_initial ? cfg.properties : cfg.sync_properties;
  562. Error err = _get_state(props, p_obj, state_vars);
  563. ERR_FAIL_COND_V_MSG(err != OK, state, "Unable to retrieve object state.");
  564. err = _encode_state(state_vars, nullptr, len);
  565. ERR_FAIL_COND_V_MSG(err != OK, state, "Unable to encode object state.");
  566. state.resize(len);
  567. _encode_state(state_vars, state.ptrw(), len);
  568. return state;
  569. }
  570. Error MultiplayerReplicator::decode_state(const ResourceUID::ID &p_scene_id, Object *p_obj, const PackedByteArray p_data, bool p_initial) {
  571. ERR_FAIL_COND_V_MSG(!replications.has(p_scene_id), ERR_INVALID_PARAMETER, vformat("Spawnable not found: %d", p_scene_id));
  572. const SceneConfig &cfg = replications[p_scene_id];
  573. const List<StringName> props = p_initial ? cfg.properties : cfg.sync_properties;
  574. int size;
  575. return _decode_state(props, p_obj, p_data.ptr(), p_data.size(), size);
  576. }
  577. void MultiplayerReplicator::scene_enter_exit_notify(const String &p_scene, Node *p_node, bool p_enter) {
  578. if (!multiplayer->has_network_peer()) {
  579. return;
  580. }
  581. Node *root_node = multiplayer->get_root_node();
  582. ERR_FAIL_COND(!p_node || !p_node->get_parent() || !root_node);
  583. NodePath path = (root_node->get_path()).rel_path_to(p_node->get_parent()->get_path());
  584. if (path.is_empty()) {
  585. return;
  586. }
  587. ResourceUID::ID id = ResourceLoader::get_resource_uid(p_scene);
  588. if (!replications.has(id)) {
  589. return;
  590. }
  591. const SceneConfig &cfg = replications[id];
  592. if (p_enter) {
  593. if (cfg.mode == REPLICATION_MODE_SERVER && multiplayer->is_network_server()) {
  594. replicated_nodes[p_node->get_instance_id()] = id;
  595. _track(id, p_node);
  596. spawn(id, p_node, 0);
  597. }
  598. emit_signal(SNAME("replicated_instance_added"), id, p_node);
  599. } else {
  600. if (cfg.mode == REPLICATION_MODE_SERVER && multiplayer->is_network_server() && replicated_nodes.has(p_node->get_instance_id())) {
  601. replicated_nodes.erase(p_node->get_instance_id());
  602. _untrack(id, p_node);
  603. despawn(id, p_node, 0);
  604. }
  605. emit_signal(SNAME("replicated_instance_removed"), id, p_node);
  606. }
  607. }
  608. void MultiplayerReplicator::spawn_all(int p_peer) {
  609. for (const KeyValue<ObjectID, ResourceUID::ID> &E : replicated_nodes) {
  610. // Only server mode adds to replicated_nodes, no need to check it.
  611. Object *obj = ObjectDB::get_instance(E.key);
  612. ERR_CONTINUE(!obj);
  613. Node *node = Object::cast_to<Node>(obj);
  614. ERR_CONTINUE(!node);
  615. spawn(E.value, node, p_peer);
  616. }
  617. }
  618. void MultiplayerReplicator::poll() {
  619. for (KeyValue<ResourceUID::ID, SceneConfig> &E : replications) {
  620. if (!E.value.sync_interval) {
  621. continue;
  622. }
  623. if (E.value.mode == REPLICATION_MODE_SERVER && !multiplayer->is_network_server()) {
  624. continue;
  625. }
  626. uint64_t time = OS::get_singleton()->get_ticks_usec();
  627. if (E.value.sync_last + E.value.sync_interval <= time) {
  628. sync_all(E.key, 0);
  629. E.value.sync_last = time;
  630. }
  631. // Handle wrapping.
  632. if (E.value.sync_last > time) {
  633. E.value.sync_last = time;
  634. }
  635. }
  636. }
  637. void MultiplayerReplicator::track(const ResourceUID::ID &p_scene_id, Object *p_obj) {
  638. ERR_FAIL_COND(!replications.has(p_scene_id));
  639. const SceneConfig &cfg = replications[p_scene_id];
  640. ERR_FAIL_COND_MSG(cfg.mode == REPLICATION_MODE_SERVER, "Manual object tracking is not allowed in server mode.");
  641. _track(p_scene_id, p_obj);
  642. }
  643. void MultiplayerReplicator::_track(const ResourceUID::ID &p_scene_id, Object *p_obj) {
  644. ERR_FAIL_COND(!p_obj);
  645. ERR_FAIL_COND(!replications.has(p_scene_id));
  646. if (!tracked_objects.has(p_scene_id)) {
  647. tracked_objects[p_scene_id] = List<ObjectID>();
  648. }
  649. tracked_objects[p_scene_id].push_back(p_obj->get_instance_id());
  650. }
  651. void MultiplayerReplicator::untrack(const ResourceUID::ID &p_scene_id, Object *p_obj) {
  652. ERR_FAIL_COND(!replications.has(p_scene_id));
  653. const SceneConfig &cfg = replications[p_scene_id];
  654. ERR_FAIL_COND_MSG(cfg.mode == REPLICATION_MODE_SERVER, "Manual object tracking is not allowed in server mode.");
  655. _untrack(p_scene_id, p_obj);
  656. }
  657. void MultiplayerReplicator::_untrack(const ResourceUID::ID &p_scene_id, Object *p_obj) {
  658. ERR_FAIL_COND(!p_obj);
  659. ERR_FAIL_COND(!replications.has(p_scene_id));
  660. if (tracked_objects.has(p_scene_id)) {
  661. tracked_objects[p_scene_id].erase(p_obj->get_instance_id());
  662. }
  663. }
  664. Error MultiplayerReplicator::sync_all(const ResourceUID::ID &p_scene_id, int p_peer) {
  665. ERR_FAIL_COND_V(!replications.has(p_scene_id), ERR_INVALID_PARAMETER);
  666. if (!tracked_objects.has(p_scene_id)) {
  667. return OK;
  668. }
  669. const SceneConfig &cfg = replications[p_scene_id];
  670. if (cfg.on_sync_send.is_valid()) {
  671. Array objs;
  672. if (tracked_objects.has(p_scene_id)) {
  673. objs.resize(tracked_objects[p_scene_id].size());
  674. int idx = 0;
  675. for (const ObjectID &obj_id : tracked_objects[p_scene_id]) {
  676. objs[idx++] = ObjectDB::get_instance(obj_id);
  677. }
  678. }
  679. Variant args[3] = { p_scene_id, objs, p_peer };
  680. Variant *argp[3] = { args, &args[1], &args[2] };
  681. Callable::CallError ce;
  682. Variant ret;
  683. cfg.on_sync_send.call((const Variant **)argp, 3, ret, ce);
  684. ERR_FAIL_COND_V_MSG(ce.error != Callable::CallError::CALL_OK, FAILED, "Custom sync function failed");
  685. return OK;
  686. } else if (cfg.sync_properties.size()) {
  687. return _sync_all_default(p_scene_id, p_peer);
  688. }
  689. return OK;
  690. }
  691. Error MultiplayerReplicator::send_sync(int p_peer_id, const ResourceUID::ID &p_scene_id, PackedByteArray p_data, Multiplayer::TransferMode p_transfer_mode, int p_channel) {
  692. ERR_FAIL_COND_V(!multiplayer->has_network_peer(), ERR_UNCONFIGURED);
  693. ERR_FAIL_COND_V(!replications.has(p_scene_id), ERR_INVALID_PARAMETER);
  694. const SceneConfig &cfg = replications[p_scene_id];
  695. ERR_FAIL_COND_V_MSG(!cfg.on_sync_send.is_valid(), ERR_UNCONFIGURED, "Sending raw sync messages is only available with custom functions");
  696. MAKE_ROOM(SYNC_CMD_OFFSET + p_data.size());
  697. uint8_t *ptr = packet_cache.ptrw();
  698. ptr[0] = MultiplayerAPI::NETWORK_COMMAND_SYNC;
  699. encode_uint64(p_scene_id, &ptr[1]);
  700. Ref<MultiplayerPeer> network_peer = multiplayer->get_network_peer();
  701. network_peer->set_target_peer(p_peer_id);
  702. network_peer->set_transfer_channel(p_channel);
  703. network_peer->set_transfer_mode(p_transfer_mode);
  704. return network_peer->put_packet(ptr, SYNC_CMD_OFFSET + p_data.size());
  705. }
  706. void MultiplayerReplicator::clear() {
  707. tracked_objects.clear();
  708. replicated_nodes.clear();
  709. }
  710. void MultiplayerReplicator::_bind_methods() {
  711. ClassDB::bind_method(D_METHOD("spawn_config", "scene_id", "spawn_mode", "properties", "custom_send", "custom_receive"), &MultiplayerReplicator::spawn_config, DEFVAL(TypedArray<StringName>()), DEFVAL(Callable()), DEFVAL(Callable()));
  712. ClassDB::bind_method(D_METHOD("sync_config", "scene_id", "interval", "properties", "custom_send", "custom_receive"), &MultiplayerReplicator::sync_config, DEFVAL(TypedArray<StringName>()), DEFVAL(Callable()), DEFVAL(Callable()));
  713. ClassDB::bind_method(D_METHOD("despawn", "scene_id", "object", "peer_id"), &MultiplayerReplicator::despawn, DEFVAL(0));
  714. ClassDB::bind_method(D_METHOD("spawn", "scene_id", "object", "peer_id"), &MultiplayerReplicator::spawn, DEFVAL(0));
  715. ClassDB::bind_method(D_METHOD("send_despawn", "peer_id", "scene_id", "data", "path"), &MultiplayerReplicator::send_despawn, DEFVAL(Variant()), DEFVAL(NodePath()));
  716. ClassDB::bind_method(D_METHOD("send_spawn", "peer_id", "scene_id", "data", "path"), &MultiplayerReplicator::send_spawn, DEFVAL(Variant()), DEFVAL(NodePath()));
  717. ClassDB::bind_method(D_METHOD("send_sync", "peer_id", "scene_id", "data", "transfer_mode", "channel"), &MultiplayerReplicator::send_sync, DEFVAL(Multiplayer::TRANSFER_MODE_RELIABLE), DEFVAL(0));
  718. ClassDB::bind_method(D_METHOD("sync_all", "scene_id", "peer_id"), &MultiplayerReplicator::sync_all, DEFVAL(0));
  719. ClassDB::bind_method(D_METHOD("track", "scene_id", "object"), &MultiplayerReplicator::track);
  720. ClassDB::bind_method(D_METHOD("untrack", "scene_id", "object"), &MultiplayerReplicator::untrack);
  721. ClassDB::bind_method(D_METHOD("encode_state", "scene_id", "object", "initial"), &MultiplayerReplicator::encode_state, DEFVAL(true));
  722. ClassDB::bind_method(D_METHOD("decode_state", "scene_id", "object", "data", "initial"), &MultiplayerReplicator::decode_state, DEFVAL(true));
  723. ADD_SIGNAL(MethodInfo("despawned", PropertyInfo(Variant::INT, "scene_id"), PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  724. ADD_SIGNAL(MethodInfo("spawned", PropertyInfo(Variant::INT, "scene_id"), PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  725. ADD_SIGNAL(MethodInfo("despawn_requested", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::INT, "scene_id"), PropertyInfo(Variant::OBJECT, "parent", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data")));
  726. ADD_SIGNAL(MethodInfo("spawn_requested", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::INT, "scene_id"), PropertyInfo(Variant::OBJECT, "parent", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data")));
  727. ADD_SIGNAL(MethodInfo("replicated_instance_added", PropertyInfo(Variant::INT, "scene_id"), PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  728. ADD_SIGNAL(MethodInfo("replicated_instance_removed", PropertyInfo(Variant::INT, "scene_id"), PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  729. BIND_ENUM_CONSTANT(REPLICATION_MODE_NONE);
  730. BIND_ENUM_CONSTANT(REPLICATION_MODE_SERVER);
  731. BIND_ENUM_CONSTANT(REPLICATION_MODE_CUSTOM);
  732. }