| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/object/ref_counted.h" |
| 34 |
#include "core/string/ustring.h" |
| 35 |
#include "core/typedefs.h" |
| 36 |
|
| 37 |
|
| 38 |
class DirAccess : public RefCounted { |
| 39 |
GDCLASS(DirAccess, RefCounted); |
| 40 |
|
| 41 |
public: |
| 42 |
enum AccessType : int32_t { |
| 43 |
ACCESS_RESOURCES, |
| 44 |
ACCESS_USERDATA, |
| 45 |
ACCESS_FILESYSTEM, |
| 46 |
ACCESS_MAX |
| 47 |
}; |
| 48 |
|
| 49 |
typedef Ref<DirAccess> (*CreateFunc)(); |
| 50 |
|
| 51 |
private: |
| 52 |
AccessType _access_type = ACCESS_FILESYSTEM; |
| 53 |
static CreateFunc create_func[ACCESS_MAX]; |
| 54 |
static Ref<DirAccess> _open(const String &p_path); |
| 55 |
|
| 56 |
Error _copy_dir(Ref<DirAccess> &p_target_da, const String &p_to, int p_chmod_flags, bool p_copy_links); |
| 57 |
PackedStringArray _get_contents(bool p_directories); |
| 58 |
|
| 59 |
static inline thread_local Error last_dir_open_error = OK; |
| 60 |
bool include_navigational = false; |
| 61 |
bool include_hidden = false; |
| 62 |
|
| 63 |
bool _is_temp = false; |
| 64 |
bool _temp_keep_after_free = false; |
| 65 |
String _temp_path; |
| 66 |
void _delete_temp(); |
| 67 |
|
| 68 |
static Ref<DirAccess> _create_temp(const String &p_prefix = "", bool p_keep = false); |
| 69 |
|
| 70 |
protected: |
| 71 |
static void _bind_methods(); |
| 72 |
|
| 73 |
String _get_root_path() const; |
| 74 |
virtual String _get_root_string() const; |
| 75 |
|
| 76 |
AccessType get_access_type() const; |
| 77 |
virtual String fix_path(const String &p_path) const; |
| 78 |
|
| 79 |
template <typename T> |
| 80 |
static Ref<DirAccess> _create_builtin() { |
| 81 |
return memnew(T); |
| 82 |
} |
| 83 |
|
| 84 |
public: |
| 85 |
virtual Error list_dir_begin() = 0; |
| 86 |
virtual String get_next() = 0; |
| 87 |
virtual bool current_is_dir() const = 0; |
| 88 |
virtual bool current_is_hidden() const = 0; |
| 89 |
|
| 90 |
virtual void list_dir_end() = 0; |
| 91 |
|
| 92 |
virtual int get_drive_count() = 0; |
| 93 |
virtual String get_drive(int p_drive) = 0; |
| 94 |
virtual int get_current_drive(); |
| 95 |
virtual bool drives_are_shortcuts(); |
| 96 |
|
| 97 |
virtual Error change_dir(String p_dir) = 0; |
| 98 |
virtual String get_current_dir(bool p_include_drive = true) const = 0; |
| 99 |
virtual Error make_dir(String p_dir) = 0; |
| 100 |
virtual Error make_dir_recursive(const String &p_dir); |
| 101 |
virtual Error erase_contents_recursive(); |
| 102 |
|
| 103 |
virtual bool file_exists(String p_file) = 0; |
| 104 |
virtual bool dir_exists(String p_dir) = 0; |
| 105 |
virtual bool is_readable(String p_dir) { return true; } |
| 106 |
virtual bool is_writable(String p_dir) { return true; } |
| 107 |
static bool exists(const String &p_dir); |
| 108 |
virtual uint64_t get_space_left() = 0; |
| 109 |
|
| 110 |
Error copy_dir(const String &p_from, String p_to, int p_chmod_flags = -1, bool p_copy_links = false); |
| 111 |
virtual Error copy(const String &p_from, const String &p_to, int p_chmod_flags = -1); |
| 112 |
virtual Error rename(String p_from, String p_to) = 0; |
| 113 |
virtual Error remove(String p_name) = 0; |
| 114 |
|
| 115 |
virtual bool is_link(String p_file) = 0; |
| 116 |
virtual String read_link(String p_file) = 0; |
| 117 |
virtual Error create_link(String p_source, String p_target) = 0; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static void remove_file_or_error(const String &p_path) { |
| 122 |
Ref<DirAccess> da = create(ACCESS_FILESYSTEM); |
| 123 |
if (da->file_exists(p_path)) { |
| 124 |
if (da->remove(p_path) != OK) { |
| 125 |
ERR_FAIL_MSG(vformat("Cannot remove file or directory: '%s'.", p_path)); |
| 126 |
} |
| 127 |
} else { |
| 128 |
ERR_FAIL_MSG(vformat("Cannot remove non-existent file or directory: '%s'.", p_path)); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
virtual String get_filesystem_type() const = 0; |
| 133 |
static String get_full_path(const String &p_path, AccessType p_access); |
| 134 |
static Ref<DirAccess> create_for_path(const String &p_path); |
| 135 |
|
| 136 |
static Ref<DirAccess> create(AccessType p_access); |
| 137 |
static Error get_open_error(); |
| 138 |
|
| 139 |
template <typename T> |
| 140 |
static void make_default(AccessType p_access) { |
| 141 |
create_func[p_access] = _create_builtin<T>; |
| 142 |
} |
| 143 |
|
| 144 |
static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr); |
| 145 |
static Ref<DirAccess> create_temp(const String &p_prefix = "", bool p_keep = false, Error *r_error = nullptr); |
| 146 |
|
| 147 |
static int _get_drive_count(); |
| 148 |
static String get_drive_name(int p_idx); |
| 149 |
|
| 150 |
static Error make_dir_absolute(const String &p_dir); |
| 151 |
static Error make_dir_recursive_absolute(const String &p_dir); |
| 152 |
static bool dir_exists_absolute(const String &p_dir); |
| 153 |
|
| 154 |
static Error copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags = -1); |
| 155 |
static Error rename_absolute(const String &p_from, const String &p_to); |
| 156 |
static Error remove_absolute(const String &p_path); |
| 157 |
|
| 158 |
PackedStringArray get_files(); |
| 159 |
static PackedStringArray get_files_at(const String &p_path); |
| 160 |
PackedStringArray get_directories(); |
| 161 |
static PackedStringArray get_directories_at(const String &p_path); |
| 162 |
String _get_next(); |
| 163 |
|
| 164 |
void set_include_navigational(bool p_enable); |
| 165 |
bool get_include_navigational() const; |
| 166 |
void set_include_hidden(bool p_enable); |
| 167 |
bool get_include_hidden() const; |
| 168 |
|
| 169 |
virtual bool is_case_sensitive(const String &p_path) const; |
| 170 |
virtual bool is_bundle(const String &p_file) const { return false; } |
| 171 |
virtual bool is_equivalent(const String &p_path_a, const String &p_path_b) const; |
| 172 |
|
| 173 |
public: |
| 174 |
virtual ~DirAccess(); |
| 175 |
}; |