file_access_memory.cpp 5.0 KB

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