resource_format_binary.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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<Resource> cache;
  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. 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);
  90. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  91. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  92. virtual bool handles_type(const String &p_type) const;
  93. virtual String get_resource_type(const String &p_path) const;
  94. virtual String get_resource_script_class(const String &p_path) const;
  95. virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes);
  96. virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
  97. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  98. virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
  99. };
  100. class ResourceFormatSaverBinaryInstance {
  101. String local_path;
  102. String path;
  103. bool relative_paths;
  104. bool bundle_resources;
  105. bool skip_editor;
  106. bool big_endian;
  107. bool takeover_paths;
  108. String magic;
  109. HashSet<Ref<Resource>> resource_set;
  110. struct NonPersistentKey { //for resource properties generated on the fly
  111. Ref<Resource> base;
  112. StringName property;
  113. bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
  114. };
  115. RBMap<NonPersistentKey, Ref<Resource>> non_persistent_map;
  116. HashMap<StringName, int> string_map;
  117. Vector<StringName> strings;
  118. HashMap<Ref<Resource>, int> external_resources;
  119. List<Ref<Resource>> saved_resources;
  120. struct Property {
  121. int name_idx;
  122. Variant value;
  123. PropertyInfo pi;
  124. };
  125. struct ResourceData {
  126. String type;
  127. List<Property> properties;
  128. };
  129. static void _pad_buffer(Ref<FileAccess> f, int p_bytes);
  130. void _find_resources(const Variant &p_variant, bool p_main = false);
  131. static void save_unicode_string(Ref<FileAccess> f, const String &p_string, bool p_bit_on_len = false);
  132. int get_string_index(const String &p_string);
  133. public:
  134. enum {
  135. FORMAT_FLAG_NAMED_SCENE_IDS = 1,
  136. FORMAT_FLAG_UIDS = 2,
  137. FORMAT_FLAG_REAL_T_IS_DOUBLE = 4,
  138. FORMAT_FLAG_HAS_SCRIPT_CLASS = 8,
  139. // Amount of reserved 32-bit fields in resource header
  140. RESERVED_FIELDS = 11
  141. };
  142. Error save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags = 0);
  143. Error set_uid(const String &p_path, ResourceUID::ID p_uid);
  144. 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());
  145. };
  146. class ResourceFormatSaverBinary : public ResourceFormatSaver {
  147. public:
  148. static ResourceFormatSaverBinary *singleton;
  149. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0);
  150. virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid);
  151. virtual bool recognize(const Ref<Resource> &p_resource) const;
  152. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const;
  153. ResourceFormatSaverBinary();
  154. };
  155. #endif // RESOURCE_FORMAT_BINARY_H