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