file_access_android.cpp 4.9 KB

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