Added a new helper method to DirAccess.

This commit is contained in:
Relintai 2022-07-03 17:29:52 +02:00
parent 2eb333b63d
commit b8d602f847
7 changed files with 43 additions and 66 deletions

View File

@ -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() {

View File

@ -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();

View File

@ -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;
}

View File

@ -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();
};

View File

@ -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);

View File

@ -85,27 +85,7 @@ void PagedArticle::_render_preview(Ref<WebServerRequest> 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);

View File

@ -33,27 +33,7 @@ void PagedArticles::_render_preview(Ref<WebServerRequest> 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();