file_access_filesystem_jandroid.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/os/os.h"
  32. #include "thread_jandroid.h"
  33. #include <unistd.h>
  34. jobject FileAccessFilesystemJAndroid::file_access_handler = nullptr;
  35. jclass FileAccessFilesystemJAndroid::cls;
  36. jmethodID FileAccessFilesystemJAndroid::_file_open = nullptr;
  37. jmethodID FileAccessFilesystemJAndroid::_file_get_size = nullptr;
  38. jmethodID FileAccessFilesystemJAndroid::_file_seek = nullptr;
  39. jmethodID FileAccessFilesystemJAndroid::_file_seek_end = nullptr;
  40. jmethodID FileAccessFilesystemJAndroid::_file_read = nullptr;
  41. jmethodID FileAccessFilesystemJAndroid::_file_tell = nullptr;
  42. jmethodID FileAccessFilesystemJAndroid::_file_eof = nullptr;
  43. jmethodID FileAccessFilesystemJAndroid::_file_close = nullptr;
  44. jmethodID FileAccessFilesystemJAndroid::_file_write = nullptr;
  45. jmethodID FileAccessFilesystemJAndroid::_file_flush = nullptr;
  46. jmethodID FileAccessFilesystemJAndroid::_file_exists = nullptr;
  47. jmethodID FileAccessFilesystemJAndroid::_file_last_modified = nullptr;
  48. String FileAccessFilesystemJAndroid::get_path() const {
  49. return path_src;
  50. }
  51. String FileAccessFilesystemJAndroid::get_path_absolute() const {
  52. return absolute_path;
  53. }
  54. Error FileAccessFilesystemJAndroid::_open(const String &p_path, int p_mode_flags) {
  55. if (is_open()) {
  56. close();
  57. }
  58. if (_file_open) {
  59. JNIEnv *env = get_jni_env();
  60. ERR_FAIL_COND_V(env == nullptr, ERR_UNCONFIGURED);
  61. String path = fix_path(p_path).simplify_path();
  62. jstring js = env->NewStringUTF(path.utf8().get_data());
  63. int res = env->CallIntMethod(file_access_handler, _file_open, js, p_mode_flags);
  64. env->DeleteLocalRef(js);
  65. if (res <= 0) {
  66. switch (res) {
  67. case 0:
  68. default:
  69. return ERR_FILE_CANT_OPEN;
  70. case -1:
  71. return ERR_FILE_NOT_FOUND;
  72. }
  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_COND(env == nullptr);
  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_COND(env == nullptr);
  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_COND(env == nullptr);
  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_COND_V(env == nullptr, 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_len() const {
  123. if (_file_get_size) {
  124. JNIEnv *env = get_jni_env();
  125. ERR_FAIL_COND_V(env == nullptr, 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_COND_V(env == nullptr, 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. uint8_t FileAccessFilesystemJAndroid::get_8() const {
  143. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  144. uint8_t byte;
  145. get_buffer(&byte, 1);
  146. return byte;
  147. }
  148. String FileAccessFilesystemJAndroid::get_line() const {
  149. ERR_FAIL_COND_V_MSG(!is_open(), String(), "File must be opened before use.");
  150. const size_t buffer_size_limit = 2048;
  151. const uint64_t file_size = get_len();
  152. const uint64_t start_position = get_position();
  153. String result;
  154. LocalVector<uint8_t> line_buffer;
  155. size_t current_buffer_size = 0;
  156. uint64_t line_buffer_position = 0;
  157. while (true) {
  158. size_t line_buffer_size = MIN(buffer_size_limit, file_size - get_position());
  159. if (line_buffer_size <= 0) {
  160. break;
  161. }
  162. current_buffer_size += line_buffer_size;
  163. line_buffer.resize(current_buffer_size);
  164. uint64_t bytes_read = get_buffer(&line_buffer[line_buffer_position], current_buffer_size - line_buffer_position);
  165. if (bytes_read <= 0) {
  166. break;
  167. }
  168. for (; bytes_read > 0; line_buffer_position++, bytes_read--) {
  169. uint8_t elem = line_buffer[line_buffer_position];
  170. if (elem == '\n' || elem == '\0') {
  171. // Found the end of the line
  172. const_cast<FileAccessFilesystemJAndroid *>(this)->seek(start_position + line_buffer_position + 1);
  173. if (result.parse_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
  174. return String();
  175. }
  176. return result;
  177. }
  178. }
  179. }
  180. if (result.parse_utf8((const char *)line_buffer.ptr(), line_buffer_position, true)) {
  181. return String();
  182. }
  183. return result;
  184. }
  185. uint64_t FileAccessFilesystemJAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  186. if (_file_read) {
  187. ERR_FAIL_COND_V_MSG(!is_open(), 0, "File must be opened before use.");
  188. if (p_length == 0) {
  189. return 0;
  190. }
  191. JNIEnv *env = get_jni_env();
  192. ERR_FAIL_COND_V(env == nullptr, 0);
  193. jobject j_buffer = env->NewDirectByteBuffer(p_dst, p_length);
  194. int length = env->CallIntMethod(file_access_handler, _file_read, id, j_buffer);
  195. env->DeleteLocalRef(j_buffer);
  196. return length;
  197. } else {
  198. return 0;
  199. }
  200. }
  201. void FileAccessFilesystemJAndroid::store_8(uint8_t p_dest) {
  202. store_buffer(&p_dest, 1);
  203. }
  204. void FileAccessFilesystemJAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  205. if (_file_write) {
  206. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  207. if (p_length == 0) {
  208. return;
  209. }
  210. JNIEnv *env = get_jni_env();
  211. ERR_FAIL_COND(env == nullptr);
  212. jobject j_buffer = env->NewDirectByteBuffer((void *)p_src, p_length);
  213. env->CallVoidMethod(file_access_handler, _file_write, id, j_buffer);
  214. env->DeleteLocalRef(j_buffer);
  215. }
  216. }
  217. Error FileAccessFilesystemJAndroid::get_error() const {
  218. if (eof_reached()) {
  219. return ERR_FILE_EOF;
  220. }
  221. return OK;
  222. }
  223. void FileAccessFilesystemJAndroid::flush() {
  224. if (_file_flush) {
  225. JNIEnv *env = get_jni_env();
  226. ERR_FAIL_COND(env == nullptr);
  227. ERR_FAIL_COND_MSG(!is_open(), "File must be opened before use.");
  228. env->CallVoidMethod(file_access_handler, _file_flush, id);
  229. }
  230. }
  231. bool FileAccessFilesystemJAndroid::file_exists(const String &p_path) {
  232. if (_file_exists) {
  233. JNIEnv *env = get_jni_env();
  234. ERR_FAIL_COND_V(env == nullptr, false);
  235. String path = fix_path(p_path).simplify_path();
  236. jstring js = env->NewStringUTF(path.utf8().get_data());
  237. bool result = env->CallBooleanMethod(file_access_handler, _file_exists, js);
  238. env->DeleteLocalRef(js);
  239. return result;
  240. } else {
  241. return false;
  242. }
  243. }
  244. uint64_t FileAccessFilesystemJAndroid::_get_modified_time(const String &p_file) {
  245. if (_file_last_modified) {
  246. JNIEnv *env = get_jni_env();
  247. ERR_FAIL_COND_V(env == nullptr, false);
  248. String path = fix_path(p_file).simplify_path();
  249. jstring js = env->NewStringUTF(path.utf8().get_data());
  250. uint64_t result = env->CallLongMethod(file_access_handler, _file_last_modified, js);
  251. env->DeleteLocalRef(js);
  252. return result;
  253. } else {
  254. return 0;
  255. }
  256. }
  257. void FileAccessFilesystemJAndroid::setup(jobject p_file_access_handler) {
  258. JNIEnv *env = get_jni_env();
  259. file_access_handler = env->NewGlobalRef(p_file_access_handler);
  260. jclass c = env->GetObjectClass(file_access_handler);
  261. cls = (jclass)env->NewGlobalRef(c);
  262. _file_open = env->GetMethodID(cls, "fileOpen", "(Ljava/lang/String;I)I");
  263. _file_get_size = env->GetMethodID(cls, "fileGetSize", "(I)J");
  264. _file_tell = env->GetMethodID(cls, "fileGetPosition", "(I)J");
  265. _file_eof = env->GetMethodID(cls, "isFileEof", "(I)Z");
  266. _file_seek = env->GetMethodID(cls, "fileSeek", "(IJ)V");
  267. _file_seek_end = env->GetMethodID(cls, "fileSeekFromEnd", "(IJ)V");
  268. _file_read = env->GetMethodID(cls, "fileRead", "(ILjava/nio/ByteBuffer;)I");
  269. _file_close = env->GetMethodID(cls, "fileClose", "(I)V");
  270. _file_write = env->GetMethodID(cls, "fileWrite", "(ILjava/nio/ByteBuffer;)V");
  271. _file_flush = env->GetMethodID(cls, "fileFlush", "(I)V");
  272. _file_exists = env->GetMethodID(cls, "fileExists", "(Ljava/lang/String;)Z");
  273. _file_last_modified = env->GetMethodID(cls, "fileLastModified", "(Ljava/lang/String;)J");
  274. }
  275. FileAccessFilesystemJAndroid::FileAccessFilesystemJAndroid() {
  276. id = 0;
  277. }
  278. FileAccessFilesystemJAndroid::~FileAccessFilesystemJAndroid() {
  279. if (is_open()) {
  280. close();
  281. }
  282. }