file_access_jandroid.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*************************************************************************/
  2. /* file_access_jandroid.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef ANDROID_NATIVE_ACTIVITY
  31. #include "file_access_jandroid.h"
  32. #include "os/os.h"
  33. #include "thread_jandroid.h"
  34. #include <unistd.h>
  35. jobject FileAccessJAndroid::io = NULL;
  36. jclass FileAccessJAndroid::cls;
  37. jmethodID FileAccessJAndroid::_file_open = 0;
  38. jmethodID FileAccessJAndroid::_file_get_size = 0;
  39. jmethodID FileAccessJAndroid::_file_seek = 0;
  40. jmethodID FileAccessJAndroid::_file_read = 0;
  41. jmethodID FileAccessJAndroid::_file_tell = 0;
  42. jmethodID FileAccessJAndroid::_file_eof = 0;
  43. jmethodID FileAccessJAndroid::_file_close = 0;
  44. FileAccess *FileAccessJAndroid::create_jandroid() {
  45. return memnew(FileAccessJAndroid);
  46. }
  47. Error FileAccessJAndroid::_open(const String &p_path, int p_mode_flags) {
  48. if (is_open())
  49. close();
  50. String path = fix_path(p_path).simplify_path();
  51. if (path.begins_with("/"))
  52. path = path.substr(1, path.length());
  53. else if (path.begins_with("res://"))
  54. path = path.substr(6, path.length());
  55. JNIEnv *env = ThreadAndroid::get_env();
  56. jstring js = env->NewStringUTF(path.utf8().get_data());
  57. int res = env->CallIntMethod(io, _file_open, js, p_mode_flags & WRITE ? true : false);
  58. env->DeleteLocalRef(js);
  59. OS::get_singleton()->print("fopen: '%s' ret %i\n", path.utf8().get_data(), res);
  60. if (res <= 0)
  61. return ERR_FILE_CANT_OPEN;
  62. id = res;
  63. return OK;
  64. }
  65. void FileAccessJAndroid::close() {
  66. if (!is_open())
  67. return;
  68. JNIEnv *env = ThreadAndroid::get_env();
  69. env->CallVoidMethod(io, _file_close, id);
  70. id = 0;
  71. }
  72. bool FileAccessJAndroid::is_open() const {
  73. return id != 0;
  74. }
  75. void FileAccessJAndroid::seek(size_t p_position) {
  76. JNIEnv *env = ThreadAndroid::get_env();
  77. ERR_FAIL_COND(!is_open());
  78. env->CallVoidMethod(io, _file_seek, id, p_position);
  79. }
  80. void FileAccessJAndroid::seek_end(int64_t p_position) {
  81. ERR_FAIL_COND(!is_open());
  82. seek(get_len());
  83. }
  84. size_t FileAccessJAndroid::get_pos() const {
  85. JNIEnv *env = ThreadAndroid::get_env();
  86. ERR_FAIL_COND_V(!is_open(), 0);
  87. return env->CallIntMethod(io, _file_tell, id);
  88. }
  89. size_t FileAccessJAndroid::get_len() const {
  90. JNIEnv *env = ThreadAndroid::get_env();
  91. ERR_FAIL_COND_V(!is_open(), 0);
  92. return env->CallIntMethod(io, _file_get_size, id);
  93. }
  94. bool FileAccessJAndroid::eof_reached() const {
  95. JNIEnv *env = ThreadAndroid::get_env();
  96. ERR_FAIL_COND_V(!is_open(), 0);
  97. return env->CallIntMethod(io, _file_eof, id);
  98. }
  99. uint8_t FileAccessJAndroid::get_8() const {
  100. ERR_FAIL_COND_V(!is_open(), 0);
  101. uint8_t byte;
  102. get_buffer(&byte, 1);
  103. return byte;
  104. }
  105. int FileAccessJAndroid::get_buffer(uint8_t *p_dst, int p_length) const {
  106. ERR_FAIL_COND_V(!is_open(), 0);
  107. if (p_length == 0)
  108. return 0;
  109. JNIEnv *env = ThreadAndroid::get_env();
  110. jbyteArray jca = (jbyteArray)env->CallObjectMethod(io, _file_read, id, p_length);
  111. int len = env->GetArrayLength(jca);
  112. env->GetByteArrayRegion(jca, 0, len, (jbyte *)p_dst);
  113. env->DeleteLocalRef((jobject)jca);
  114. return len;
  115. }
  116. Error FileAccessJAndroid::get_error() const {
  117. if (eof_reached())
  118. return ERR_FILE_EOF;
  119. return OK;
  120. }
  121. void FileAccessJAndroid::store_8(uint8_t p_dest) {
  122. }
  123. bool FileAccessJAndroid::file_exists(const String &p_path) {
  124. JNIEnv *env = ThreadAndroid::get_env();
  125. String path = fix_path(p_path).simplify_path();
  126. if (path.begins_with("/"))
  127. path = path.substr(1, path.length());
  128. else if (path.begins_with("res://"))
  129. path = path.substr(6, path.length());
  130. jstring js = env->NewStringUTF(path.utf8().get_data());
  131. int res = env->CallIntMethod(io, _file_open, js, false);
  132. if (res <= 0) {
  133. env->DeleteLocalRef(js);
  134. return false;
  135. }
  136. env->CallVoidMethod(io, _file_close, res);
  137. env->DeleteLocalRef(js);
  138. return true;
  139. }
  140. void FileAccessJAndroid::setup(jobject p_io) {
  141. io = p_io;
  142. JNIEnv *env = ThreadAndroid::get_env();
  143. jclass c = env->GetObjectClass(io);
  144. cls = (jclass)env->NewGlobalRef(c);
  145. _file_open = env->GetMethodID(cls, "file_open", "(Ljava/lang/String;Z)I");
  146. _file_get_size = env->GetMethodID(cls, "file_get_size", "(I)I");
  147. _file_tell = env->GetMethodID(cls, "file_tell", "(I)I");
  148. _file_eof = env->GetMethodID(cls, "file_eof", "(I)Z");
  149. _file_seek = env->GetMethodID(cls, "file_seek", "(II)V");
  150. _file_read = env->GetMethodID(cls, "file_read", "(II)[B");
  151. _file_close = env->GetMethodID(cls, "file_close", "(I)V");
  152. }
  153. FileAccessJAndroid::FileAccessJAndroid() {
  154. id = 0;
  155. }
  156. FileAccessJAndroid::~FileAccessJAndroid() {
  157. if (is_open())
  158. close();
  159. }
  160. #endif