2
0

file_access_filesystem_jandroid.cpp 11 KB

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