From b8d602f847a1d67784d7fdb155b4de19c98c3367 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 3 Jul 2022 17:29:52 +0200 Subject: [PATCH] Added a new helper method to DirAccess. --- core/bind/core_bind.cpp | 6 ++++ core/bind/core_bind.h | 2 ++ core/os/dir_access.cpp | 30 +++++++++++++++++++ core/os/dir_access.h | 2 ++ modules/web/file_cache.cpp | 24 +-------------- .../web/nodes/paged_article/paged_article.cpp | 22 +------------- .../nodes/paged_article/paged_articles.cpp | 23 +------------- 7 files changed, 43 insertions(+), 66 deletions(-) diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 66c991d05..e8be8c3d2 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -2473,6 +2473,11 @@ Error _Directory::remove(String p_name) { return d->remove(p_name); } +String _Directory::get_filesystem_abspath_for(String p_path) { + ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use."); + return DirAccess::get_filesystem_abspath_for(p_path); +} + void _Directory::_bind_methods() { ClassDB::bind_method(D_METHOD("open", "path"), &_Directory::open); ClassDB::bind_method(D_METHOD("list_dir_begin", "skip_navigational", "skip_hidden"), &_Directory::list_dir_begin, DEFVAL(false), DEFVAL(false)); @@ -2493,6 +2498,7 @@ void _Directory::_bind_methods() { ClassDB::bind_method(D_METHOD("copy", "from", "to"), &_Directory::copy); ClassDB::bind_method(D_METHOD("rename", "from", "to"), &_Directory::rename); ClassDB::bind_method(D_METHOD("remove", "path"), &_Directory::remove); + ClassDB::bind_method(D_METHOD("get_filesystem_abspath_for", "path"), &_Directory::get_filesystem_abspath_for); } _Directory::_Directory() { diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 521289bca..332b03dce 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -626,6 +626,8 @@ public: Error rename(String p_from, String p_to); Error remove(String p_name); + String get_filesystem_abspath_for(String p_path); + _Directory(); virtual ~_Directory(); diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index ad8bf423e..ebc7eb929 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -420,6 +420,36 @@ bool DirAccess::exists(String p_dir) { return valid; } +String DirAccess::get_filesystem_abspath_for(String p_path) { + if (p_path.begins_with("res://")) { + if (ProjectSettings::get_singleton()) { + String resource_path = ProjectSettings::get_singleton()->get_resource_path(); + if (resource_path != "") { + p_path = p_path.replace_first("res:/", resource_path); + } else { + p_path = p_path.replace_first("res://", ""); + } + } + } else if (p_path.begins_with("user://")) { + String data_dir = OS::get_singleton()->get_user_data_dir(); + if (data_dir != "") { + p_path = p_path.replace_first("user:/", data_dir); + } else { + p_path = p_path.replace_first("user://", ""); + } + } + + DirAccess *d = DirAccess::create(ACCESS_FILESYSTEM); + if (!d) { + return p_path; + } + + d->change_dir(p_path); + String full = d->get_current_dir(); + memdelete(d); + return full; +} + DirAccess::DirAccess() { _access_type = ACCESS_FILESYSTEM; } diff --git a/core/os/dir_access.h b/core/os/dir_access.h index e5409b036..1bd6b7739 100644 --- a/core/os/dir_access.h +++ b/core/os/dir_access.h @@ -132,6 +132,8 @@ public: static DirAccess *open(const String &p_path, Error *r_error = nullptr); + static String get_filesystem_abspath_for(String p_path); + DirAccess(); virtual ~DirAccess(); }; diff --git a/modules/web/file_cache.cpp b/modules/web/file_cache.cpp index a52bcaed8..831443cd2 100644 --- a/modules/web/file_cache.cpp +++ b/modules/web/file_cache.cpp @@ -84,30 +84,8 @@ void FileCache::wwwroot_refresh_cache() { _registered_files.clear(); - //TODO this should probably be a static method in DirAccess if (_wwwroot_orig != "") { - 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 = DirAccess::get_filesystem_abspath_for(_wwwroot_orig); _wwwroot = _wwwroot.path_clean_end_slash(); wwwroot_evaluate_dir(_wwwroot); diff --git a/modules/web/nodes/paged_article/paged_article.cpp b/modules/web/nodes/paged_article/paged_article.cpp index 9fb594198..825fcdf86 100644 --- a/modules/web/nodes/paged_article/paged_article.cpp +++ b/modules/web/nodes/paged_article/paged_article.cpp @@ -85,27 +85,7 @@ void PagedArticle::_render_preview(Ref request) { void PagedArticle::load() { ERR_FAIL_COND_MSG(articles_folder == "", "Error: PagedArticle::load called, but a articles_folder is not set!"); - String path = articles_folder; - - 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://", ""); - } - } - - _articles_folder_abs = DirAccess::get_full_path(path, DirAccess::ACCESS_FILESYSTEM); + _articles_folder_abs = DirAccess::get_filesystem_abspath_for(articles_folder); _articles_folder_abs = _articles_folder_abs.path_ensure_end_slash(); DirAccess *dir = DirAccess::open(_articles_folder_abs); diff --git a/modules/web/nodes/paged_article/paged_articles.cpp b/modules/web/nodes/paged_article/paged_articles.cpp index bcc6097f5..ecd1a112c 100644 --- a/modules/web/nodes/paged_article/paged_articles.cpp +++ b/modules/web/nodes/paged_article/paged_articles.cpp @@ -33,27 +33,7 @@ void PagedArticles::_render_preview(Ref request) { void PagedArticles::load() { ERR_FAIL_COND_MSG(_folder == "", "Error: PagedArticles::load called, but a folder is not set!"); - String folder = _folder; - - if (folder.begins_with("res://")) { - if (ProjectSettings::get_singleton()) { - String resource_path = ProjectSettings::get_singleton()->get_resource_path(); - if (resource_path != "") { - folder = folder.replace_first("res:/", resource_path); - } else { - folder = folder.replace_first("res://", ""); - } - } - } else if (folder.begins_with("user://")) { - String data_dir = OS::get_singleton()->get_user_data_dir(); - if (data_dir != "") { - folder = folder.replace_first("user:/", data_dir); - } else { - folder = folder.replace_first("user://", ""); - } - } - - folder = DirAccess::get_full_path(folder, DirAccess::ACCESS_FILESYSTEM); + String folder = DirAccess::get_filesystem_abspath_for(_folder); folder = folder.path_clean_end_slash(); DirAccess *dir = DirAccess::open(folder); @@ -78,7 +58,6 @@ void PagedArticles::load() { } dir->list_dir_end(); - memdelete(dir); generate_index_page();