From 9767533408cc6073604eb72ca931b971799353af Mon Sep 17 00:00:00 2001 From: nikitalita <69168929+nikitalita@users.noreply.github.com> Date: Sat, 28 Dec 2024 17:34:03 -0800 Subject: [PATCH] Fix GDREPackedData::set_default_file_access --- utility/file_access_gdre.cpp | 132 ++++++++++++++++++++++++----------- utility/file_access_gdre.h | 6 ++ 2 files changed, 99 insertions(+), 39 deletions(-) diff --git a/utility/file_access_gdre.cpp b/utility/file_access_gdre.cpp index 7ef93546..5aa6f6d3 100644 --- a/utility/file_access_gdre.cpp +++ b/utility/file_access_gdre.cpp @@ -778,6 +778,31 @@ DirAccessGDRE::~DirAccessGDRE() { // }; // static DirAccessType default_dir_res_create_type = UNKNOWN; +#if defined(WINDOWS_ENABLED) +#define FILE_ACCESS_OS FileAccessWindows +#define DIR_ACCESS_OS DirAccessWindows +#define FILE_ACCESS_OS_STR "FileAccessWindows" +#define DIR_ACCESS_OS_STR "DirAccessWindows" +#elif defined(ANDROID_ENABLED) +#define FILE_ACCESS_OS FileAccessAndroid +#define DIR_ACCESS_OS DirAccessJAndroid +#define FILE_ACCESS_OS_STR "FileAccessAndroid" +#define DIR_ACCESS_OS_STR "DirAccessJAndroid" + +#elif defined(MACOS_ENABLED) +#define FILE_ACCESS_OS FileAccessUnix +#define DIR_ACCESS_OS DirAccessMacOS +#define FILE_ACCESS_OS_STR "FileAccessUnix" +#define DIR_ACCESS_OS_STR "DirAccessMacOS" +#elif defined(UNIX_ENABLED) // -- covers Linux, FreeBSD, Web. +#define FILE_ACCESS_OS FileAccessUnix +#define DIR_ACCESS_OS DirAccessUnix +#define FILE_ACCESS_OS_STR "FileAccessUnix" +#define DIR_ACCESS_OS_STR "DirAccessUnix" +#else +#error "Unknown platform" +#endif + String FileAccessGDRE::fix_path(const String &p_path) const { return PathFinder::_fix_path_file_access(p_path.replace("\\", "/")); } @@ -785,6 +810,48 @@ String FileAccessGDRE::fix_path(const String &p_path) const { String DirAccessGDRE::fix_path(const String &p_path) const { return PathFinder::_fix_path_file_access(p_path); } +//get_current_file_access_class +String GDREPackedData::get_current_file_access_class(FileAccess::AccessType p_access_type) { + Ref fa = FileAccess::create(p_access_type); + Ref fa_os = fa; + if (fa_os.is_valid()) { + return FILE_ACCESS_OS_STR; + } + Ref fa_pack = fa; + if (fa_pack.is_valid()) { + return "FileAccessPack"; + } + Ref fa_gdre = fa; + if (fa_gdre.is_valid()) { + return "FileAccessGDRE"; + } + return ""; +} + +String GDREPackedData::get_current_dir_access_class(DirAccess::AccessType p_access_type) { + Ref da = DirAccess::create(p_access_type); + Ref da_os = da; + if (da_os.is_valid()) { + return DIR_ACCESS_OS_STR; + } + Ref da_pack = da; + if (da_pack.is_valid()) { + return "DirAccessPack"; + } + Ref da_gdre = da; + if (da_gdre.is_valid()) { + return "DirAccessGDRE"; + } + return ""; +} + +String GDREPackedData::get_os_file_access_class_name() { + return FILE_ACCESS_OS_STR; +} + +String GDREPackedData::get_os_dir_access_class_name() { + return DIR_ACCESS_OS_STR; +} Ref DirAccessGDRE::_open_filesystem() { // DirAccessGDRE is only made default for DirAccess::ACCESS_RESOURCES @@ -792,65 +859,52 @@ Ref DirAccessGDRE::_open_filesystem() { if (path == "") { path = "res://"; } -#if defined(WINDOWS_ENABLED) - Ref> dir_proxy = memnew(DirAccessProxy); -#elif defined(MACOS_ENABLED) - Ref> dir_proxy = memnew(DirAccessProxy); -#elif defined(ANDROID_ENABLED) - // TODO: Handle apk expansion!! - Ref> dir_proxy = memnew(DirAccessProxy); -#else defined(UNIX_ENABLED) // -- covers OSX, Linux, FreeBSD, Web. - Ref> dir_proxy = memnew(DirAccessProxy); -#endif + Ref> dir_proxy = memnew(DirAccessProxy); dir_proxy->change_dir(path); return dir_proxy; } void GDREPackedData::set_default_file_access() { + if (set_file_access_defaults) { + return; + } + if (old_dir_access_class.is_empty()) { + old_dir_access_class = get_current_dir_access_class(DirAccess::ACCESS_RESOURCES); + } FileAccess::make_default(FileAccess::ACCESS_RESOURCES); FileAccess::make_default(FileAccess::ACCESS_USERDATA); // for user:// files in the pack DirAccess::make_default(DirAccess::ACCESS_RESOURCES); + set_file_access_defaults = true; } void GDREPackedData::reset_default_file_access() { + if (!set_file_access_defaults) { + return; + } // we need to check to see if the real PackedData has the GDRE packed data loaded // if it does, we need to reset the default DirAccess to DirAccessPack // (FileAccessPack is never set to the default for ACCESS_RESOURCES) - if (packed_data_was_enabled) { + if (old_dir_access_class == "DirAccessPack") { DirAccess::make_default(DirAccess::ACCESS_RESOURCES); + } else if (old_dir_access_class == DIR_ACCESS_OS_STR) { + DirAccess::make_default(DirAccess::ACCESS_RESOURCES); } else { -#if defined(WINDOWS_ENABLED) - DirAccess::make_default(DirAccess::ACCESS_RESOURCES); -#elif defined(MACOS_ENABLED) - DirAccess::make_default(DirAccess::ACCESS_RESOURCES); -#elif defined(ANDROID_ENABLED) - // TODO: Handle apk expansion!! - DirAccess::make_default(DirAccess::ACCESS_RESOURCES); -#else defined(UNIX_ENABLED) // -- covers OSX, Linux, FreeBSD, Web. - DirAccess::make_default(DirAccess::ACCESS_RESOURCES); -#endif + WARN_PRINT("WARNING: reset_default_file_access: Unknown default DirAccess class, guessing..."); + if (real_packed_data_has_pack_loaded()) { // if the real PackedData has the GDRE packed data loaded + DirAccess::make_default(DirAccess::ACCESS_RESOURCES); + } else { + DirAccess::make_default(DirAccess::ACCESS_RESOURCES); + } } -#if defined(WINDOWS_ENABLED) - FileAccess::make_default(FileAccess::ACCESS_RESOURCES); - FileAccess::make_default(FileAccess::ACCESS_USERDATA); - -#elif defined(ANDROID_ENABLED) - FileAccess::make_default(FileAccess::ACCESS_RESOURCES); - FileAccess::make_default(FileAccess::ACCESS_USERDATA); -#else - FileAccess::make_default(FileAccess::ACCESS_RESOURCES); - FileAccess::make_default(FileAccess::ACCESS_USERDATA); -#endif + old_dir_access_class = ""; + FileAccess::make_default(FileAccess::ACCESS_RESOURCES); + FileAccess::make_default(FileAccess::ACCESS_USERDATA); + set_file_access_defaults = false; } Ref FileAccessGDRE::_open_filesystem(const String &p_path, int p_mode_flags, Error *r_error) { -#ifdef WINDOWS_ENABLED - Ref> file_proxy = memnew(FileAccessProxy); -#elif defined(ANDROID_ENABLED) - Ref> file_proxy = memnew(FileAccessProxy); -#else // UNIX_ENABLED -- covers OSX, Linux, FreeBSD, Web. - Ref> file_proxy = memnew(FileAccessProxy); -#endif + Ref> file_proxy = memnew(FileAccessProxy); + Error err = file_proxy->open_internal(p_path, p_mode_flags); if (r_error) { *r_error = err; diff --git a/utility/file_access_gdre.h b/utility/file_access_gdre.h index 2dfddc2b..245f24dc 100644 --- a/utility/file_access_gdre.h +++ b/utility/file_access_gdre.h @@ -49,6 +49,8 @@ class GDREPackedData { static GDREPackedData *singleton; bool disabled = false; bool packed_data_was_enabled = false; + String old_dir_access_class; + bool set_file_access_defaults = false; void _free_packed_dirs(PackedDir *p_dir); void _get_file_paths(PackedDir *p_dir, const String &p_parent_dir, HashSet &r_paths) const; @@ -83,6 +85,10 @@ class GDREPackedData { bool has_loaded_packs(); String fix_res_path(const String &p_path); + static String get_current_file_access_class(FileAccess::AccessType p_access_type); + static String get_current_dir_access_class(DirAccess::AccessType p_access_type); + static String get_os_file_access_class_name(); + static String get_os_dir_access_class_name(); GDREPackedData(); ~GDREPackedData(); };