Path: blob/master/platform/android/file_access_android.cpp
10277 views
/**************************************************************************/1/* file_access_android.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "file_access_android.h"3132#include "core/string/print_string.h"33#include "thread_jandroid.h"3435#include <android/asset_manager_jni.h>3637AAssetManager *FileAccessAndroid::asset_manager = nullptr;38jobject FileAccessAndroid::j_asset_manager = nullptr;3940String FileAccessAndroid::get_path() const {41return path_src;42}4344String FileAccessAndroid::get_path_absolute() const {45return absolute_path;46}4748Error FileAccessAndroid::open_internal(const String &p_path, int p_mode_flags) {49_close();5051path_src = p_path;52String path = fix_path(p_path).simplify_path();53absolute_path = path;54if (path.begins_with("/")) {55path = path.substr(1);56} else if (path.begins_with("res://")) {57path = path.substr(6);58}5960ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, ERR_UNAVAILABLE); //can't write on android..61asset = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);62if (!asset) {63return ERR_CANT_OPEN;64}65len = AAsset_getLength(asset);66pos = 0;67eof = false;6869return OK;70}7172void FileAccessAndroid::_close() {73if (!asset) {74return;75}76AAsset_close(asset);77asset = nullptr;78}7980bool FileAccessAndroid::is_open() const {81return asset != nullptr;82}8384void FileAccessAndroid::seek(uint64_t p_position) {85ERR_FAIL_NULL(asset);8687AAsset_seek(asset, p_position, SEEK_SET);88pos = p_position;89if (pos > len) {90pos = len;91eof = true;92} else {93eof = false;94}95}9697void FileAccessAndroid::seek_end(int64_t p_position) {98ERR_FAIL_NULL(asset);99AAsset_seek(asset, p_position, SEEK_END);100pos = len + p_position;101}102103uint64_t FileAccessAndroid::get_position() const {104return pos;105}106107uint64_t FileAccessAndroid::get_length() const {108return len;109}110111bool FileAccessAndroid::eof_reached() const {112return eof;113}114115uint64_t FileAccessAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {116ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);117118int r = AAsset_read(asset, p_dst, p_length);119120if (pos + p_length > len) {121eof = true;122}123124if (r >= 0) {125pos += r;126if (pos > len) {127pos = len;128}129}130131return r;132}133134int64_t FileAccessAndroid::_get_size(const String &p_file) {135return AAsset_getLength64(asset);136}137138Error FileAccessAndroid::get_error() const {139return eof ? ERR_FILE_EOF : OK; // not sure what else it may happen140}141142void FileAccessAndroid::flush() {143ERR_FAIL();144}145146bool FileAccessAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {147ERR_FAIL_V(false);148}149150bool FileAccessAndroid::file_exists(const String &p_path) {151String path = fix_path(p_path).simplify_path();152if (path.begins_with("/")) {153path = path.substr(1);154} else if (path.begins_with("res://")) {155path = path.substr(6);156}157158AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);159160if (!at) {161return false;162}163164AAsset_close(at);165return true;166}167168void FileAccessAndroid::close() {169_close();170}171172FileAccessAndroid::~FileAccessAndroid() {173_close();174}175176void FileAccessAndroid::setup(jobject p_asset_manager) {177JNIEnv *env = get_jni_env();178j_asset_manager = env->NewGlobalRef(p_asset_manager);179asset_manager = AAssetManager_fromJava(env, j_asset_manager);180}181182void FileAccessAndroid::terminate() {183JNIEnv *env = get_jni_env();184ERR_FAIL_NULL(env);185186env->DeleteGlobalRef(j_asset_manager);187}188189190