resource_format_binary.h 7.6 KB

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