Skip to content

Commit

Permalink
Refactor loading embedded pck
Browse files Browse the repository at this point in the history
  • Loading branch information
yvie-k committed Sep 6, 2023
1 parent 973876f commit a5d5ec8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
20 changes: 19 additions & 1 deletion core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,24 @@ void ProjectSettings::_emit_changed() {
emit_signal("settings_changed");
}

bool ProjectSettings::_load_embedded_resource_pack(const String &p_pack, bool p_replace_files) {
if (PackedData::get_singleton()->is_disabled()) {
return false;
}

bool ok = PackedData::get_singleton()->add_embedded_pack(p_pack, p_replace_files) == OK;

if (!ok) {
return false;
}

//if data.pck is found, all directory access will be from here
DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
using_datapack = true;

return true;
}

bool ProjectSettings::_load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset) {
if (PackedData::get_singleton()->is_disabled()) {
return false;
Expand Down Expand Up @@ -549,7 +567,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b
// and if so, we attempt loading it at the end.

// Attempt with PCK bundled into executable.
bool found = _load_resource_pack(exec_path);
bool found = _load_embedded_resource_pack(exec_path);

// Attempt with exec_name.pck.
// (This is the usual case when distributing a Godot game.)
Expand Down
1 change: 1 addition & 0 deletions core/config/project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class ProjectSettings : public Object {
void _convert_to_last_version(int p_from_version);

bool _load_resource_pack(const String &p_pack, bool p_replace_files = true, int p_offset = 0);
bool _load_embedded_resource_pack(const String &p_pack, bool p_replace_files = true);

void _add_property_info_bind(const Dictionary &p_info);

Expand Down
25 changes: 22 additions & 3 deletions core/io/file_access_pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ Error PackedData::add_pack(const String &p_path, bool p_replace_files, uint64_t
return ERR_FILE_UNRECOGNIZED;
}

Error PackedData::add_embedded_pack(const String &p_path, bool p_replace_files) {
for (int i = 0; i < sources.size(); i++) {
if (sources[i]->try_open_pack_embedded(p_path, p_replace_files)) {
return OK;
}
}

return ERR_FILE_UNRECOGNIZED;
}

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) {
String simplified_path = p_path.simplify_path();
PathMD5 pmd5(simplified_path.md5_buffer());
Expand Down Expand Up @@ -312,9 +322,18 @@ bool PCKFile::file_exists(const String &p_path, bool p_case_sensitive) {

bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) {
if (!file.try_open_pack(p_path, p_offset)) {
if (!file.try_open_embedded(p_path)) {
return false;
}
return false;
}

for (auto [path, f] : file.get_files()) {
PackedData::get_singleton()->add_path(p_path, path, f.offset, f.size, f.md5, this, p_replace_files, f.encrypted);
}
return true;
}

bool PackedSourcePCK::try_open_pack_embedded(const String &p_path, bool p_replace_files) {
if (!file.try_open_embedded(p_path)) {
return false;
}

for (auto [path, f] : file.get_files()) {
Expand Down
3 changes: 3 additions & 0 deletions core/io/file_access_pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class PackedData {

static PackedData *get_singleton() { return singleton; }
Error add_pack(const String &p_path, bool p_replace_files, uint64_t p_offset);
Error add_embedded_pack(const String &p_path, bool p_replace_files);

_FORCE_INLINE_ Ref<FileAccess> try_open_path(const String &p_path);
_FORCE_INLINE_ bool has_path(const String &p_path);
Expand All @@ -131,6 +132,7 @@ class PackedData {
class PackSource {
public:
virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) = 0;
virtual bool try_open_pack_embedded(const String &p_path, bool p_replace_files) = 0;
virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) = 0;
virtual ~PackSource() {}
};
Expand All @@ -156,6 +158,7 @@ class PackedSourcePCK : public PackSource {
PCKFile file;
public:
virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) override;
virtual bool try_open_pack_embedded(const String &p_path, bool p_replace_files) override;
virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override;
};

Expand Down
5 changes: 5 additions & 0 deletions core/io/file_access_zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files, uint6
return true;
}

bool ZipArchive::try_open_pack_embedded(const String &p_path, bool p_replace_files) {
// We do not support zip files embedded in the binary
return false;
}

bool ZipArchive::file_exists(String p_name) const {
return files.has(p_name);
}
Expand Down
1 change: 1 addition & 0 deletions core/io/file_access_zip.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ZipArchive : public PackSource {
bool file_exists(String p_name) const;

virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) override;
virtual bool try_open_pack_embedded(const String &p_path, bool p_replace_files) override;
Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override;

static ZipArchive *get_singleton();
Expand Down

0 comments on commit a5d5ec8

Please sign in to comment.