resource_format_binary.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**************************************************************************/
  2. /* resource_format_binary.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. #ifndef RESOURCE_FORMAT_BINARY_H
  31. #define RESOURCE_FORMAT_BINARY_H
  32. #include "core/io/file_access.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/io/resource_saver.h"
  35. class ResourceLoaderBinary {
  36. bool translation_remapped = false;
  37. String local_path;
  38. String res_path;
  39. String type;
  40. Ref<Resource> resource;
  41. uint32_t ver_format = 0;
  42. Ref<FileAccess> f;
  43. uint64_t importmd_ofs = 0;
  44. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  45. Vector<char> str_buf;
  46. List<Ref<Resource>> resource_cache;
  47. Vector<StringName> string_map;
  48. StringName _get_string();
  49. struct ExtResource {
  50. String path;
  51. String type;
  52. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  53. Ref<ResourceLoader::LoadToken> load_token;
  54. };
  55. bool using_named_scene_ids = false;
  56. bool using_uids = false;
  57. String script_class;
  58. bool use_sub_threads = false;
  59. float *progress = nullptr;
  60. Vector<ExtResource> external_resources;
  61. struct IntResource {
  62. String path;
  63. uint64_t offset;
  64. };
  65. Vector<IntResource> internal_resources;
  66. HashMap<String, Ref<Resource>> internal_index_cache;
  67. String get_unicode_string();
  68. void _advance_padding(uint32_t p_len);
  69. HashMap<String, String> remaps;
  70. Error error = OK;
  71. ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE;
  72. ResourceFormatLoader::CacheMode cache_mode_for_external = ResourceFormatLoader::CACHE_MODE_REUSE;
  73. friend class ResourceFormatLoaderBinary;
  74. Error parse_variant(Variant &r_v);
  75. HashMap<String, Ref<Resource>> dependency_cache;
  76. public:
  77. Ref<Resource> get_resource();
  78. Error load();
  79. void set_translation_remapped(bool p_remapped);
  80. void set_remaps(const HashMap<String, String> &p_remaps) { remaps = p_remaps; }
  81. void open(Ref<FileAccess> p_f, bool p_no_resources = false, bool p_keep_uuid_paths = false);
  82. String recognize(Ref<FileAccess> p_f);
  83. String recognize_script_class(Ref<FileAccess> p_f);
  84. void get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types);
  85. void get_classes_used(Ref<FileAccess> p_f, HashSet<StringName> *p_classes);
  86. ResourceLoaderBinary() {}
  87. };
  88. class ResourceFormatLoaderBinary : public ResourceFormatLoader {
  89. public:
  90. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  91. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const override;
  92. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  93. virtual bool handles_type(const String &p_type) const override;
  94. virtual String get_resource_type(const String &p_path) const override;
  95. virtual String get_resource_script_class(const String &p_path) const override;
  96. virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes) override;
  97. virtual ResourceUID::ID get_resource_uid(const String &p_path) const override;
  98. virtual bool has_custom_uid_support() const override;
  99. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;
  100. virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) override;
  101. };
  102. class ResourceFormatSaverBinaryInstance {
  103. String local_path;
  104. String path;
  105. bool relative_paths;
  106. bool bundle_resources;
  107. bool skip_editor;
  108. bool big_endian;
  109. bool takeover_paths;
  110. String magic;
  111. HashSet<Ref<Resource>> resource_set;
  112. struct NonPersistentKey { //for resource properties generated on the fly
  113. Ref<Resource> base;
  114. StringName property;
  115. bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
  116. };
  117. RBMap<NonPersistentKey, Variant> non_persistent_map;
  118. HashMap<StringName, int> string_map;
  119. Vector<StringName> strings;
  120. HashMap<Ref<Resource>, int> external_resources;
  121. List<Ref<Resource>> saved_resources;
  122. struct Property {
  123. int name_idx;
  124. Variant value;
  125. PropertyInfo pi;
  126. };
  127. struct ResourceData {
  128. String type;
  129. List<Property> properties;
  130. };
  131. static void _pad_buffer(Ref<FileAccess> f, int p_bytes);
  132. void _find_resources(const Variant &p_variant, bool p_main = false);
  133. static void save_unicode_string(Ref<FileAccess> f, const String &p_string, bool p_bit_on_len = false);
  134. int get_string_index(const String &p_string);
  135. public:
  136. enum {
  137. FORMAT_FLAG_NAMED_SCENE_IDS = 1,
  138. FORMAT_FLAG_UIDS = 2,
  139. FORMAT_FLAG_REAL_T_IS_DOUBLE = 4,
  140. FORMAT_FLAG_HAS_SCRIPT_CLASS = 8,
  141. // Amount of reserved 32-bit fields in resource header
  142. RESERVED_FIELDS = 11
  143. };
  144. Error save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags = 0);
  145. Error set_uid(const String &p_path, ResourceUID::ID p_uid);
  146. static void write_variant(Ref<FileAccess> f, const Variant &p_property, HashMap<Ref<Resource>, int> &resource_map, HashMap<Ref<Resource>, int> &external_resources, HashMap<StringName, int> &string_map, const PropertyInfo &p_hint = PropertyInfo());
  147. };
  148. class ResourceFormatSaverBinary : public ResourceFormatSaver {
  149. public:
  150. static ResourceFormatSaverBinary *singleton;
  151. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
  152. virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid) override;
  153. virtual bool recognize(const Ref<Resource> &p_resource) const override;
  154. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
  155. ResourceFormatSaverBinary();
  156. };
  157. #endif // RESOURCE_FORMAT_BINARY_H