file_access_filesystem_jandroid.cpp 13 KB

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