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