| 1 | /**************************************************************************/ |
| 2 | /* packed_scene.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 | #ifndef PACKED_SCENE_H |
| 32 | #define PACKED_SCENE_H |
| 33 | |
| 34 | #include "core/io/resource.h" |
| 35 | #include "scene/main/node.h" |
| 36 | |
| 37 | class SceneState : public RefCounted { |
| 38 | GDCLASS(SceneState, RefCounted); |
| 39 | |
| 40 | Vector<StringName> names; |
| 41 | Vector<Variant> variants; |
| 42 | Vector<NodePath> node_paths; |
| 43 | Vector<NodePath> editable_instances; |
| 44 | mutable HashMap<NodePath, int> node_path_cache; |
| 45 | mutable HashMap<int, int> base_scene_node_remap; |
| 46 | |
| 47 | int base_scene_idx = -1; |
| 48 | |
| 49 | enum { |
| 50 | NO_PARENT_SAVED = 0x7FFFFFFF, |
| 51 | NAME_INDEX_BITS = 18, |
| 52 | NAME_MASK = (1 << NAME_INDEX_BITS) - 1, |
| 53 | }; |
| 54 | |
| 55 | struct NodeData { |
| 56 | int parent = 0; |
| 57 | int owner = 0; |
| 58 | int type = 0; |
| 59 | int name = 0; |
| 60 | int instance = 0; |
| 61 | int index = 0; |
| 62 | |
| 63 | struct Property { |
| 64 | int name = 0; |
| 65 | int value = 0; |
| 66 | }; |
| 67 | |
| 68 | Vector<Property> properties; |
| 69 | Vector<int> groups; |
| 70 | }; |
| 71 | |
| 72 | struct DeferredNodePathProperties { |
| 73 | Node *base = nullptr; |
| 74 | StringName property; |
| 75 | Variant value; |
| 76 | }; |
| 77 | |
| 78 | Vector<NodeData> nodes; |
| 79 | |
| 80 | struct ConnectionData { |
| 81 | int from = 0; |
| 82 | int to = 0; |
| 83 | int signal = 0; |
| 84 | int method = 0; |
| 85 | int flags = 0; |
| 86 | int unbinds = 0; |
| 87 | Vector<int> binds; |
| 88 | }; |
| 89 | |
| 90 | Vector<ConnectionData> connections; |
| 91 | |
| 92 | Error _parse_node(Node *p_owner, Node *p_node, int p_parent_idx, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map); |
| 93 | Error _parse_connections(Node *p_owner, Node *p_node, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map); |
| 94 | |
| 95 | String path; |
| 96 | |
| 97 | uint64_t last_modified_time = 0; |
| 98 | |
| 99 | static bool disable_placeholders; |
| 100 | |
| 101 | Vector<String> _get_node_groups(int p_idx) const; |
| 102 | |
| 103 | int _find_base_scene_node_remap_key(int p_idx) const; |
| 104 | |
| 105 | #ifdef TOOLS_ENABLED |
| 106 | public: |
| 107 | typedef void (*InstantiationWarningNotify)(const String &p_warning); |
| 108 | |
| 109 | private: |
| 110 | static InstantiationWarningNotify instantiation_warn_notify; |
| 111 | #endif |
| 112 | |
| 113 | protected: |
| 114 | static void _bind_methods(); |
| 115 | |
| 116 | public: |
| 117 | enum { |
| 118 | FLAG_ID_IS_PATH = (1 << 30), |
| 119 | TYPE_INSTANTIATED = 0x7FFFFFFF, |
| 120 | FLAG_INSTANCE_IS_PLACEHOLDER = (1 << 30), |
| 121 | FLAG_PATH_PROPERTY_IS_NODE = (1 << 30), |
| 122 | FLAG_PROP_NAME_MASK = FLAG_PATH_PROPERTY_IS_NODE - 1, |
| 123 | FLAG_MASK = (1 << 24) - 1, |
| 124 | }; |
| 125 | |
| 126 | enum GenEditState { |
| 127 | GEN_EDIT_STATE_DISABLED, |
| 128 | GEN_EDIT_STATE_INSTANCE, |
| 129 | GEN_EDIT_STATE_MAIN, |
| 130 | GEN_EDIT_STATE_MAIN_INHERITED, |
| 131 | }; |
| 132 | |
| 133 | struct PackState { |
| 134 | Ref<SceneState> state; |
| 135 | int node = -1; |
| 136 | }; |
| 137 | |
| 138 | static void set_disable_placeholders(bool p_disable); |
| 139 | static Ref<Resource> get_remap_resource(const Ref<Resource> &p_resource, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache, const Ref<Resource> &p_fallback, Node *p_for_scene); |
| 140 | |
| 141 | int find_node_by_path(const NodePath &p_node) const; |
| 142 | Variant get_property_value(int p_node, const StringName &p_property, bool &found) const; |
| 143 | bool is_node_in_group(int p_node, const StringName &p_group) const; |
| 144 | bool is_connection(int p_node, const StringName &p_signal, int p_to_node, const StringName &p_to_method) const; |
| 145 | |
| 146 | void set_bundled_scene(const Dictionary &p_dictionary); |
| 147 | Dictionary get_bundled_scene() const; |
| 148 | |
| 149 | Error pack(Node *p_scene); |
| 150 | |
| 151 | void set_path(const String &p_path); |
| 152 | String get_path() const; |
| 153 | |
| 154 | void clear(); |
| 155 | Error copy_from(const Ref<SceneState> &p_scene_state); |
| 156 | |
| 157 | bool can_instantiate() const; |
| 158 | Node *instantiate(GenEditState p_edit_state) const; |
| 159 | |
| 160 | Ref<SceneState> get_base_scene_state() const; |
| 161 | |
| 162 | void update_instance_resource(String p_path, Ref<PackedScene> p_packed_scene); |
| 163 | |
| 164 | //unbuild API |
| 165 | |
| 166 | int get_node_count() const; |
| 167 | StringName get_node_type(int p_idx) const; |
| 168 | StringName get_node_name(int p_idx) const; |
| 169 | NodePath get_node_path(int p_idx, bool p_for_parent = false) const; |
| 170 | NodePath get_node_owner_path(int p_idx) const; |
| 171 | Ref<PackedScene> get_node_instance(int p_idx) const; |
| 172 | String get_node_instance_placeholder(int p_idx) const; |
| 173 | bool is_node_instance_placeholder(int p_idx) const; |
| 174 | Vector<StringName> get_node_groups(int p_idx) const; |
| 175 | int get_node_index(int p_idx) const; |
| 176 | |
| 177 | int get_node_property_count(int p_idx) const; |
| 178 | StringName get_node_property_name(int p_idx, int p_prop) const; |
| 179 | Variant get_node_property_value(int p_idx, int p_prop) const; |
| 180 | Vector<String> get_node_deferred_nodepath_properties(int p_idx) const; |
| 181 | |
| 182 | int get_connection_count() const; |
| 183 | NodePath get_connection_source(int p_idx) const; |
| 184 | StringName get_connection_signal(int p_idx) const; |
| 185 | NodePath get_connection_target(int p_idx) const; |
| 186 | StringName get_connection_method(int p_idx) const; |
| 187 | int get_connection_flags(int p_idx) const; |
| 188 | int get_connection_unbinds(int p_idx) const; |
| 189 | Array get_connection_binds(int p_idx) const; |
| 190 | |
| 191 | bool has_connection(const NodePath &p_node_from, const StringName &p_signal, const NodePath &p_node_to, const StringName &p_method, bool p_no_inheritance = false); |
| 192 | |
| 193 | Vector<NodePath> get_editable_instances() const; |
| 194 | |
| 195 | //build API |
| 196 | |
| 197 | int add_name(const StringName &p_name); |
| 198 | int add_value(const Variant &p_value); |
| 199 | int add_node_path(const NodePath &p_path); |
| 200 | int add_node(int p_parent, int p_owner, int p_type, int p_name, int p_instance, int p_index); |
| 201 | void add_node_property(int p_node, int p_name, int p_value, bool p_deferred_node_path = false); |
| 202 | void add_node_group(int p_node, int p_group); |
| 203 | void set_base_scene(int p_idx); |
| 204 | void add_connection(int p_from, int p_to, int p_signal, int p_method, int p_flags, int p_unbinds, const Vector<int> &p_binds); |
| 205 | void add_editable_instance(const NodePath &p_path); |
| 206 | |
| 207 | virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; } |
| 208 | uint64_t get_last_modified_time() const { return last_modified_time; } |
| 209 | |
| 210 | // Used when saving pointers (saves a path property instead). |
| 211 | static String get_meta_pointer_property(const String &p_property); |
| 212 | |
| 213 | #ifdef TOOLS_ENABLED |
| 214 | static void set_instantiation_warning_notify_func(InstantiationWarningNotify p_warn_notify) { instantiation_warn_notify = p_warn_notify; } |
| 215 | #endif |
| 216 | |
| 217 | SceneState(); |
| 218 | }; |
| 219 | |
| 220 | VARIANT_ENUM_CAST(SceneState::GenEditState) |
| 221 | |
| 222 | class PackedScene : public Resource { |
| 223 | GDCLASS(PackedScene, Resource); |
| 224 | RES_BASE_EXTENSION("scn" ); |
| 225 | |
| 226 | Ref<SceneState> state; |
| 227 | |
| 228 | void _set_bundled_scene(const Dictionary &p_scene); |
| 229 | Dictionary _get_bundled_scene() const; |
| 230 | |
| 231 | protected: |
| 232 | virtual bool editor_can_reload_from_file() override { return false; } // this is handled by editor better |
| 233 | static void _bind_methods(); |
| 234 | virtual void reset_state() override; |
| 235 | |
| 236 | public: |
| 237 | enum GenEditState { |
| 238 | GEN_EDIT_STATE_DISABLED, |
| 239 | GEN_EDIT_STATE_INSTANCE, |
| 240 | GEN_EDIT_STATE_MAIN, |
| 241 | GEN_EDIT_STATE_MAIN_INHERITED, |
| 242 | }; |
| 243 | |
| 244 | Error pack(Node *p_scene); |
| 245 | |
| 246 | void clear(); |
| 247 | |
| 248 | bool can_instantiate() const; |
| 249 | Node *instantiate(GenEditState p_edit_state = GEN_EDIT_STATE_DISABLED) const; |
| 250 | |
| 251 | void recreate_state(); |
| 252 | void replace_state(Ref<SceneState> p_by); |
| 253 | |
| 254 | virtual void reload_from_file() override; |
| 255 | |
| 256 | virtual void set_path(const String &p_path, bool p_take_over = false) override; |
| 257 | #ifdef TOOLS_ENABLED |
| 258 | virtual void set_last_modified_time(uint64_t p_time) override { |
| 259 | Resource::set_last_modified_time(p_time); |
| 260 | state->set_last_modified_time(p_time); |
| 261 | } |
| 262 | |
| 263 | #endif |
| 264 | Ref<SceneState> get_state() const; |
| 265 | |
| 266 | PackedScene(); |
| 267 | }; |
| 268 | |
| 269 | VARIANT_ENUM_CAST(PackedScene::GenEditState) |
| 270 | |
| 271 | #endif // PACKED_SCENE_H |
| 272 | |