file_access_pack.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*************************************************************************/
  2. /* file_access_pack.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef FILE_ACCESS_PACK_H
  30. #define FILE_ACCESS_PACK_H
  31. #include "os/file_access.h"
  32. #include "os/dir_access.h"
  33. #include "map.h"
  34. #include "list.h"
  35. #include "print_string.h"
  36. class PackSource;
  37. class PackedData {
  38. friend class FileAccessPack;
  39. friend class DirAccessPack;
  40. friend class PackSource;
  41. public:
  42. struct PackedFile {
  43. String pack;
  44. uint64_t offset; //if offset is ZERO, the file was ERASED
  45. uint64_t size;
  46. uint8_t md5[16];
  47. PackSource* src;
  48. };
  49. private:
  50. struct PackedDir {
  51. PackedDir *parent;
  52. String name;
  53. Map<String,PackedDir*> subdirs;
  54. Set<String> files;
  55. };
  56. struct PathMD5 {
  57. uint64_t a;
  58. uint64_t b;
  59. bool operator < (const PathMD5& p_md5) const {
  60. if (p_md5.a == a) {
  61. return b < p_md5.b;
  62. } else {
  63. return a < p_md5.a;
  64. }
  65. }
  66. bool operator == (const PathMD5& p_md5) const {
  67. return a == p_md5.a && b == p_md5.b;
  68. };
  69. PathMD5() {
  70. a = b = 0;
  71. };
  72. PathMD5(const Vector<uint8_t> p_buf) {
  73. a = *((uint64_t*)&p_buf[0]);
  74. b = *((uint64_t*)&p_buf[8]);
  75. };
  76. };
  77. Map<PathMD5,PackedFile> files;
  78. Vector<PackSource*> sources;
  79. PackedDir *root;
  80. //Map<String,PackedDir*> dirs;
  81. static PackedData *singleton;
  82. bool disabled;
  83. void _free_packed_dirs(PackedDir *p_dir);
  84. public:
  85. void add_pack_source(PackSource* p_source);
  86. 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
  87. void set_disabled(bool p_disabled) { disabled=p_disabled; }
  88. _FORCE_INLINE_ bool is_disabled() const { return disabled; }
  89. static PackedData *get_singleton() { return singleton; }
  90. Error add_pack(const String& p_path);
  91. _FORCE_INLINE_ FileAccess *try_open_path(const String& p_path);
  92. _FORCE_INLINE_ bool has_path(const String& p_path);
  93. PackedData();
  94. ~PackedData();
  95. };
  96. class PackSource {
  97. public:
  98. virtual bool try_open_pack(const String& p_path)=0;
  99. virtual FileAccess* get_file(const String& p_path, PackedData::PackedFile* p_file)=0;
  100. virtual ~PackSource() {}
  101. };
  102. class PackedSourcePCK : public PackSource {
  103. public:
  104. virtual bool try_open_pack(const String &p_path);
  105. virtual FileAccess* get_file(const String& p_path, PackedData::PackedFile* p_file);
  106. };
  107. class FileAccessPack : public FileAccess {
  108. PackedData::PackedFile pf;
  109. mutable size_t pos;
  110. mutable bool eof;
  111. FileAccess *f;
  112. virtual Error _open(const String& p_path, int p_mode_flags);
  113. virtual uint64_t _get_modified_time(const String& p_file) { return 0; }
  114. public:
  115. virtual void close();
  116. virtual bool is_open() const;
  117. virtual void seek(size_t p_position);
  118. virtual void seek_end(int64_t p_position=0);
  119. virtual size_t get_pos() const;
  120. virtual size_t get_len() const;
  121. virtual bool eof_reached() const;
  122. virtual uint8_t get_8() const;
  123. virtual int get_buffer(uint8_t *p_dst,int p_length) const;
  124. virtual void set_endian_swap(bool p_swap);
  125. virtual Error get_error() const;
  126. virtual void store_8(uint8_t p_dest);
  127. virtual void store_buffer(const uint8_t *p_src,int p_length);
  128. virtual bool file_exists(const String& p_name);
  129. FileAccessPack(const String& p_path, const PackedData::PackedFile& p_file);
  130. ~FileAccessPack();
  131. };
  132. FileAccess *PackedData::try_open_path(const String& p_path) {
  133. //print_line("try open path " + p_path);
  134. PathMD5 pmd5(p_path.md5_buffer());
  135. Map<PathMD5,PackedFile>::Element *E=files.find(pmd5);
  136. if (!E)
  137. return NULL; //not found
  138. if (E->get().offset==0)
  139. return NULL; //was erased
  140. return E->get().src->get_file(p_path, &E->get());
  141. }
  142. bool PackedData::has_path(const String& p_path) {
  143. return files.has(PathMD5(p_path.md5_buffer()));
  144. }
  145. class DirAccessPack : public DirAccess {
  146. PackedData::PackedDir *current;
  147. List<String> list_dirs;
  148. List<String> list_files;
  149. bool cdir;
  150. public:
  151. virtual bool list_dir_begin();
  152. virtual String get_next();
  153. virtual bool current_is_dir() const;
  154. virtual bool current_is_hidden() const;
  155. virtual void list_dir_end();
  156. virtual int get_drive_count();
  157. virtual String get_drive(int p_drive);
  158. virtual Error change_dir(String p_dir);
  159. virtual String get_current_dir();
  160. virtual bool file_exists(String p_file);
  161. virtual bool dir_exists(String p_dir);
  162. virtual Error make_dir(String p_dir);
  163. virtual Error rename(String p_from, String p_to);
  164. virtual Error remove(String p_name);
  165. size_t get_space_left();
  166. DirAccessPack();
  167. ~DirAccessPack();
  168. };
  169. #endif // FILE_ACCESS_PACK_H