file_access_pack.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*************************************************************************/
  2. /* file_access_pack.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 FILE_ACCESS_PACK_H
  31. #define FILE_ACCESS_PACK_H
  32. #include "core/os/dir_access.h"
  33. #include "core/os/file_access.h"
  34. #include "core/string/print_string.h"
  35. #include "core/templates/list.h"
  36. #include "core/templates/map.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. };
  44. enum PackFileFlags {
  45. PACK_FILE_ENCRYPTED = 1 << 0
  46. };
  47. class PackSource;
  48. class PackedData {
  49. friend class FileAccessPack;
  50. friend class DirAccessPack;
  51. friend class PackSource;
  52. public:
  53. struct PackedFile {
  54. String pack;
  55. uint64_t offset; //if offset is ZERO, the file was ERASED
  56. uint64_t size;
  57. uint8_t md5[16];
  58. PackSource *src;
  59. bool encrypted;
  60. };
  61. private:
  62. struct PackedDir {
  63. PackedDir *parent = nullptr;
  64. String name;
  65. Map<String, PackedDir *> subdirs;
  66. Set<String> files;
  67. };
  68. struct PathMD5 {
  69. uint64_t a = 0;
  70. uint64_t b = 0;
  71. bool operator<(const PathMD5 &p_md5) const {
  72. if (p_md5.a == a) {
  73. return b < p_md5.b;
  74. } else {
  75. return a < p_md5.a;
  76. }
  77. }
  78. bool operator==(const PathMD5 &p_md5) const {
  79. return a == p_md5.a && b == p_md5.b;
  80. }
  81. PathMD5() {}
  82. PathMD5(const Vector<uint8_t> p_buf) {
  83. a = *((uint64_t *)&p_buf[0]);
  84. b = *((uint64_t *)&p_buf[8]);
  85. }
  86. };
  87. Map<PathMD5, PackedFile> files;
  88. Vector<PackSource *> sources;
  89. PackedDir *root;
  90. static PackedData *singleton;
  91. bool disabled = false;
  92. void _free_packed_dirs(PackedDir *p_dir);
  93. public:
  94. void add_pack_source(PackSource *p_source);
  95. void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted = false); // for PackSource
  96. void set_disabled(bool p_disabled) { disabled = p_disabled; }
  97. _FORCE_INLINE_ bool is_disabled() const { return disabled; }
  98. static PackedData *get_singleton() { return singleton; }
  99. Error add_pack(const String &p_path, bool p_replace_files, size_t p_offset);
  100. _FORCE_INLINE_ FileAccess *try_open_path(const String &p_path);
  101. _FORCE_INLINE_ bool has_path(const String &p_path);
  102. _FORCE_INLINE_ DirAccess *try_open_directory(const String &p_path);
  103. _FORCE_INLINE_ bool has_directory(const String &p_path);
  104. PackedData();
  105. ~PackedData();
  106. };
  107. class PackSource {
  108. public:
  109. virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset) = 0;
  110. virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file) = 0;
  111. virtual ~PackSource() {}
  112. };
  113. class PackedSourcePCK : public PackSource {
  114. public:
  115. virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset);
  116. virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file);
  117. };
  118. class FileAccessPack : public FileAccess {
  119. PackedData::PackedFile pf;
  120. mutable size_t pos;
  121. mutable bool eof;
  122. uint64_t off;
  123. FileAccess *f;
  124. virtual Error _open(const String &p_path, int p_mode_flags);
  125. virtual uint64_t _get_modified_time(const String &p_file) { return 0; }
  126. virtual uint32_t _get_unix_permissions(const String &p_file) { return 0; }
  127. virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) { return FAILED; }
  128. public:
  129. virtual void close();
  130. virtual bool is_open() const;
  131. virtual void seek(size_t p_position);
  132. virtual void seek_end(int64_t p_position = 0);
  133. virtual size_t get_position() const;
  134. virtual size_t get_len() const;
  135. virtual bool eof_reached() const;
  136. virtual uint8_t get_8() const;
  137. virtual int get_buffer(uint8_t *p_dst, int p_length) const;
  138. virtual void set_endian_swap(bool p_swap);
  139. virtual Error get_error() const;
  140. virtual void flush();
  141. virtual void store_8(uint8_t p_dest);
  142. virtual void store_buffer(const uint8_t *p_src, int p_length);
  143. virtual bool file_exists(const String &p_name);
  144. FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file);
  145. ~FileAccessPack();
  146. };
  147. FileAccess *PackedData::try_open_path(const String &p_path) {
  148. PathMD5 pmd5(p_path.md5_buffer());
  149. Map<PathMD5, PackedFile>::Element *E = files.find(pmd5);
  150. if (!E) {
  151. return nullptr; //not found
  152. }
  153. if (E->get().offset == 0) {
  154. return nullptr; //was erased
  155. }
  156. return E->get().src->get_file(p_path, &E->get());
  157. }
  158. bool PackedData::has_path(const String &p_path) {
  159. return files.has(PathMD5(p_path.md5_buffer()));
  160. }
  161. bool PackedData::has_directory(const String &p_path) {
  162. DirAccess *da = try_open_directory(p_path);
  163. if (da) {
  164. memdelete(da);
  165. return true;
  166. } else {
  167. return false;
  168. }
  169. }
  170. class DirAccessPack : public DirAccess {
  171. PackedData::PackedDir *current;
  172. List<String> list_dirs;
  173. List<String> list_files;
  174. bool cdir = false;
  175. PackedData::PackedDir *_find_dir(String p_dir);
  176. public:
  177. virtual Error list_dir_begin();
  178. virtual String get_next();
  179. virtual bool current_is_dir() const;
  180. virtual bool current_is_hidden() const;
  181. virtual void list_dir_end();
  182. virtual int get_drive_count();
  183. virtual String get_drive(int p_drive);
  184. virtual Error change_dir(String p_dir);
  185. virtual String get_current_dir(bool p_include_drive = true);
  186. virtual bool file_exists(String p_file);
  187. virtual bool dir_exists(String p_dir);
  188. virtual Error make_dir(String p_dir);
  189. virtual Error rename(String p_from, String p_to);
  190. virtual Error remove(String p_name);
  191. size_t get_space_left();
  192. virtual String get_filesystem_type() const;
  193. DirAccessPack();
  194. ~DirAccessPack() {}
  195. };
  196. DirAccess *PackedData::try_open_directory(const String &p_path) {
  197. DirAccess *da = memnew(DirAccessPack());
  198. if (da->change_dir(p_path) != OK) {
  199. memdelete(da);
  200. da = nullptr;
  201. }
  202. return da;
  203. }
  204. #endif // FILE_ACCESS_PACK_H