file_access_memory.cpp 5.1 KB

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