file_access_filesystem_jandroid.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. uint16_t FileAccessFilesystemJAndroid::get_16() const {
  159. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  160. uint16_t bytes = 0;
  161. get_buffer(reinterpret_cast<uint8_t *>(&bytes), 2);
  162. if (big_endian) {
  163. bytes = BSWAP16(bytes);
  164. }
  165. return bytes;
  166. }
  167. uint32_t FileAccessFilesystemJAndroid::get_32() const {
  168. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  169. uint32_t bytes = 0;
  170. get_buffer(reinterpret_cast<uint8_t *>(&bytes), 4);
  171. if (big_endian) {
  172. bytes = BSWAP32(bytes);
  173. }
  174. return bytes;
  175. }
  176. uint64_t FileAccessFilesystemJAndroid::get_64() const {
  177. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  178. uint64_t bytes = 0;
  179. get_buffer(reinterpret_cast<uint8_t *>(&bytes), 8);
  180. if (big_endian) {
  181. bytes = BSWAP64(bytes);
  182. }
  183. return bytes;
  184. }
  185. String FileAccessFilesystemJAndroid::get_line() const {
  186. ERR_FAIL_COND_V_MSG(!is_open(), String(), "File must be opened before use.");
  187. const size_t buffer_size_limit = 2048;
  188. const uint64_t file_size = get_length();
  189. const uint64_t start_position = get_position();
  190. String result;
  191. LocalVector<uint8_t> line_buffer;
  192. size_t current_buffer_size = 0;
  193. uint64_t line_buffer_position = 0;
  194. while (true) {
  195. size_t line_buffer_size = MIN(buffer_size_limit, file_size - get_position());
  196. if (line_buffer_size <= 0) {
  197. const_cast<FileAccessFilesystemJAndroid *>(this)->_set_eof(true);
  198. break;
  199. }
  200. current_buffer_size += line_buffer_size;
  201. line_buffer.resize(current_buffer_size);
  202. uint64_t bytes_read = get_buffer(&line_buffer[line_buffer_position], current_buffer_size - line_buffer_position);
  203. if (bytes_read <= 0) {
  204. break;
  205. }
  206. for (; bytes_read > 0; line_buffer_position++, bytes_read--) {
  207. uint8_t elem = line_buffer[line_buffer_position];
  208. if (elem == '\n' || elem == '\0') {
  209. // Found the end of the line
  210. const_cast<FileAccessFilesystemJAndroid *>(this)->seek(start_position + line_buffer_position + 1);
  211. if (result.parse_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
  212. return String();
  213. }
  214. return result;
  215. }
  216. }
  217. }
  218. if (result.parse_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
  219. return String();
  220. }
  221. return result;
  222. }
  223. uint64_t FileAccessFilesystemJAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  224. if (_file_read) {
  225. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  226. if (p_length == 0) {
  227. return 0;
  228. }
  229. JNIEnv *env = get_jni_env();
  230. ERR_FAIL_NULL_V(env, 0);
  231. jobject j_buffer = env->NewDirectByteBuffer(p_dst, p_length);
  232. int length = env->CallIntMethod(file_access_handler, _file_read, id, j_buffer);
  233. env->DeleteLocalRef(j_buffer);
  234. return length;
  235. } else {
  236. return 0;
  237. }
  238. }
  239. void FileAccessFilesystemJAndroid::store_8(uint8_t p_dest) {
  240. store_buffer(&p_dest, 1);
  241. }
  242. void FileAccessFilesystemJAndroid::store_16(uint16_t p_dest) {
  243. if (big_endian) {
  244. p_dest = BSWAP16(p_dest);
  245. }
  246. store_buffer(reinterpret_cast<uint8_t *>(&p_dest), 2);
  247. }
  248. void FileAccessFilesystemJAndroid::store_32(uint32_t p_dest) {
  249. if (big_endian) {
  250. p_dest = BSWAP32(p_dest);
  251. }
  252. store_buffer(reinterpret_cast<uint8_t *>(&p_dest), 4);
  253. }
  254. void FileAccessFilesystemJAndroid::store_64(uint64_t p_dest) {
  255. if (big_endian) {
  256. p_dest = BSWAP64(p_dest);
  257. }
  258. store_buffer(reinterpret_cast<uint8_t *>(&p_dest), 8);
  259. }
  260. void FileAccessFilesystemJAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  261. if (_file_write) {
  262. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  263. if (p_length == 0) {
  264. return;
  265. }
  266. JNIEnv *env = get_jni_env();
  267. ERR_FAIL_NULL(env);
  268. jobject j_buffer = env->NewDirectByteBuffer((void *)p_src, p_length);
  269. env->CallVoidMethod(file_access_handler, _file_write, id, j_buffer);
  270. env->DeleteLocalRef(j_buffer);
  271. }
  272. }
  273. Error FileAccessFilesystemJAndroid::get_error() const {
  274. if (eof_reached()) {
  275. return ERR_FILE_EOF;
  276. }
  277. return OK;
  278. }
  279. void FileAccessFilesystemJAndroid::flush() {
  280. if (_file_flush) {
  281. JNIEnv *env = get_jni_env();
  282. ERR_FAIL_NULL(env);
  283. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  284. env->CallVoidMethod(file_access_handler, _file_flush, id);
  285. }
  286. }
  287. bool FileAccessFilesystemJAndroid::file_exists(const String &p_path) {
  288. if (_file_exists) {
  289. JNIEnv *env = get_jni_env();
  290. ERR_FAIL_NULL_V(env, false);
  291. String path = fix_path(p_path).simplify_path();
  292. jstring js = env->NewStringUTF(path.utf8().get_data());
  293. bool result = env->CallBooleanMethod(file_access_handler, _file_exists, js);
  294. env->DeleteLocalRef(js);
  295. return result;
  296. } else {
  297. return false;
  298. }
  299. }
  300. uint64_t FileAccessFilesystemJAndroid::_get_modified_time(const String &p_file) {
  301. if (_file_last_modified) {
  302. JNIEnv *env = get_jni_env();
  303. ERR_FAIL_NULL_V(env, false);
  304. String path = fix_path(p_file).simplify_path();
  305. jstring js = env->NewStringUTF(path.utf8().get_data());
  306. uint64_t result = env->CallLongMethod(file_access_handler, _file_last_modified, js);
  307. env->DeleteLocalRef(js);
  308. return result;
  309. } else {
  310. return 0;
  311. }
  312. }
  313. void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) {
  314. JNIEnv *env = get_jni_env();
  315. file_access_handler = env->NewGlobalRef(p_file_access_handler);
  316. jclass c = env->GetObjectClass(file_access_handler);
  317. cls = (jclass)env->NewGlobalRef(c);
  318. _file_open = env->GetMethodID(cls, "fileOpen", "(Ljava/lang/String;I)I");
  319. _file_get_size = env->GetMethodID(cls, "fileGetSize", "(I)J");
  320. _file_tell = env->GetMethodID(cls, "fileGetPosition", "(I)J");
  321. _file_eof = env->GetMethodID(cls, "isFileEof", "(I)Z");
  322. _file_set_eof = env->GetMethodID(cls, "setFileEof", "(IZ)V");
  323. _file_seek = env->GetMethodID(cls, "fileSeek", "(IJ)V");
  324. _file_seek_end = env->GetMethodID(cls, "fileSeekFromEnd", "(IJ)V");
  325. _file_read = env->GetMethodID(cls, "fileRead", "(ILjava/nio/ByteBuffer;)I");
  326. _file_close = env->GetMethodID(cls, "fileClose", "(I)V");
  327. _file_write = env->GetMethodID(cls, "fileWrite", "(ILjava/nio/ByteBuffer;)V");
  328. _file_flush = env->GetMethodID(cls, "fileFlush", "(I)V");
  329. _file_exists = env->GetMethodID(cls, "fileExists", "(Ljava/lang/String;)Z");
  330. _file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J");
  331. }
  332. void FileAccessFilesystemJAndroid::terminate() {
  333. JNIEnv *env = get_jni_env();
  334. ERR_FAIL_NULL(env);
  335. env->DeleteGlobalRef(cls);
  336. env->DeleteGlobalRef(file_access_handler);
  337. }
  338. void FileAccessFilesystemJAndroid::close() {
  339. if (is_open()) {
  340. _close();
  341. }
  342. }
  343. FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() {
  344. id = 0;
  345. }
  346. FileAccessFilesystemJAndroid::~FileAccessFilesystemJAndroid() {
  347. if (is_open()) {
  348. _close();
  349. }
  350. }