godot/platform/android/file_access_android.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

185 lines
5.3 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/**************************************************************************/
/* file_access_android.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
2014-02-10 01:10:30 +00:00
#include "file_access_android.h"
#include "core/string/print_string.h"
#include "thread_jandroid.h"
#include <android/asset_manager_jni.h>
2014-02-10 01:10:30 +00:00
2020-04-01 23:20:12 +00:00
AAssetManager *FileAccessAndroid::asset_manager = nullptr;
jobject FileAccessAndroid::j_asset_manager = nullptr;
2014-02-10 01:10:30 +00:00
String FileAccessAndroid::get_path() const {
return path_src;
}
String FileAccessAndroid::get_path_absolute() const {
return absolute_path;
2014-02-10 01:10:30 +00:00
}
Error FileAccessAndroid::open_internal(const String &p_path, int p_mode_flags) {
_close();
path_src = p_path;
2014-02-10 01:10:30 +00:00
String path = fix_path(p_path).simplify_path();
absolute_path = path;
if (path.begins_with("/")) {
2014-02-10 01:10:30 +00:00
path = path.substr(1, path.length());
} else if (path.begins_with("res://")) {
2014-02-10 01:10:30 +00:00
path = path.substr(6, path.length());
}
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, ERR_UNAVAILABLE); //can't write on android..
2022-05-30 21:13:49 +00:00
asset = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
if (!asset) {
2014-02-10 01:10:30 +00:00
return ERR_CANT_OPEN;
}
2022-05-30 21:13:49 +00:00
len = AAsset_getLength(asset);
2014-02-10 01:10:30 +00:00
pos = 0;
eof = false;
return OK;
}
void FileAccessAndroid::_close() {
2022-05-30 21:13:49 +00:00
if (!asset) {
2014-02-10 01:10:30 +00:00
return;
}
2022-05-30 21:13:49 +00:00
AAsset_close(asset);
asset = nullptr;
2014-02-10 01:10:30 +00:00
}
2014-02-10 01:10:30 +00:00
bool FileAccessAndroid::is_open() const {
2022-05-30 21:13:49 +00:00
return asset != nullptr;
2014-02-10 01:10:30 +00:00
}
void FileAccessAndroid::seek(uint64_t p_position) {
2022-05-30 21:13:49 +00:00
ERR_FAIL_NULL(asset);
2022-05-30 21:13:49 +00:00
AAsset_seek(asset, p_position, SEEK_SET);
2014-02-10 01:10:30 +00:00
pos = p_position;
if (pos > len) {
pos = len;
eof = true;
} else {
eof = false;
}
}
2014-02-10 01:10:30 +00:00
void FileAccessAndroid::seek_end(int64_t p_position) {
2022-05-30 21:13:49 +00:00
ERR_FAIL_NULL(asset);
AAsset_seek(asset, p_position, SEEK_END);
2014-02-10 01:10:30 +00:00
pos = len + p_position;
}
uint64_t FileAccessAndroid::get_position() const {
2014-02-10 01:10:30 +00:00
return pos;
}
2021-05-25 06:58:49 +00:00
uint64_t FileAccessAndroid::get_length() const {
2014-02-10 01:10:30 +00:00
return len;
}
bool FileAccessAndroid::eof_reached() const {
return eof;
}
uint64_t FileAccessAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
2022-05-30 21:13:49 +00:00
int r = AAsset_read(asset, p_dst, p_length);
2014-02-10 01:10:30 +00:00
if (pos + p_length > len) {
eof = true;
}
2014-02-10 01:10:30 +00:00
if (r >= 0) {
pos += r;
if (pos > len) {
pos = len;
}
}
2024-05-20 16:07:27 +00:00
2014-02-10 01:10:30 +00:00
return r;
}
Error FileAccessAndroid::get_error() const {
return eof ? ERR_FILE_EOF : OK; // not sure what else it may happen
2014-02-10 01:10:30 +00:00
}
void FileAccessAndroid::flush() {
ERR_FAIL();
}
2024-05-20 16:07:27 +00:00
void FileAccessAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL();
}
2014-02-10 01:10:30 +00:00
bool FileAccessAndroid::file_exists(const String &p_path) {
String path = fix_path(p_path).simplify_path();
if (path.begins_with("/")) {
2014-02-10 01:10:30 +00:00
path = path.substr(1, path.length());
} else if (path.begins_with("res://")) {
2014-02-10 01:10:30 +00:00
path = path.substr(6, path.length());
}
2014-02-10 01:10:30 +00:00
AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
if (!at) {
2014-02-10 01:10:30 +00:00
return false;
}
2014-02-10 01:10:30 +00:00
AAsset_close(at);
return true;
}
2023-02-16 13:25:32 +00:00
void FileAccessAndroid::close() {
_close();
}
2014-02-10 01:10:30 +00:00
FileAccessAndroid::~FileAccessAndroid() {
_close();
2014-02-10 01:10:30 +00:00
}
void FileAccessAndroid::setup(jobject p_asset_manager) {
JNIEnv *env = get_jni_env();
j_asset_manager = env->NewGlobalRef(p_asset_manager);
asset_manager = AAssetManager_fromJava(env, j_asset_manager);
}
void FileAccessAndroid::terminate() {
JNIEnv *env = get_jni_env();
ERR_FAIL_NULL(env);
env->DeleteGlobalRef(j_asset_manager);
}