file_access_memory.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**************************************************************************/
  2. /* file_access_memory.cpp */
  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. #include "file_access_memory.h"
  31. #include "core/config/project_settings.h"
  32. static HashMap<String, Vector<uint8_t>> *files = nullptr;
  33. void FileAccessMemory::register_file(const String &p_name, const Vector<uint8_t> &p_data) {
  34. if (!files) {
  35. files = memnew((HashMap<String, Vector<uint8_t>>));
  36. }
  37. String name;
  38. if (ProjectSettings::get_singleton()) {
  39. name = ProjectSettings::get_singleton()->globalize_path(p_name);
  40. } else {
  41. name = p_name;
  42. }
  43. //name = DirAccess::normalize_path(name);
  44. (*files)[name] = p_data;
  45. }
  46. void FileAccessMemory::cleanup() {
  47. if (!files) {
  48. return;
  49. }
  50. memdelete(files);
  51. }
  52. Ref<FileAccess> FileAccessMemory::create() {
  53. return memnew(FileAccessMemory);
  54. }
  55. bool FileAccessMemory::file_exists(const String &p_name) {
  56. String name = fix_path(p_name);
  57. //name = DirAccess::normalize_path(name);
  58. return files && (files->find(name) != nullptr);
  59. }
  60. Error FileAccessMemory::open_custom(const uint8_t *p_data, uint64_t p_len) {
  61. data = (uint8_t *)p_data;
  62. length = p_len;
  63. pos = 0;
  64. return OK;
  65. }
  66. Error FileAccessMemory::open_internal(const String &p_path, int p_mode_flags) {
  67. ERR_FAIL_NULL_V(files, ERR_FILE_NOT_FOUND);
  68. String name = fix_path(p_path);
  69. //name = DirAccess::normalize_path(name);
  70. HashMap<String, Vector<uint8_t>>::Iterator E = files->find(name);
  71. ERR_FAIL_COND_V_MSG(!E, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s'.", p_path));
  72. data = E->value.ptrw();
  73. length = E->value.size();
  74. pos = 0;
  75. return OK;
  76. }
  77. bool FileAccessMemory::is_open() const {
  78. return data != nullptr;
  79. }
  80. void FileAccessMemory::seek(uint64_t p_position) {
  81. ERR_FAIL_NULL(data);
  82. pos = p_position;
  83. }
  84. void FileAccessMemory::seek_end(int64_t p_position) {
  85. ERR_FAIL_NULL(data);
  86. pos = length + p_position;
  87. }
  88. uint64_t FileAccessMemory::get_position() const {
  89. ERR_FAIL_NULL_V(data, 0);
  90. return pos;
  91. }
  92. uint64_t FileAccessMemory::get_length() const {
  93. ERR_FAIL_NULL_V(data, 0);
  94. return length;
  95. }
  96. bool FileAccessMemory::eof_reached() const {
  97. return pos >= length;
  98. }
  99. uint64_t FileAccessMemory::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  100. if (!p_length) {
  101. return 0;
  102. }
  103. ERR_FAIL_NULL_V(p_dst, -1);
  104. ERR_FAIL_NULL_V(data, -1);
  105. uint64_t left = length - pos;
  106. uint64_t read = MIN(p_length, left);
  107. if (read < p_length) {
  108. WARN_PRINT("Reading less data than requested");
  109. }
  110. memcpy(p_dst, &data[pos], read);
  111. pos += read;
  112. return read;
  113. }
  114. Error FileAccessMemory::get_error() const {
  115. return pos >= length ? ERR_FILE_EOF : OK;
  116. }
  117. void FileAccessMemory::flush() {
  118. ERR_FAIL_NULL(data);
  119. }
  120. bool FileAccessMemory::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  121. if (!p_length) {
  122. return true;
  123. }
  124. ERR_FAIL_NULL_V(p_src, false);
  125. uint64_t left = length - pos;
  126. uint64_t write = MIN(p_length, left);
  127. memcpy(&data[pos], p_src, write);
  128. pos += write;
  129. ERR_FAIL_COND_V_MSG(write < p_length, false, "Writing less data than requested.");
  130. return true;
  131. }