| 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/io/compression.h" |
| 34 |
#include "core/math/math_defs.h" |
| 35 |
#include "core/object/ref_counted.h" |
| 36 |
#include "core/os/memory.h" |
| 37 |
#include "core/string/ustring.h" |
| 38 |
#include "core/typedefs.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class FileAccess : public RefCounted { |
| 45 |
GDCLASS(FileAccess, RefCounted); |
| 46 |
|
| 47 |
public: |
| 48 |
enum AccessType : int32_t { |
| 49 |
ACCESS_RESOURCES, |
| 50 |
ACCESS_USERDATA, |
| 51 |
ACCESS_FILESYSTEM, |
| 52 |
ACCESS_PIPE, |
| 53 |
ACCESS_MAX |
| 54 |
}; |
| 55 |
|
| 56 |
enum ModeFlags : int32_t { |
| 57 |
READ = 1, |
| 58 |
WRITE = 2, |
| 59 |
READ_WRITE = 3, |
| 60 |
WRITE_READ = 7, |
| 61 |
SKIP_PACK = 16, |
| 62 |
}; |
| 63 |
|
| 64 |
enum UnixPermissionFlags : int32_t { |
| 65 |
UNIX_EXECUTE_OTHER = 0x001, |
| 66 |
UNIX_WRITE_OTHER = 0x002, |
| 67 |
UNIX_READ_OTHER = 0x004, |
| 68 |
UNIX_EXECUTE_GROUP = 0x008, |
| 69 |
UNIX_WRITE_GROUP = 0x010, |
| 70 |
UNIX_READ_GROUP = 0x020, |
| 71 |
UNIX_EXECUTE_OWNER = 0x040, |
| 72 |
UNIX_WRITE_OWNER = 0x080, |
| 73 |
UNIX_READ_OWNER = 0x100, |
| 74 |
UNIX_RESTRICTED_DELETE = 0x200, |
| 75 |
UNIX_SET_GROUP_ID = 0x400, |
| 76 |
UNIX_SET_USER_ID = 0x800, |
| 77 |
}; |
| 78 |
|
| 79 |
enum CompressionMode : int32_t { |
| 80 |
COMPRESSION_FASTLZ = Compression::MODE_FASTLZ, |
| 81 |
COMPRESSION_DEFLATE = Compression::MODE_DEFLATE, |
| 82 |
COMPRESSION_ZSTD = Compression::MODE_ZSTD, |
| 83 |
COMPRESSION_GZIP = Compression::MODE_GZIP, |
| 84 |
COMPRESSION_BROTLI = Compression::MODE_BROTLI, |
| 85 |
}; |
| 86 |
|
| 87 |
typedef void (*FileCloseFailNotify)(const String &); |
| 88 |
|
| 89 |
typedef Ref<FileAccess> (*CreateFunc)(); |
| 90 |
#ifdef BIG_ENDIAN_ENABLED |
| 91 |
bool big_endian = true; |
| 92 |
#else |
| 93 |
bool big_endian = false; |
| 94 |
#endif |
| 95 |
bool real_is_double = false; |
| 96 |
|
| 97 |
virtual BitField<UnixPermissionFlags> _get_unix_permissions(const String &p_file) = 0; |
| 98 |
virtual Error _set_unix_permissions(const String &p_file, BitField<UnixPermissionFlags> p_permissions) = 0; |
| 99 |
|
| 100 |
virtual bool _get_hidden_attribute(const String &p_file) = 0; |
| 101 |
virtual Error _set_hidden_attribute(const String &p_file, bool p_hidden) = 0; |
| 102 |
virtual bool _get_read_only_attribute(const String &p_file) = 0; |
| 103 |
virtual Error _set_read_only_attribute(const String &p_file, bool p_ro) = 0; |
| 104 |
|
| 105 |
virtual PackedByteArray _get_extended_attribute(const String &p_file, const String &p_attribute_name) { return PackedByteArray(); } |
| 106 |
virtual Error _set_extended_attribute(const String &p_file, const String &p_attribute_name, const PackedByteArray &p_data) { return ERR_UNAVAILABLE; } |
| 107 |
virtual Error _remove_extended_attribute(const String &p_file, const String &p_attribute_name) { return ERR_UNAVAILABLE; } |
| 108 |
virtual PackedStringArray _get_extended_attributes_list(const String &p_file) { return PackedStringArray(); } |
| 109 |
|
| 110 |
protected: |
| 111 |
static void _bind_methods(); |
| 112 |
|
| 113 |
friend class Main; |
| 114 |
AccessType get_access_type() const; |
| 115 |
virtual String fix_path(const String &p_path) const; |
| 116 |
virtual Error open_internal(const String &p_path, int p_mode_flags) = 0; |
| 117 |
virtual uint64_t _get_modified_time(const String &p_file) = 0; |
| 118 |
virtual uint64_t _get_access_time(const String &p_file) = 0; |
| 119 |
virtual int64_t _get_size(const String &p_file) = 0; |
| 120 |
virtual void _set_access_type(AccessType p_access); |
| 121 |
|
| 122 |
static inline FileCloseFailNotify close_fail_notify = nullptr; |
| 123 |
|
| 124 |
#ifndef DISABLE_DEPRECATED |
| 125 |
static Ref<FileAccess> _open_encrypted_bind_compat_98918(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key); |
| 126 |
static Ref<FileAccess> _create_temp_compat_114053(int p_mode_flags, const String &p_prefix = "", const String &p_extension = "", bool p_keep = false); |
| 127 |
|
| 128 |
void store_8_bind_compat_78289(uint8_t p_dest); |
| 129 |
void store_16_bind_compat_78289(uint16_t p_dest); |
| 130 |
void store_32_bind_compat_78289(uint32_t p_dest); |
| 131 |
void store_64_bind_compat_78289(uint64_t p_dest); |
| 132 |
void store_buffer_bind_compat_78289(const Vector<uint8_t> &p_buffer); |
| 133 |
void store_var_bind_compat_78289(const Variant &p_var, bool p_full_objects = false); |
| 134 |
void store_half_bind_compat_78289(float p_dest); |
| 135 |
void store_float_bind_compat_78289(float p_dest); |
| 136 |
void store_double_bind_compat_78289(double p_dest); |
| 137 |
void store_real_bind_compat_78289(real_t p_real); |
| 138 |
void store_string_bind_compat_78289(const String &p_string); |
| 139 |
void store_line_bind_compat_78289(const String &p_line); |
| 140 |
void store_csv_line_bind_compat_78289(const Vector<String> &p_values, const String &p_delim = ","); |
| 141 |
void store_pascal_string_bind_compat_78289(const String &p_string); |
| 142 |
String get_as_text_bind_compat_110867(bool p_skip_cr) const; |
| 143 |
|
| 144 |
static void _bind_compatibility_methods(); |
| 145 |
#endif |
| 146 |
|
| 147 |
private: |
| 148 |
static inline bool backup_save = false; |
| 149 |
static inline thread_local Error last_file_open_error = OK; |
| 150 |
|
| 151 |
AccessType _access_type = ACCESS_FILESYSTEM; |
| 152 |
static inline CreateFunc create_func[ACCESS_MAX]; |
| 153 |
template <typename T> |
| 154 |
static Ref<FileAccess> _create_builtin() { |
| 155 |
return memnew(T); |
| 156 |
} |
| 157 |
|
| 158 |
static Ref<FileAccess> _open(const String &p_path, ModeFlags p_mode_flags); |
| 159 |
|
| 160 |
bool _is_temp_file = false; |
| 161 |
bool _temp_keep_after_use = false; |
| 162 |
String _temp_path; |
| 163 |
void _delete_temp(); |
| 164 |
|
| 165 |
static Ref<FileAccess> _create_temp(ModeFlags p_mode_flags, const String &p_prefix = "", const String &p_extension = "", bool p_keep = false); |
| 166 |
|
| 167 |
public: |
| 168 |
static void set_file_close_fail_notify_callback(FileCloseFailNotify p_cbk) { close_fail_notify = p_cbk; } |
| 169 |
|
| 170 |
virtual bool is_open() const = 0; |
| 171 |
|
| 172 |
virtual String get_path() const { return ""; } |
| 173 |
virtual String get_path_absolute() const { return ""; } |
| 174 |
|
| 175 |
virtual void seek(uint64_t p_position) = 0; |
| 176 |
virtual void seek_end(int64_t p_position = 0) = 0; |
| 177 |
virtual uint64_t get_position() const = 0; |
| 178 |
virtual uint64_t get_length() const = 0; |
| 179 |
|
| 180 |
virtual bool eof_reached() const = 0; |
| 181 |
|
| 182 |
virtual uint8_t get_8() const; |
| 183 |
virtual uint16_t get_16() const; |
| 184 |
virtual uint32_t get_32() const; |
| 185 |
virtual uint64_t get_64() const; |
| 186 |
|
| 187 |
virtual float get_half() const; |
| 188 |
virtual float get_float() const; |
| 189 |
virtual double get_double() const; |
| 190 |
virtual real_t get_real() const; |
| 191 |
|
| 192 |
Variant get_var(bool p_allow_objects = false) const; |
| 193 |
|
| 194 |
virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const = 0; |
| 195 |
Vector<uint8_t> get_buffer(int64_t p_length) const; |
| 196 |
virtual String get_line() const; |
| 197 |
virtual String get_token() const; |
| 198 |
virtual Vector<String> get_csv_line(const String &p_delim = ",") const; |
| 199 |
String get_as_text() const; |
| 200 |
virtual String get_as_utf8_string() const; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
virtual void set_big_endian(bool p_big_endian) { big_endian = p_big_endian; } |
| 209 |
inline bool is_big_endian() const { return big_endian; } |
| 210 |
|
| 211 |
virtual Error get_error() const = 0; |
| 212 |
|
| 213 |
virtual Error resize(int64_t p_length) = 0; |
| 214 |
virtual void flush() = 0; |
| 215 |
virtual bool store_8(uint8_t p_dest); |
| 216 |
virtual bool store_16(uint16_t p_dest); |
| 217 |
virtual bool store_32(uint32_t p_dest); |
| 218 |
virtual bool store_64(uint64_t p_dest); |
| 219 |
|
| 220 |
virtual bool store_half(float p_dest); |
| 221 |
virtual bool store_float(float p_dest); |
| 222 |
virtual bool store_double(double p_dest); |
| 223 |
virtual bool store_real(real_t p_real); |
| 224 |
|
| 225 |
virtual bool store_string(const String &p_string); |
| 226 |
virtual bool store_line(const String &p_line); |
| 227 |
virtual bool store_csv_line(const Vector<String> &p_values, const String &p_delim = ","); |
| 228 |
|
| 229 |
virtual bool store_pascal_string(const String &p_string); |
| 230 |
virtual String get_pascal_string(); |
| 231 |
|
| 232 |
virtual bool store_buffer(const uint8_t *p_src, uint64_t p_length) = 0; |
| 233 |
bool store_buffer(const Vector<uint8_t> &p_buffer); |
| 234 |
|
| 235 |
bool store_var(const Variant &p_var, bool p_full_objects = false); |
| 236 |
|
| 237 |
virtual void close() = 0; |
| 238 |
|
| 239 |
virtual bool file_exists(const String &p_name) = 0; |
| 240 |
|
| 241 |
virtual Error reopen(const String &p_path, int p_mode_flags); |
| 242 |
|
| 243 |
static Ref<FileAccess> create(AccessType p_access); |
| 244 |
static Ref<FileAccess> create_for_path(const String &p_path); |
| 245 |
static Ref<FileAccess> open(const String &p_path, int p_mode_flags, Error *r_error = nullptr); |
| 246 |
static Ref<FileAccess> create_temp(ModeFlags p_mode_flags, const String &p_prefix = "", const String &p_extension = "", bool p_keep = false, Error *r_error = nullptr); |
| 247 |
|
| 248 |
static Ref<FileAccess> open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key, const Vector<uint8_t> &p_iv = Vector<uint8_t>()); |
| 249 |
static Ref<FileAccess> open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass); |
| 250 |
static Ref<FileAccess> open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode = COMPRESSION_FASTLZ); |
| 251 |
static Error get_open_error(); |
| 252 |
|
| 253 |
static CreateFunc get_create_func(AccessType p_access); |
| 254 |
static bool exists(const String &p_name); |
| 255 |
static uint64_t get_modified_time(const String &p_file); |
| 256 |
static uint64_t get_access_time(const String &p_file); |
| 257 |
static int64_t get_size(const String &p_file); |
| 258 |
static BitField<FileAccess::UnixPermissionFlags> get_unix_permissions(const String &p_file); |
| 259 |
static Error set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions); |
| 260 |
|
| 261 |
static bool get_hidden_attribute(const String &p_file); |
| 262 |
static Error set_hidden_attribute(const String &p_file, bool p_hidden); |
| 263 |
static bool get_read_only_attribute(const String &p_file); |
| 264 |
static Error set_read_only_attribute(const String &p_file, bool p_ro); |
| 265 |
|
| 266 |
static PackedByteArray get_extended_attribute(const String &p_file, const String &p_attribute_name); |
| 267 |
static String get_extended_attribute_string(const String &p_file, const String &p_attribute_name); |
| 268 |
static Error set_extended_attribute(const String &p_file, const String &p_attribute_name, const PackedByteArray &p_data); |
| 269 |
static Error set_extended_attribute_string(const String &p_file, const String &p_attribute_name, const String &p_data); |
| 270 |
static Error remove_extended_attribute(const String &p_file, const String &p_attribute_name); |
| 271 |
static PackedStringArray get_extended_attributes_list(const String &p_file); |
| 272 |
|
| 273 |
static void set_backup_save(bool p_enable) { backup_save = p_enable; } |
| 274 |
static bool is_backup_save_enabled() { return backup_save; } |
| 275 |
|
| 276 |
static String get_md5(const String &p_file); |
| 277 |
static String get_sha256(const String &p_file); |
| 278 |
static String get_multiple_md5(const Vector<String> &p_file); |
| 279 |
|
| 280 |
static Vector<uint8_t> get_file_as_bytes(const String &p_path, Error *r_error = nullptr); |
| 281 |
static String get_file_as_string(const String &p_path, Error *r_error = nullptr); |
| 282 |
|
| 283 |
static PackedByteArray _get_file_as_bytes(const String &p_path) { return get_file_as_bytes(p_path, &last_file_open_error); } |
| 284 |
static String _get_file_as_string(const String &p_path) { return get_file_as_string(p_path, &last_file_open_error); } |
| 285 |
|
| 286 |
template <typename T> |
| 287 |
static void make_default(AccessType p_access) { |
| 288 |
create_func[p_access] = _create_builtin<T>; |
| 289 |
} |
| 290 |
|
| 291 |
public: |
| 292 |
virtual ~FileAccess(); |
| 293 |
}; |
| 294 |
|
| 295 |
VARIANT_ENUM_CAST(FileAccess::CompressionMode); |
| 296 |
VARIANT_ENUM_CAST(FileAccess::ModeFlags); |
| 297 |
VARIANT_BITFIELD_CAST(FileAccess::UnixPermissionFlags); |