1/**************************************************************************/
2/* file_access_pack.cpp */
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#include "file_access_pack.h"
32
33#include "core/io/file_access_encrypted.h"
34#include "core/object/script_language.h"
35#include "core/os/os.h"
36#include "core/version.h"
37
38#include <stdio.h>
39
40Error PackedData::add_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) {
41 for (int i = 0; i < sources.size(); i++) {
42 if (sources[i]->try_open_pack(p_path, p_replace_files, p_offset)) {
43 return OK;
44 }
45 }
46
47 return ERR_FILE_UNRECOGNIZED;
48}
49
50void PackedData::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) {
51 String simplified_path = p_path.simplify_path();
52 PathMD5 pmd5(simplified_path.md5_buffer());
53
54 bool exists = files.has(pmd5);
55
56 PackedFile pf;
57 pf.encrypted = p_encrypted;
58 pf.pack = p_pkg_path;
59 pf.offset = p_ofs;
60 pf.size = p_size;
61 for (int i = 0; i < 16; i++) {
62 pf.md5[i] = p_md5[i];
63 }
64 pf.src = p_src;
65
66 if (!exists || p_replace_files) {
67 files[pmd5] = pf;
68 }
69
70 if (!exists) {
71 //search for dir
72 String p = simplified_path.replace_first("res://", "");
73 PackedDir *cd = root;
74
75 if (p.contains("/")) { //in a subdir
76
77 Vector<String> ds = p.get_base_dir().split("/");
78
79 for (int j = 0; j < ds.size(); j++) {
80 if (!cd->subdirs.has(ds[j])) {
81 PackedDir *pd = memnew(PackedDir);
82 pd->name = ds[j];
83 pd->parent = cd;
84 cd->subdirs[pd->name] = pd;
85 cd = pd;
86 } else {
87 cd = cd->subdirs[ds[j]];
88 }
89 }
90 }
91 String filename = simplified_path.get_file();
92 // Don't add as a file if the path points to a directory
93 if (!filename.is_empty()) {
94 cd->files.insert(filename);
95 }
96 }
97}
98
99void PackedData::add_pack_source(PackSource *p_source) {
100 if (p_source != nullptr) {
101 sources.push_back(p_source);
102 }
103}
104
105PackedData *PackedData::singleton = nullptr;
106
107PackedData::PackedData() {
108 singleton = this;
109 root = memnew(PackedDir);
110
111 add_pack_source(memnew(PackedSourcePCK));
112}
113
114void PackedData::_free_packed_dirs(PackedDir *p_dir) {
115 for (const KeyValue<String, PackedDir *> &E : p_dir->subdirs) {
116 _free_packed_dirs(E.value);
117 }
118 memdelete(p_dir);
119}
120
121PackedData::~PackedData() {
122 for (int i = 0; i < sources.size(); i++) {
123 memdelete(sources[i]);
124 }
125 _free_packed_dirs(root);
126}
127
128//////////////////////////////////////////////////////////////////
129
130bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) {
131 Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
132 if (f.is_null()) {
133 return false;
134 }
135
136 bool pck_header_found = false;
137
138 // Search for the header at the start offset - standalone PCK file.
139 f->seek(p_offset);
140 uint32_t magic = f->get_32();
141 if (magic == PACK_HEADER_MAGIC) {
142 pck_header_found = true;
143 }
144
145 // Search for the header in the executable "pck" section - self contained executable.
146 if (!pck_header_found) {
147 // Loading with offset feature not supported for self contained exe files.
148 if (p_offset != 0) {
149 ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported.");
150 }
151
152 int64_t pck_off = OS::get_singleton()->get_embedded_pck_offset();
153 if (pck_off != 0) {
154 // Search for the header, in case PCK start and section have different alignment.
155 for (int i = 0; i < 8; i++) {
156 f->seek(pck_off);
157 magic = f->get_32();
158 if (magic == PACK_HEADER_MAGIC) {
159#ifdef DEBUG_ENABLED
160 print_verbose("PCK header found in executable pck section, loading from offset 0x" + String::num_int64(pck_off - 4, 16));
161#endif
162 pck_header_found = true;
163 break;
164 }
165 pck_off++;
166 }
167 }
168 }
169
170 // Search for the header at the end of file - self contained executable.
171 if (!pck_header_found) {
172 // Loading with offset feature not supported for self contained exe files.
173 if (p_offset != 0) {
174 ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported.");
175 }
176
177 f->seek_end();
178 f->seek(f->get_position() - 4);
179 magic = f->get_32();
180
181 if (magic == PACK_HEADER_MAGIC) {
182 f->seek(f->get_position() - 12);
183 uint64_t ds = f->get_64();
184 f->seek(f->get_position() - ds - 8);
185 magic = f->get_32();
186 if (magic == PACK_HEADER_MAGIC) {
187#ifdef DEBUG_ENABLED
188 print_verbose("PCK header found at the end of executable, loading from offset 0x" + String::num_int64(f->get_position() - 4, 16));
189#endif
190 pck_header_found = true;
191 }
192 }
193 }
194
195 if (!pck_header_found) {
196 return false;
197 }
198
199 uint32_t version = f->get_32();
200 uint32_t ver_major = f->get_32();
201 uint32_t ver_minor = f->get_32();
202 f->get_32(); // patch number, not used for validation.
203
204 ERR_FAIL_COND_V_MSG(version != PACK_FORMAT_VERSION, false, "Pack version unsupported: " + itos(version) + ".");
205 ERR_FAIL_COND_V_MSG(ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR), false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + ".");
206
207 uint32_t pack_flags = f->get_32();
208 uint64_t file_base = f->get_64();
209
210 bool enc_directory = (pack_flags & PACK_DIR_ENCRYPTED);
211
212 for (int i = 0; i < 16; i++) {
213 //reserved
214 f->get_32();
215 }
216
217 int file_count = f->get_32();
218
219 if (enc_directory) {
220 Ref<FileAccessEncrypted> fae;
221 fae.instantiate();
222 ERR_FAIL_COND_V_MSG(fae.is_null(), false, "Can't open encrypted pack directory.");
223
224 Vector<uint8_t> key;
225 key.resize(32);
226 for (int i = 0; i < key.size(); i++) {
227 key.write[i] = script_encryption_key[i];
228 }
229
230 Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
231 ERR_FAIL_COND_V_MSG(err, false, "Can't open encrypted pack directory.");
232 f = fae;
233 }
234
235 for (int i = 0; i < file_count; i++) {
236 uint32_t sl = f->get_32();
237 CharString cs;
238 cs.resize(sl + 1);
239 f->get_buffer((uint8_t *)cs.ptr(), sl);
240 cs[sl] = 0;
241
242 String path;
243 path.parse_utf8(cs.ptr());
244
245 uint64_t ofs = file_base + f->get_64();
246 uint64_t size = f->get_64();
247 uint8_t md5[16];
248 f->get_buffer(md5, 16);
249 uint32_t flags = f->get_32();
250
251 PackedData::get_singleton()->add_path(p_path, path, ofs + p_offset, size, md5, this, p_replace_files, (flags & PACK_FILE_ENCRYPTED));
252 }
253
254 return true;
255}
256
257Ref<FileAccess> PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) {
258 return memnew(FileAccessPack(p_path, *p_file));
259}
260
261//////////////////////////////////////////////////////////////////
262
263Error FileAccessPack::open_internal(const String &p_path, int p_mode_flags) {
264 ERR_PRINT("Can't open pack-referenced file.");
265 return ERR_UNAVAILABLE;
266}
267
268bool FileAccessPack::is_open() const {
269 if (f.is_valid()) {
270 return f->is_open();
271 } else {
272 return false;
273 }
274}
275
276void FileAccessPack::seek(uint64_t p_position) {
277 ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");
278
279 if (p_position > pf.size) {
280 eof = true;
281 } else {
282 eof = false;
283 }
284
285 f->seek(off + p_position);
286 pos = p_position;
287}
288
289void FileAccessPack::seek_end(int64_t p_position) {
290 seek(pf.size + p_position);
291}
292
293uint64_t FileAccessPack::get_position() const {
294 return pos;
295}
296
297uint64_t FileAccessPack::get_length() const {
298 return pf.size;
299}
300
301bool FileAccessPack::eof_reached() const {
302 return eof;
303}
304
305uint8_t FileAccessPack::get_8() const {
306 ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use.");
307 if (pos >= pf.size) {
308 eof = true;
309 return 0;
310 }
311
312 pos++;
313 return f->get_8();
314}
315
316uint64_t FileAccessPack::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
317 ERR_FAIL_COND_V_MSG(f.is_null(), -1, "File must be opened before use.");
318 ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
319
320 if (eof) {
321 return 0;
322 }
323
324 int64_t to_read = p_length;
325 if (to_read + pos > pf.size) {
326 eof = true;
327 to_read = (int64_t)pf.size - (int64_t)pos;
328 }
329
330 pos += p_length;
331
332 if (to_read <= 0) {
333 return 0;
334 }
335 f->get_buffer(p_dst, to_read);
336
337 return to_read;
338}
339
340void FileAccessPack::set_big_endian(bool p_big_endian) {
341 ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");
342
343 FileAccess::set_big_endian(p_big_endian);
344 f->set_big_endian(p_big_endian);
345}
346
347Error FileAccessPack::get_error() const {
348 if (eof) {
349 return ERR_FILE_EOF;
350 }
351 return OK;
352}
353
354void FileAccessPack::flush() {
355 ERR_FAIL();
356}
357
358void FileAccessPack::store_8(uint8_t p_dest) {
359 ERR_FAIL();
360}
361
362void FileAccessPack::store_buffer(const uint8_t *p_src, uint64_t p_length) {
363 ERR_FAIL();
364}
365
366bool FileAccessPack::file_exists(const String &p_name) {
367 return false;
368}
369
370void FileAccessPack::close() {
371 f = Ref<FileAccess>();
372}
373
374FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
375 pf(p_file),
376 f(FileAccess::open(pf.pack, FileAccess::READ)) {
377 ERR_FAIL_COND_MSG(f.is_null(), "Can't open pack-referenced file '" + String(pf.pack) + "'.");
378
379 f->seek(pf.offset);
380 off = pf.offset;
381
382 if (pf.encrypted) {
383 Ref<FileAccessEncrypted> fae;
384 fae.instantiate();
385 ERR_FAIL_COND_MSG(fae.is_null(), "Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
386
387 Vector<uint8_t> key;
388 key.resize(32);
389 for (int i = 0; i < key.size(); i++) {
390 key.write[i] = script_encryption_key[i];
391 }
392
393 Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
394 ERR_FAIL_COND_MSG(err, "Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
395 f = fae;
396 off = 0;
397 }
398 pos = 0;
399 eof = false;
400}
401
402//////////////////////////////////////////////////////////////////////////////////
403// DIR ACCESS
404//////////////////////////////////////////////////////////////////////////////////
405
406Error DirAccessPack::list_dir_begin() {
407 list_dirs.clear();
408 list_files.clear();
409
410 for (const KeyValue<String, PackedData::PackedDir *> &E : current->subdirs) {
411 list_dirs.push_back(E.key);
412 }
413
414 for (const String &E : current->files) {
415 list_files.push_back(E);
416 }
417
418 return OK;
419}
420
421String DirAccessPack::get_next() {
422 if (list_dirs.size()) {
423 cdir = true;
424 String d = list_dirs.front()->get();
425 list_dirs.pop_front();
426 return d;
427 } else if (list_files.size()) {
428 cdir = false;
429 String f = list_files.front()->get();
430 list_files.pop_front();
431 return f;
432 } else {
433 return String();
434 }
435}
436
437bool DirAccessPack::current_is_dir() const {
438 return cdir;
439}
440
441bool DirAccessPack::current_is_hidden() const {
442 return false;
443}
444
445void DirAccessPack::list_dir_end() {
446 list_dirs.clear();
447 list_files.clear();
448}
449
450int DirAccessPack::get_drive_count() {
451 return 0;
452}
453
454String DirAccessPack::get_drive(int p_drive) {
455 return "";
456}
457
458PackedData::PackedDir *DirAccessPack::_find_dir(String p_dir) {
459 String nd = p_dir.replace("\\", "/");
460
461 // Special handling since simplify_path() will forbid it
462 if (p_dir == "..") {
463 return current->parent;
464 }
465
466 bool absolute = false;
467 if (nd.begins_with("res://")) {
468 nd = nd.replace_first("res://", "");
469 absolute = true;
470 }
471
472 nd = nd.simplify_path();
473
474 if (nd.is_empty()) {
475 nd = ".";
476 }
477
478 if (nd.begins_with("/")) {
479 nd = nd.replace_first("/", "");
480 absolute = true;
481 }
482
483 Vector<String> paths = nd.split("/");
484
485 PackedData::PackedDir *pd;
486
487 if (absolute) {
488 pd = PackedData::get_singleton()->root;
489 } else {
490 pd = current;
491 }
492
493 for (int i = 0; i < paths.size(); i++) {
494 String p = paths[i];
495 if (p == ".") {
496 continue;
497 } else if (p == "..") {
498 if (pd->parent) {
499 pd = pd->parent;
500 }
501 } else if (pd->subdirs.has(p)) {
502 pd = pd->subdirs[p];
503
504 } else {
505 return nullptr;
506 }
507 }
508
509 return pd;
510}
511
512Error DirAccessPack::change_dir(String p_dir) {
513 PackedData::PackedDir *pd = _find_dir(p_dir);
514 if (pd) {
515 current = pd;
516 return OK;
517 } else {
518 return ERR_INVALID_PARAMETER;
519 }
520}
521
522String DirAccessPack::get_current_dir(bool p_include_drive) const {
523 PackedData::PackedDir *pd = current;
524 String p = current->name;
525
526 while (pd->parent) {
527 pd = pd->parent;
528 p = pd->name.path_join(p);
529 }
530
531 return "res://" + p;
532}
533
534bool DirAccessPack::file_exists(String p_file) {
535 p_file = fix_path(p_file);
536
537 PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir());
538 if (!pd) {
539 return false;
540 }
541 return pd->files.has(p_file.get_file());
542}
543
544bool DirAccessPack::dir_exists(String p_dir) {
545 p_dir = fix_path(p_dir);
546
547 return _find_dir(p_dir) != nullptr;
548}
549
550Error DirAccessPack::make_dir(String p_dir) {
551 return ERR_UNAVAILABLE;
552}
553
554Error DirAccessPack::rename(String p_from, String p_to) {
555 return ERR_UNAVAILABLE;
556}
557
558Error DirAccessPack::remove(String p_name) {
559 return ERR_UNAVAILABLE;
560}
561
562uint64_t DirAccessPack::get_space_left() {
563 return 0;
564}
565
566String DirAccessPack::get_filesystem_type() const {
567 return "PCK";
568}
569
570DirAccessPack::DirAccessPack() {
571 current = PackedData::get_singleton()->root;
572}
573