file_access_pack.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**************************************************************************/
  2. /* file_access_pack.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. #pragma once
  31. #include "core/io/dir_access.h"
  32. #include "core/io/file_access.h"
  33. #include "core/string/print_string.h"
  34. #include "core/templates/hash_set.h"
  35. #include "core/templates/list.h"
  36. // Godot's packed file magic header ("GDPC" in ASCII).
  37. #define PACK_HEADER_MAGIC 0x43504447
  38. #define PACK_FORMAT_VERSION_V2 2
  39. #define PACK_FORMAT_VERSION_V3 3
  40. // The current packed file format version number.
  41. #define PACK_FORMAT_VERSION PACK_FORMAT_VERSION_V3
  42. enum PackFlags {
  43. PACK_DIR_ENCRYPTED = 1 << 0,
  44. PACK_REL_FILEBASE = 1 << 1,
  45. PACK_SPARSE_BUNDLE = 1 << 2,
  46. };
  47. enum PackFileFlags {
  48. PACK_FILE_ENCRYPTED = 1 << 0,
  49. PACK_FILE_REMOVAL = 1 << 1,
  50. };
  51. class PackSource;
  52. class PackedData {
  53. friend class FileAccessPack;
  54. friend class DirAccessPack;
  55. friend class PackSource;
  56. public:
  57. struct PackedFile {
  58. String pack;
  59. uint64_t offset; //if offset is ZERO, the file was ERASED
  60. uint64_t size;
  61. uint8_t md5[16];
  62. PackSource *src = nullptr;
  63. bool encrypted;
  64. bool bundle;
  65. };
  66. private:
  67. struct PackedDir {
  68. PackedDir *parent = nullptr;
  69. String name;
  70. HashMap<String, PackedDir *> subdirs;
  71. HashSet<String> files;
  72. };
  73. struct PathMD5 {
  74. uint64_t a = 0;
  75. uint64_t b = 0;
  76. bool operator==(const PathMD5 &p_val) const {
  77. return (a == p_val.a) && (b == p_val.b);
  78. }
  79. static uint32_t hash(const PathMD5 &p_val) {
  80. uint32_t h = hash_murmur3_one_32(p_val.a);
  81. return hash_fmix32(hash_murmur3_one_32(p_val.b, h));
  82. }
  83. PathMD5() {}
  84. explicit PathMD5(const Vector<uint8_t> &p_buf) {
  85. a = *((uint64_t *)&p_buf[0]);
  86. b = *((uint64_t *)&p_buf[8]);
  87. }
  88. };
  89. HashMap<PathMD5, PackedFile, PathMD5> files;
  90. Vector<PackSource *> sources;
  91. PackedDir *root = nullptr;
  92. static inline PackedData *singleton = nullptr;
  93. bool disabled = false;
  94. void _free_packed_dirs(PackedDir *p_dir);
  95. void _get_file_paths(PackedDir *p_dir, const String &p_parent_dir, HashSet<String> &r_paths) const;
  96. public:
  97. void add_pack_source(PackSource *p_source);
  98. void add_path(const String &p_pkg_path, const String &p_path, uint64_t p_ofs, uint64_t p_size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted = false, bool p_bundle = false); // for PackSource
  99. void remove_path(const String &p_path);
  100. uint8_t *get_file_hash(const String &p_path);
  101. HashSet<String> get_file_paths() const;
  102. void set_disabled(bool p_disabled) { disabled = p_disabled; }
  103. _FORCE_INLINE_ bool is_disabled() const { return disabled; }
  104. static PackedData *get_singleton() { return singleton; }
  105. Error add_pack(const String &p_path, bool p_replace_files, uint64_t p_offset);
  106. void clear();
  107. _FORCE_INLINE_ Ref<FileAccess> try_open_path(const String &p_path);
  108. _FORCE_INLINE_ bool has_path(const String &p_path);
  109. _FORCE_INLINE_ int64_t get_size(const String &p_path);
  110. _FORCE_INLINE_ Ref<DirAccess> try_open_directory(const String &p_path);
  111. _FORCE_INLINE_ bool has_directory(const String &p_path);
  112. PackedData();
  113. ~PackedData();
  114. };
  115. class PackSource {
  116. public:
  117. virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) = 0;
  118. virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) = 0;
  119. virtual ~PackSource() {}
  120. };
  121. class PackedSourcePCK : public PackSource {
  122. public:
  123. virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) override;
  124. virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override;
  125. };
  126. class PackedSourceDirectory : public PackSource {
  127. void add_directory(const String &p_path, bool p_replace_files);
  128. public:
  129. virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) override;
  130. virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override;
  131. };
  132. class FileAccessPack : public FileAccess {
  133. GDSOFTCLASS(FileAccessPack, FileAccess);
  134. PackedData::PackedFile pf;
  135. mutable uint64_t pos;
  136. mutable bool eof;
  137. uint64_t off;
  138. Ref<FileAccess> f;
  139. virtual Error open_internal(const String &p_path, int p_mode_flags) override;
  140. virtual uint64_t _get_modified_time(const String &p_file) override { return 0; }
  141. virtual uint64_t _get_access_time(const String &p_file) override { return 0; }
  142. virtual int64_t _get_size(const String &p_file) override { return -1; }
  143. virtual BitField<FileAccess::UnixPermissionFlags> _get_unix_permissions(const String &p_file) override { return 0; }
  144. virtual Error _set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) override { return FAILED; }
  145. virtual bool _get_hidden_attribute(const String &p_file) override { return false; }
  146. virtual Error _set_hidden_attribute(const String &p_file, bool p_hidden) override { return ERR_UNAVAILABLE; }
  147. virtual bool _get_read_only_attribute(const String &p_file) override { return false; }
  148. virtual Error _set_read_only_attribute(const String &p_file, bool p_ro) override { return ERR_UNAVAILABLE; }
  149. public:
  150. virtual bool is_open() const override;
  151. virtual void seek(uint64_t p_position) override;
  152. virtual void seek_end(int64_t p_position = 0) override;
  153. virtual uint64_t get_position() const override;
  154. virtual uint64_t get_length() const override;
  155. virtual bool eof_reached() const override;
  156. virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;
  157. virtual void set_big_endian(bool p_big_endian) override;
  158. virtual Error get_error() const override;
  159. virtual Error resize(int64_t p_length) override { return ERR_UNAVAILABLE; }
  160. virtual void flush() override;
  161. virtual bool store_buffer(const uint8_t *p_src, uint64_t p_length) override;
  162. virtual bool file_exists(const String &p_name) override;
  163. virtual void close() override;
  164. FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file);
  165. };
  166. int64_t PackedData::get_size(const String &p_path) {
  167. String simplified_path = p_path.simplify_path();
  168. PathMD5 pmd5(simplified_path.md5_buffer());
  169. HashMap<PathMD5, PackedFile, PathMD5>::Iterator E = files.find(pmd5);
  170. if (!E) {
  171. return -1; // File not found.
  172. }
  173. if (E->value.offset == 0) {
  174. return -1; // File was erased.
  175. }
  176. return E->value.size;
  177. }
  178. Ref<FileAccess> PackedData::try_open_path(const String &p_path) {
  179. String simplified_path = p_path.simplify_path().trim_prefix("res://");
  180. PathMD5 pmd5(simplified_path.md5_buffer());
  181. HashMap<PathMD5, PackedFile, PathMD5>::Iterator E = files.find(pmd5);
  182. if (!E) {
  183. return nullptr; // Not found.
  184. }
  185. return E->value.src->get_file(p_path, &E->value);
  186. }
  187. bool PackedData::has_path(const String &p_path) {
  188. return files.has(PathMD5(p_path.simplify_path().trim_prefix("res://").md5_buffer()));
  189. }
  190. bool PackedData::has_directory(const String &p_path) {
  191. Ref<DirAccess> da = try_open_directory(p_path);
  192. if (da.is_valid()) {
  193. return true;
  194. } else {
  195. return false;
  196. }
  197. }
  198. class DirAccessPack : public DirAccess {
  199. GDSOFTCLASS(DirAccessPack, DirAccess);
  200. PackedData::PackedDir *current;
  201. List<String> list_dirs;
  202. List<String> list_files;
  203. bool cdir = false;
  204. PackedData::PackedDir *_find_dir(const String &p_dir);
  205. public:
  206. virtual Error list_dir_begin() override;
  207. virtual String get_next() override;
  208. virtual bool current_is_dir() const override;
  209. virtual bool current_is_hidden() const override;
  210. virtual void list_dir_end() override;
  211. virtual int get_drive_count() override;
  212. virtual String get_drive(int p_drive) override;
  213. virtual Error change_dir(String p_dir) override;
  214. virtual String get_current_dir(bool p_include_drive = true) const override;
  215. virtual bool file_exists(String p_file) override;
  216. virtual bool dir_exists(String p_dir) override;
  217. virtual Error make_dir(String p_dir) override;
  218. virtual Error rename(String p_from, String p_to) override;
  219. virtual Error remove(String p_name) override;
  220. uint64_t get_space_left() override;
  221. virtual bool is_link(String p_file) override { return false; }
  222. virtual String read_link(String p_file) override { return p_file; }
  223. virtual Error create_link(String p_source, String p_target) override { return FAILED; }
  224. virtual String get_filesystem_type() const override;
  225. DirAccessPack();
  226. };
  227. Ref<DirAccess> PackedData::try_open_directory(const String &p_path) {
  228. Ref<DirAccess> da = memnew(DirAccessPack());
  229. if (da->change_dir(p_path) != OK) {
  230. da = Ref<DirAccess>();
  231. }
  232. return da;
  233. }