2
0

file_access_pack.h 8.0 KB

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