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