| 1 | /**************************************************************************/ |
| 2 | /* scene_cache_interface.h */ |
| 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 | #pragma once |
| 32 | |
| 33 | #include "core/object/ref_counted.h" |
| 34 | |
| 35 | class Node; |
| 36 | class SceneMultiplayer; |
| 37 | |
| 38 | class SceneCacheInterface : public RefCounted { |
| 39 | GDCLASS(SceneCacheInterface, RefCounted); |
| 40 | |
| 41 | private: |
| 42 | SceneMultiplayer *multiplayer = nullptr; |
| 43 | |
| 44 | //path sent caches |
| 45 | struct NodeCache { |
| 46 | int cache_id = 0; |
| 47 | HashMap<int, int> recv_ids; // peer id, remote cache id |
| 48 | HashMap<int, bool> confirmed_peers; // peer id, confirmed |
| 49 | }; |
| 50 | |
| 51 | struct RecvNode { |
| 52 | ObjectID oid; |
| 53 | NodePath path; |
| 54 | |
| 55 | RecvNode(const ObjectID &p_oid, const NodePath &p_path) { |
| 56 | oid = p_oid; |
| 57 | path = p_path; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | struct PeerInfo { |
| 62 | HashMap<int, RecvNode> recv_nodes; // remote cache id, (ObjectID, NodePath) |
| 63 | HashSet<ObjectID> sent_nodes; |
| 64 | }; |
| 65 | |
| 66 | HashMap<ObjectID, NodeCache> nodes_cache; |
| 67 | HashMap<int, ObjectID> assigned_ids; |
| 68 | HashMap<int, PeerInfo> peers_info; |
| 69 | int last_send_cache_id = 1; |
| 70 | |
| 71 | void _remove_node_cache(ObjectID p_oid); |
| 72 | NodeCache &_track(Node *p_node); |
| 73 | |
| 74 | protected: |
| 75 | Error _send_confirm_path(Node *p_node, NodeCache &p_cache, const List<int> &p_peers); |
| 76 | |
| 77 | public: |
| 78 | void clear(); |
| 79 | void on_peer_change(int p_id, bool p_connected); |
| 80 | void process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len); |
| 81 | void process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len); |
| 82 | |
| 83 | // Returns true if all peers have cached path. |
| 84 | bool send_object_cache(Object *p_obj, int p_target, int &p_id); |
| 85 | int make_object_cache(Object *p_obj); |
| 86 | Object *get_cached_object(int p_from, uint32_t p_cache_id); |
| 87 | bool is_cache_confirmed(Node *p_path, int p_peer); |
| 88 | |
| 89 | SceneCacheInterface(SceneMultiplayer *p_multiplayer) { multiplayer = p_multiplayer; } |
| 90 | }; |