file_access_android.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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, path.length());
  49. } else if (path.begins_with("res://")) {
  50. path = path.substr(6, path.length());
  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. uint8_t FileAccessAndroid::get_8() const {
  98. if (pos >= len) {
  99. eof = true;
  100. return 0;
  101. }
  102. uint8_t byte;
  103. AAsset_read(asset, &byte, 1);
  104. pos++;
  105. return byte;
  106. }
  107. uint16_t FileAccessAndroid::get_16() const {
  108. if (pos >= len) {
  109. eof = true;
  110. return 0;
  111. }
  112. uint16_t bytes = 0;
  113. int r = AAsset_read(asset, &bytes, 2);
  114. if (r >= 0) {
  115. pos += r;
  116. if (pos >= len) {
  117. eof = true;
  118. }
  119. }
  120. if (big_endian) {
  121. bytes = BSWAP16(bytes);
  122. }
  123. return bytes;
  124. }
  125. uint32_t FileAccessAndroid::get_32() const {
  126. if (pos >= len) {
  127. eof = true;
  128. return 0;
  129. }
  130. uint32_t bytes = 0;
  131. int r = AAsset_read(asset, &bytes, 4);
  132. if (r >= 0) {
  133. pos += r;
  134. if (pos >= len) {
  135. eof = true;
  136. }
  137. }
  138. if (big_endian) {
  139. bytes = BSWAP32(bytes);
  140. }
  141. return bytes;
  142. }
  143. uint64_t FileAccessAndroid::get_64() const {
  144. if (pos >= len) {
  145. eof = true;
  146. return 0;
  147. }
  148. uint64_t bytes = 0;
  149. int r = AAsset_read(asset, &bytes, 8);
  150. if (r >= 0) {
  151. pos += r;
  152. if (pos >= len) {
  153. eof = true;
  154. }
  155. }
  156. if (big_endian) {
  157. bytes = BSWAP64(bytes);
  158. }
  159. return bytes;
  160. }
  161. uint64_t FileAccessAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  162. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  163. int r = AAsset_read(asset, p_dst, p_length);
  164. if (pos + p_length > len) {
  165. eof = true;
  166. }
  167. if (r >= 0) {
  168. pos += r;
  169. if (pos > len) {
  170. pos = len;
  171. }
  172. }
  173. return r;
  174. }
  175. Error FileAccessAndroid::get_error() const {
  176. return eof ? ERR_FILE_EOF : OK; // not sure what else it may happen
  177. }
  178. void FileAccessAndroid::flush() {
  179. ERR_FAIL();
  180. }
  181. void FileAccessAndroid::store_8(uint8_t p_dest) {
  182. ERR_FAIL();
  183. }
  184. void FileAccessAndroid::store_16(uint16_t p_dest) {
  185. ERR_FAIL();
  186. }
  187. void FileAccessAndroid::store_32(uint32_t p_dest) {
  188. ERR_FAIL();
  189. }
  190. void FileAccessAndroid::store_64(uint64_t p_dest) {
  191. ERR_FAIL();
  192. }
  193. bool FileAccessAndroid::file_exists(const String &p_path) {
  194. String path = fix_path(p_path).simplify_path();
  195. if (path.begins_with("/")) {
  196. path = path.substr(1, path.length());
  197. } else if (path.begins_with("res://")) {
  198. path = path.substr(6, path.length());
  199. }
  200. AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
  201. if (!at) {
  202. return false;
  203. }
  204. AAsset_close(at);
  205. return true;
  206. }
  207. void FileAccessAndroid::close() {
  208. _close();
  209. }
  210. FileAccessAndroid::~FileAccessAndroid() {
  211. _close();
  212. }
  213. void FileAccessAndroid::setup(jobject p_asset_manager) {
  214. JNIEnv *env = get_jni_env();
  215. j_asset_manager = env->NewGlobalRef(p_asset_manager);
  216. asset_manager = AAssetManager_fromJava(env, j_asset_manager);
  217. }
  218. void FileAccessAndroid::terminate() {
  219. JNIEnv *env = get_jni_env();
  220. ERR_FAIL_NULL(env);
  221. env->DeleteGlobalRef(j_asset_manager);
  222. }