2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* file_access_jandroid.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifndef ANDROID_NATIVE_ACTIVITY
|
|
|
|
|
|
|
|
#include "file_access_jandroid.h"
|
|
|
|
#include "os/os.h"
|
|
|
|
#include "thread_jandroid.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <unistd.h>
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
jobject FileAccessJAndroid::io = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
jclass FileAccessJAndroid::cls;
|
2017-03-05 16:44:50 +01:00
|
|
|
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;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccess *FileAccessJAndroid::create_jandroid() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return memnew(FileAccessJAndroid);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error FileAccessJAndroid::_open(const String &p_path, int p_mode_flags) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (is_open())
|
|
|
|
close();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String path = fix_path(p_path).simplify_path();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (path.begins_with("/"))
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(1, path.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
else if (path.begins_with("res://"))
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(6, path.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
|
|
|
|
|
|
|
jstring js = env->NewStringUTF(path.utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
int res = env->CallIntMethod(io, _file_open, js, p_mode_flags & WRITE ? true : false);
|
2014-02-10 02:10:30 +01:00
|
|
|
env->DeleteLocalRef(js);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
OS::get_singleton()->print("fopen: '%s' ret %i\n", path.utf8().get_data(), res);
|
2015-09-04 04:24:55 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (res <= 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_FILE_CANT_OPEN;
|
2017-03-05 16:44:50 +01:00
|
|
|
id = res;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccessJAndroid::close() {
|
|
|
|
|
|
|
|
if (!is_open())
|
|
|
|
return;
|
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
env->CallVoidMethod(io, _file_close, id);
|
|
|
|
id = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
bool FileAccessJAndroid::is_open() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return id != 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccessJAndroid::seek(size_t p_position) {
|
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!is_open());
|
2017-03-05 16:44:50 +01:00
|
|
|
env->CallVoidMethod(io, _file_seek, id, p_position);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void FileAccessJAndroid::seek_end(int64_t p_position) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!is_open());
|
|
|
|
|
|
|
|
seek(get_len());
|
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
size_t FileAccessJAndroid::get_position() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!is_open(), 0);
|
|
|
|
return env->CallIntMethod(io, _file_tell, id);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
size_t FileAccessJAndroid::get_len() const {
|
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!is_open(), 0);
|
|
|
|
return env->CallIntMethod(io, _file_get_size, id);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileAccessJAndroid::eof_reached() const {
|
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!is_open(), 0);
|
|
|
|
return env->CallIntMethod(io, _file_eof, id);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t FileAccessJAndroid::get_8() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!is_open(), 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
uint8_t byte;
|
2017-03-05 16:44:50 +01:00
|
|
|
get_buffer(&byte, 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
int FileAccessJAndroid::get_buffer(uint8_t *p_dst, int p_length) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!is_open(), 0);
|
|
|
|
if (p_length == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
jbyteArray jca = (jbyteArray)env->CallObjectMethod(io, _file_read, id, p_length);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int len = env->GetArrayLength(jca);
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetByteArrayRegion(jca, 0, len, (jbyte *)p_dst);
|
2014-02-10 02:10:30 +01:00
|
|
|
env->DeleteLocalRef((jobject)jca);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error FileAccessJAndroid::get_error() const {
|
|
|
|
|
|
|
|
if (eof_reached())
|
|
|
|
return ERR_FILE_EOF;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-09-22 07:56:02 +02:00
|
|
|
void FileAccessJAndroid::flush() {
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void FileAccessJAndroid::store_8(uint8_t p_dest) {
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool FileAccessJAndroid::file_exists(const String &p_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String path = fix_path(p_path).simplify_path();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (path.begins_with("/"))
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(1, path.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
else if (path.begins_with("res://"))
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(6, path.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
jstring js = env->NewStringUTF(path.utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
int res = env->CallIntMethod(io, _file_open, js, false);
|
|
|
|
if (res <= 0) {
|
2016-07-11 06:41:54 +02:00
|
|
|
env->DeleteLocalRef(js);
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2016-07-11 06:41:54 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
env->CallVoidMethod(io, _file_close, res);
|
2014-02-10 02:10:30 +01:00
|
|
|
env->DeleteLocalRef(js);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void FileAccessJAndroid::setup(jobject p_io) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
io = p_io;
|
2014-02-10 02:10:30 +01:00
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "STEP5");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
jclass c = env->GetObjectClass(io);
|
2017-03-05 16:44:50 +01:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "STEP6");
|
|
|
|
cls = (jclass)env->NewGlobalRef(c);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
_file_open = env->GetMethodID(cls, "file_open", "(Ljava/lang/String;Z)I");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (_file_open != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_open ok!!");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
_file_get_size = env->GetMethodID(cls, "file_get_size", "(I)I");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (_file_get_size != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_get_size ok!!");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
_file_tell = env->GetMethodID(cls, "file_tell", "(I)I");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (_file_tell != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_tell ok!!");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
_file_eof = env->GetMethodID(cls, "file_eof", "(I)Z");
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (_file_eof != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_eof ok!!");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
_file_seek = env->GetMethodID(cls, "file_seek", "(II)V");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (_file_seek != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_seek ok!!");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
_file_read = env->GetMethodID(cls, "file_read", "(II)[B");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (_file_read != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_read ok!!");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
_file_close = env->GetMethodID(cls, "file_close", "(I)V");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (_file_close != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _file_close ok!!");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//(*env)->CallVoidMethod(env,obj,aMethodID, myvar);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 12:07:01 +02:00
|
|
|
FileAccessJAndroid::FileAccessJAndroid() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
id = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 12:07:01 +02:00
|
|
|
FileAccessJAndroid::~FileAccessJAndroid() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (is_open())
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|