file_access_pack.h 9.5 KB

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