file_access_buffered_fa.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*************************************************************************/
  2. /* file_access_buffered_fa.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_BUFFERED_FA_H
  31. #define FILE_ACCESS_BUFFERED_FA_H
  32. #include "core/io/file_access_buffered.h"
  33. template <class T>
  34. class FileAccessBufferedFA : public FileAccessBuffered {
  35. T f;
  36. int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const {
  37. ERR_FAIL_COND_V(!f.is_open(), -1);
  38. ((T *)&f)->seek(p_offset);
  39. if (p_dest) {
  40. f.get_buffer(p_dest, p_size);
  41. return p_size;
  42. } else {
  43. cache.offset = p_offset;
  44. cache.buffer.resize(p_size);
  45. // on dvector
  46. //PoolVector<uint8_t>::Write write = cache.buffer.write();
  47. //f.get_buffer(write.ptrw(), p_size);
  48. // on vector
  49. f.get_buffer(cache.buffer.ptrw(), p_size);
  50. return p_size;
  51. };
  52. };
  53. static FileAccess *create() {
  54. return memnew(FileAccessBufferedFA<T>());
  55. };
  56. protected:
  57. virtual void _set_access_type(AccessType p_access) {
  58. f._set_access_type(p_access);
  59. FileAccessBuffered::_set_access_type(p_access);
  60. };
  61. public:
  62. void flush() {
  63. f.flush();
  64. };
  65. void store_8(uint8_t p_dest) {
  66. f.store_8(p_dest);
  67. };
  68. void store_buffer(const uint8_t *p_src, int p_length) {
  69. f.store_buffer(p_src, p_length);
  70. };
  71. bool file_exists(const String &p_name) {
  72. return f.file_exists(p_name);
  73. };
  74. Error _open(const String &p_path, int p_mode_flags) {
  75. close();
  76. Error ret = f._open(p_path, p_mode_flags);
  77. if (ret != OK)
  78. return ret;
  79. //ERR_FAIL_COND_V( ret != OK, ret );
  80. file.size = f.get_len();
  81. file.offset = 0;
  82. file.open = true;
  83. file.name = p_path;
  84. file.access_flags = p_mode_flags;
  85. cache.buffer.resize(0);
  86. cache.offset = 0;
  87. return set_error(OK);
  88. };
  89. void close() {
  90. f.close();
  91. file.offset = 0;
  92. file.size = 0;
  93. file.open = false;
  94. file.name = "";
  95. cache.buffer.resize(0);
  96. cache.offset = 0;
  97. set_error(OK);
  98. };
  99. /*
  100. static void make_default() {
  101. FileAccess::create_func = FileAccessBufferedFA<T>::create;
  102. };
  103. */
  104. virtual uint64_t _get_modified_time(const String &p_file) {
  105. return f._get_modified_time(p_file);
  106. }
  107. FileAccessBufferedFA(){
  108. };
  109. };
  110. #endif // FILE_ACCESS_BUFFERED_FA_H