file_access_filesystem_jandroid.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. jmethodID FileAccessFilesystemJAndroid::_file_last_accessed = nullptr;
  51. jmethodID FileAccessFilesystemJAndroid::_file_resize = nullptr;
  52. jmethodID FileAccessFilesystemJAndroid::_file_size = nullptr;
  53. String FileAccessFilesystemJAndroid::get_path() const {
  54. return path_src;
  55. }
  56. String FileAccessFilesystemJAndroid::get_path_absolute() const {
  57. return absolute_path;
  58. }
  59. Error FileAccessFilesystemJAndroid::open_internal(const String &p_path, int p_mode_flags) {
  60. if (is_open()) {
  61. _close();
  62. }
  63. if (_file_open) {
  64. JNIEnv *env = get_jni_env();
  65. ERR_FAIL_NULL_V(env, ERR_UNCONFIGURED);
  66. String path = fix_path(p_path).simplify_path();
  67. jstring js = env->NewStringUTF(path.utf8().get_data());
  68. int res = env->CallIntMethod(file_access_handler, _file_open, js, p_mode_flags);
  69. env->DeleteLocalRef(js);
  70. if (res < 0) {
  71. // Errors are passed back as their negative value to differentiate from the positive file id.
  72. return static_cast<Error>(-res);
  73. }
  74. id = res;
  75. path_src = p_path;
  76. absolute_path = path;
  77. return OK;
  78. } else {
  79. return ERR_UNCONFIGURED;
  80. }
  81. }
  82. void FileAccessFilesystemJAndroid::_close() {
  83. if (!is_open()) {
  84. return;
  85. }
  86. if (_file_close) {
  87. JNIEnv *env = get_jni_env();
  88. ERR_FAIL_NULL(env);
  89. env->CallVoidMethod(file_access_handler, _file_close, id);
  90. }
  91. id = 0;
  92. }
  93. bool FileAccessFilesystemJAndroid::is_open() const {
  94. return id != 0;
  95. }
  96. void FileAccessFilesystemJAndroid::seek(uint64_t p_position) {
  97. if (_file_seek) {
  98. JNIEnv *env = get_jni_env();
  99. ERR_FAIL_NULL(env);
  100. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  101. env->CallVoidMethod(file_access_handler, _file_seek, id, p_position);
  102. }
  103. }
  104. void FileAccessFilesystemJAndroid::seek_end(int64_t p_position) {
  105. if (_file_seek_end) {
  106. JNIEnv *env = get_jni_env();
  107. ERR_FAIL_NULL(env);
  108. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  109. env->CallVoidMethod(file_access_handler, _file_seek_end, id, p_position);
  110. }
  111. }
  112. uint64_t FileAccessFilesystemJAndroid::get_position() const {
  113. if (_file_tell) {
  114. JNIEnv *env = get_jni_env();
  115. ERR_FAIL_NULL_V(env, 0);
  116. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  117. return env->CallLongMethod(file_access_handler, _file_tell, id);
  118. } else {
  119. return 0;
  120. }
  121. }
  122. uint64_t FileAccessFilesystemJAndroid::get_length() const {
  123. if (_file_get_size) {
  124. JNIEnv *env = get_jni_env();
  125. ERR_FAIL_NULL_V(env, 0);
  126. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  127. return env->CallLongMethod(file_access_handler, _file_get_size, id);
  128. } else {
  129. return 0;
  130. }
  131. }
  132. bool FileAccessFilesystemJAndroid::eof_reached() const {
  133. if (_file_eof) {
  134. JNIEnv *env = get_jni_env();
  135. ERR_FAIL_NULL_V(env, false);
  136. ERR_FAIL_COND_V_MSG(!is_open(), false, "File must be opened before use.");
  137. return env->CallBooleanMethod(file_access_handler, _file_eof, id);
  138. } else {
  139. return false;
  140. }
  141. }
  142. void FileAccessFilesystemJAndroid::_set_eof(bool eof) {
  143. if (_file_set_eof) {
  144. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  145. JNIEnv *env = get_jni_env();
  146. ERR_FAIL_NULL(env);
  147. env->CallVoidMethod(file_access_handler, _file_set_eof, id, eof);
  148. }
  149. }
  150. String FileAccessFilesystemJAndroid::get_line() const {
  151. ERR_FAIL_COND_V_MSG(!is_open(), String(), "File must be opened before use.");
  152. const size_t buffer_size_limit = 2048;
  153. const uint64_t file_size = get_length();
  154. const uint64_t start_position = get_position();
  155. String result;
  156. LocalVector<uint8_t> line_buffer;
  157. size_t current_buffer_size = 0;
  158. uint64_t line_buffer_position = 0;
  159. while (true) {
  160. size_t line_buffer_size = MIN(buffer_size_limit, file_size - get_position());
  161. if (line_buffer_size <= 0) {
  162. const_cast<FileAccessFilesystemJAndroid *>(this)->_set_eof(true);
  163. break;
  164. }
  165. current_buffer_size += line_buffer_size;
  166. line_buffer.resize(current_buffer_size);
  167. uint64_t bytes_read = get_buffer(&line_buffer[line_buffer_position], current_buffer_size - line_buffer_position);
  168. if (bytes_read <= 0) {
  169. break;
  170. }
  171. for (; bytes_read > 0; line_buffer_position++, bytes_read--) {
  172. uint8_t elem = line_buffer[line_buffer_position];
  173. if (elem == '\n' || elem == '\0') {
  174. // Found the end of the line
  175. const_cast<FileAccessFilesystemJAndroid *>(this)->seek(start_position + line_buffer_position + 1);
  176. if (result.append_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
  177. return String();
  178. }
  179. return result;
  180. }
  181. }
  182. }
  183. if (result.append_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
  184. return String();
  185. }
  186. return result;
  187. }
  188. uint64_t FileAccessFilesystemJAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  189. if (_file_read) {
  190. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  191. if (p_length == 0) {
  192. return 0;
  193. }
  194. JNIEnv *env = get_jni_env();
  195. ERR_FAIL_NULL_V(env, 0);
  196. jobject j_buffer = env->NewDirectByteBuffer(p_dst, p_length);
  197. int length = env->CallIntMethod(file_access_handler, _file_read, id, j_buffer);
  198. env->DeleteLocalRef(j_buffer);
  199. return length;
  200. } else {
  201. return 0;
  202. }
  203. }
  204. bool FileAccessFilesystemJAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  205. if (_file_write) {
  206. ERR_FAIL_COND_V_MSG(!is_open(), false, "File must be opened before use.");
  207. ERR_FAIL_COND_V(!p_src && p_length > 0, false);
  208. if (p_length == 0) {
  209. return true;
  210. }
  211. JNIEnv *env = get_jni_env();
  212. ERR_FAIL_NULL_V(env, false);
  213. jobject j_buffer = env->NewDirectByteBuffer((void *)p_src, p_length);
  214. bool ok = env->CallBooleanMethod(file_access_handler, _file_write, id, j_buffer);
  215. env->DeleteLocalRef(j_buffer);
  216. return ok;
  217. } else {
  218. return false;
  219. }
  220. }
  221. Error FileAccessFilesystemJAndroid::get_error() const {
  222. if (eof_reached()) {
  223. return ERR_FILE_EOF;
  224. }
  225. return OK;
  226. }
  227. Error FileAccessFilesystemJAndroid::resize(int64_t p_length) {
  228. if (_file_resize) {
  229. JNIEnv *env = get_jni_env();
  230. ERR_FAIL_NULL_V(env, FAILED);
  231. ERR_FAIL_COND_V_MSG(!is_open(), FAILED, "File must be opened before use.");
  232. int res = env->CallIntMethod(file_access_handler, _file_resize, id, p_length);
  233. return static_cast<Error>(res);
  234. } else {
  235. return ERR_UNAVAILABLE;
  236. }
  237. }
  238. void FileAccessFilesystemJAndroid::flush() {
  239. if (_file_flush) {
  240. JNIEnv *env = get_jni_env();
  241. ERR_FAIL_NULL(env);
  242. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  243. env->CallVoidMethod(file_access_handler, _file_flush, id);
  244. }
  245. }
  246. bool FileAccessFilesystemJAndroid::file_exists(const String &p_path) {
  247. if (_file_exists) {
  248. JNIEnv *env = get_jni_env();
  249. ERR_FAIL_NULL_V(env, false);
  250. String path = fix_path(p_path).simplify_path();
  251. jstring js = env->NewStringUTF(path.utf8().get_data());
  252. bool result = env->CallBooleanMethod(file_access_handler, _file_exists, js);
  253. env->DeleteLocalRef(js);
  254. return result;
  255. } else {
  256. return false;
  257. }
  258. }
  259. uint64_t FileAccessFilesystemJAndroid::_get_modified_time(const String &p_file) {
  260. if (_file_last_modified) {
  261. JNIEnv *env = get_jni_env();
  262. ERR_FAIL_NULL_V(env, 0);
  263. String path = fix_path(p_file).simplify_path();
  264. jstring js = env->NewStringUTF(path.utf8().get_data());
  265. uint64_t result = env->CallLongMethod(file_access_handler, _file_last_modified, js);
  266. env->DeleteLocalRef(js);
  267. return result;
  268. } else {
  269. return 0;
  270. }
  271. }
  272. uint64_t FileAccessFilesystemJAndroid::_get_access_time(const String &p_file) {
  273. if (_file_last_accessed) {
  274. JNIEnv *env = get_jni_env();
  275. ERR_FAIL_NULL_V(env, 0);
  276. String path = fix_path(p_file).simplify_path();
  277. jstring js = env->NewStringUTF(path.utf8().get_data());
  278. uint64_t result = env->CallLongMethod(file_access_handler, _file_last_accessed, js);
  279. env->DeleteLocalRef(js);
  280. return result;
  281. } else {
  282. return 0;
  283. }
  284. }
  285. int64_t FileAccessFilesystemJAndroid::_get_size(const String &p_file) {
  286. if (_file_size) {
  287. JNIEnv *env = get_jni_env();
  288. ERR_FAIL_NULL_V(env, -1);
  289. String path = fix_path(p_file).simplify_path();
  290. jstring js = env->NewStringUTF(path.utf8().get_data());
  291. int64_t result = env->CallLongMethod(file_access_handler, _file_size, js);
  292. env->DeleteLocalRef(js);
  293. return result;
  294. } else {
  295. return -1;
  296. }
  297. }
  298. void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) {
  299. JNIEnv *env = get_jni_env();
  300. file_access_handler = env->NewGlobalRef(p_file_access_handler);
  301. jclass c = env->GetObjectClass(file_access_handler);
  302. cls = (jclass)env->NewGlobalRef(c);
  303. _file_open = env->GetMethodID(cls, "fileOpen", "(Ljava/lang/String;I)I");
  304. _file_get_size = env->GetMethodID(cls, "fileGetSize", "(I)J");
  305. _file_tell = env->GetMethodID(cls, "fileGetPosition", "(I)J");
  306. _file_eof = env->GetMethodID(cls, "isFileEof", "(I)Z");
  307. _file_set_eof = env->GetMethodID(cls, "setFileEof", "(IZ)V");
  308. _file_seek = env->GetMethodID(cls, "fileSeek", "(IJ)V");
  309. _file_seek_end = env->GetMethodID(cls, "fileSeekFromEnd", "(IJ)V");
  310. _file_read = env->GetMethodID(cls, "fileRead", "(ILjava/nio/ByteBuffer;)I");
  311. _file_close = env->GetMethodID(cls, "fileClose", "(I)V");
  312. _file_write = env->GetMethodID(cls, "fileWrite", "(ILjava/nio/ByteBuffer;)Z");
  313. _file_flush = env->GetMethodID(cls, "fileFlush", "(I)V");
  314. _file_exists = env->GetMethodID(cls, "fileExists", "(Ljava/lang/String;)Z");
  315. _file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J");
  316. _file_last_accessed = env->GetMethodID(cls, "fileLastAccessed", "(Ljava/lang/String;)J");
  317. _file_resize = env->GetMethodID(cls, "fileResize", "(IJ)I");
  318. _file_size = env->GetMethodID(cls, "fileSize", "(Ljava/lang/String;)J");
  319. }
  320. void FileAccessFilesystemJAndroid::terminate() {
  321. JNIEnv *env = get_jni_env();
  322. ERR_FAIL_NULL(env);
  323. env->DeleteGlobalRef(cls);
  324. env->DeleteGlobalRef(file_access_handler);
  325. }
  326. void FileAccessFilesystemJAndroid::close() {
  327. if (is_open()) {
  328. _close();
  329. }
  330. }
  331. FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() {
  332. id = 0;
  333. }
  334. FileAccessFilesystemJAndroid::~FileAccessFilesystemJAndroid() {
  335. if (is_open()) {
  336. _close();
  337. }
  338. }