diff options
Diffstat (limited to 'platform/android/file_access_jandroid.cpp')
-rw-r--r-- | platform/android/file_access_jandroid.cpp | 139 |
1 files changed, 61 insertions, 78 deletions
diff --git a/platform/android/file_access_jandroid.cpp b/platform/android/file_access_jandroid.cpp index a4e9c191e9..b3624e9f26 100644 --- a/platform/android/file_access_jandroid.cpp +++ b/platform/android/file_access_jandroid.cpp @@ -30,51 +30,46 @@ #include "file_access_jandroid.h" #include "os/os.h" -#include <unistd.h> #include "thread_jandroid.h" +#include <unistd.h> -jobject FileAccessJAndroid::io=NULL; +jobject FileAccessJAndroid::io = NULL; jclass FileAccessJAndroid::cls; -jmethodID FileAccessJAndroid::_file_open=0; -jmethodID FileAccessJAndroid::_file_get_size=0; -jmethodID FileAccessJAndroid::_file_seek=0; -jmethodID FileAccessJAndroid::_file_read=0; -jmethodID FileAccessJAndroid::_file_tell=0; -jmethodID FileAccessJAndroid::_file_eof=0; -jmethodID FileAccessJAndroid::_file_close=0; - +jmethodID FileAccessJAndroid::_file_open = 0; +jmethodID FileAccessJAndroid::_file_get_size = 0; +jmethodID FileAccessJAndroid::_file_seek = 0; +jmethodID FileAccessJAndroid::_file_read = 0; +jmethodID FileAccessJAndroid::_file_tell = 0; +jmethodID FileAccessJAndroid::_file_eof = 0; +jmethodID FileAccessJAndroid::_file_close = 0; -FileAccess* FileAccessJAndroid::create_jandroid() { +FileAccess *FileAccessJAndroid::create_jandroid() { return memnew(FileAccessJAndroid); } -Error FileAccessJAndroid::_open(const String& p_path, int p_mode_flags) { +Error FileAccessJAndroid::_open(const String &p_path, int p_mode_flags) { if (is_open()) close(); - String path=fix_path(p_path).simplify_path(); + String path = fix_path(p_path).simplify_path(); if (path.begins_with("/")) - path=path.substr(1,path.length()); + path = path.substr(1, path.length()); else if (path.begins_with("res://")) - path=path.substr(6,path.length()); + path = path.substr(6, path.length()); JNIEnv *env = ThreadAndroid::get_env(); - - jstring js = env->NewStringUTF(path.utf8().get_data()); - int res = env->CallIntMethod(io,_file_open,js,p_mode_flags&WRITE?true:false); + int res = env->CallIntMethod(io, _file_open, js, p_mode_flags & WRITE ? true : false); env->DeleteLocalRef(js); - OS::get_singleton()->print("fopen: '%s' ret %i\n",path.utf8().get_data(),res); + OS::get_singleton()->print("fopen: '%s' ret %i\n", path.utf8().get_data(), res); - - if (res<=0) + if (res <= 0) return ERR_FILE_CANT_OPEN; - id=res; - + id = res; return OK; } @@ -86,14 +81,13 @@ void FileAccessJAndroid::close() { JNIEnv *env = ThreadAndroid::get_env(); - env->CallVoidMethod(io,_file_close,id); - id=0; - + env->CallVoidMethod(io, _file_close, id); + id = 0; } bool FileAccessJAndroid::is_open() const { - return id!=0; + return id != 0; } void FileAccessJAndroid::seek(size_t p_position) { @@ -101,7 +95,7 @@ void FileAccessJAndroid::seek(size_t p_position) { JNIEnv *env = ThreadAndroid::get_env(); ERR_FAIL_COND(!is_open()); - env->CallVoidMethod(io,_file_seek,id,p_position); + env->CallVoidMethod(io, _file_seek, id, p_position); } void FileAccessJAndroid::seek_end(int64_t p_position) { @@ -109,56 +103,50 @@ void FileAccessJAndroid::seek_end(int64_t p_position) { ERR_FAIL_COND(!is_open()); seek(get_len()); - } size_t FileAccessJAndroid::get_pos() const { JNIEnv *env = ThreadAndroid::get_env(); - ERR_FAIL_COND_V(!is_open(),0); - return env->CallIntMethod(io,_file_tell,id); - + ERR_FAIL_COND_V(!is_open(), 0); + return env->CallIntMethod(io, _file_tell, id); } size_t FileAccessJAndroid::get_len() const { JNIEnv *env = ThreadAndroid::get_env(); - ERR_FAIL_COND_V(!is_open(),0); - return env->CallIntMethod(io,_file_get_size,id); - + ERR_FAIL_COND_V(!is_open(), 0); + return env->CallIntMethod(io, _file_get_size, id); } bool FileAccessJAndroid::eof_reached() const { JNIEnv *env = ThreadAndroid::get_env(); - ERR_FAIL_COND_V(!is_open(),0); - return env->CallIntMethod(io,_file_eof,id); - + ERR_FAIL_COND_V(!is_open(), 0); + return env->CallIntMethod(io, _file_eof, id); } uint8_t FileAccessJAndroid::get_8() const { - ERR_FAIL_COND_V(!is_open(),0); + ERR_FAIL_COND_V(!is_open(), 0); uint8_t byte; - get_buffer(&byte,1); + get_buffer(&byte, 1); return byte; } int FileAccessJAndroid::get_buffer(uint8_t *p_dst, int p_length) const { - ERR_FAIL_COND_V(!is_open(),0); - if (p_length==0) + ERR_FAIL_COND_V(!is_open(), 0); + if (p_length == 0) return 0; JNIEnv *env = ThreadAndroid::get_env(); - jbyteArray jca = (jbyteArray)env->CallObjectMethod(io,_file_read,id,p_length); - + jbyteArray jca = (jbyteArray)env->CallObjectMethod(io, _file_read, id, p_length); int len = env->GetArrayLength(jca); - env->GetByteArrayRegion(jca,0,len,(jbyte*)p_dst); + env->GetByteArrayRegion(jca, 0, len, (jbyte *)p_dst); env->DeleteLocalRef((jobject)jca); return len; - } Error FileAccessJAndroid::get_error() const { @@ -169,80 +157,76 @@ Error FileAccessJAndroid::get_error() const { } void FileAccessJAndroid::store_8(uint8_t p_dest) { - } -bool FileAccessJAndroid::file_exists(const String& p_path) { +bool FileAccessJAndroid::file_exists(const String &p_path) { JNIEnv *env = ThreadAndroid::get_env(); - String path=fix_path(p_path).simplify_path(); + String path = fix_path(p_path).simplify_path(); if (path.begins_with("/")) - path=path.substr(1,path.length()); + path = path.substr(1, path.length()); else if (path.begins_with("res://")) - path=path.substr(6,path.length()); + path = path.substr(6, path.length()); jstring js = env->NewStringUTF(path.utf8().get_data()); - int res = env->CallIntMethod(io,_file_open,js,false); - if (res<=0) { + int res = env->CallIntMethod(io, _file_open, js, false); + if (res <= 0) { env->DeleteLocalRef(js); return false; } - env->CallVoidMethod(io,_file_close,res); + env->CallVoidMethod(io, _file_close, res); env->DeleteLocalRef(js); return true; - } +void FileAccessJAndroid::setup(jobject p_io) { -void FileAccessJAndroid::setup( jobject p_io) { - - io=p_io; + io = p_io; JNIEnv *env = ThreadAndroid::get_env(); - __android_log_print(ANDROID_LOG_INFO,"godot","STEP5"); + __android_log_print(ANDROID_LOG_INFO, "godot", "STEP5"); jclass c = env->GetObjectClass(io); - __android_log_print(ANDROID_LOG_INFO,"godot","STEP6"); - cls=(jclass)env->NewGlobalRef(c); + __android_log_print(ANDROID_LOG_INFO, "godot", "STEP6"); + cls = (jclass)env->NewGlobalRef(c); _file_open = env->GetMethodID(cls, "file_open", "(Ljava/lang/String;Z)I"); - if(_file_open != 0) { - __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_open ok!!"); + if (_file_open != 0) { + __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_open ok!!"); } _file_get_size = env->GetMethodID(cls, "file_get_size", "(I)I"); - if(_file_get_size != 0) { - __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_get_size ok!!"); + if (_file_get_size != 0) { + __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_get_size ok!!"); } _file_tell = env->GetMethodID(cls, "file_tell", "(I)I"); - if(_file_tell != 0) { - __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_tell ok!!"); + if (_file_tell != 0) { + __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_tell ok!!"); } _file_eof = env->GetMethodID(cls, "file_eof", "(I)Z"); - if(_file_eof != 0) { - __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_eof ok!!"); + if (_file_eof != 0) { + __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_eof ok!!"); } _file_seek = env->GetMethodID(cls, "file_seek", "(II)V"); - if(_file_seek != 0) { - __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_seek ok!!"); + if (_file_seek != 0) { + __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_seek ok!!"); } _file_read = env->GetMethodID(cls, "file_read", "(II)[B"); - if(_file_read != 0) { - __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_read ok!!"); + if (_file_read != 0) { + __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_read ok!!"); } _file_close = env->GetMethodID(cls, "file_close", "(I)V"); - if(_file_close != 0) { - __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _file_close ok!!"); + if (_file_close != 0) { + __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_close ok!!"); } //(*env)->CallVoidMethod(env,obj,aMethodID, myvar); } - FileAccessJAndroid::FileAccessJAndroid() { - id=0; + id = 0; } FileAccessJAndroid::~FileAccessJAndroid() { @@ -251,5 +235,4 @@ FileAccessJAndroid::~FileAccessJAndroid() { close(); } - #endif |