file_access_pack.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 = nullptr;
  57. String name;
  58. Map<String, PackedDir *> subdirs;
  59. Set<String> files;
  60. };
  61. struct PathMD5 {
  62. uint64_t a = 0;
  63. uint64_t b = 0;
  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. PathMD5(const Vector<uint8_t> p_buf) {
  76. a = *((uint64_t *)&p_buf[0]);
  77. b = *((uint64_t *)&p_buf[8]);
  78. }
  79. };
  80. Map<PathMD5, PackedFile> files;
  81. Vector<PackSource *> sources;
  82. PackedDir *root;
  83. static PackedData *singleton;
  84. bool disabled = false;
  85. void _free_packed_dirs(PackedDir *p_dir);
  86. public:
  87. void add_pack_source(PackSource *p_source);
  88. 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
  89. void set_disabled(bool p_disabled) { disabled = p_disabled; }
  90. _FORCE_INLINE_ bool is_disabled() const { return disabled; }
  91. static PackedData *get_singleton() { return singleton; }
  92. Error add_pack(const String &p_path, bool p_replace_files);
  93. _FORCE_INLINE_ FileAccess *try_open_path(const String &p_path);
  94. _FORCE_INLINE_ bool has_path(const String &p_path);
  95. PackedData();
  96. ~PackedData();
  97. };
  98. class PackSource {
  99. public:
  100. virtual bool try_open_pack(const String &p_path, bool p_replace_files) = 0;
  101. virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file) = 0;
  102. virtual ~PackSource() {}
  103. };
  104. class PackedSourcePCK : public PackSource {
  105. public:
  106. virtual bool try_open_pack(const String &p_path, bool p_replace_files);
  107. virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file);
  108. };
  109. class FileAccessPack : public FileAccess {
  110. PackedData::PackedFile pf;
  111. mutable size_t pos;
  112. mutable bool eof;
  113. FileAccess *f;
  114. virtual Error _open(const String &p_path, int p_mode_flags);
  115. virtual uint64_t _get_modified_time(const String &p_file) { return 0; }
  116. virtual uint32_t _get_unix_permissions(const String &p_file) { return 0; }
  117. virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) { return FAILED; }
  118. public:
  119. virtual void close();
  120. virtual bool is_open() const;
  121. virtual void seek(size_t p_position);
  122. virtual void seek_end(int64_t p_position = 0);
  123. virtual size_t get_position() const;
  124. virtual size_t get_len() const;
  125. virtual bool eof_reached() const;
  126. virtual uint8_t get_8() const;
  127. virtual int get_buffer(uint8_t *p_dst, int p_length) const;
  128. virtual void set_endian_swap(bool p_swap);
  129. virtual Error get_error() const;
  130. virtual void flush();
  131. virtual void store_8(uint8_t p_dest);
  132. virtual void store_buffer(const uint8_t *p_src, int p_length);
  133. virtual bool file_exists(const String &p_name);
  134. FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file);
  135. ~FileAccessPack();
  136. };
  137. FileAccess *PackedData::try_open_path(const String &p_path) {
  138. PathMD5 pmd5(p_path.md5_buffer());
  139. Map<PathMD5, PackedFile>::Element *E = files.find(pmd5);
  140. if (!E)
  141. return nullptr; //not found
  142. if (E->get().offset == 0)
  143. return nullptr; //was erased
  144. return E->get().src->get_file(p_path, &E->get());
  145. }
  146. bool PackedData::has_path(const String &p_path) {
  147. return files.has(PathMD5(p_path.md5_buffer()));
  148. }
  149. class DirAccessPack : public DirAccess {
  150. PackedData::PackedDir *current;
  151. List<String> list_dirs;
  152. List<String> list_files;
  153. bool cdir = false;
  154. public:
  155. virtual Error list_dir_begin();
  156. virtual String get_next();
  157. virtual bool current_is_dir() const;
  158. virtual bool current_is_hidden() const;
  159. virtual void list_dir_end();
  160. virtual int get_drive_count();
  161. virtual String get_drive(int p_drive);
  162. virtual Error change_dir(String p_dir);
  163. virtual String get_current_dir(bool p_include_drive = true);
  164. virtual bool file_exists(String p_file);
  165. virtual bool dir_exists(String p_dir);
  166. virtual Error make_dir(String p_dir);
  167. virtual Error rename(String p_from, String p_to);
  168. virtual Error remove(String p_name);
  169. size_t get_space_left();
  170. virtual String get_filesystem_type() const;
  171. DirAccessPack();
  172. ~DirAccessPack() {}
  173. };
  174. #endif // FILE_ACCESS_PACK_H