file_access.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 {
  45. ACCESS_RESOURCES,
  46. ACCESS_USERDATA,
  47. ACCESS_FILESYSTEM,
  48. ACCESS_MAX
  49. };
  50. enum ModeFlags {
  51. READ = 1,
  52. WRITE = 2,
  53. READ_WRITE = 3,
  54. WRITE_READ = 7,
  55. };
  56. enum CompressionMode {
  57. COMPRESSION_FASTLZ = Compression::MODE_FASTLZ,
  58. COMPRESSION_DEFLATE = Compression::MODE_DEFLATE,
  59. COMPRESSION_ZSTD = Compression::MODE_ZSTD,
  60. COMPRESSION_GZIP = Compression::MODE_GZIP
  61. };
  62. typedef void (*FileCloseFailNotify)(const String &);
  63. typedef Ref<FileAccess> (*CreateFunc)();
  64. bool big_endian = false;
  65. bool real_is_double = false;
  66. virtual uint32_t _get_unix_permissions(const String &p_file) = 0;
  67. virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) = 0;
  68. protected:
  69. static void _bind_methods();
  70. AccessType get_access_type() const;
  71. String fix_path(const String &p_path) const;
  72. virtual Error open_internal(const String &p_path, int p_mode_flags) = 0; ///< open a file
  73. virtual uint64_t _get_modified_time(const String &p_file) = 0;
  74. virtual void _set_access_type(AccessType p_access);
  75. static FileCloseFailNotify close_fail_notify;
  76. private:
  77. static bool backup_save;
  78. thread_local static Error last_file_open_error;
  79. AccessType _access_type = ACCESS_FILESYSTEM;
  80. static CreateFunc create_func[ACCESS_MAX]; /** default file access creation function for a platform */
  81. template <class T>
  82. static Ref<FileAccess> _create_builtin() {
  83. return memnew(T);
  84. }
  85. static Ref<FileAccess> _open(const String &p_path, ModeFlags p_mode_flags);
  86. public:
  87. static void set_file_close_fail_notify_callback(FileCloseFailNotify p_cbk) { close_fail_notify = p_cbk; }
  88. virtual bool is_open() const = 0; ///< true when file is open
  89. virtual String get_path() const { return ""; } /// returns the path for the current open file
  90. virtual String get_path_absolute() const { return ""; } /// returns the absolute path for the current open file
  91. virtual void seek(uint64_t p_position) = 0; ///< seek to a given position
  92. virtual void seek_end(int64_t p_position = 0) = 0; ///< seek from the end of file with negative offset
  93. virtual uint64_t get_position() const = 0; ///< get position in the file
  94. virtual uint64_t get_length() const = 0; ///< get size of the file
  95. virtual bool eof_reached() const = 0; ///< reading passed EOF
  96. virtual uint8_t get_8() const = 0; ///< get a byte
  97. virtual uint16_t get_16() const; ///< get 16 bits uint
  98. virtual uint32_t get_32() const; ///< get 32 bits uint
  99. virtual uint64_t get_64() const; ///< get 64 bits uint
  100. virtual float get_float() const;
  101. virtual double get_double() const;
  102. virtual real_t get_real() const;
  103. Variant get_var(bool p_allow_objects = false) const;
  104. virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const; ///< get an array of bytes
  105. Vector<uint8_t> _get_buffer(int64_t p_length) const;
  106. virtual String get_line() const;
  107. virtual String get_token() const;
  108. virtual Vector<String> get_csv_line(const String &p_delim = ",") const;
  109. String get_as_text(bool p_skip_cr = false) const;
  110. virtual String get_as_utf8_string(bool p_skip_cr = false) const;
  111. /**
  112. * Use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  113. * It's not about the current CPU type but file formats.
  114. * This flag gets reset to `false` (little endian) on each open.
  115. */
  116. virtual void set_big_endian(bool p_big_endian) { big_endian = p_big_endian; }
  117. inline bool is_big_endian() const { return big_endian; }
  118. virtual Error get_error() const = 0; ///< get last error
  119. virtual void flush() = 0;
  120. virtual void store_8(uint8_t p_dest) = 0; ///< store a byte
  121. virtual void store_16(uint16_t p_dest); ///< store 16 bits uint
  122. virtual void store_32(uint32_t p_dest); ///< store 32 bits uint
  123. virtual void store_64(uint64_t p_dest); ///< store 64 bits uint
  124. virtual void store_float(float p_dest);
  125. virtual void store_double(double p_dest);
  126. virtual void store_real(real_t p_real);
  127. virtual void store_string(const String &p_string);
  128. virtual void store_line(const String &p_line);
  129. virtual void store_csv_line(const Vector<String> &p_values, const String &p_delim = ",");
  130. virtual void store_pascal_string(const String &p_string);
  131. virtual String get_pascal_string();
  132. virtual void store_buffer(const uint8_t *p_src, uint64_t p_length); ///< store an array of bytes
  133. void _store_buffer(const Vector<uint8_t> &p_buffer);
  134. void store_var(const Variant &p_var, bool p_full_objects = false);
  135. virtual bool file_exists(const String &p_name) = 0; ///< return true if a file exists
  136. virtual Error reopen(const String &p_path, int p_mode_flags); ///< does not change the AccessType
  137. static Ref<FileAccess> create(AccessType p_access); /// Create a file access (for the current platform) this is the only portable way of accessing files.
  138. static Ref<FileAccess> create_for_path(const String &p_path);
  139. 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.
  140. static Ref<FileAccess> open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key);
  141. static Ref<FileAccess> open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass);
  142. static Ref<FileAccess> open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode = COMPRESSION_FASTLZ);
  143. static Error get_open_error();
  144. static CreateFunc get_create_func(AccessType p_access);
  145. static bool exists(const String &p_name); ///< return true if a file exists
  146. static uint64_t get_modified_time(const String &p_file);
  147. static uint32_t get_unix_permissions(const String &p_file);
  148. static Error set_unix_permissions(const String &p_file, uint32_t p_permissions);
  149. static void set_backup_save(bool p_enable) { backup_save = p_enable; };
  150. static bool is_backup_save_enabled() { return backup_save; };
  151. static String get_md5(const String &p_file);
  152. static String get_sha256(const String &p_file);
  153. static String get_multiple_md5(const Vector<String> &p_file);
  154. static Vector<uint8_t> get_file_as_bytes(const String &p_path, Error *r_error = nullptr);
  155. static String get_file_as_string(const String &p_path, Error *r_error = nullptr);
  156. static PackedByteArray _get_file_as_bytes(const String &p_path) { return get_file_as_bytes(p_path); }
  157. static String _get_file_as_string(const String &p_path) { return get_file_as_string(p_path); };
  158. template <class T>
  159. static void make_default(AccessType p_access) {
  160. create_func[p_access] = _create_builtin<T>;
  161. }
  162. FileAccess() {}
  163. virtual ~FileAccess() {}
  164. };
  165. VARIANT_ENUM_CAST(FileAccess::CompressionMode);
  166. VARIANT_ENUM_CAST(FileAccess::ModeFlags);
  167. #endif // FILE_ACCESS_H