file_access_jandroid.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*************************************************************************/
  2. /* file_access_jandroid.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef ANDROID_NATIVE_ACTIVITY
  30. #include "file_access_jandroid.h"
  31. #include "os/os.h"
  32. #include <unistd.h>
  33. #include "thread_jandroid.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_pos() 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::store_8(uint8_t p_dest) {
  121. }
  122. bool FileAccessJAndroid::file_exists(const String& p_path) {
  123. JNIEnv *env = ThreadAndroid::get_env();
  124. String path=fix_path(p_path).simplify_path();
  125. if (path.begins_with("/"))
  126. path=path.substr(1,path.length());
  127. else if (path.begins_with("res://"))
  128. path=path.substr(6,path.length());
  129. jstring js = env->NewStringUTF(path.utf8().get_data());
  130. int res = env->CallIntMethod(io,_file_open,js,false);
  131. if (res<=0) {
  132. env->DeleteLocalRef(js);
  133. return false;
  134. }
  135. env->CallVoidMethod(io,_file_close,res);
  136. env->DeleteLocalRef(js);
  137. return true;
  138. }
  139. void FileAccessJAndroid::setup( jobject p_io) {
  140. io=p_io;
  141. JNIEnv *env = ThreadAndroid::get_env();
  142. __android_log_print(ANDROID_LOG_INFO,"godot","STEP5");
  143. jclass c = env->GetObjectClass(io);
  144. __android_log_print(ANDROID_LOG_INFO,"godot","STEP6");
  145. cls=(jclass)env->NewGlobalRef(c);
  146. _file_open = env->GetMethodID(cls, "file_open", "(Ljava/lang/String;Z)I");
  147. if(_file_open != 0) {
  148. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_open ok!!");
  149. }
  150. _file_get_size = env->GetMethodID(cls, "file_get_size", "(I)I");
  151. if(_file_get_size != 0) {
  152. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_get_size ok!!");
  153. }
  154. _file_tell = env->GetMethodID(cls, "file_tell", "(I)I");
  155. if(_file_tell != 0) {
  156. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_tell ok!!");
  157. }
  158. _file_eof = env->GetMethodID(cls, "file_eof", "(I)Z");
  159. if(_file_eof != 0) {
  160. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_eof ok!!");
  161. }
  162. _file_seek = env->GetMethodID(cls, "file_seek", "(II)V");
  163. if(_file_seek != 0) {
  164. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_seek ok!!");
  165. }
  166. _file_read = env->GetMethodID(cls, "file_read", "(II)[B");
  167. if(_file_read != 0) {
  168. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_read ok!!");
  169. }
  170. _file_close = env->GetMethodID(cls, "file_close", "(I)V");
  171. if(_file_close != 0) {
  172. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_close ok!!");
  173. }
  174. // (*env)->CallVoidMethod(env,obj,aMethodID, myvar);
  175. }
  176. FileAccessJAndroid::FileAccessJAndroid()
  177. {
  178. id=0;
  179. }
  180. FileAccessJAndroid::~FileAccessJAndroid()
  181. {
  182. if (is_open())
  183. close();
  184. }
  185. #endif