resource_format_binary.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*************************************************************************/
  2. /* resource_format_binary.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/os/file_access.h"
  35. class ResourceLoaderBinary {
  36. bool translation_remapped;
  37. String local_path;
  38. String res_path;
  39. String type;
  40. Ref<Resource> resource;
  41. uint32_t ver_format;
  42. FileAccess *f;
  43. uint64_t importmd_ofs;
  44. Vector<char> str_buf;
  45. List<RES> resource_cache;
  46. Vector<StringName> string_map;
  47. StringName _get_string();
  48. struct ExtResource {
  49. String path;
  50. String type;
  51. RES cache;
  52. };
  53. bool use_sub_threads;
  54. float *progress;
  55. Vector<ExtResource> external_resources;
  56. struct IntResource {
  57. String path;
  58. uint64_t offset;
  59. RES cache;
  60. };
  61. Vector<IntResource> internal_resources;
  62. String get_unicode_string();
  63. void _advance_padding(uint32_t p_len);
  64. Map<String, String> remaps;
  65. Error error;
  66. bool use_nocache;
  67. friend class ResourceFormatLoaderBinary;
  68. Error parse_variant(Variant &r_v);
  69. Map<String, RES> dependency_cache;
  70. public:
  71. void set_local_path(const String &p_local_path);
  72. Ref<Resource> get_resource();
  73. Error load();
  74. void set_translation_remapped(bool p_remapped);
  75. void set_remaps(const Map<String, String> &p_remaps) { remaps = p_remaps; }
  76. void open(FileAccess *p_f);
  77. String recognize(FileAccess *p_f);
  78. void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
  79. ResourceLoaderBinary();
  80. ~ResourceLoaderBinary();
  81. };
  82. class ResourceFormatLoaderBinary : public ResourceFormatLoader {
  83. public:
  84. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, bool p_no_cache = false);
  85. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  86. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  87. virtual bool handles_type(const String &p_type) const;
  88. virtual String get_resource_type(const String &p_path) const;
  89. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  90. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  91. };
  92. class ResourceFormatSaverBinaryInstance {
  93. String local_path;
  94. String path;
  95. bool relative_paths;
  96. bool bundle_resources;
  97. bool skip_editor;
  98. bool big_endian;
  99. bool takeover_paths;
  100. FileAccess *f;
  101. String magic;
  102. Set<RES> resource_set;
  103. struct NonPersistentKey { //for resource properties generated on the fly
  104. RES base;
  105. StringName property;
  106. bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
  107. };
  108. Map<NonPersistentKey, RES> non_persistent_map;
  109. Map<StringName, int> string_map;
  110. Vector<StringName> strings;
  111. Map<RES, int> external_resources;
  112. List<RES> saved_resources;
  113. struct Property {
  114. int name_idx;
  115. Variant value;
  116. PropertyInfo pi;
  117. };
  118. struct ResourceData {
  119. String type;
  120. List<Property> properties;
  121. };
  122. static void _pad_buffer(FileAccess *f, int p_bytes);
  123. void _write_variant(const Variant &p_property, const PropertyInfo &p_hint = PropertyInfo());
  124. void _find_resources(const Variant &p_variant, bool p_main = false);
  125. static void save_unicode_string(FileAccess *f, const String &p_string, bool p_bit_on_len = false);
  126. int get_string_index(const String &p_string);
  127. public:
  128. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  129. static void write_variant(FileAccess *f, const Variant &p_property, Set<RES> &resource_set, Map<RES, int> &external_resources, Map<StringName, int> &string_map, const PropertyInfo &p_hint = PropertyInfo());
  130. };
  131. class ResourceFormatSaverBinary : public ResourceFormatSaver {
  132. public:
  133. static ResourceFormatSaverBinary *singleton;
  134. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  135. virtual bool recognize(const RES &p_resource) const;
  136. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  137. ResourceFormatSaverBinary();
  138. };
  139. #endif // RESOURCE_FORMAT_BINARY_H