resource_format_binary.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/os/file_access.h"
  35. class ResourceInteractiveLoaderBinary : public ResourceInteractiveLoader {
  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. //Map<int,StringName> string_map;
  47. Vector<StringName> string_map;
  48. StringName _get_string();
  49. struct ExtResource {
  50. String path;
  51. String type;
  52. };
  53. Vector<ExtResource> external_resources;
  54. struct IntResource {
  55. String path;
  56. uint64_t offset;
  57. };
  58. Vector<IntResource> internal_resources;
  59. Map<uint32_t, RES> internal_resources_cache;
  60. String get_unicode_string();
  61. void _advance_padding(uint32_t p_len);
  62. Map<String, String> remaps;
  63. Error error;
  64. int stage;
  65. friend class ResourceFormatLoaderBinary;
  66. Error parse_variant(Variant &r_v);
  67. public:
  68. virtual void set_local_path(const String &p_local_path);
  69. virtual Ref<Resource> get_resource();
  70. virtual Error poll();
  71. virtual int get_stage() const;
  72. virtual int get_stage_count() const;
  73. virtual void set_translation_remapped(bool p_remapped);
  74. void set_remaps(const Map<String, String> &p_remaps) { remaps = p_remaps; }
  75. void open(FileAccess *p_f);
  76. String recognize(FileAccess *p_f);
  77. void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
  78. ResourceInteractiveLoaderBinary();
  79. ~ResourceInteractiveLoaderBinary();
  80. };
  81. class ResourceFormatLoaderBinary : public ResourceFormatLoader {
  82. public:
  83. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_no_subresource_cache = false);
  84. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  85. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  86. virtual bool handles_type(const String &p_type) const;
  87. virtual String get_resource_type(const String &p_path) const;
  88. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  89. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  90. };
  91. class ResourceFormatSaverBinaryInstance {
  92. String local_path;
  93. String path;
  94. bool relative_paths;
  95. bool bundle_resources;
  96. bool skip_editor;
  97. bool big_endian;
  98. bool takeover_paths;
  99. FileAccess *f;
  100. String magic;
  101. Set<RES> resource_set;
  102. struct NonPersistentKey { //for resource properties generated on the fly
  103. RES base;
  104. StringName property;
  105. bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
  106. };
  107. Map<NonPersistentKey, RES> non_persistent_map;
  108. Map<StringName, int> string_map;
  109. Vector<StringName> strings;
  110. Map<RES, int> external_resources;
  111. List<RES> saved_resources;
  112. struct Property {
  113. int name_idx;
  114. Variant value;
  115. PropertyInfo pi;
  116. };
  117. struct ResourceData {
  118. String type;
  119. List<Property> properties;
  120. };
  121. static void _pad_buffer(FileAccess *f, int p_bytes);
  122. void _write_variant(const Variant &p_property, const PropertyInfo &p_hint = PropertyInfo());
  123. void _find_resources(const Variant &p_variant, bool p_main = false);
  124. static void save_unicode_string(FileAccess *f, const String &p_string, bool p_bit_on_len = false);
  125. int get_string_index(const String &p_string);
  126. public:
  127. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  128. 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());
  129. };
  130. class ResourceFormatSaverBinary : public ResourceFormatSaver {
  131. public:
  132. static ResourceFormatSaverBinary *singleton;
  133. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  134. virtual bool recognize(const RES &p_resource) const;
  135. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  136. ResourceFormatSaverBinary();
  137. };
  138. #endif // RESOURCE_FORMAT_BINARY_H