file_access_memory.cpp 5.3 KB

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