2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* dir_access.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2020-01-01 18:16:22 +08:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 09:10:30 +08:00
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
|
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/typedefs.h"
|
|
|
|
#include "core/ustring.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
|
2014-02-10 09:10:30 +08:00
|
|
|
class DirAccess {
|
|
|
|
public:
|
|
|
|
enum AccessType {
|
|
|
|
ACCESS_RESOURCES,
|
|
|
|
ACCESS_USERDATA,
|
|
|
|
ACCESS_FILESYSTEM,
|
|
|
|
ACCESS_MAX
|
|
|
|
};
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
typedef DirAccess *(*CreateFunc)();
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
AccessType _access_type;
|
|
|
|
static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object
|
2017-10-02 23:01:43 +08:00
|
|
|
|
|
|
|
Error _copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flags);
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
protected:
|
|
|
|
String _get_root_path() const;
|
|
|
|
String _get_root_string() const;
|
|
|
|
|
|
|
|
String fix_path(String p_path) const;
|
|
|
|
bool next_is_dir;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
template <class T>
|
|
|
|
static DirAccess *_create_builtin() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
return memnew(T);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-03-05 23:44:50 +08:00
|
|
|
virtual Error list_dir_begin() = 0; ///< This starts dir listing
|
|
|
|
virtual String get_next() = 0;
|
|
|
|
virtual bool current_is_dir() const = 0;
|
|
|
|
virtual bool current_is_hidden() const = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
virtual void list_dir_end() = 0; ///<
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-03-05 23:44:50 +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
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
virtual Error change_dir(String p_dir) = 0; ///< can be relative or absolute, return false on success
|
2020-02-10 16:19:29 +08:00
|
|
|
virtual String get_current_dir(bool p_include_drive = true) = 0; ///< return current dir location
|
2017-03-05 23:44:50 +08:00
|
|
|
virtual Error make_dir(String p_dir) = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
virtual Error make_dir_recursive(String p_dir);
|
|
|
|
virtual Error erase_contents_recursive(); //super dangerous, use with care!
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
virtual bool file_exists(String p_file) = 0;
|
|
|
|
virtual bool dir_exists(String p_dir) = 0;
|
2015-04-13 03:45:59 +08:00
|
|
|
static bool exists(String p_dir);
|
2017-03-05 23:44:50 +08:00
|
|
|
virtual size_t get_space_left() = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-01-04 12:17:30 +08:00
|
|
|
Error copy_dir(String p_from, String p_to, int p_chmod_flags = -1);
|
|
|
|
virtual Error copy(String p_from, String p_to, int p_chmod_flags = -1);
|
2017-03-05 23:44:50 +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
|
|
|
|
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).
|
|
|
|
static void remove_file_or_error(String p_path) {
|
|
|
|
DirAccess *da = create(ACCESS_FILESYSTEM);
|
|
|
|
if (da->file_exists(p_path)) {
|
|
|
|
if (da->remove(p_path) != OK) {
|
|
|
|
ERR_FAIL_MSG("Cannot remove file or directory: " + p_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memdelete(da);
|
|
|
|
}
|
|
|
|
|
2019-01-27 05:35:31 +08:00
|
|
|
virtual String get_filesystem_type() const = 0;
|
2017-03-05 23:44:50 +08:00
|
|
|
static String get_full_path(const String &p_path, AccessType p_access);
|
|
|
|
static DirAccess *create_for_path(const String &p_path);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
/*
|
2014-02-10 09:10:30 +08:00
|
|
|
enum DirType {
|
|
|
|
|
|
|
|
FILE_TYPE_INVALID,
|
|
|
|
FILE_TYPE_FILE,
|
|
|
|
FILE_TYPE_DIR,
|
|
|
|
};
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
//virtual DirType get_file_type() const=0;
|
|
|
|
*/
|
|
|
|
static DirAccess *create(AccessType p_access);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
template <class T>
|
2014-02-10 09:10:30 +08:00
|
|
|
static void make_default(AccessType p_access) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
create_func[p_access] = _create_builtin<T>;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
static DirAccess *open(const String &p_path, Error *r_error = NULL);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
DirAccess();
|
|
|
|
virtual ~DirAccess();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DirAccessRef {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_FORCE_INLINE_ DirAccess *operator->() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
operator bool() const { return f != NULL; }
|
2014-02-10 09:10:30 +08:00
|
|
|
DirAccess *f;
|
|
|
|
DirAccessRef(DirAccess *fa) { f = fa; }
|
2017-03-05 23:44:50 +08:00
|
|
|
~DirAccessRef() {
|
|
|
|
if (f) memdelete(f);
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|