resource_format_binary.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 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. String get_unicode_string();
  60. void _advance_padding(uint32_t p_len);
  61. Map<String, String> remaps;
  62. Error error;
  63. int stage;
  64. friend class ResourceFormatLoaderBinary;
  65. Error parse_variant(Variant &r_v);
  66. public:
  67. virtual void set_local_path(const String &p_local_path);
  68. virtual Ref<Resource> get_resource();
  69. virtual Error poll();
  70. virtual int get_stage() const;
  71. virtual int get_stage_count() const;
  72. virtual void set_translation_remapped(bool p_remapped);
  73. void set_remaps(const Map<String, String> &p_remaps) { remaps = p_remaps; }
  74. void open(FileAccess *p_f);
  75. String recognize(FileAccess *p_f);
  76. void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
  77. ResourceInteractiveLoaderBinary();
  78. ~ResourceInteractiveLoaderBinary();
  79. };
  80. class ResourceFormatLoaderBinary : public ResourceFormatLoader {
  81. GDCLASS(ResourceFormatLoaderBinary, ResourceFormatLoader)
  82. public:
  83. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  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. bool relative_paths;
  94. bool bundle_resources;
  95. bool skip_editor;
  96. bool big_endian;
  97. bool takeover_paths;
  98. FileAccess *f;
  99. String magic;
  100. Set<RES> resource_set;
  101. Map<StringName, int> string_map;
  102. Vector<StringName> strings;
  103. Map<RES, int> external_resources;
  104. List<RES> saved_resources;
  105. struct Property {
  106. int name_idx;
  107. Variant value;
  108. PropertyInfo pi;
  109. };
  110. struct ResourceData {
  111. String type;
  112. List<Property> properties;
  113. };
  114. static void _pad_buffer(FileAccess *f, int p_bytes);
  115. void _write_variant(const Variant &p_property, const PropertyInfo &p_hint = PropertyInfo());
  116. void _find_resources(const Variant &p_variant, bool p_main = false);
  117. static void save_unicode_string(FileAccess *f, const String &p_string, bool p_bit_on_len = false);
  118. int get_string_index(const String &p_string);
  119. public:
  120. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  121. 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());
  122. };
  123. class ResourceFormatSaverBinary : public ResourceFormatSaver {
  124. GDCLASS(ResourceFormatSaverBinary, ResourceFormatSaver)
  125. public:
  126. static ResourceFormatSaverBinary *singleton;
  127. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  128. virtual bool recognize(const RES &p_resource) const;
  129. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  130. ResourceFormatSaverBinary();
  131. };
  132. #endif // RESOURCE_FORMAT_BINARY_H