Skip to content

Commit d17ce4c

Browse files
committed
Fix "res://" being replaced by resource packs in the editor and on Android
1 parent 4ce466d commit d17ce4c

File tree

5 files changed

+79
-12
lines changed

5 files changed

+79
-12
lines changed

core/config/project_settings.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,30 @@ void ProjectSettings::_emit_changed() {
472472
emit_signal("settings_changed");
473473
}
474474

475-
bool ProjectSettings::_load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset) {
475+
bool ProjectSettings::load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset) {
476+
return ProjectSettings::_load_resource_pack(p_pack, p_replace_files, p_offset, false);
477+
}
478+
479+
bool ProjectSettings::_load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset, bool p_main_pack) {
476480
if (PackedData::get_singleton()->is_disabled()) {
477481
return false;
478482
}
479483

480-
bool ok = PackedData::get_singleton()->add_pack(p_pack, p_replace_files, p_offset) == OK;
484+
if (p_pack == "res://") {
485+
// Loading the resource directory as a pack source is reserved for internal use only.
486+
return false;
487+
}
481488

489+
if (!p_main_pack && !using_datapack && !OS::get_singleton()->get_resource_dir().is_empty()) {
490+
// Add the project's resource file system to PackedData so directory access keeps working when
491+
// the game is running without a main pack, like in the editor or on Android.
492+
PackedData::get_singleton()->add_pack_source(memnew(PackedSourceDirectory));
493+
PackedData::get_singleton()->add_pack("res://", false, 0);
494+
DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
495+
using_datapack = true;
496+
}
497+
498+
bool ok = PackedData::get_singleton()->add_pack(p_pack, p_replace_files, p_offset) == OK;
482499
if (!ok) {
483500
return false;
484501
}
@@ -491,9 +508,11 @@ bool ProjectSettings::_load_resource_pack(const String &p_pack, bool p_replace_f
491508
ResourceUID::get_singleton()->load_from_cache(false);
492509
}
493510

494-
//if data.pck is found, all directory access will be from here
495-
DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
496-
using_datapack = true;
511+
// If the data pack was found, all directory access will be from here.
512+
if (!using_datapack) {
513+
DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
514+
using_datapack = true;
515+
}
497516

498517
return true;
499518
}
@@ -572,7 +591,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b
572591
// Attempt with a user-defined main pack first
573592

574593
if (!p_main_pack.is_empty()) {
575-
bool ok = _load_resource_pack(p_main_pack);
594+
bool ok = _load_resource_pack(p_main_pack, false, 0, true);
576595
ERR_FAIL_COND_V_MSG(!ok, ERR_CANT_OPEN, vformat("Cannot open resource pack '%s'.", p_main_pack));
577596

578597
Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
@@ -591,7 +610,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b
591610
// and if so, we attempt loading it at the end.
592611

593612
// Attempt with PCK bundled into executable.
594-
bool found = _load_resource_pack(exec_path);
613+
bool found = _load_resource_pack(exec_path, false, 0, true);
595614

596615
// Attempt with exec_name.pck.
597616
// (This is the usual case when distributing a Godot game.)
@@ -607,20 +626,20 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b
607626
#ifdef MACOS_ENABLED
608627
if (!found) {
609628
// Attempt to load PCK from macOS .app bundle resources.
610-
found = _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_basename + ".pck")) || _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_filename + ".pck"));
629+
found = _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_basename + ".pck"), false, 0, true) || _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_filename + ".pck"), false, 0, true);
611630
}
612631
#endif
613632

614633
if (!found) {
615634
// Try to load data pack at the location of the executable.
616635
// As mentioned above, we have two potential names to attempt.
617-
found = _load_resource_pack(exec_dir.path_join(exec_basename + ".pck")) || _load_resource_pack(exec_dir.path_join(exec_filename + ".pck"));
636+
found = _load_resource_pack(exec_dir.path_join(exec_basename + ".pck"), false, 0, true) || _load_resource_pack(exec_dir.path_join(exec_filename + ".pck"), false, 0, true);
618637
}
619638

620639
if (!found) {
621640
// If we couldn't find them next to the executable, we attempt
622641
// the current working directory. Same story, two tests.
623-
found = _load_resource_pack(exec_basename + ".pck") || _load_resource_pack(exec_filename + ".pck");
642+
found = _load_resource_pack(exec_basename + ".pck", false, 0, true) || _load_resource_pack(exec_filename + ".pck", false, 0, true);
624643
}
625644

