2014-02-10 09:10:30 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* dir_access.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#ifndef DIR_ACCESS_H
|
|
|
|
#define DIR_ACCESS_H
|
|
|
|
|
2022-03-23 17:08:58 +08:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/string/ustring.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/typedefs.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-09-13 09:38:39 +08:00
|
|
|
//@ TODO, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies
|
2022-03-23 17:08:58 +08:00
|
|
|
class DirAccess : public RefCounted {
|
2022-09-05 19:01:31 +08:00
|
|
|
GDCLASS(DirAccess, RefCounted);
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
public:
|
|
|
|
enum AccessType {
|
|
|
|
ACCESS_RESOURCES,
|
|
|
|
ACCESS_USERDATA,
|
|
|
|
ACCESS_FILESYSTEM,
|
|
|
|
ACCESS_MAX
|
|
|
|
};
|
|
|
|
|
2022-03-23 17:08:58 +08:00
|
|
|
typedef Ref<DirAccess> (*CreateFunc)();
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
private:
|
2020-05-12 23:01:17 +08:00
|
|
|
AccessType _access_type = ACCESS_FILESYSTEM;
|
2014-02-10 09:10:30 +08:00
|
|
|
static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object
|
2022-09-27 23:35:52 +08:00
|
|
|
static Ref<DirAccess> _open(const String &p_path);
|
2017-10-02 23:01:43 +08:00
|
|
|
|
2024-01-09 09:36:19 +08:00
|
|
|
Error _copy_dir(Ref<DirAccess> &p_target_da, const String &p_to, int p_chmod_flags, bool p_copy_links);
|
2022-09-27 23:35:52 +08:00
|
|
|
PackedStringArray _get_contents(bool p_directories);
|
2017-10-02 23:01:43 +08:00
|
|
|
|
2022-09-05 19:01:31 +08:00
|
|
|
thread_local static Error last_dir_open_error;
|
|
|
|
bool include_navigational = false;
|
|
|
|
bool include_hidden = false;
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
protected:
|
2022-09-05 19:01:31 +08:00
|
|
|
static void _bind_methods();
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
String _get_root_path() const;
|
2022-08-31 01:00:03 +08:00
|
|
|
virtual String _get_root_string() const;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
AccessType get_access_type() const;
|
2024-01-09 09:36:19 +08:00
|
|
|
virtual String fix_path(const String &p_path) const;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2022-03-23 17:08:58 +08:00
|
|
|
static Ref<DirAccess> _create_builtin() {
|
2014-02-10 09:10:30 +08:00
|
|
|
return memnew(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-01-14 20:16:41 +08:00
|
|
|
virtual Error list_dir_begin() = 0; ///< This starts dir listing
|
2014-02-10 09:10:30 +08:00
|
|
|
virtual String get_next() = 0;
|
|
|
|
virtual bool current_is_dir() const = 0;
|
2015-03-22 01:33:32 +08:00
|
|
|
virtual bool current_is_hidden() const = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2016-03-09 07:00:52 +08:00
|
|
|
virtual void list_dir_end() = 0; ///<
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
virtual int get_drive_count() = 0;
|
|
|
|
virtual String get_drive(int p_drive) = 0;
|
2015-05-20 08:36:35 +08:00
|
|
|
virtual int get_current_drive();
|
2020-02-10 16:19:29 +08:00
|
|
|
virtual bool drives_are_shortcuts();
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
virtual Error change_dir(String p_dir) = 0; ///< can be relative or absolute, return false on success
|
2022-03-23 17:08:58 +08:00
|
|
|
virtual String get_current_dir(bool p_include_drive = true) const = 0; ///< return current dir location
|
2014-02-10 09:10:30 +08:00
|
|
|
virtual Error make_dir(String p_dir) = 0;
|
2024-01-09 09:36:19 +08:00
|
|
|
virtual Error make_dir_recursive(const String &p_dir);
|
2014-02-10 09:10:30 +08:00
|
|
|
virtual Error erase_contents_recursive(); //super dangerous, use with care!
|
|
|
|
|
|
|
|
virtual bool file_exists(String p_file) = 0;
|
2014-05-25 11:34:51 +08:00
|
|
|
virtual bool dir_exists(String p_dir) = 0;
|
2021-03-19 20:01:03 +08:00
|
|
|
virtual bool is_readable(String p_dir) { return true; };
|
|
|
|
virtual bool is_writable(String p_dir) { return true; };
|
2024-01-09 09:36:19 +08:00
|
|
|
static bool exists(const String &p_dir);
|
2019-03-27 01:51:13 +08:00
|
|
|
virtual uint64_t get_space_left() = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2024-01-09 09:36:19 +08:00
|
|
|
Error copy_dir(const String &p_from, String p_to, int p_chmod_flags = -1, bool p_copy_links = false);
|
|
|
|
virtual Error copy(const String &p_from, const String &p_to, int p_chmod_flags = -1);
|
2014-02-10 09:10:30 +08:00
|
|
|
virtual Error rename(String p_from, String p_to) = 0;
|
|
|
|
virtual Error remove(String p_name) = 0;
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2021-03-10 18:55:31 +08:00
|
|
|
virtual bool is_link(String p_file) = 0;
|
|
|
|
virtual String read_link(String p_file) = 0;
|
|
|
|
virtual Error create_link(String p_source, String p_target) = 0;
|
|
|
|
|
2019-08-09 19:45:30 +08:00
|
|
|
// Meant for editor code when we want to quickly remove a file without custom
|
|
|
|
// handling (e.g. removing a cache file).
|
2024-01-09 09:36:19 +08:00
|
|
|
static void remove_file_or_error(const String &p_path) {
|
2022-03-23 17:08:58 +08:00
|
|
|
Ref<DirAccess> da = create(ACCESS_FILESYSTEM);
|
2019-08-09 19:45:30 +08:00
|
|
|
if (da->file_exists(p_path)) {
|
|
|
|
if (da->remove(p_path) != OK) {
|
|
|
|
ERR_FAIL_MSG("Cannot remove file or directory: " + p_path);
|
|
|
|
}
|
2022-07-31 19:15:02 +08:00
|
|
|
} else {
|
|
|
|
ERR_FAIL_MSG("Cannot remove non-existent file or directory: " + p_path);
|
2019-08-09 19:45:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 05:35:31 +08:00
|
|
|
virtual String get_filesystem_type() const = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
static String get_full_path(const String &p_path, AccessType p_access);
|
2022-03-23 17:08:58 +08:00
|
|
|
static Ref<DirAccess> create_for_path(const String &p_path);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2022-03-23 17:08:58 +08:00
|
|
|
static Ref<DirAccess> create(AccessType p_access);
|
2022-09-05 19:01:31 +08:00
|
|
|
static Error get_open_error();
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void make_default(AccessType p_access) {
|
|
|
|
create_func[p_access] = _create_builtin<T>;
|
|
|
|
}
|
|
|
|
|
2022-03-23 17:08:58 +08:00
|
|
|
static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr);
|
2022-09-05 19:01:31 +08:00
|
|
|
|
2022-04-21 19:10:36 +08:00
|
|
|
static int _get_drive_count();
|
|
|
|
static String get_drive_name(int p_idx);
|
|
|
|
|
|
|
|
static Error make_dir_absolute(const String &p_dir);
|
|
|
|
static Error make_dir_recursive_absolute(const String &p_dir);
|
|
|
|
static bool dir_exists_absolute(const String &p_dir);
|
|
|
|
|
|
|
|
static Error copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags = -1);
|
|
|
|
static Error rename_absolute(const String &p_from, const String &p_to);
|
|
|
|
static Error remove_absolute(const String &p_path);
|
|
|
|
|
2022-09-05 19:01:31 +08:00
|
|
|
PackedStringArray get_files();
|
2022-04-21 19:10:36 +08:00
|
|
|
static PackedStringArray get_files_at(const String &p_path);
|
2022-09-05 19:01:31 +08:00
|
|
|
PackedStringArray get_directories();
|
2022-04-21 19:10:36 +08:00
|
|
|
static PackedStringArray get_directories_at(const String &p_path);
|
2022-09-05 19:01:31 +08:00
|
|
|
String _get_next();
|
|
|
|
|
|
|
|
void set_include_navigational(bool p_enable);
|
|
|
|
bool get_include_navigational() const;
|
|
|
|
void set_include_hidden(bool p_enable);
|
|
|
|
bool get_include_hidden() const;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2023-10-07 19:36:41 +08:00
|
|
|
virtual bool is_case_sensitive(const String &p_path) const;
|
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
DirAccess() {}
|
|
|
|
virtual ~DirAccess() {}
|
2014-02-10 09:10:30 +08:00
|
|
|
};
|
|
|
|
|
2020-03-25 18:10:34 +08:00
|
|
|
#endif // DIR_ACCESS_H
|