2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* file_access_pack.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "file_access_pack.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
#include "core/io/file_access_encrypted.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/object/script_language.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/version.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
Error PackedData::add_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < sources.size(); i++) {
|
2020-07-13 18:22:06 +02:00
|
|
|
if (sources[i]->try_open_pack(p_path, p_replace_files, p_offset)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return ERR_FILE_UNRECOGNIZED;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
void 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) {
|
|
|
|
PathMD5 pmd5(p_path.md5_buffer());
|
2014-08-02 03:10:38 +02:00
|
|
|
|
|
|
|
bool exists = files.has(pmd5);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
PackedFile pf;
|
2020-04-28 19:51:29 +02:00
|
|
|
pf.encrypted = p_encrypted;
|
2019-03-26 18:51:13 +01:00
|
|
|
pf.pack = p_pkg_path;
|
|
|
|
pf.offset = p_ofs;
|
|
|
|
pf.size = p_size;
|
2020-05-14 16:41:43 +02:00
|
|
|
for (int i = 0; i < 16; i++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
pf.md5[i] = p_md5[i];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
pf.src = p_src;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!exists || p_replace_files) {
|
2019-09-23 22:41:25 +02:00
|
|
|
files[pmd5] = pf;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
//search for dir
|
2019-03-26 18:51:13 +01:00
|
|
|
String p = p_path.replace_first("res://", "");
|
2017-03-05 16:44:50 +01:00
|
|
|
PackedDir *cd = root;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p.find("/") != -1) { //in a subdir
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<String> ds = p.get_base_dir().split("/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < ds.size(); j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!cd->subdirs.has(ds[j])) {
|
2017-03-05 16:44:50 +01:00
|
|
|
PackedDir *pd = memnew(PackedDir);
|
|
|
|
pd->name = ds[j];
|
|
|
|
pd->parent = cd;
|
|
|
|
cd->subdirs[pd->name] = pd;
|
|
|
|
cd = pd;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
cd = cd->subdirs[ds[j]];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 18:51:13 +01:00
|
|
|
String filename = p_path.get_file();
|
2020-04-20 10:56:27 +02:00
|
|
|
// Don't add as a file if the path points to a directory
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!filename.is_empty()) {
|
2018-03-18 14:04:50 +01:00
|
|
|
cd->files.insert(filename);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackedData::add_pack_source(PackSource *p_source) {
|
2020-04-02 01:20:12 +02:00
|
|
|
if (p_source != nullptr) {
|
2015-05-07 01:37:25 +02:00
|
|
|
sources.push_back(p_source);
|
|
|
|
}
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
PackedData *PackedData::singleton = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
PackedData::PackedData() {
|
2017-03-05 16:44:50 +01:00
|
|
|
singleton = this;
|
|
|
|
root = memnew(PackedDir);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
add_pack_source(memnew(PackedSourcePCK));
|
|
|
|
}
|
|
|
|
|
2015-04-21 00:38:02 +02:00
|
|
|
void PackedData::_free_packed_dirs(PackedDir *p_dir) {
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<String, PackedDir *> &E : p_dir->subdirs) {
|
|
|
|
_free_packed_dirs(E.value);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-04-21 00:38:02 +02:00
|
|
|
memdelete(p_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
PackedData::~PackedData() {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < sources.size(); i++) {
|
2015-04-21 00:38:02 +02:00
|
|
|
memdelete(sources[i]);
|
|
|
|
}
|
|
|
|
_free_packed_dirs(root);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) {
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!f) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-07-13 18:22:06 +02:00
|
|
|
f->seek(p_offset);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t magic = f->get_32();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-11-30 17:22:22 +01:00
|
|
|
if (magic != PACK_HEADER_MAGIC) {
|
2020-07-13 18:22:06 +02:00
|
|
|
// loading with offset feature not supported for self contained exe files
|
|
|
|
if (p_offset != 0) {
|
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported.");
|
|
|
|
}
|
|
|
|
|
2019-03-25 20:56:33 +01:00
|
|
|
//maybe at the end.... self contained exe
|
2014-02-10 02:10:30 +01:00
|
|
|
f->seek_end();
|
2017-09-10 15:37:49 +02:00
|
|
|
f->seek(f->get_position() - 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
magic = f->get_32();
|
2019-11-30 17:22:22 +01:00
|
|
|
if (magic != PACK_HEADER_MAGIC) {
|
2019-10-03 19:45:41 +02:00
|
|
|
f->close();
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-10 15:37:49 +02:00
|
|
|
f->seek(f->get_position() - 12);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
uint64_t ds = f->get_64();
|
2017-09-10 15:37:49 +02:00
|
|
|
f->seek(f->get_position() - ds - 8);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
magic = f->get_32();
|
2019-11-30 17:22:22 +01:00
|
|
|
if (magic != PACK_HEADER_MAGIC) {
|
2019-10-03 19:45:41 +02:00
|
|
|
f->close();
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 06:01:39 +01:00
|
|
|
uint32_t version = f->get_32();
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t ver_major = f->get_32();
|
|
|
|
uint32_t ver_minor = f->get_32();
|
2019-11-30 17:22:22 +01:00
|
|
|
f->get_32(); // patch number, not used for validation.
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-11-30 17:22:22 +01:00
|
|
|
if (version != PACK_FORMAT_VERSION) {
|
2019-10-03 19:45:41 +02:00
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(false, "Pack version unsupported: " + itos(version) + ".");
|
|
|
|
}
|
|
|
|
if (ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR)) {
|
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + ".");
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
uint32_t pack_flags = f->get_32();
|
|
|
|
uint64_t file_base = f->get_64();
|
|
|
|
|
|
|
|
bool enc_directory = (pack_flags & PACK_DIR_ENCRYPTED);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 16; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
//reserved
|
|
|
|
f->get_32();
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_count = f->get_32();
|
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
if (enc_directory) {
|
|
|
|
FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
|
|
|
|
if (!fae) {
|
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<uint8_t> key;
|
|
|
|
key.resize(32);
|
|
|
|
for (int i = 0; i < key.size(); i++) {
|
|
|
|
key.write[i] = script_encryption_key[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
|
|
|
|
if (err) {
|
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
memdelete(fae);
|
|
|
|
ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory.");
|
|
|
|
}
|
|
|
|
f = fae;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < file_count; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t sl = f->get_32();
|
|
|
|
CharString cs;
|
2017-03-05 16:44:50 +01:00
|
|
|
cs.resize(sl + 1);
|
|
|
|
f->get_buffer((uint8_t *)cs.ptr(), sl);
|
|
|
|
cs[sl] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String path;
|
|
|
|
path.parse_utf8(cs.ptr());
|
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
uint64_t ofs = file_base + f->get_64();
|
2014-02-10 02:10:30 +01:00
|
|
|
uint64_t size = f->get_64();
|
2014-02-13 22:03:28 +01:00
|
|
|
uint8_t md5[16];
|
2017-03-05 16:44:50 +01:00
|
|
|
f->get_buffer(md5, 16);
|
2020-04-28 19:51:29 +02:00
|
|
|
uint32_t flags = f->get_32();
|
|
|
|
|
|
|
|
PackedData::get_singleton()->add_path(p_path, path, ofs + p_offset, size, md5, this, p_replace_files, (flags & PACK_FILE_ENCRYPTED));
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-03 19:45:41 +02:00
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccess *PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) {
|
|
|
|
return memnew(FileAccessPack(p_path, *p_file));
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error FileAccessPack::_open(const String &p_path, int p_mode_flags) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(ERR_UNAVAILABLE);
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccessPack::close() {
|
|
|
|
f->close();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool FileAccessPack::is_open() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return f->is_open();
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
void FileAccessPack::seek(uint64_t p_position) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_position > pf.size) {
|
|
|
|
eof = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
eof = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 19:51:29 +02:00
|
|
|
f->seek(off + p_position);
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = p_position;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void FileAccessPack::seek_end(int64_t p_position) {
|
|
|
|
seek(pf.size + p_position);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
uint64_t FileAccessPack::get_position() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return pos;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-05-25 08:58:49 +02:00
|
|
|
uint64_t FileAccessPack::get_length() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return pf.size;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool FileAccessPack::eof_reached() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t FileAccessPack::get_8() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pos >= pf.size) {
|
|
|
|
eof = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
return f->get_8();
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
uint64_t FileAccessPack::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
|
2021-03-16 22:55:11 +01:00
|
|
|
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
|
2021-03-09 02:37:35 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (eof) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
int64_t to_read = p_length;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (to_read + pos > pf.size) {
|
|
|
|
eof = true;
|
2019-03-26 18:51:13 +01:00
|
|
|
to_read = (int64_t)pf.size - (int64_t)pos;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pos += p_length;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (to_read <= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
f->get_buffer(p_dst, to_read);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return to_read;
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:58:03 +02:00
|
|
|
void FileAccessPack::set_big_endian(bool p_big_endian) {
|
|
|
|
FileAccess::set_big_endian(p_big_endian);
|
|
|
|
f->set_big_endian(p_big_endian);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Error FileAccessPack::get_error() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (eof) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_FILE_EOF;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-09-22 07:56:02 +02:00
|
|
|
void FileAccessPack::flush() {
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void FileAccessPack::store_8(uint8_t p_dest) {
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
void FileAccessPack::store_buffer(const uint8_t *p_src, uint64_t p_length) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool FileAccessPack::file_exists(const String &p_name) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
|
|
|
|
pf(p_file),
|
|
|
|
f(FileAccess::open(pf.pack, FileAccess::READ)) {
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'.");
|
2019-08-15 04:57:49 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
f->seek(pf.offset);
|
2020-04-28 19:51:29 +02:00
|
|
|
off = pf.offset;
|
|
|
|
|
|
|
|
if (pf.encrypted) {
|
|
|
|
FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
|
|
|
|
if (!fae) {
|
|
|
|
ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<uint8_t> key;
|
|
|
|
key.resize(32);
|
|
|
|
for (int i = 0; i < key.size(); i++) {
|
|
|
|
key.write[i] = script_encryption_key[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
|
|
|
|
if (err) {
|
|
|
|
memdelete(fae);
|
|
|
|
ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
|
|
|
|
}
|
|
|
|
f = fae;
|
|
|
|
off = 0;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = 0;
|
|
|
|
eof = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileAccessPack::~FileAccessPack() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (f) {
|
2020-04-28 19:51:29 +02:00
|
|
|
f->close();
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DIR ACCESS
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-01-14 13:16:41 +01:00
|
|
|
Error DirAccessPack::list_dir_begin() {
|
2014-02-10 02:10:30 +01:00
|
|
|
list_dirs.clear();
|
|
|
|
list_files.clear();
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<String, PackedData::PackedDir *> &E : current->subdirs) {
|
|
|
|
list_dirs.push_back(E.key);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Set<String>::Element *E = current->files.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
list_files.push_back(E->get());
|
|
|
|
}
|
|
|
|
|
2017-01-14 13:16:41 +01:00
|
|
|
return OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String DirAccessPack::get_next() {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (list_dirs.size()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
cdir = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
String d = list_dirs.front()->get();
|
|
|
|
list_dirs.pop_front();
|
|
|
|
return d;
|
|
|
|
} else if (list_files.size()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
cdir = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
String f = list_files.front()->get();
|
|
|
|
list_files.pop_front();
|
|
|
|
return f;
|
|
|
|
} else {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool DirAccessPack::current_is_dir() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return cdir;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool DirAccessPack::current_is_hidden() const {
|
2015-03-21 18:33:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void DirAccessPack::list_dir_end() {
|
|
|
|
list_dirs.clear();
|
|
|
|
list_files.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DirAccessPack::get_drive_count() {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String DirAccessPack::get_drive(int p_drive) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:06:40 +02:00
|
|
|
PackedData::PackedDir *DirAccessPack::_find_dir(String p_dir) {
|
2017-03-05 16:44:50 +01:00
|
|
|
String nd = p_dir.replace("\\", "/");
|
2020-07-27 12:06:40 +02:00
|
|
|
|
|
|
|
// Special handling since simplify_path() will forbid it
|
|
|
|
if (p_dir == "..") {
|
|
|
|
return current->parent;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool absolute = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (nd.begins_with("res://")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
nd = nd.replace_first("res://", "");
|
|
|
|
absolute = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
nd = nd.simplify_path();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-12-09 10:42:46 +01:00
|
|
|
if (nd.is_empty()) {
|
2020-05-10 12:56:01 +02:00
|
|
|
nd = ".";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-09-04 00:35:42 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (nd.begins_with("/")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
nd = nd.replace_first("/", "");
|
|
|
|
absolute = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> paths = nd.split("/");
|
|
|
|
|
|
|
|
PackedData::PackedDir *pd;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (absolute) {
|
2014-02-10 02:10:30 +01:00
|
|
|
pd = PackedData::get_singleton()->root;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
pd = current;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < paths.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
String p = paths[i];
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p == ".") {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (p == "..") {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (pd->parent) {
|
2017-03-05 16:44:50 +01:00
|
|
|
pd = pd->parent;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} else if (pd->subdirs.has(p)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
pd = pd->subdirs[p];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else {
|
2020-07-27 12:06:40 +02:00
|
|
|
return nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:06:40 +02:00
|
|
|
return pd;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-07-27 12:06:40 +02:00
|
|
|
Error DirAccessPack::change_dir(String p_dir) {
|
|
|
|
PackedData::PackedDir *pd = _find_dir(p_dir);
|
|
|
|
if (pd) {
|
|
|
|
current = pd;
|
|
|
|
return OK;
|
|
|
|
} else {
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 09:19:29 +01:00
|
|
|
String DirAccessPack::get_current_dir(bool p_include_drive) {
|
2014-02-10 02:10:30 +01:00
|
|
|
PackedData::PackedDir *pd = current;
|
2017-05-04 02:20:53 +02:00
|
|
|
String p = current->name;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-04 02:20:53 +02:00
|
|
|
while (pd->parent) {
|
|
|
|
pd = pd->parent;
|
2018-11-08 23:05:12 +01:00
|
|
|
p = pd->name.plus_file(p);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return "res://" + p;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool DirAccessPack::file_exists(String p_file) {
|
2019-08-17 19:03:18 +02:00
|
|
|
p_file = fix_path(p_file);
|
|
|
|
|
2020-07-27 12:06:40 +02:00
|
|
|
PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir());
|
|
|
|
if (!pd) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return pd->files.has(p_file.get_file());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-25 05:34:51 +02:00
|
|
|
bool DirAccessPack::dir_exists(String p_dir) {
|
2019-08-17 19:03:18 +02:00
|
|
|
p_dir = fix_path(p_dir);
|
|
|
|
|
2020-07-27 12:06:40 +02:00
|
|
|
return _find_dir(p_dir) != nullptr;
|
2014-05-25 05:34:51 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessPack::make_dir(String p_dir) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessPack::rename(String p_from, String p_to) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessPack::remove(String p_name) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
uint64_t DirAccessPack::get_space_left() {
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-21 19:23:08 +01:00
|
|
|
String DirAccessPack::get_filesystem_type() const {
|
|
|
|
return "PCK";
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
DirAccessPack::DirAccessPack() {
|
2017-03-05 16:44:50 +01:00
|
|
|
current = PackedData::get_singleton()->root;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|