626645
// If we opened our package, try and load our project.
@@ -1418,7 +1437,7 @@ void ProjectSettings::_bind_methods() {
14181437
ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
14191438
ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
14201439
ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
1421-
ClassDB::bind_method(D_METHOD("load_resource_pack", "pack", "replace_files", "offset"), &ProjectSettings::_load_resource_pack, DEFVAL(true), DEFVAL(0));
1440+
ClassDB::bind_method(D_METHOD("load_resource_pack", "pack", "replace_files", "offset"), &ProjectSettings::load_resource_pack, DEFVAL(true), DEFVAL(0));
14221441

14231442
ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
14241443

core/config/project_settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ class ProjectSettings : public Object {
135135

136136
void _convert_to_last_version(int p_from_version);
137137

138-
bool _load_resource_pack(const String &p_pack, bool p_replace_files = true, int p_offset = 0);
138+
bool load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset);
139+
bool _load_resource_pack(const String &p_pack, bool p_replace_files = true, int p_offset = 0, bool p_main_pack = false);
139140

140141
void _add_property_info_bind(const Dictionary &p_info);
141142

core/io/file_access_pack.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,44 @@ Ref<FileAccess> PackedSourcePCK::get_file(const String &p_path, PackedData::Pack
331331

332332
//////////////////////////////////////////////////////////////////
333333

334+
bool PackedSourceDirectory::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) {
335+
// Load with offset feature only supported for PCK files.
336+
ERR_FAIL_COND_V_MSG(p_offset != 0, false, "Invalid PCK data. Note that loading files with a non-zero offset isn't supported with directories.");
337+
338+
if (p_path != "res://") {
339+
return false;
340+
}
341+
add_directory(p_path, p_replace_files);
342+
return true;
343+
}
344+
345+
Ref<FileAccess> PackedSourceDirectory::get_file(const String &p_path, PackedData::PackedFile *p_file) {
346+
Ref<FileAccess> ret = FileAccess::create_for_path(p_path);
347+
ret->reopen(p_path, FileAccess::READ);
348+
return ret;
349+
}
350+
351+
void PackedSourceDirectory::add_directory(const String &p_path, bool p_replace_files) {
352+
Ref<DirAccess> da = DirAccess::open(p_path);
353+
if (da.is_null()) {
354+
return;
355+
}
356+
da->set_include_hidden(true);
357+
358+
for (const String &file_name : da->get_files()) {
359+
String file_path = p_path.path_join(file_name);
360+
uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
361+
PackedData::get_singleton()->add_path(p_path, file_path, 0, 0, md5, this, p_replace_files, false);
362+
}
363+
364+
for (const String &sub_dir_name : da->get_directories()) {
365+
String sub_dir_path = p_path.path_join(sub_dir_name);
366+
add_directory(sub_dir_path, p_replace_files);
367+
}
368+
}
369+
370+
//////////////////////////////////////////////////////////////////
371+
334372
Error FileAccessPack::open_internal(const String &p_path, int p_mode_flags) {
335373
ERR_PRINT("Can't open pack-referenced file.");
336374
return ERR_UNAVAILABLE;

core/io/file_access_pack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ class PackedSourcePCK : public PackSource {
147147
virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override;
148148
};
149149

150+
class PackedSourceDirectory : public PackSource {
151+
void add_directory(const String &p_path, bool p_replace_files);
152+
153+
public:
154+
virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) override;
155+
virtual Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override;
156+
};
157+
150158
class FileAccessPack : public FileAccess {
151159
PackedData::PackedFile pf;
152160

doc/classes/ProjectSettings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
Loads the contents of the .pck or .zip file specified by [param pack] into the resource filesystem ([code]res://[/code]). Returns [code]true[/code] on success.
152152
[b]Note:[/b] If a file from [param pack] shares the same path as a file already in the resource filesystem, any attempts to load that file will use the file from [param pack] unless [param replace_files] is set to [code]false[/code].
153153
[b]Note:[/b] The optional [param offset] parameter can be used to specify the offset in bytes to the start of the resource pack. This is only supported for .pck files.
154+
[b]Note:[/b] [DirAccess] will not show changes made to the contents of [code]res://[/code] after calling this function.
154155
</description>
155156
</method>
156157
<method name="localize_path" qualifiers="const">

0 commit comments

Comments
 (0)