file_access_pack.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. class PackSource;
  38. class PackedData {
  39. friend class FileAccessPack;
  40. friend class DirAccessPack;
  41. friend class PackSource;
  42. public:
  43. struct PackedFile {
  44. String pack;
  45. uint64_t offset; //if offset is ZERO, the file was ERASED
  46. uint64_t size;
  47. uint8_t md5[16];
  48. PackSource *src;
  49. };
  50. private:
  51. struct PackedDir {
  52. PackedDir *parent;
  53. String name;
  54. Map<String, PackedDir *> subdirs;
  55. Set<String> files;
  56. };
  57. struct PathMD5 {
  58. uint64_t a;
  59. uint64_t b;
  60. bool operator<(const PathMD5 &p_md5) const {
  61. if (p_md5.a == a) {
  62. return b < p_md5.b;
  63. } else {
  64. return a < p_md5.a;
  65. }
  66. }
  67. bool operator==(const PathMD5 &p_md5) const {
  68. return a == p_md5.a && b == p_md5.b;
  69. };
  70. PathMD5() {
  71. a = b = 0;
  72. };
  73. PathMD5(const Vector<uint8_t> p_buf) {
  74. a = *((uint64_t *)&p_buf[0]);
  75. b = *((uint64_t *)&p_buf[8]);
  76. };
  77. };
  78. Map<PathMD5, PackedFile> files;
  79. Vector<PackSource *> sources;
  80. PackedDir *root;
  81. //Map<String,PackedDir*> dirs;
  82. static PackedData *singleton;
  83. bool disabled;
  84. void _free_packed_dirs(PackedDir *p_dir);
  85. public:
  86. void add_pack_source(PackSource *p_source);
  87. void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src); // for PackSource
  88. void set_disabled(bool p_disabled) { disabled = p_disabled; }
  89. _FORCE_INLINE_ bool is_disabled() const { return disabled; }
  90. static PackedData *get_singleton() { return singleton; }
  91. Error add_pack(const String &p_path);
  92. _FORCE_INLINE_ FileAccess *try_open_path(const String &p_path);
  93. _FORCE_INLINE_ bool has_path(const String &p_path);
  94. PackedData();
  95. ~PackedData();
  96. };
  97. class PackSource {
  98. public:
  99. virtual bool try_open_pack(const String &p_path) = 0;
  100. virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file) = 0;
  101. virtual ~PackSource() {}
  102. };
  103. class PackedSourcePCK : public PackSource {
  104. public:
  105. virtual bool try_open_pack(const String &p_path);
  106. virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file);
  107. };
  108. class FileAccessPack : public FileAccess {
  109. PackedData::PackedFile pf;
  110. mutable size_t pos;
  111. mutable bool eof;
  112. FileAccess *f;
  113. virtual Error _open(const String &p_path, int p_mode_flags);
  114. virtual uint64_t _get_modified_time(const String &p_file) { return 0; }
  115. public:
  116. virtual void close();
  117. virtual bool is_open() const;
  118. virtual void seek(size_t p_position);
  119. virtual void seek_end(int64_t p_position = 0);
  120. virtual size_t get_position() const;
  121. virtual size_t get_len() const;
  122. virtual bool eof_reached() const;
  123. virtual uint8_t get_8() const;
  124. virtual int get_buffer(uint8_t *p_dst, int p_length) const;
  125. virtual void set_endian_swap(bool p_swap);
  126. virtual Error get_error() const;
  127. virtual void flush();
  128. virtual void store_8(uint8_t p_dest);
  129. virtual void store_buffer(const uint8_t *p_src, int p_length);
  130. virtual bool file_exists(const String &p_name);
  131. FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file);
  132. ~FileAccessPack();
  133. };
  134. FileAccess *PackedData::try_open_path(const String &p_path) {
  135. PathMD5 pmd5(p_path.md5_buffer());
  136. Map<PathMD5, PackedFile>::Element *E = files.find(pmd5);
  137. if (!E)
  138. return NULL; //not found
  139. if (E->get().offset == 0)
  140. return NULL; //was erased
  141. return E->get().src->get_file(p_path, &E->get());
  142. }
  143. bool PackedData::has_path(const String &p_path) {
  144. return files.has(PathMD5(p_path.md5_buffer()));
  145. }
  146. class DirAccessPack : public DirAccess {
  147. PackedData::PackedDir *current;
  148. List<String> list_dirs;
  149. List<String> list_files;
  150. bool cdir;
  151. public:
  152. virtual Error list_dir_begin();
  153. virtual String get_next();
  154. virtual bool current_is_dir() const;
  155. virtual bool current_is_hidden() const;
  156. virtual void list_dir_end();
  157. virtual int get_drive_count();
  158. virtual String get_drive(int p_drive);
  159. virtual Error change_dir(String p_dir);
  160. virtual String get_current_dir();
  161. virtual bool file_exists(String p_file);
  162. virtual bool dir_exists(String p_dir);
  163. virtual Error make_dir(String p_dir);
  164. virtual Error rename(String p_from, String p_to);
  165. virtual Error remove(String p_name);
  166. size_t get_space_left();
  167. DirAccessPack();
  168. ~DirAccessPack();
  169. };
  170. #endif // FILE_ACCESS_PACK_H