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