file_access_filesystem_jandroid.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**************************************************************************/
  2. /* file_access_filesystem_jandroid.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_filesystem_jandroid.h"
  31. #include "thread_jandroid.h"
  32. #include "core/os/os.h"
  33. #include "core/templates/local_vector.h"
  34. #include <unistd.h>
  35. jobject FileAccessFilesystemJAndroid::file_access_handler = nullptr;
  36. jclass FileAccessFilesystemJAndroid::cls;
  37. jmethodID FileAccessFilesystemJAndroid::_file_open = nullptr;
  38. jmethodID FileAccessFilesystemJAndroid::_file_get_size = nullptr;
  39. jmethodID FileAccessFilesystemJAndroid::_file_seek = nullptr;
  40. jmethodID FileAccessFilesystemJAndroid::_file_seek_end = nullptr;
  41. jmethodID FileAccessFilesystemJAndroid::_file_read = nullptr;
  42. jmethodID FileAccessFilesystemJAndroid::_file_tell = nullptr;
  43. jmethodID FileAccessFilesystemJAndroid::_file_eof = nullptr;
  44. jmethodID FileAccessFilesystemJAndroid::_file_set_eof = nullptr;
  45. jmethodID FileAccessFilesystemJAndroid::_file_close = nullptr;
  46. jmethodID FileAccessFilesystemJAndroid::_file_write = nullptr;
  47. jmethodID FileAccessFilesystemJAndroid::_file_flush = nullptr;
  48. jmethodID FileAccessFilesystemJAndroid::_file_exists = nullptr;
  49. jmethodID FileAccessFilesystemJAndroid::_file_last_modified = nullptr;
  50. String FileAccessFilesystemJAndroid::get_path() const {
  51. return path_src;
  52. }
  53. String FileAccessFilesystemJAndroid::get_path_absolute() const {
  54. return absolute_path;
  55. }
  56. Error FileAccessFilesystemJAndroid::open_internal(const String &p_path, int p_mode_flags) {
  57. if (is_open()) {
  58. _close();
  59. }
  60. if (_file_open) {
  61. JNIEnv *env = get_jni_env();
  62. ERR_FAIL_NULL_V(env, ERR_UNCONFIGURED);
  63. String path = fix_path(p_path).simplify_path();
  64. jstring js = env->NewStringUTF(path.utf8().get_data());
  65. int res = env->CallIntMethod(file_access_handler, _file_open, js, p_mode_flags);
  66. env->DeleteLocalRef(js);
  67. if (res <= 0) {
  68. switch (res) {
  69. case 0:
  70. default:
  71. return ERR_FILE_CANT_OPEN;
  72. case -1:
  73. return ERR_FILE_NOT_FOUND;
  74. }
  75. }
  76. id = res;
  77. path_src = p_path;
  78. absolute_path = path;
  79. return OK;
  80. } else {
  81. return ERR_UNCONFIGURED;
  82. }
  83. }
  84. void FileAccessFilesystemJAndroid::_close() {
  85. if (!is_open()) {
  86. return;
  87. }
  88. if (_file_close) {
  89. JNIEnv *env = get_jni_env();
  90. ERR_FAIL_NULL(env);
  91. env->CallVoidMethod(file_access_handler, _file_close, id);
  92. }
  93. id = 0;
  94. }
  95. bool FileAccessFilesystemJAndroid::is_open() const {
  96. return id != 0;
  97. }
  98. void FileAccessFilesystemJAndroid::seek(uint64_t p_position) {
  99. if (_file_seek) {
  100. JNIEnv *env = get_jni_env();
  101. ERR_FAIL_NULL(env);
  102. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  103. env->CallVoidMethod(file_access_handler, _file_seek, id, p_position);
  104. }
  105. }
  106. void FileAccessFilesystemJAndroid::seek_end(int64_t p_position) {
  107. if (_file_seek_end) {
  108. JNIEnv *env = get_jni_env();
  109. ERR_FAIL_NULL(env);
  110. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  111. env->CallVoidMethod(file_access_handler, _file_seek_end, id, p_position);
  112. }
  113. }
  114. uint64_t FileAccessFilesystemJAndroid::get_position() const {
  115. if (_file_tell) {
  116. JNIEnv *env = get_jni_env();
  117. ERR_FAIL_NULL_V(env, 0);
  118. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  119. return env->CallLongMethod(file_access_handler, _file_tell, id);
  120. } else {
  121. return 0;
  122. }
  123. }
  124. uint64_t FileAccessFilesystemJAndroid::get_length() const {
  125. if (_file_get_size) {
  126. JNIEnv *env = get_jni_env();
  127. ERR_FAIL_NULL_V(env, 0);
  128. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  129. return env->CallLongMethod(file_access_handler, _file_get_size, id);
  130. } else {
  131. return 0;
  132. }
  133. }
  134. bool FileAccessFilesystemJAndroid::eof_reached() const {
  135. if (_file_eof) {
  136. JNIEnv *env = get_jni_env();
  137. ERR_FAIL_NULL_V(env, false);
  138. ERR_FAIL_COND_V_MSG(!is_open(), false, "File must be opened before use.");
  139. return env->CallBooleanMethod(file_access_handler, _file_eof, id);
  140. } else {
  141. return false;
  142. }
  143. }
  144. void FileAccessFilesystemJAndroid::_set_eof(bool eof) {
  145. if (_file_set_eof) {
  146. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  147. JNIEnv *env = get_jni_env();
  148. ERR_FAIL_NULL(env);
  149. env->CallVoidMethod(file_access_handler, _file_set_eof, id, eof);
  150. }
  151. }
  152. uint8_t FileAccessFilesystemJAndroid::get_8() const {
  153. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  154. uint8_t byte;
  155. get_buffer(&byte, 1);
  156. return byte;
  157. }
  158. String FileAccessFilesystemJAndroid::get_line() const {
  159. ERR_FAIL_COND_V_MSG(!is_open(), String(), "File must be opened before use.");
  160. const size_t buffer_size_limit = 2048;
  161. const uint64_t file_size = get_length();
  162. const uint64_t start_position = get_position();
  163. String result;
  164. LocalVector<uint8_t> line_buffer;
  165. size_t current_buffer_size = 0;
  166. uint64_t line_buffer_position = 0;
  167. while (true) {
  168. size_t line_buffer_size = MIN(buffer_size_limit, file_size - get_position());
  169. if (line_buffer_size <= 0) {
  170. const_cast<FileAccessFilesystemJAndroid *>(this)->_set_eof(true);
  171. break;
  172. }
  173. current_buffer_size += line_buffer_size;
  174. line_buffer.resize(current_buffer_size);
  175. uint64_t bytes_read = get_buffer(&line_buffer[line_buffer_position], current_buffer_size - line_buffer_position);
  176. if (bytes_read <= 0) {
  177. break;
  178. }
  179. for (; bytes_read > 0; line_buffer_position++, bytes_read--) {
  180. uint8_t elem = line_buffer[line_buffer_position];
  181. if (elem == '\n' || elem == '\0') {
  182. // Found the end of the line
  183. const_cast<FileAccessFilesystemJAndroid *>(this)->seek(start_position + line_buffer_position + 1);
  184. if (result.parse_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
  185. return String();
  186. }
  187. return result;
  188. }
  189. }
  190. }
  191. if (result.parse_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
  192. return String();
  193. }
  194. return result;
  195. }
  196. uint64_t FileAccessFilesystemJAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  197. if (_file_read) {
  198. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  199. if (p_length == 0) {
  200. return 0;
  201. }
  202. JNIEnv *env = get_jni_env();
  203. ERR_FAIL_NULL_V(env, 0);
  204. jobject j_buffer = env->NewDirectByteBuffer(p_dst, p_length);
  205. int length = env->CallIntMethod(file_access_handler, _file_read, id, j_buffer);
  206. env->DeleteLocalRef(j_buffer);
  207. return length;
  208. } else {
  209. return 0;
  210. }
  211. }
  212. void FileAccessFilesystemJAndroid::store_8(uint8_t p_dest) {
  213. store_buffer(&p_dest, 1);
  214. }
  215. void FileAccessFilesystemJAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  216. if (_file_write) {
  217. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  218. if (p_length == 0) {
  219. return;
  220. }
  221. JNIEnv *env = get_jni_env();
  222. ERR_FAIL_NULL(env);
  223. jobject j_buffer = env->NewDirectByteBuffer((void *)p_src, p_length);
  224. env->CallVoidMethod(file_access_handler, _file_write, id, j_buffer);
  225. env->DeleteLocalRef(j_buffer);
  226. }
  227. }
  228. Error FileAccessFilesystemJAndroid::get_error() const {
  229. if (eof_reached()) {
  230. return ERR_FILE_EOF;
  231. }
  232. return OK;
  233. }
  234. void FileAccessFilesystemJAndroid::flush() {
  235. if (_file_flush) {
  236. JNIEnv *env = get_jni_env();
  237. ERR_FAIL_NULL(env);
  238. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  239. env->CallVoidMethod(file_access_handler, _file_flush, id);
  240. }
  241. }
  242. bool FileAccessFilesystemJAndroid::file_exists(const String &p_path) {
  243. if (_file_exists) {
  244. JNIEnv *env = get_jni_env();
  245. ERR_FAIL_NULL_V(env, false);
  246. String path = fix_path(p_path).simplify_path();
  247. jstring js = env->NewStringUTF(path.utf8().get_data());
  248. bool result = env->CallBooleanMethod(file_access_handler, _file_exists, js);
  249. env->DeleteLocalRef(js);
  250. return result;
  251. } else {
  252. return false;
  253. }
  254. }
  255. uint64_t FileAccessFilesystemJAndroid::_get_modified_time(const String &p_file) {
  256. if (_file_last_modified) {
  257. JNIEnv *env = get_jni_env();
  258. ERR_FAIL_NULL_V(env, false);
  259. String path = fix_path(p_file).simplify_path();
  260. jstring js = env->NewStringUTF(path.utf8().get_data());
  261. uint64_t result = env->CallLongMethod(file_access_handler, _file_last_modified, js);
  262. env->DeleteLocalRef(js);
  263. return result;
  264. } else {
  265. return 0;
  266. }
  267. }
  268. void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) {
  269. JNIEnv *env = get_jni_env();
  270. file_access_handler = env->NewGlobalRef(p_file_access_handler);
  271. jclass c = env->GetObjectClass(file_access_handler);
  272. cls = (jclass)env->NewGlobalRef(c);
  273. _file_open = env->GetMethodID(cls, "fileOpen", "(Ljava/lang/String;I)I");
  274. _file_get_size = env->GetMethodID(cls, "fileGetSize", "(I)J");
  275. _file_tell = env->GetMethodID(cls, "fileGetPosition", "(I)J");
  276. _file_eof = env->GetMethodID(cls, "isFileEof", "(I)Z");
  277. _file_set_eof = env->GetMethodID(cls, "setFileEof", "(IZ)V");
  278. _file_seek = env->GetMethodID(cls, "fileSeek", "(IJ)V");
  279. _file_seek_end = env->GetMethodID(cls, "fileSeekFromEnd", "(IJ)V");
  280. _file_read = env->GetMethodID(cls, "fileRead", "(ILjava/nio/ByteBuffer;)I");
  281. _file_close = env->GetMethodID(cls, "fileClose", "(I)V");
  282. _file_write = env->GetMethodID(cls, "fileWrite", "(ILjava/nio/ByteBuffer;)V");
  283. _file_flush = env->GetMethodID(cls, "fileFlush", "(I)V");
  284. _file_exists = env->GetMethodID(cls, "fileExists", "(Ljava/lang/String;)Z");
  285. _file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J");
  286. }
  287. void FileAccessFilesystemJAndroid::close() {
  288. if (is_open()) {
  289. _close();
  290. }
  291. }
  292. FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() {
  293. id = 0;
  294. }
  295. FileAccessFilesystemJAndroid::~FileAccessFilesystemJAndroid() {
  296. if (is_open()) {
  297. _close();
  298. }
  299. }