file_access_android.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*************************************************************************/
  2. /* file_access_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_android.h"
  31. #include "core/string/print_string.h"
  32. AAssetManager *FileAccessAndroid::asset_manager = nullptr;
  33. String FileAccessAndroid::get_path() const {
  34. return path_src;
  35. }
  36. String FileAccessAndroid::get_path_absolute() const {
  37. return absolute_path;
  38. }
  39. Error FileAccessAndroid::open_internal(const String &p_path, int p_mode_flags) {
  40. _close();
  41. path_src = p_path;
  42. String path = fix_path(p_path).simplify_path();
  43. absolute_path = path;
  44. if (path.begins_with("/")) {
  45. path = path.substr(1, path.length());
  46. } else if (path.begins_with("res://")) {
  47. path = path.substr(6, path.length());
  48. }
  49. ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, ERR_UNAVAILABLE); //can't write on android..
  50. asset = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
  51. if (!asset) {
  52. return ERR_CANT_OPEN;
  53. }
  54. len = AAsset_getLength(asset);
  55. pos = 0;
  56. eof = false;
  57. return OK;
  58. }
  59. void FileAccessAndroid::_close() {
  60. if (!asset) {
  61. return;
  62. }
  63. AAsset_close(asset);
  64. asset = nullptr;
  65. }
  66. bool FileAccessAndroid::is_open() const {
  67. return asset != nullptr;
  68. }
  69. void FileAccessAndroid::seek(uint64_t p_position) {
  70. ERR_FAIL_NULL(asset);
  71. AAsset_seek(asset, p_position, SEEK_SET);
  72. pos = p_position;
  73. if (pos > len) {
  74. pos = len;
  75. eof = true;
  76. } else {
  77. eof = false;
  78. }
  79. }
  80. void FileAccessAndroid::seek_end(int64_t p_position) {
  81. ERR_FAIL_NULL(asset);
  82. AAsset_seek(asset, p_position, SEEK_END);
  83. pos = len + p_position;
  84. }
  85. uint64_t FileAccessAndroid::get_position() const {
  86. return pos;
  87. }
  88. uint64_t FileAccessAndroid::get_length() const {
  89. return len;
  90. }
  91. bool FileAccessAndroid::eof_reached() const {
  92. return eof;
  93. }
  94. uint8_t FileAccessAndroid::get_8() const {
  95. if (pos >= len) {
  96. eof = true;
  97. return 0;
  98. }
  99. uint8_t byte;
  100. AAsset_read(asset, &byte, 1);
  101. pos++;
  102. return byte;
  103. }
  104. uint64_t FileAccessAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  105. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  106. int r = AAsset_read(asset, p_dst, p_length);
  107. if (pos + p_length > len) {
  108. eof = true;
  109. }
  110. if (r >= 0) {
  111. pos += r;
  112. if (pos > len) {
  113. pos = len;
  114. }
  115. }
  116. return r;
  117. }
  118. Error FileAccessAndroid::get_error() const {
  119. return eof ? ERR_FILE_EOF : OK; // not sure what else it may happen
  120. }
  121. void FileAccessAndroid::flush() {
  122. ERR_FAIL();
  123. }
  124. void FileAccessAndroid::store_8(uint8_t p_dest) {
  125. ERR_FAIL();
  126. }
  127. bool FileAccessAndroid::file_exists(const String &p_path) {
  128. String path = fix_path(p_path).simplify_path();
  129. if (path.begins_with("/")) {
  130. path = path.substr(1, path.length());
  131. } else if (path.begins_with("res://")) {
  132. path = path.substr(6, path.length());
  133. }
  134. AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
  135. if (!at) {
  136. return false;
  137. }
  138. AAsset_close(at);
  139. return true;
  140. }
  141. FileAccessAndroid::~FileAccessAndroid() {
  142. _close();
  143. }