file_access_pack.h 7.8 KB

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