file_access.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**************************************************************************/
  2. /* file_access.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef FILE_ACCESS_H
  31. #define FILE_ACCESS_H
  32. #include "core/io/compression.h"
  33. #include "core/math/math_defs.h"
  34. #include "core/object/ref_counted.h"
  35. #include "core/os/memory.h"
  36. #include "core/string/ustring.h"
  37. #include "core/typedefs.h"
  38. /**
  39. * Multi-Platform abstraction for accessing to files.
  40. */
  41. class FileAccess : public RefCounted {
  42. GDCLASS(FileAccess, RefCounted);
  43. public:
  44. enum AccessType : int32_t {
  45. ACCESS_RESOURCES,
  46. ACCESS_USERDATA,
  47. ACCESS_FILESYSTEM,
  48. ACCESS_PIPE,
  49. ACCESS_MAX
  50. };
  51. enum ModeFlags : int32_t {
  52. READ = 1,
  53. WRITE = 2,
  54. READ_WRITE = 3,
  55. WRITE_READ = 7,
  56. };
  57. enum UnixPermissionFlags : int32_t {
  58. UNIX_EXECUTE_OTHER = 0x001,
  59. UNIX_WRITE_OTHER = 0x002,
  60. UNIX_READ_OTHER = 0x004,
  61. UNIX_EXECUTE_GROUP = 0x008,
  62. UNIX_WRITE_GROUP = 0x010,
  63. UNIX_READ_GROUP = 0x020,
  64. UNIX_EXECUTE_OWNER = 0x040,
  65. UNIX_WRITE_OWNER = 0x080,
  66. UNIX_READ_OWNER = 0x100,
  67. UNIX_RESTRICTED_DELETE = 0x200,
  68. UNIX_SET_GROUP_ID = 0x400,
  69. UNIX_SET_USER_ID = 0x800,
  70. };
  71. enum CompressionMode : int32_t {
  72. COMPRESSION_FASTLZ = Compression::MODE_FASTLZ,
  73. COMPRESSION_DEFLATE = Compression::MODE_DEFLATE,
  74. COMPRESSION_ZSTD = Compression::MODE_ZSTD,
  75. COMPRESSION_GZIP = Compression::MODE_GZIP,
  76. COMPRESSION_BROTLI = Compression::MODE_BROTLI,
  77. };
  78. typedef void (*FileCloseFailNotify)(const String &);
  79. typedef Ref<FileAccess> (*CreateFunc)();
  80. bool big_endian = false;
  81. bool real_is_double = false;
  82. virtual BitField<UnixPermissionFlags> _get_unix_permissions(const String &p_file) = 0;
  83. virtual Error _set_unix_permissions(const String &p_file, BitField<UnixPermissionFlags> p_permissions) = 0;
  84. virtual bool _get_hidden_attribute(const String &p_file) = 0;
  85. virtual Error _set_hidden_attribute(const String &p_file, bool p_hidden) = 0;
  86. virtual bool _get_read_only_attribute(const String &p_file) = 0;
  87. virtual Error _set_read_only_attribute(const String &p_file, bool p_ro) = 0;
  88. protected:
  89. static void _bind_methods();
  90. AccessType get_access_type() const;
  91. virtual String fix_path(const String &p_path) const;
  92. virtual Error open_internal(const String &p_path, int p_mode_flags) = 0; ///< open a file
  93. virtual uint64_t _get_modified_time(const String &p_file) = 0;
  94. virtual void _set_access_type(AccessType p_access);
  95. static FileCloseFailNotify close_fail_notify;
  96. #ifndef DISABLE_DEPRECATED
  97. static Ref<FileAccess> _open_encrypted_bind_compat_98918(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key);
  98. void store_8_bind_compat_78289(uint8_t p_dest);
  99. void store_16_bind_compat_78289(uint16_t p_dest);
  100. void store_32_bind_compat_78289(uint32_t p_dest);
  101. void store_64_bind_compat_78289(uint64_t p_dest);
  102. void store_buffer_bind_compat_78289(const Vector<uint8_t> &p_buffer);
  103. void store_var_bind_compat_78289(const Variant &p_var, bool p_full_objects = false);
  104. void store_half_bind_compat_78289(float p_dest);
  105. void store_float_bind_compat_78289(float p_dest);
  106. void store_double_bind_compat_78289(double p_dest);
  107. void store_real_bind_compat_78289(real_t p_real);
  108. void store_string_bind_compat_78289(const String &p_string);
  109. void store_line_bind_compat_78289(const String &p_line);
  110. void store_csv_line_bind_compat_78289(const Vector<String> &p_values, const String &p_delim = ",");
  111. void store_pascal_string_bind_compat_78289(const String &p_string);
  112. static void _bind_compatibility_methods();
  113. #endif
  114. private:
  115. static bool backup_save;
  116. thread_local static Error last_file_open_error;
  117. AccessType _access_type = ACCESS_FILESYSTEM;
  118. static CreateFunc create_func[ACCESS_MAX]; /** default file access creation function for a platform */
  119. template <typename T>
  120. static Ref<FileAccess> _create_builtin() {
  121. return memnew(T);
  122. }
  123. static Ref<FileAccess> _open(const String &p_path, ModeFlags p_mode_flags);
  124. bool _is_temp_file = false;
  125. bool _temp_keep_after_use = false;
  126. String _temp_path;
  127. void _delete_temp();
  128. static Ref<FileAccess> _create_temp(int p_mode_flags, const String &p_prefix = "", const String &p_extension = "", bool p_keep = false);
  129. public:
  130. static void set_file_close_fail_notify_callback(FileCloseFailNotify p_cbk) { close_fail_notify = p_cbk; }
  131. virtual bool is_open() const = 0; ///< true when file is open
  132. virtual String get_path() const { return ""; } /// returns the path for the current open file
  133. virtual String get_path_absolute() const { return ""; } /// returns the absolute path for the current open file
  134. virtual void seek(uint64_t p_position) = 0; ///< seek to a given position
  135. virtual void seek_end(int64_t p_position = 0) = 0; ///< seek from the end of file with negative offset
  136. virtual uint64_t get_position() const = 0; ///< get position in the file
  137. virtual uint64_t get_length() const = 0; ///< get size of the file
  138. virtual bool eof_reached() const = 0; ///< reading passed EOF
  139. virtual uint8_t get_8() const; ///< get a byte
  140. virtual uint16_t get_16() const; ///< get 16 bits uint
  141. virtual uint32_t get_32() const; ///< get 32 bits uint
  142. virtual uint64_t get_64() const; ///< get 64 bits uint
  143. virtual float get_half() const;
  144. virtual float get_float() const;
  145. virtual double get_double() const;
  146. virtual real_t get_real() const;
  147. Variant get_var(bool p_allow_objects = false) const;
  148. virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const = 0; ///< get an array of bytes, needs to be overwritten by children.
  149. Vector<uint8_t> get_buffer(int64_t p_length) const;
  150. virtual String get_line() const;
  151. virtual String get_token() const;
  152. virtual Vector<String> get_csv_line(const String &p_delim = ",") const;
  153. String get_as_text(bool p_skip_cr = false) const;
  154. virtual String get_as_utf8_string(bool p_skip_cr = false) const;
  155. /**
  156. * Use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  157. * It's not about the current CPU type but file formats.
  158. * This flag gets reset to `false` (little endian) on each open.
  159. */
  160. virtual void set_big_endian(bool p_big_endian) { big_endian = p_big_endian; }
  161. inline bool is_big_endian() const { return big_endian; }
  162. virtual Error get_error() const = 0; ///< get last error
  163. virtual Error resize(int64_t p_length) = 0;
  164. virtual void flush() = 0;
  165. virtual bool store_8(uint8_t p_dest); ///< store a byte
  166. virtual bool store_16(uint16_t p_dest); ///< store 16 bits uint
  167. virtual bool store_32(uint32_t p_dest); ///< store 32 bits uint
  168. virtual bool store_64(uint64_t p_dest); ///< store 64 bits uint
  169. virtual bool store_half(float p_dest);
  170. virtual bool store_float(float p_dest);
  171. virtual bool store_double(double p_dest);
  172. virtual bool store_real(real_t p_real);
  173. virtual bool store_string(const String &p_string);
  174. virtual bool store_line(const String &p_line);
  175. virtual bool store_csv_line(const Vector<String> &p_values, const String &p_delim = ",");
  176. virtual bool store_pascal_string(const String &p_string);
  177. virtual String get_pascal_string();
  178. virtual bool store_buffer(const uint8_t *p_src, uint64_t p_length) = 0; ///< store an array of bytes, needs to be overwritten by children.
  179. bool store_buffer(const Vector<uint8_t> &p_buffer);
  180. bool store_var(const Variant &p_var, bool p_full_objects = false);
  181. virtual void close() = 0;
  182. virtual bool file_exists(const String &p_name) = 0; ///< return true if a file exists
  183. virtual Error reopen(const String &p_path, int p_mode_flags); ///< does not change the AccessType
  184. static Ref<FileAccess> create(AccessType p_access); /// Create a file access (for the current platform) this is the only portable way of accessing files.
  185. static Ref<FileAccess> create_for_path(const String &p_path);
  186. static Ref<FileAccess> open(const String &p_path, int p_mode_flags, Error *r_error = nullptr); /// Create a file access (for the current platform) this is the only portable way of accessing files.
  187. static Ref<FileAccess> create_temp(int p_mode_flags, const String &p_prefix = "", const String &p_extension = "", bool p_keep = false, Error *r_error = nullptr);
  188. 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>());
  189. static Ref<FileAccess> open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass);
  190. static Ref<FileAccess> open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode = COMPRESSION_FASTLZ);
  191. static Error get_open_error();
  192. static CreateFunc get_create_func(AccessType p_access);
  193. static bool exists(const String &p_name); ///< return true if a file exists
  194. static uint64_t get_modified_time(const String &p_file);
  195. static BitField<FileAccess::UnixPermissionFlags> get_unix_permissions(const String &p_file);
  196. static Error set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions);
  197. static bool get_hidden_attribute(const String &p_file);
  198. static Error set_hidden_attribute(const String &p_file, bool p_hidden);
  199. static bool get_read_only_attribute(const String &p_file);
  200. static Error set_read_only_attribute(const String &p_file, bool p_ro);
  201. static void set_backup_save(bool p_enable) { backup_save = p_enable; }
  202. static bool is_backup_save_enabled() { return backup_save; }
  203. static String get_md5(const String &p_file);
  204. static String get_sha256(const String &p_file);
  205. static String get_multiple_md5(const Vector<String> &p_file);
  206. static Vector<uint8_t> get_file_as_bytes(const String &p_path, Error *r_error = nullptr);
  207. static String get_file_as_string(const String &p_path, Error *r_error = nullptr);
  208. static PackedByteArray _get_file_as_bytes(const String &p_path) { return get_file_as_bytes(p_path, &last_file_open_error); }
  209. static String _get_file_as_string(const String &p_path) { return get_file_as_string(p_path, &last_file_open_error); }
  210. template <typename T>
  211. static void make_default(AccessType p_access) {
  212. create_func[p_access] = _create_builtin<T>;
  213. }
  214. public:
  215. FileAccess() {}
  216. virtual ~FileAccess();
  217. };
  218. VARIANT_ENUM_CAST(FileAccess::CompressionMode);
  219. VARIANT_ENUM_CAST(FileAccess::ModeFlags);
  220. VARIANT_BITFIELD_CAST(FileAccess::UnixPermissionFlags);
  221. #endif // FILE_ACCESS_H