Debian Codegraph @

core/io/file_access_pack.h

raw symbols SCIP
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
31 #pragma once
32
33 #include "core/io/dir_access.h"
34 #include "core/io/file_access.h"
35 #include "core/string/print_string.h"
36 #include "core/templates/hash_set.h"
37 #include "core/templates/list.h"
38
39 // Godot's packed file magic header ("GDPC" in ASCII).
40 #define PACK_HEADER_MAGIC 0x43504447
41
42 #define PACK_FORMAT_VERSION_V2 2
43 #define PACK_FORMAT_VERSION_V3 3
44
45 // The current packed file format version number.
46 #define PACK_FORMAT_VERSION PACK_FORMAT_VERSION_V3
47
48 enum PackFlags {
49 PACK_DIR_ENCRYPTED = 1 << 0,
50 PACK_REL_FILEBASE = 1 << 1,
51 PACK_SPARSE_BUNDLE = 1 << 2,
52 };
53
54 enum PackFileFlags {
55 PACK_FILE_ENCRYPTED = 1 << 0,
56 PACK_FILE_REMOVAL = 1 << 1,
57 PACK_FILE_DELTA = 1 << 2,
58 };
59
60 class PackSource;
61
62 class PackedData {
63 friend class FileAccessPack;
64 friend class DirAccessPack;
65 friend class PackSource;
66
67 public:
68 struct PackedFile {
69 String pack;
70 uint64_t offset; //if offset is ZERO, the file was ERASED
71 uint64_t size;
72 uint8_t md5[16];
73 PackSource *src = nullptr;
74 bool encrypted;
75 bool bundle;
76 bool delta;
77 };
78
79 private:
80 struct PackedDir {
81 PackedDir *parent = nullptr;
82 String name;
83 HashMap<String, PackedDir *> subdirs;
84 HashSet<String> files;
85 };
86
87 struct PathMD5 {
88 uint64_t a = 0;
89 uint64_t b = 0;
90
91 bool operator==(const PathMD5 &p_val) const {
92 return (a == p_val.a) && (b == p_val.b);
93 }
94 static uint32_t hash(const PathMD5 &p_val) {
95 uint32_t h = hash_murmur3_one_32(p_val.a);
96 return hash_fmix32(hash_murmur3_one_32(p_val.b, h));
97 }
98
99 PathMD5() {}
100
101 explicit PathMD5(const Vector<uint8_t> &p_buf) {
102 a = *((uint64_t *)&p_buf[0]);
103 b = *((uint64_t *)&p_buf[8]);
104 }
105 };
106
107 HashMap<PathMD5, PackedFile, PathMD5> files;
108 HashMap<PathMD5, Vector<PackedFile>, PathMD5> delta_patches;
109
110 Vector<PackSource *> sources;
111
112 PackedDir *root = nullptr;
113
114 static inline PackedData *singleton = nullptr;
115 bool disabled = false;
116
117 void _free_packed_dirs(PackedDir *p_dir);
118 void _get_file_paths(PackedDir *p_dir, const String &p_parent_dir, HashSet<String> &r_paths) const;
119
120 public:
121 void add_pack_source(PackSource *p_source);
122 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, bool p_encrypted = false, bool p_bundle = false, bool p_delta = false); // for PackSource
123 void remove_path(const String &p_path);
124 uint8_t *get_file_hash(const String &p_path);
125 Vector<PackedFile> get_delta_patches(const String &p_path) const;
126 bool has_delta_patches(const String &p_path) const;
127 HashSet<String> get_file_paths() const;
128
129 void set_disabled(bool p_disabled) { disabled = p_disabled; }
130 _FORCE_INLINE_ bool is_disabled() const { return disabled; }
131
132 static PackedData *get_singleton() { return singleton; }
133 Error add_pack(const String &p_path, bool p_replace_files, uint64_t p_offset);
134
135 void clear();
136
137 _FORCE_INLINE_ Ref<FileAccess> try_open_path(const String &p_path);
138 _FORCE_INLINE_ bool has_path(const String &p_path);
139
140 _FORCE_INLINE_ int64_t get_size(const String &p_path);
141
142 _FORCE_INLINE_ Ref<DirAccess> try_open_directory(const String &p_path);
143 _FORCE_INLINE_ bool has_directory(const String &p_path);
144
145 PackedData();
146 ~PackedData();
147 };
148
149 class PackSource {
150 public:
151 virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) = 0;
152 virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) = 0;
153 virtual ~PackSource() {}
154 };
155
156 class PackedSourcePCK : public PackSource {
157 public:
158 virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) override;
159 virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override;
160 };
161
162 class PackedSourceDirectory : public PackSource {
163 void add_directory(const String &p_path, bool p_replace_files);
164
165 public:
166 virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) override;
167 virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override;
168 };
169
170 class FileAccessPack : public FileAccess {
171 GDSOFTCLASS(FileAccessPack, FileAccess);
172 PackedData::PackedFile pf;
173
174 String path;
175 mutable uint64_t pos;
176 mutable bool eof;
177 uint64_t off;
178
179 Ref<FileAccess> f;
180 virtual Error open_internal(const String &p_path, int p_mode_flags) override;
181 virtual uint64_t _get_modified_time(const String &p_file) override { return 0; }
182 virtual uint64_t _get_access_time(const String &p_file) override { return 0; }
183 virtual int64_t _get_size(const String &p_file) override { return -1; }
184 virtual BitField<FileAccess::UnixPermissionFlags> _get_unix_permissions(const String &p_file) override { return 0; }
185 virtual Error _set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) override { return FAILED; }
186
187 virtual bool _get_hidden_attribute(const String &p_file) override { return false; }
188 virtual Error _set_hidden_attribute(const String &p_file, bool p_hidden) override { return ERR_UNAVAILABLE; }
189 virtual bool _get_read_only_attribute(const String &p_file) override { return false; }
190 virtual Error _set_read_only_attribute(const String &p_file, bool p_ro) override { return ERR_UNAVAILABLE; }
191
192 public:
193 virtual bool is_open() const override;
194
195 virtual String get_path() const override { return path; }
196 virtual String get_path_absolute() const override { return path; }
197
198 virtual void seek(uint64_t p_position) override;
199 virtual void seek_end(int64_t p_position = 0) override;
200 virtual uint64_t get_position() const override;
201 virtual uint64_t get_length() const override;
202
203 virtual bool eof_reached() const override;
204
205 virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;
206
207 virtual void set_big_endian(bool p_big_endian) override;
208
209 virtual Error get_error() const override;
210
211 virtual Error resize(int64_t p_length) override { return ERR_UNAVAILABLE; }
212 virtual void flush() override;
213 virtual bool store_buffer(const uint8_t *p_src, uint64_t p_length) override;
214
215 virtual bool file_exists(const String &p_name) override;
216
217 virtual void close() override;
218
219 FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file);
220 };
221
222 int64_t PackedData::get_size(const String &p_path) {
223 String simplified_path = p_path.simplify_path();
224 PathMD5 pmd5(simplified_path.md5_buffer());
225 HashMap<PathMD5, PackedFile, PathMD5>::Iterator E = files.find(pmd5);
226 if (!E) {
227 return -1; // File not found.
228 }
229 if (E->value.offset == 0) {
230 return -1; // File was erased.
231 }
232 return E->value.size;
233 }
234
235 Ref<FileAccess> PackedData::try_open_path(const String &p_path) {
236 String simplified_path = p_path.simplify_path().trim_prefix("res://");
237 PathMD5 pmd5(simplified_path.md5_buffer());
238 HashMap<PathMD5, PackedFile, PathMD5>::Iterator E = files.find(pmd5);
239 if (!E) {
240 return nullptr; // Not found.
241 }
242
243 return E->value.src->get_file(p_path, &E->value);
244 }
245
246 bool PackedData::has_path(const String &p_path) {
247 return files.has(PathMD5(p_path.simplify_path().trim_prefix("res://").md5_buffer()));
248 }
249
250 bool PackedData::has_directory(const String &p_path) {
251 Ref<DirAccess> da = try_open_directory(p_path);
252 if (da.is_valid()) {
253 return true;
254 } else {
255 return false;
256 }
257 }
258
259 class DirAccessPack : public DirAccess {
260 GDSOFTCLASS(DirAccessPack, DirAccess);
261 PackedData::PackedDir *current;
262
263 List<String> list_dirs;
264 List<String> list_files;
265 bool cdir = false;
266
267 PackedData::PackedDir *_find_dir(const String &p_dir);
268
269 public:
270 virtual Error list_dir_begin() override;
271 virtual String get_next() override;
272 virtual bool current_is_dir() const override;
273 virtual bool current_is_hidden() const override;
274 virtual void list_dir_end() override;
275
276 virtual int get_drive_count() override;
277 virtual String get_drive(int p_drive) override;
278
279 virtual Error change_dir(String p_dir) override;
280 virtual String get_current_dir(bool p_include_drive = true) const override;
281
282 virtual bool file_exists(String p_file) override;
283 virtual bool dir_exists(String p_dir) override;
284
285 virtual Error make_dir(String p_dir) override;
286
287 virtual Error rename(String p_from, String p_to) override;
288 virtual Error remove(String p_name) override;
289
290 uint64_t get_space_left() override;
291
292 virtual bool is_link(String p_file) override { return false; }
293 virtual String read_link(String p_file) override { return p_file; }
294 virtual Error create_link(String p_source, String p_target) override { return FAILED; }
295
296 virtual String get_filesystem_type() const override;
297
298 DirAccessPack();
299 };
300
301 Ref<DirAccess> PackedData::try_open_directory(const String &p_path) {
302 Ref<DirAccess> da = memnew(DirAccessPack());
303 if (da->change_dir(p_path) != OK) {
304 da = Ref<DirAccess>();
305 }
306 return da;
307 }