From 8840439f7a03b4693df75e0ac14f8df26722c722 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 2 Jul 2022 19:58:06 +0200 Subject: [PATCH] Fix path handling in FileCache. --- modules/web/file_cache.cpp | 32 ++++++++++++++++++++++++++++++-- modules/web/file_cache.h | 1 + 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/modules/web/file_cache.cpp b/modules/web/file_cache.cpp index a12485e18..a52bcaed8 100644 --- a/modules/web/file_cache.cpp +++ b/modules/web/file_cache.cpp @@ -4,6 +4,7 @@ #include "core/os/file_access.h" #include "core/os/os.h" #include "core/print_string.h" +#include "core/project_settings.h" String FileCache::get_wwwroot() { return _wwwroot_orig; @@ -13,7 +14,7 @@ void FileCache::set_wwwroot(const String &val) { } String FileCache::get_wwwroot_abs() { - return _wwwroot_orig; + return _wwwroot; } int FileCache::get_cache_invalidation_time() { @@ -74,15 +75,41 @@ String FileCache::wwwroot_get_file_orig_path(const int index) { return _registered_files[index].orig_path; } +String FileCache::wwwroot_get_file_orig_path_abs(const int index) { + return get_wwwroot_abs() + wwwroot_get_file_orig_path(index); +} + void FileCache::wwwroot_refresh_cache() { _lock.write_lock(); _registered_files.clear(); + //TODO this should probably be a static method in DirAccess if (_wwwroot_orig != "") { - _wwwroot = DirAccess::get_full_path(_wwwroot_orig, DirAccess::ACCESS_FILESYSTEM); + String path = _wwwroot_orig; + + if (path.begins_with("res://")) { + if (ProjectSettings::get_singleton()) { + String resource_path = ProjectSettings::get_singleton()->get_resource_path(); + if (resource_path != "") { + path = path.replace_first("res:/", resource_path); + } else { + path = path.replace_first("res://", ""); + } + } + } else if (path.begins_with("user://")) { + String data_dir = OS::get_singleton()->get_user_data_dir(); + if (data_dir != "") { + path = path.replace_first("user:/", data_dir); + } else { + path = path.replace_first("user://", ""); + } + } + + _wwwroot = DirAccess::get_full_path(path, DirAccess::ACCESS_FILESYSTEM); _wwwroot = _wwwroot.path_clean_end_slash(); + wwwroot_evaluate_dir(_wwwroot); } @@ -107,6 +134,7 @@ void FileCache::wwwroot_evaluate_dir(const String &path, const bool should_exist String np = path + "/" + f; np = np.substr(_wwwroot.size() - 1, np.size() - _wwwroot.size()); wwwroot_register_file(np); + } else { wwwroot_evaluate_dir(path + "/" + f); } diff --git a/modules/web/file_cache.h b/modules/web/file_cache.h index e9c3f6ec1..618764943 100644 --- a/modules/web/file_cache.h +++ b/modules/web/file_cache.h @@ -30,6 +30,7 @@ public: //return -1 if does not exists int wwwroot_get_file_index(const String &file_path); String wwwroot_get_file_orig_path(const int index); + String wwwroot_get_file_orig_path_abs(const int index); void wwwroot_refresh_cache(); void wwwroot_evaluate_dir(const String &path, const bool should_exist = true);