file_access_buffered_fa.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-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_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. //DVector<uint8_t>::Write write = cache.buffer.write();
  47. //f.get_buffer(write.ptr(), p_size);
  48. // on vector
  49. f.get_buffer(cache.buffer.ptr(), 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 store_8(uint8_t p_dest) {
  63. f.store_8(p_dest);
  64. };
  65. void store_buffer(const uint8_t *p_src, int p_length) {
  66. f.store_buffer(p_src, p_length);
  67. };
  68. bool file_exists(const String &p_name) {
  69. return f.file_exists(p_name);
  70. };
  71. Error _open(const String &p_path, int p_mode_flags) {
  72. close();
  73. Error ret = f._open(p_path, p_mode_flags);
  74. if (ret != OK)
  75. return ret;
  76. //ERR_FAIL_COND_V( ret != OK, ret );
  77. file.size = f.get_len();
  78. file.offset = 0;
  79. file.open = true;
  80. file.name = p_path;
  81. file.access_flags = p_mode_flags;
  82. cache.buffer.resize(0);
  83. cache.offset = 0;
  84. return set_error(OK);
  85. };
  86. void close() {
  87. f.close();
  88. file.offset = 0;
  89. file.size = 0;
  90. file.open = false;
  91. file.name = "";
  92. cache.buffer.resize(0);
  93. cache.offset = 0;
  94. set_error(OK);
  95. };
  96. // static void make_default() {
  97. //FileAccess::create_func = FileAccessBufferedFA<T>::create;
  98. // };
  99. virtual uint64_t _get_modified_time(const String &p_file) {
  100. return f._get_modified_time(p_file);
  101. }
  102. FileAccessBufferedFA(){
  103. };
  104. };
  105. #endif // FILE_ACCESS_BUFFERED_FA_H