file_access_android.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/string/print_string.h"
  32. #include "thread_jandroid.h"
  33. #include <android/asset_manager_jni.h>
  34. AAssetManager *FileAccessAndroid::asset_manager = nullptr;
  35. jobject FileAccessAndroid::j_asset_manager = nullptr;
  36. String FileAccessAndroid::get_path() const {
  37. return path_src;
  38. }
  39. String FileAccessAndroid::get_path_absolute() const {
  40. return absolute_path;
  41. }
  42. Error FileAccessAndroid::open_internal(const String &p_path, int p_mode_flags) {
  43. _close();
  44. path_src = p_path;
  45. String path = fix_path(p_path).simplify_path();
  46. absolute_path = path;
  47. if (path.begins_with("/")) {
  48. path = path.substr(1);
  49. } else if (path.begins_with("res://")) {
  50. path = path.substr(6);
  51. }
  52. ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, ERR_UNAVAILABLE); //can't write on android..
  53. asset = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
  54. if (!asset) {
  55. return ERR_CANT_OPEN;
  56. }
  57. len = AAsset_getLength(asset);
  58. pos = 0;
  59. eof = false;
  60. return OK;
  61. }
  62. void FileAccessAndroid::_close() {
  63. if (!asset) {
  64. return;
  65. }
  66. AAsset_close(asset);
  67. asset = nullptr;
  68. }
  69. bool FileAccessAndroid::is_open() const {
  70. return asset != nullptr;
  71. }
  72. void FileAccessAndroid::seek(uint64_t p_position) {
  73. ERR_FAIL_NULL(asset);
  74. AAsset_seek(asset, p_position, SEEK_SET);
  75. pos = p_position;
  76. if (pos > len) {
  77. pos = len;
  78. eof = true;
  79. } else {
  80. eof = false;
  81. }
  82. }
  83. void FileAccessAndroid::seek_end(int64_t p_position) {
  84. ERR_FAIL_NULL(asset);
  85. AAsset_seek(asset, p_position, SEEK_END);
  86. pos = len + p_position;
  87. }
  88. uint64_t FileAccessAndroid::get_position() const {
  89. return pos;
  90. }
  91. uint64_t FileAccessAndroid::get_length() const {
  92. return len;
  93. }
  94. bool FileAccessAndroid::eof_reached() const {
  95. return eof;
  96. }
  97. uint64_t FileAccessAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  98. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  99. int r = AAsset_read(asset, p_dst, p_length);
  100. if (pos + p_length > len) {
  101. eof = true;
  102. }
  103. if (r >= 0) {
  104. pos += r;
  105. if (pos > len) {
  106. pos = len;
  107. }
  108. }
  109. return r;
  110. }
  111. int64_t FileAccessAndroid::_get_size(const String &p_file) {
  112. return AAsset_getLength64(asset);
  113. }
  114. Error FileAccessAndroid::get_error() const {
  115. return eof ? ERR_FILE_EOF : OK; // not sure what else it may happen
  116. }
  117. void FileAccessAndroid::flush() {
  118. ERR_FAIL();
  119. }
  120. bool FileAccessAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  121. ERR_FAIL_V(false);
  122. }
  123. bool FileAccessAndroid::file_exists(const String &p_path) {
  124. String path = fix_path(p_path).simplify_path();
  125. if (path.begins_with("/")) {
  126. path = path.substr(1);
  127. } else if (path.begins_with("res://")) {
  128. path = path.substr(6);
  129. }
  130. AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
  131. if (!at) {
  132. return false;
  133. }
  134. AAsset_close(at);
  135. return true;
  136. }
  137. void FileAccessAndroid::close() {
  138. _close();
  139. }
  140. FileAccessAndroid::~FileAccessAndroid() {
  141. _close();
  142. }
  143. void FileAccessAndroid::setup(jobject p_asset_manager) {
  144. JNIEnv *env = get_jni_env();
  145. j_asset_manager = env->NewGlobalRef(p_asset_manager);
  146. asset_manager = AAssetManager_fromJava(env, j_asset_manager);
  147. }
  148. void FileAccessAndroid::terminate() {
  149. JNIEnv *env = get_jni_env();
  150. ERR_FAIL_NULL(env);
  151. env->DeleteGlobalRef(j_asset_manager);
  152. }