packed_scene.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #pragma once
  31. #include "core/io/resource.h"
  32. #include "scene/main/node.h"
  33. class SceneState : public RefCounted {
  34. GDCLASS(SceneState, RefCounted);
  35. Vector<StringName> names;
  36. Vector<Variant> variants;
  37. Vector<NodePath> node_paths;
  38. Vector<NodePath> editable_instances;
  39. mutable HashMap<NodePath, int> node_path_cache;
  40. mutable HashMap<int, int> base_scene_node_remap;
  41. int base_scene_idx = -1;
  42. enum {
  43. NO_PARENT_SAVED = 0x7FFFFFFF,
  44. NAME_INDEX_BITS = 18,
  45. NAME_MASK = (1 << NAME_INDEX_BITS) - 1,
  46. };
  47. struct NodeData {
  48. int parent = 0;
  49. int owner = 0;
  50. int type = 0;
  51. int name = 0;
  52. int instance = 0;
  53. int index = 0;
  54. struct Property {
  55. int name = 0;
  56. int value = 0;
  57. };
  58. Vector<Property> properties;
  59. Vector<int> groups;
  60. };
  61. struct DeferredNodePathProperties {
  62. ObjectID base;
  63. StringName property;
  64. Variant value;
  65. };
  66. Vector<NodeData> nodes;
  67. struct ConnectionData {
  68. int from = 0;
  69. int to = 0;
  70. int signal = 0;
  71. int method = 0;
  72. int flags = 0;
  73. int unbinds = 0;
  74. Vector<int> binds;
  75. };
  76. Vector<ConnectionData> connections;
  77. 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);
  78. 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);
  79. String path;
  80. uint64_t last_modified_time = 0;
  81. static bool disable_placeholders;
  82. Vector<String> _get_node_groups(int p_idx) const;
  83. int _find_base_scene_node_remap_key(int p_idx) const;
  84. #ifdef TOOLS_ENABLED
  85. public:
  86. typedef void (*InstantiationWarningNotify)(const String &p_warning);
  87. private:
  88. static InstantiationWarningNotify instantiation_warn_notify;
  89. #endif
  90. protected:
  91. static void _bind_methods();
  92. public:
  93. enum {
  94. FLAG_ID_IS_PATH = (1 << 30),
  95. TYPE_INSTANTIATED = 0x7FFFFFFF,
  96. FLAG_INSTANCE_IS_PLACEHOLDER = (1 << 30),
  97. FLAG_PATH_PROPERTY_IS_NODE = (1 << 30),
  98. FLAG_PROP_NAME_MASK = FLAG_PATH_PROPERTY_IS_NODE - 1,
  99. FLAG_MASK = (1 << 24) - 1,
  100. };
  101. enum GenEditState {
  102. GEN_EDIT_STATE_DISABLED,
  103. GEN_EDIT_STATE_INSTANCE,
  104. GEN_EDIT_STATE_MAIN,
  105. GEN_EDIT_STATE_MAIN_INHERITED,
  106. };
  107. struct PackState {
  108. Ref<SceneState> state;
  109. int node = -1;
  110. };
  111. static void set_disable_placeholders(bool p_disable);
  112. 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);
  113. int find_node_by_path(const NodePath &p_node) const;
  114. Variant get_property_value(int p_node, const StringName &p_property, bool &r_found, bool &r_node_deferred) const;
  115. bool is_node_in_group(int p_node, const StringName &p_group) const;
  116. bool is_connection(int p_node, const StringName &p_signal, int p_to_node, const StringName &p_to_method) const;
  117. void set_bundled_scene(const Dictionary &p_dictionary);
  118. Dictionary get_bundled_scene() const;
  119. Error pack(Node *p_scene);
  120. void set_path(const String &p_path);
  121. String get_path() const;
  122. void clear();
  123. Error copy_from(const Ref<SceneState> &p_scene_state);
  124. bool can_instantiate() const;
  125. Node *instantiate(GenEditState p_edit_state) const;
  126. Array setup_resources_in_array(Array &array_to_scan, const SceneState::NodeData &n, HashMap<Ref<Resource>, Ref<Resource>> &resources_local_to_sub_scene, Node *node, const StringName sname, HashMap<Ref<Resource>, Ref<Resource>> &resources_local_to_scene, int i, Node **ret_nodes, SceneState::GenEditState p_edit_state) const;
  127. Dictionary setup_resources_in_dictionary(Dictionary &p_dictionary_to_scan, const SceneState::NodeData &p_n, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_sub_scene, Node *p_node, const StringName p_sname, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_scene, int p_i, Node **p_ret_nodes, SceneState::GenEditState p_edit_state) const;
  128. Variant make_local_resource(Variant &value, const SceneState::NodeData &p_node_data, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_sub_scene, Node *p_node, const StringName p_sname, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_scene, int p_i, Node **p_ret_nodes, SceneState::GenEditState p_edit_state) const;
  129. bool has_local_resource(const Array &p_array) const;
  130. Ref<SceneState> get_base_scene_state() const;
  131. void update_instance_resource(String p_path, Ref<PackedScene> p_packed_scene);
  132. //unbuild API
  133. int get_node_count() const;
  134. StringName get_node_type(int p_idx) const;
  135. StringName get_node_name(int p_idx) const;
  136. NodePath get_node_path(int p_idx, bool p_for_parent = false) const;
  137. NodePath get_node_owner_path(int p_idx) const;
  138. Ref<PackedScene> get_node_instance(int p_idx) const;
  139. String get_node_instance_placeholder(int p_idx) const;
  140. bool is_node_instance_placeholder(int p_idx) const;
  141. Vector<StringName> get_node_groups(int p_idx) const;
  142. int get_node_index(int p_idx) const;
  143. int get_node_property_count(int p_idx) const;
  144. StringName get_node_property_name(int p_idx, int p_prop) const;
  145. Variant get_node_property_value(int p_idx, int p_prop) const;
  146. Vector<String> get_node_deferred_nodepath_properties(int p_idx) const;
  147. int get_connection_count() const;
  148. NodePath get_connection_source(int p_idx) const;
  149. StringName get_connection_signal(int p_idx) const;
  150. NodePath get_connection_target(int p_idx) const;
  151. StringName get_connection_method(int p_idx) const;
  152. int get_connection_flags(int p_idx) const;
  153. int get_connection_unbinds(int p_idx) const;
  154. Array get_connection_binds(int p_idx) const;
  155. 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);
  156. Vector<NodePath> get_editable_instances() const;
  157. Ref<Resource> get_sub_resource(const String &p_path);
  158. //build API
  159. int add_name(const StringName &p_name);
  160. int add_value(const Variant &p_value);
  161. int add_node_path(const NodePath &p_path);
  162. int add_node(int p_parent, int p_owner, int p_type, int p_name, int p_instance, int p_index);
  163. void add_node_property(int p_node, int p_name, int p_value, bool p_deferred_node_path = false);
  164. void add_node_group(int p_node, int p_group);
  165. void set_base_scene(int p_idx);
  166. 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);
  167. void add_editable_instance(const NodePath &p_path);
  168. bool remove_group_references(const StringName &p_name);
  169. bool rename_group_references(const StringName &p_old_name, const StringName &p_new_name);
  170. HashSet<StringName> get_all_groups();
  171. virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; }
  172. uint64_t get_last_modified_time() const { return last_modified_time; }
  173. // Used when saving pointers (saves a path property instead).
  174. static String get_meta_pointer_property(const String &p_property);
  175. #ifdef TOOLS_ENABLED
  176. static void set_instantiation_warning_notify_func(InstantiationWarningNotify p_warn_notify) { instantiation_warn_notify = p_warn_notify; }
  177. #endif
  178. SceneState();
  179. };
  180. VARIANT_ENUM_CAST(SceneState::GenEditState)
  181. class PackedScene : public Resource {
  182. GDCLASS(PackedScene, Resource);
  183. RES_BASE_EXTENSION("scn");
  184. Ref<SceneState> state;
  185. void _set_bundled_scene(const Dictionary &p_scene);
  186. Dictionary _get_bundled_scene() const;
  187. protected:
  188. virtual bool editor_can_reload_from_file() override { return false; } // this is handled by editor better
  189. static void _bind_methods();
  190. virtual void reset_state() override;
  191. public:
  192. enum GenEditState {
  193. GEN_EDIT_STATE_DISABLED,
  194. GEN_EDIT_STATE_INSTANCE,
  195. GEN_EDIT_STATE_MAIN,
  196. GEN_EDIT_STATE_MAIN_INHERITED,
  197. };
  198. Error pack(Node *p_scene);
  199. void clear();
  200. bool can_instantiate() const;
  201. Node *instantiate(GenEditState p_edit_state = GEN_EDIT_STATE_DISABLED) const;
  202. void recreate_state();
  203. void replace_state(Ref<SceneState> p_by);
  204. virtual void reload_from_file() override;
  205. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  206. virtual void set_path_cache(const String &p_path) override;
  207. #ifdef TOOLS_ENABLED
  208. virtual void set_last_modified_time(uint64_t p_time) override {
  209. Resource::set_last_modified_time(p_time);
  210. state->set_last_modified_time(p_time);
  211. }
  212. static HashSet<StringName> get_scene_groups(const String &p_path);
  213. #endif
  214. Ref<SceneState> get_state() const;
  215. PackedScene();
  216. };
  217. VARIANT_ENUM_CAST(PackedScene::GenEditState)