file_access_pack.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/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. // 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 1
  41. class PackSource;
  42. class PackedData {
  43. friend class FileAccessPack;
  44. friend class DirAccessPack;
  45. friend class PackSource;
  46. public:
  47. struct PackedFile {
  48. String pack;
  49. uint64_t offset; //if offset is ZERO, the file was ERASED
  50. uint64_t size;
  51. uint8_t md5[16];
  52. PackSource *src;
  53. };
  54. private:
  55. struct PackedDir {
  56. PackedDir *parent;
  57. String name;
  58. Map<String, PackedDir *> subdirs;
  59. Set<String> files;
  60. };
  61. struct PathMD5 {
  62. uint64_t a;
  63. uint64_t b;
  64. bool operator<(const PathMD5 &p_md5) const {
  65. if (p_md5.a == a) {
  66. return b < p_md5.b;
  67. } else {
  68. return a < p_md5.a;
  69. }
  70. }
  71. bool operator==(const PathMD5 &p_md5) const {
  72. return a == p_md5.a && b == p_md5.b;
  73. };
  74. PathMD5() {
  75. a = b = 0;
  76. };
  77. PathMD5(const Vector<uint8_t> p_buf) {
  78. a = *((uint64_t *)&p_buf[0]);
  79. b = *((uint64_t *)&p_buf[8]);
  80. };
  81. };
  82. Map<PathMD5, PackedFile> files;
  83. Vector<PackSource *> sources;
  84. PackedDir *root;
  85. //Map<String,PackedDir*> dirs;
  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 &pkg_path, const String &path, uint64_t ofs, uint64_t 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, size_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, size_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, size_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 size_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(size_t p_position);
  127. virtual void seek_end(int64_t p_position = 0);
  128. virtual size_t get_position() const;
  129. virtual size_t get_len() const;
  130. virtual bool eof_reached() const;
  131. virtual uint8_t get_8() const;
  132. virtual int get_buffer(uint8_t *p_dst, int 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, int 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 NULL; //not found
  147. if (E->get().offset == 0)
  148. return NULL; //was erased
  149. return E->get().src->get_file(p_path, &E->get());
  150. }
  151. bool PackedData::has_path(const String &p_path) {
  152. return files.has(PathMD5(p_path.md5_buffer()));
  153. }
  154. bool PackedData::has_directory(const String &p_path) {
  155. DirAccess *da = try_open_directory(p_path);
  156. if (da) {
  157. memdelete(da);
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. class DirAccessPack : public DirAccess {
  164. PackedData::PackedDir *current;
  165. List<String> list_dirs;
  166. List<String> list_files;
  167. bool cdir;
  168. PackedData::PackedDir *_find_dir(String p_dir);
  169. public:
  170. virtual Error list_dir_begin();
  171. virtual String get_next();
  172. virtual bool current_is_dir() const;
  173. virtual bool current_is_hidden() const;
  174. virtual void list_dir_end();
  175. virtual int get_drive_count();
  176. virtual String get_drive(int p_drive);
  177. virtual Error change_dir(String p_dir);
  178. virtual String get_current_dir();
  179. virtual bool file_exists(String p_file);
  180. virtual bool dir_exists(String p_dir);
  181. virtual Error make_dir(String p_dir);
  182. virtual Error rename(String p_from, String p_to);
  183. virtual Error remove(String p_name);
  184. size_t get_space_left();
  185. virtual String get_filesystem_type() const;
  186. DirAccessPack();
  187. ~DirAccessPack();
  188. };
  189. DirAccess *PackedData::try_open_directory(const String &p_path) {
  190. DirAccess *da = memnew(DirAccessPack());
  191. if (da->change_dir(p_path) != OK) {
  192. memdelete(da);
  193. da = NULL;
  194. }
  195. return da;
  196. }
  197. #endif // FILE_ACCESS_PACK_H