file_access_jandroid.cpp 6.4 KB

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