file_access_android.cpp 4.8 KB

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