godot/core/io/file_access_pack.cpp

534 lines
12 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* file_access_pack.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 01:10:30 +00: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. */
/*************************************************************************/
2014-02-10 01:10:30 +00:00
#include "file_access_pack.h"
#include "core/project_settings.h"
#include "core/version.h"
2014-02-10 01:10:30 +00:00
#include <stdio.h>
Error PackedData::add_pack(const String &p_path, bool p_replace_files, const String &p_destination) {
2014-02-10 01:10:30 +00:00
for (int i = 0; i < sources.size(); i++) {
2014-02-10 01:10:30 +00:00
if (sources[i]->try_open_pack(p_path, p_replace_files, p_destination)) {
2014-02-10 01:10:30 +00:00
return OK;
};
};
return ERR_FILE_UNRECOGNIZED;
};
void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files) {
2014-02-10 01:10:30 +00:00
PathMD5 pmd5(path.md5_buffer());
//printf("adding path %ls, %lli, %lli\n", path.c_str(), pmd5.a, pmd5.b);
bool exists = files.has(pmd5);
2014-02-10 01:10:30 +00:00
PackedFile pf;
pf.pack = pkg_path;
pf.offset = ofs;
pf.size = size;
for (int i = 0; i < 16; i++)
pf.md5[i] = p_md5[i];
2014-02-10 01:10:30 +00:00
pf.src = p_src;
if (!exists || p_replace_files)
files[pmd5] = pf;
2014-02-10 01:10:30 +00:00
if (!exists) {
//search for dir
String p = path.replace_first("res://", "");
PackedDir *cd = root;
2014-02-10 01:10:30 +00:00
if (p.find("/") != -1) { //in a subdir
2014-02-10 01:10:30 +00:00
Vector<String> ds = p.get_base_dir().split("/");
2014-02-10 01:10:30 +00:00
for (int j = 0; j < ds.size(); j++) {
2014-02-10 01:10:30 +00:00
if (!cd->subdirs.has(ds[j])) {
PackedDir *pd = memnew(PackedDir);
pd->name = ds[j];
pd->parent = cd;
cd->subdirs[pd->name] = pd;
cd = pd;
2014-02-10 01:10:30 +00:00
} else {
cd = cd->subdirs[ds[j]];
2014-02-10 01:10:30 +00:00
}
}
}
String filename = path.get_file();
// Don't add as a file if the path points to a directory.
if (!filename.empty()) {
cd->files.insert(filename);
}
2014-02-10 01:10:30 +00:00
}
}
void PackedData::add_pack_source(PackSource *p_source) {
2020-04-01 23:20:12 +00:00
if (p_source != nullptr) {
2015-05-06 23:37:25 +00:00
sources.push_back(p_source);
}
2014-02-10 01:10:30 +00:00
};
2020-04-01 23:20:12 +00:00
PackedData *PackedData::singleton = nullptr;
2014-02-10 01:10:30 +00:00
PackedData::PackedData() {
singleton = this;
root = memnew(PackedDir);
2020-04-01 23:20:12 +00:00
root->parent = nullptr;
disabled = false;
2014-02-10 01:10:30 +00:00
add_pack_source(memnew(PackedSourcePCK));
}
void PackedData::_free_packed_dirs(PackedDir *p_dir) {
for (Map<String, PackedDir *>::Element *E = p_dir->subdirs.front(); E; E = E->next())
_free_packed_dirs(E->get());
memdelete(p_dir);
}
PackedData::~PackedData() {
for (int i = 0; i < sources.size(); i++) {
memdelete(sources[i]);
}
_free_packed_dirs(root);
}
2014-02-10 01:10:30 +00:00
//////////////////////////////////////////////////////////////////
bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, const String &p_destination) {
2014-02-10 01:10:30 +00:00
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
2014-02-10 01:10:30 +00:00
if (!f)
return false;
uint32_t magic = f->get_32();
2014-02-10 01:10:30 +00:00
if (magic != PACK_HEADER_MAGIC) {
//maybe at the end.... self contained exe
2014-02-10 01:10:30 +00:00
f->seek_end();
f->seek(f->get_position() - 4);
2014-02-10 01:10:30 +00:00
magic = f->get_32();
if (magic != PACK_HEADER_MAGIC) {
2014-02-10 01:10:30 +00:00
f->close();
2014-02-10 01:10:30 +00:00
memdelete(f);
return false;
}
f->seek(f->get_position() - 12);
2014-02-10 01:10:30 +00:00
uint64_t ds = f->get_64();
f->seek(f->get_position() - ds - 8);
2014-02-10 01:10:30 +00:00
magic = f->get_32();
if (magic != PACK_HEADER_MAGIC) {
2014-02-10 01:10:30 +00:00
f->close();
2014-02-10 01:10:30 +00:00
memdelete(f);
return false;
}
}
uint32_t version = f->get_32();
2014-02-10 01:10:30 +00:00
uint32_t ver_major = f->get_32();
uint32_t ver_minor = f->get_32();
f->get_32(); // patch number, not used for validation.
2014-02-10 01:10:30 +00:00
if (version != PACK_FORMAT_VERSION) {
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 01:10:30 +00:00
for (int i = 0; i < 16; i++) {
2014-02-10 01:10:30 +00:00
//reserved
f->get_32();
}
int file_count = f->get_32();
for (int i = 0; i < file_count; i++) {
2014-02-10 01:10:30 +00:00
uint32_t sl = f->get_32();
CharString cs;
cs.resize(sl + 1);
f->get_buffer((uint8_t *)cs.ptr(), sl);
cs[sl] = 0;
2014-02-10 01:10:30 +00:00
String path;
path.parse_utf8(cs.ptr());
if (p_destination != "") {
String destination = ProjectSettings::get_singleton()->localize_path(p_destination);
ERR_FAIL_COND_V_MSG(!destination.begins_with("res://"), false, "The destination path must be within the resource filesystem (res://).");
if (!destination.ends_with("/")) {
destination += "/";
}
DirAccess *dir = DirAccess::create(DirAccess::ACCESS_RESOURCES);
if (!dir->dir_exists(destination)) {
memdelete(dir);
ERR_FAIL_V_MSG(false, vformat("The destination path \"%s\" does not exist.", destination));
}
memdelete(dir);
path = path.replace_first("res://", destination);
}
2014-02-10 01:10:30 +00:00
uint64_t ofs = f->get_64();
uint64_t size = f->get_64();
uint8_t md5[16];
f->get_buffer(md5, 16);
PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5, this, p_replace_files);
2014-02-10 01:10:30 +00:00
};
f->close();
memdelete(f);
2014-02-10 01:10:30 +00:00
return true;
};
FileAccess *PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) {
2014-02-10 01:10:30 +00:00
return memnew(FileAccessPack(p_path, *p_file));
2014-02-10 01:10:30 +00:00
};
//////////////////////////////////////////////////////////////////
Error FileAccessPack::_open(const String &p_path, int p_mode_flags) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_V(ERR_UNAVAILABLE);
return ERR_UNAVAILABLE;
}
void FileAccessPack::close() {
f->close();
}
bool FileAccessPack::is_open() const {
2014-02-10 01:10:30 +00:00
return f->is_open();
}
void FileAccessPack::seek(size_t p_position) {
2014-02-10 01:10:30 +00:00
if (p_position > pf.size) {
eof = true;
2014-02-10 01:10:30 +00:00
} else {
eof = false;
2014-02-10 01:10:30 +00:00
}
f->seek(pf.offset + p_position);
pos = p_position;
2014-02-10 01:10:30 +00:00
}
void FileAccessPack::seek_end(int64_t p_position) {
2014-02-10 01:10:30 +00:00
seek(pf.size + p_position);
2014-02-10 01:10:30 +00:00
}
size_t FileAccessPack::get_position() const {
2014-02-10 01:10:30 +00:00
return pos;
}
size_t FileAccessPack::get_len() const {
2014-02-10 01:10:30 +00:00
return pf.size;
}
bool FileAccessPack::eof_reached() const {
2014-02-10 01:10:30 +00:00
return eof;
}
uint8_t FileAccessPack::get_8() const {
if (pos >= pf.size) {
eof = true;
2014-02-10 01:10:30 +00:00
return 0;
}
pos++;
return f->get_8();
}
int FileAccessPack::get_buffer(uint8_t *p_dst, int p_length) const {
2014-02-10 01:10:30 +00:00
if (eof)
return 0;
uint64_t to_read = p_length;
if (to_read + pos > pf.size) {
eof = true;
to_read = int64_t(pf.size) - int64_t(pos);
2014-02-10 01:10:30 +00:00
}
pos += p_length;
2014-02-10 01:10:30 +00:00
if (to_read <= 0)
2014-02-10 01:10:30 +00:00
return 0;
f->get_buffer(p_dst, to_read);
2014-02-10 01:10:30 +00:00
return to_read;
}
void FileAccessPack::set_endian_swap(bool p_swap) {
FileAccess::set_endian_swap(p_swap);
f->set_endian_swap(p_swap);
}
Error FileAccessPack::get_error() const {
if (eof)
return ERR_FILE_EOF;
return OK;
}
void FileAccessPack::flush() {
ERR_FAIL();
}
2014-02-10 01:10:30 +00:00
void FileAccessPack::store_8(uint8_t p_dest) {
ERR_FAIL();
}
void FileAccessPack::store_buffer(const uint8_t *p_src, int p_length) {
2014-02-10 01:10:30 +00:00
ERR_FAIL();
}
bool FileAccessPack::file_exists(const String &p_name) {
2014-02-10 01:10:30 +00:00
return false;
}
FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
pf(p_file),
f(FileAccess::open(pf.pack, FileAccess::READ)) {
2019-09-25 08:28:50 +00:00
ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'.");
2014-02-10 01:10:30 +00:00
f->seek(pf.offset);
pos = 0;
eof = false;
2014-02-10 01:10:30 +00:00
}
FileAccessPack::~FileAccessPack() {
if (f)
memdelete(f);
}
//////////////////////////////////////////////////////////////////////////////////
// DIR ACCESS
//////////////////////////////////////////////////////////////////////////////////
Error DirAccessPack::list_dir_begin() {
2014-02-10 01:10:30 +00:00
list_dirs.clear();
list_files.clear();
for (Map<String, PackedData::PackedDir *>::Element *E = current->subdirs.front(); E; E = E->next()) {
2014-02-10 01:10:30 +00:00
list_dirs.push_back(E->key());
}
for (Set<String>::Element *E = current->files.front(); E; E = E->next()) {
2014-02-10 01:10:30 +00:00
list_files.push_back(E->get());
}
return OK;
2014-02-10 01:10:30 +00:00
}
String DirAccessPack::get_next() {
2014-02-10 01:10:30 +00:00
if (list_dirs.size()) {
cdir = true;
2014-02-10 01:10:30 +00:00
String d = list_dirs.front()->get();
list_dirs.pop_front();
return d;
} else if (list_files.size()) {
cdir = false;
2014-02-10 01:10:30 +00:00
String f = list_files.front()->get();
list_files.pop_front();
return f;
} else {
return String();
}
}
bool DirAccessPack::current_is_dir() const {
2014-02-10 01:10:30 +00:00
return cdir;
}
bool DirAccessPack::current_is_hidden() const {
2015-03-21 17:33:32 +00:00
return false;
}
2014-02-10 01:10:30 +00:00
void DirAccessPack::list_dir_end() {
list_dirs.clear();
list_files.clear();
}
int DirAccessPack::get_drive_count() {
return 0;
}
String DirAccessPack::get_drive(int p_drive) {
return "";
}
Error DirAccessPack::change_dir(String p_dir) {
String nd = p_dir.replace("\\", "/");
bool absolute = false;
2014-02-10 01:10:30 +00:00
if (nd.begins_with("res://")) {
nd = nd.replace_first("res://", "");
absolute = true;
2014-02-10 01:10:30 +00:00
}
nd = nd.simplify_path();
2014-02-10 01:10:30 +00:00
2016-09-03 22:35:42 +00:00
if (nd == "") nd = ".";
2014-02-10 01:10:30 +00:00
if (nd.begins_with("/")) {
nd = nd.replace_first("/", "");
absolute = true;
2014-02-10 01:10:30 +00:00
}
Vector<String> paths = nd.split("/");
PackedData::PackedDir *pd;
if (absolute)
pd = PackedData::get_singleton()->root;
else
pd = current;
for (int i = 0; i < paths.size(); i++) {
2014-02-10 01:10:30 +00:00
String p = paths[i];
if (p == ".") {
2014-02-10 01:10:30 +00:00
continue;
} else if (p == "..") {
2014-02-10 01:10:30 +00:00
if (pd->parent) {
pd = pd->parent;
2014-02-10 01:10:30 +00:00
}
} else if (pd->subdirs.has(p)) {
pd = pd->subdirs[p];
2014-02-10 01:10:30 +00:00
} else {
return ERR_INVALID_PARAMETER;
}
}
current = pd;
2014-02-10 01:10:30 +00:00
return OK;
}
String DirAccessPack::get_current_dir(bool p_include_drive) {
2014-02-10 01:10:30 +00:00
PackedData::PackedDir *pd = current;
String p = current->name;
2014-02-10 01:10:30 +00:00
while (pd->parent) {
pd = pd->parent;
p = pd->name.plus_file(p);
2014-02-10 01:10:30 +00:00
}
return "res://" + p;
2014-02-10 01:10:30 +00:00
}
bool DirAccessPack::file_exists(String p_file) {
2014-02-10 01:10:30 +00:00
p_file = fix_path(p_file);
2014-02-10 01:10:30 +00:00
return current->files.has(p_file);
}
2014-05-25 03:34:51 +00:00
bool DirAccessPack::dir_exists(String p_dir) {
p_dir = fix_path(p_dir);
2014-05-25 03:34:51 +00:00
return current->subdirs.has(p_dir);
}
Error DirAccessPack::make_dir(String p_dir) {
2014-02-10 01:10:30 +00:00
return ERR_UNAVAILABLE;
}
Error DirAccessPack::rename(String p_from, String p_to) {
2014-02-10 01:10:30 +00:00
return ERR_UNAVAILABLE;
}
Error DirAccessPack::remove(String p_name) {
2014-02-10 01:10:30 +00:00
return ERR_UNAVAILABLE;
}
size_t DirAccessPack::get_space_left() {
2014-02-10 01:10:30 +00:00
return 0;
}
String DirAccessPack::get_filesystem_type() const {
return "PCK";
}
2014-02-10 01:10:30 +00:00
DirAccessPack::DirAccessPack() {
current = PackedData::get_singleton()->root;
cdir = false;
2014-02-10 01:10:30 +00:00
}
DirAccessPack::~DirAccessPack() {
}