resource_format_binary.h 5.9 KB

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