file_access.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* file_access.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "math_defs.h"
  33. #include "os/memory.h"
  34. #include "typedefs.h"
  35. #include "ustring.h"
  36. /**
  37. * Multi-Platform abstraction for accessing to files.
  38. */
  39. class FileAccess {
  40. public:
  41. enum AccessType {
  42. ACCESS_RESOURCES,
  43. ACCESS_USERDATA,
  44. ACCESS_FILESYSTEM,
  45. ACCESS_MAX
  46. };
  47. typedef void (*FileCloseFailNotify)(const String &);
  48. typedef FileAccess *(*CreateFunc)();
  49. bool endian_swap;
  50. bool real_is_double;
  51. protected:
  52. String fix_path(const String &p_path) const;
  53. virtual Error _open(const String &p_path, int p_mode_flags) = 0; ///< open a file
  54. virtual uint64_t _get_modified_time(const String &p_file) = 0;
  55. static FileCloseFailNotify close_fail_notify;
  56. private:
  57. static bool backup_save;
  58. AccessType _access_type;
  59. static CreateFunc create_func[ACCESS_MAX]; /** default file access creation function for a platform */
  60. template <class T>
  61. static FileAccess *_create_builtin() {
  62. return memnew(T);
  63. }
  64. public:
  65. static void set_file_close_fail_notify_callback(FileCloseFailNotify p_cbk) { close_fail_notify = p_cbk; }
  66. virtual void _set_access_type(AccessType p_access);
  67. enum ModeFlags {
  68. READ = 1,
  69. WRITE = 2,
  70. READ_WRITE = 3,
  71. WRITE_READ = 7,
  72. };
  73. virtual void close() = 0; ///< close a file
  74. virtual bool is_open() const = 0; ///< true when file is open
  75. virtual void seek(size_t p_position) = 0; ///< seek to a given position
  76. virtual void seek_end(int64_t p_position = 0) = 0; ///< seek from the end of file
  77. virtual size_t get_pos() const = 0; ///< get position in the file
  78. virtual size_t get_len() const = 0; ///< get size of the file
  79. virtual bool eof_reached() const = 0; ///< reading passed EOF
  80. virtual uint8_t get_8() const = 0; ///< get a byte
  81. virtual uint16_t get_16() const; ///< get 16 bits uint
  82. virtual uint32_t get_32() const; ///< get 32 bits uint
  83. virtual uint64_t get_64() const; ///< get 64 bits uint
  84. virtual float get_float() const;
  85. virtual double get_double() const;
  86. virtual real_t get_real() const;
  87. virtual int get_buffer(uint8_t *p_dst, int p_length) const; ///< get an array of bytes
  88. virtual String get_line() const;
  89. virtual Vector<String> get_csv_line(String delim = ",") const;
  90. /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  91. * It's not about the current CPU type but file formats.
  92. * this flags get reset to false (little endian) on each open
  93. */
  94. virtual void set_endian_swap(bool p_swap) { endian_swap = p_swap; }
  95. inline bool get_endian_swap() const { return endian_swap; }
  96. virtual Error get_error() const = 0; ///< get last error
  97. virtual void store_8(uint8_t p_dest) = 0; ///< store a byte
  98. virtual void store_16(uint16_t p_dest); ///< store 16 bits uint
  99. virtual void store_32(uint32_t p_dest); ///< store 32 bits uint
  100. virtual void store_64(uint64_t p_dest); ///< store 64 bits uint
  101. virtual void store_float(float p_dest);
  102. virtual void store_double(double p_dest);
  103. virtual void store_real(real_t p_real);
  104. virtual void store_string(const String &p_string);
  105. virtual void store_line(const String &p_string);
  106. virtual void store_pascal_string(const String &p_string);
  107. virtual String get_pascal_string();
  108. virtual void store_buffer(const uint8_t *p_src, int p_length); ///< store an array of bytes
  109. virtual bool file_exists(const String &p_name) = 0; ///< return true if a file exists
  110. virtual Error reopen(const String &p_path, int p_mode_flags); ///< does not change the AccessType
  111. virtual Error _chmod(const String &p_path, int p_mod) { return ERR_UNAVAILABLE; }
  112. static FileAccess *create(AccessType p_access); /// Create a file access (for the current platform) this is the only portable way of accessing files.
  113. static FileAccess *create_for_path(const String &p_path);
  114. static FileAccess *open(const String &p_path, int p_mode_flags, Error *r_error = NULL); /// Create a file access (for the current platform) this is the only portable way of accessing files.
  115. static CreateFunc get_create_func(AccessType p_access);
  116. static bool exists(const String &p_name); ///< return true if a file exists
  117. static uint64_t get_modified_time(const String &p_file);
  118. static void set_backup_save(bool p_enable) { backup_save = p_enable; };
  119. static bool is_backup_save_enabled() { return backup_save; };
  120. static String get_md5(const String &p_file);
  121. static String get_sha256(const String &p_file);
  122. static Vector<uint8_t> get_file_as_array(const String &p_path);
  123. template <class T>
  124. static void make_default(AccessType p_access) {
  125. create_func[p_access] = _create_builtin<T>;
  126. }
  127. FileAccess();
  128. virtual ~FileAccess() {}
  129. };
  130. struct FileAccessRef {
  131. _FORCE_INLINE_ FileAccess *operator->() {
  132. return f;
  133. }
  134. operator bool() const { return f != NULL; }
  135. FileAccess *f;
  136. FileAccessRef(FileAccess *fa) { f = fa; }
  137. ~FileAccessRef() {
  138. if (f) memdelete(f);
  139. }
  140. };
  141. #endif