Skip to content

Commit

Permalink
Merge pull request #58986 from akien-mga/diraccessref
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Mar 11, 2022
2 parents 1e099af + 768f942 commit 015fdfc
Show file tree
Hide file tree
Showing 28 changed files with 154 additions and 259 deletions.
10 changes: 2 additions & 8 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,14 @@ String ProjectSettings::localize_path(const String &p_path) const {
return p_path.simplify_path();
}

DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
DirAccessRef dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);

String path = p_path.replace("\\", "/").simplify_path();

if (dir->change_dir(path) == OK) {
String cwd = dir->get_current_dir();
cwd = cwd.replace("\\", "/");

memdelete(dir);

// Ensure that we end with a '/'.
// This is important to ensure that we do not wrongly localize the resource path
// in an absolute path that just happens to contain this string but points to a
Expand All @@ -173,8 +171,6 @@ String ProjectSettings::localize_path(const String &p_path) const {

return cwd.replace_first(res_path, "res://");
} else {
memdelete(dir);

int sep = path.rfind("/");
if (sep == -1) {
return "res://" + path;
Expand Down Expand Up @@ -541,7 +537,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b
// Nothing was found, try to find a project file in provided path (`p_path`)
// or, if requested (`p_upwards`) in parent directories.

DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
ERR_FAIL_COND_V_MSG(!d, ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_path + "'.");
d->change_dir(p_path);

Expand Down Expand Up @@ -573,8 +569,6 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b
}
}

memdelete(d);

if (!found) {
return err;
}
Expand Down
34 changes: 10 additions & 24 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1569,21 +1569,17 @@ String Directory::get_current_dir() {
Error Directory::make_dir(String p_dir) {
ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory is not configured properly.");
if (!p_dir.is_relative_path()) {
DirAccess *d = DirAccess::create_for_path(p_dir);
Error err = d->make_dir(p_dir);
memdelete(d);
return err;
DirAccessRef da = DirAccess::create_for_path(p_dir);
return da->make_dir(p_dir);
}
return d->make_dir(p_dir);
}

Error Directory::make_dir_recursive(String p_dir) {
ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory is not configured properly.");
if (!p_dir.is_relative_path()) {
DirAccess *d = DirAccess::create_for_path(p_dir);
Error err = d->make_dir_recursive(p_dir);
memdelete(d);
return err;
DirAccessRef da = DirAccess::create_for_path(p_dir);
return da->make_dir_recursive(p_dir);
}
return d->make_dir_recursive(p_dir);
}
Expand All @@ -1593,19 +1589,14 @@ bool Directory::file_exists(String p_file) {
if (!p_file.is_relative_path()) {
return FileAccess::exists(p_file);
}

return d->file_exists(p_file);
}

bool Directory::dir_exists(String p_dir) {
ERR_FAIL_COND_V_MSG(!d, false, "Directory is not configured properly.");
if (!p_dir.is_relative_path()) {
DirAccess *d = DirAccess::create_for_path(p_dir);
bool exists = d->dir_exists(p_dir);
memdelete(d);
return exists;
return DirAccess::exists(p_dir);
}

return d->dir_exists(p_dir);
}

Expand All @@ -1624,11 +1615,9 @@ Error Directory::rename(String p_from, String p_to) {
ERR_FAIL_COND_V_MSG(p_from.is_empty() || p_from == "." || p_from == "..", ERR_INVALID_PARAMETER, "Invalid path to rename.");

if (!p_from.is_relative_path()) {
DirAccess *d = DirAccess::create_for_path(p_from);
ERR_FAIL_COND_V_MSG(!d->file_exists(p_from) && !d->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist.");
Error err = d->rename(p_from, p_to);
memdelete(d);
return err;
DirAccessRef da = DirAccess::create_for_path(p_from);
ERR_FAIL_COND_V_MSG(!da->file_exists(p_from) && !da->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist.");
return da->rename(p_from, p_to);
}

ERR_FAIL_COND_V_MSG(!d->file_exists(p_from) && !d->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist.");
Expand All @@ -1638,10 +1627,8 @@ Error Directory::rename(String p_from, String p_to) {
Error Directory::remove(String p_name) {
ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
if (!p_name.is_relative_path()) {
DirAccess *d = DirAccess::create_for_path(p_name);
Error err = d->remove(p_name);
memdelete(d);
return err;
DirAccessRef da = DirAccess::create_for_path(p_name);
return da->remove(p_name);
}

return d->remove(p_name);
Expand All @@ -1664,7 +1651,6 @@ void Directory::_bind_methods() {
ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &Directory::make_dir_recursive);
ClassDB::bind_method(D_METHOD("file_exists", "path"), &Directory::file_exists);
ClassDB::bind_method(D_METHOD("dir_exists", "path"), &Directory::dir_exists);
//ClassDB::bind_method(D_METHOD("get_modified_time","file"),&Directory::get_modified_time);
ClassDB::bind_method(D_METHOD("get_space_left"), &Directory::get_space_left);
ClassDB::bind_method(D_METHOD("copy", "from", "to"), &Directory::copy);
ClassDB::bind_method(D_METHOD("rename", "from", "to"), &Directory::rename);
Expand Down
6 changes: 2 additions & 4 deletions core/io/dir_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,6 @@ Error DirAccess::copy_dir(String p_from, String p_to, int p_chmod_flags, bool p_
}

bool DirAccess::exists(String p_dir) {
DirAccess *da = DirAccess::create_for_path(p_dir);
bool valid = da->change_dir(p_dir) == OK;
memdelete(da);
return valid;
DirAccessRef da = DirAccess::create_for_path(p_dir);
return da->change_dir(p_dir) == OK;
}
2 changes: 1 addition & 1 deletion core/io/dir_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ struct DirAccessRef {

operator bool() const { return f != nullptr; }

DirAccess *f;
DirAccess *f = nullptr;

DirAccessRef(DirAccess *fa) { f = fa; }
~DirAccessRef() {
Expand Down
10 changes: 3 additions & 7 deletions core/io/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void RotatedFileLogger::clear_old_backups() {
String basename = base_path.get_file().get_basename();
String extension = base_path.get_extension();

DirAccess *da = DirAccess::open(base_path.get_base_dir());
DirAccessRef da = DirAccess::open(base_path.get_base_dir());
if (!da) {
return;
}
Expand All @@ -152,8 +152,6 @@ void RotatedFileLogger::clear_old_backups() {
da->remove(E->get());
}
}

memdelete(da);
}

void RotatedFileLogger::rotate_file() {
Expand All @@ -167,18 +165,16 @@ void RotatedFileLogger::rotate_file() {
backup_name += "." + base_path.get_extension();
}

DirAccess *da = DirAccess::open(base_path.get_base_dir());
DirAccessRef da = DirAccess::open(base_path.get_base_dir());
if (da) {
da->copy(base_path, backup_name);
memdelete(da);
}
clear_old_backups();
}
} else {
DirAccess *da = DirAccess::create(DirAccess::ACCESS_USERDATA);
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_USERDATA);
if (da) {
da->make_dir_recursive(base_path.get_base_dir());
memdelete(da);
}
}

Expand Down
22 changes: 8 additions & 14 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,6 @@ RES ResourceFormatLoaderBinary::load(const String &p_path, const String &p_origi
String path = !p_original_path.is_empty() ? p_original_path : p_path;
loader.local_path = ProjectSettings::get_singleton()->localize_path(path);
loader.res_path = loader.local_path;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
loader.open(f);

err = loader.load();
Expand Down Expand Up @@ -1086,17 +1085,14 @@ void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<Str
ResourceLoaderBinary loader;
loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
loader.res_path = loader.local_path;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
loader.get_dependencies(f, p_dependencies, p_add_types);
}

Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
//Error error=OK;

FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Cannot open file '" + p_path + "'.");

FileAccess *fw = nullptr; //=FileAccess::open(p_path+".depren");
FileAccess *fw = nullptr;

String local_path = p_path.get_base_dir();

Expand Down Expand Up @@ -1158,10 +1154,12 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons
if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
memdelete(f);
memdelete(fw);
DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
da->remove(p_path + ".depren");
memdelete(da);
//use the old approach
{
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
da->remove(p_path + ".depren");
}

// Use the old approach.

WARN_PRINT("This file is old, so it can't refactor dependencies, opening and resaving '" + p_path + "'.");

Expand All @@ -1174,7 +1172,6 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons
loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
loader.res_path = loader.local_path;
loader.remaps = p_map;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
loader.open(f);

err = loader.load();
Expand Down Expand Up @@ -1304,10 +1301,9 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons
return ERR_CANT_CREATE;
}

DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
da->remove(p_path);
da->rename(p_path + ".depren", p_path);
memdelete(da);
return OK;
}

Expand All @@ -1320,7 +1316,6 @@ String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const
ResourceLoaderBinary loader;
loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
loader.res_path = loader.local_path;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
String r = loader.recognize(f);
return ClassDB::get_compatibility_remapped_class(r);
}
Expand All @@ -1339,7 +1334,6 @@ ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_pat
ResourceLoaderBinary loader;
loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
loader.res_path = loader.local_path;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
loader.open(f, true);
if (loader.error != OK) {
return ResourceUID::INVALID_ID; //could not read
Expand Down
8 changes: 2 additions & 6 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,13 @@ void OS::yield() {

void OS::ensure_user_data_dir() {
String dd = get_user_data_dir();
DirAccess *da = DirAccess::open(dd);
if (da) {
memdelete(da);
if (DirAccess::exists(dd)) {
return;
}

da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
Error err = da->make_dir_recursive(dd);
ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + ".");

memdelete(da);
}

String OS::get_model_name() const {
Expand Down
3 changes: 1 addition & 2 deletions editor/dependency_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,11 @@ void OrphanResourcesDialog::_find_to_delete(TreeItem *p_item, List<String> &path
}

void OrphanResourcesDialog::_delete_confirm() {
DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
for (const String &E : paths) {
da->remove(E);
EditorFileSystem::get_singleton()->update_file(E);
}
memdelete(da);
refresh();
}

Expand Down
4 changes: 1 addition & 3 deletions editor/editor_asset_installer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,8 @@ void EditorAssetInstaller::ok_pressed() {
dirpath = dirpath.substr(0, dirpath.length() - 1);
}

DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
da->make_dir(dirpath);
memdelete(da);

} else {
Vector<uint8_t> data;
data.resize(info.uncompressed_size);
Expand Down
3 changes: 1 addition & 2 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,12 +1094,11 @@ void EditorFileSystem::_delete_internal_files(String p_file) {
if (FileAccess::exists(p_file + ".import")) {
List<String> paths;
ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
for (const String &E : paths) {
da->remove(E);
}
da->remove(p_file + ".import");
memdelete(da);
}
}

Expand Down
4 changes: 1 addition & 3 deletions editor/editor_fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Ref<FontData> load_cached_internal_font(const uint8_t *p_data, size_t p_size, Te
}

void editor_register_fonts(Ref<Theme> p_theme) {
DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
DirAccessRef dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);

/* Custom font */

Expand Down Expand Up @@ -236,8 +236,6 @@ void editor_register_fonts(Ref<Theme> p_theme) {
EditorSettings::get_singleton()->set_manually("interface/editor/code_font", "");
}

memdelete(dir);

/* Noto Sans */

Ref<FontData> DefaultFont = load_cached_internal_font(_font_NotoSans_Regular, _font_NotoSans_Regular_size, font_hinting, font_antialiased, true, font_subpixel_positioning);
Expand Down
9 changes: 3 additions & 6 deletions editor/editor_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ bool EditorSettings::is_dark_theme() {
void EditorSettings::list_text_editor_themes() {
String themes = "Default,Godot 2,Custom";

DirAccess *d = DirAccess::open(get_text_editor_themes_dir());
DirAccessRef d = DirAccess::open(get_text_editor_themes_dir());
if (d) {
List<String> custom_themes;
d->list_dir_begin();
Expand All @@ -1234,7 +1234,6 @@ void EditorSettings::list_text_editor_themes() {
file = d->get_next();
}
d->list_dir_end();
memdelete(d);

custom_themes.sort();
for (const String &E : custom_themes) {
Expand Down Expand Up @@ -1289,10 +1288,9 @@ bool EditorSettings::import_text_editor_theme(String p_file) {
return false;
}

DirAccess *d = DirAccess::open(get_text_editor_themes_dir());
DirAccessRef d = DirAccess::open(get_text_editor_themes_dir());
if (d) {
d->copy(p_file, get_text_editor_themes_dir().plus_file(p_file.get_file()));
memdelete(d);
return true;
}
}
Expand Down Expand Up @@ -1342,7 +1340,7 @@ Vector<String> EditorSettings::get_script_templates(const String &p_extension, c
if (!p_custom_path.is_empty()) {
template_dir = p_custom_path;
}
DirAccess *d = DirAccess::open(template_dir);
DirAccessRef d = DirAccess::open(template_dir);
if (d) {
d->list_dir_begin();
String file = d->get_next();
Expand All @@ -1353,7 +1351,6 @@ Vector<String> EditorSettings::get_script_templates(const String &p_extension, c
file = d->get_next();
}
d->list_dir_end();
memdelete(d);
}
return templates;
}
Expand Down
Loading

0 comments on commit 015fdfc

Please sign in to comment.