2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* file_access_android.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
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "file_access_android.h"
|
2022-02-16 13:56:32 +01:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/print_string.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
AAssetManager *FileAccessAndroid::asset_manager = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-07-11 03:39:31 +02:00
|
|
|
String FileAccessAndroid::get_path() const {
|
|
|
|
return path_src;
|
|
|
|
}
|
|
|
|
|
|
|
|
String FileAccessAndroid::get_path_absolute() const {
|
|
|
|
return absolute_path;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error FileAccessAndroid::_open(const String &p_path, int p_mode_flags) {
|
2022-04-12 10:15:02 +02:00
|
|
|
_close();
|
|
|
|
|
2021-07-11 03:39:31 +02:00
|
|
|
path_src = p_path;
|
2017-03-05 16:44:50 +01:00
|
|
|
String path = fix_path(p_path).simplify_path();
|
2021-07-11 03:39:31 +02:00
|
|
|
absolute_path = path;
|
2022-02-16 13:56:32 +01:00
|
|
|
if (path.begins_with("/")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(1, path.length());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (path.begins_with("res://")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(6, path.length());
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, ERR_UNAVAILABLE); //can't write on android..
|
2022-05-30 23:13:49 +02:00
|
|
|
asset = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
|
|
|
|
if (!asset) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_CANT_OPEN;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2022-05-30 23:13:49 +02:00
|
|
|
len = AAsset_getLength(asset);
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = 0;
|
|
|
|
eof = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:15:02 +02:00
|
|
|
void FileAccessAndroid::_close() {
|
2022-05-30 23:13:49 +02:00
|
|
|
if (!asset) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2022-05-30 23:13:49 +02:00
|
|
|
AAsset_close(asset);
|
|
|
|
asset = nullptr;
|
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 FileAccessAndroid::is_open() const {
|
2022-05-30 23:13:49 +02:00
|
|
|
return asset != nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
void FileAccessAndroid::seek(uint64_t p_position) {
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL(asset);
|
2019-03-26 18:51:13 +01:00
|
|
|
|
2022-05-30 23:13:49 +02:00
|
|
|
AAsset_seek(asset, p_position, SEEK_SET);
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = p_position;
|
|
|
|
if (pos > len) {
|
|
|
|
pos = len;
|
|
|
|
eof = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
eof = false;
|
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 FileAccessAndroid::seek_end(int64_t p_position) {
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL(asset);
|
|
|
|
AAsset_seek(asset, p_position, SEEK_END);
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = len + p_position;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
uint64_t FileAccessAndroid::get_position() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return pos;
|
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2021-05-25 08:58:49 +02:00
|
|
|
uint64_t FileAccessAndroid::get_length() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileAccessAndroid::eof_reached() const {
|
|
|
|
return eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t FileAccessAndroid::get_8() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pos >= len) {
|
|
|
|
eof = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
uint8_t byte;
|
2022-05-30 23:13:49 +02:00
|
|
|
AAsset_read(asset, &byte, 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
pos++;
|
|
|
|
return byte;
|
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
uint64_t FileAccessAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
|
2021-04-23 21:32:46 +02:00
|
|
|
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
|
2021-03-09 02:37:35 +01:00
|
|
|
|
2022-05-30 23:13:49 +02:00
|
|
|
int r = AAsset_read(asset, p_dst, p_length);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pos + p_length > len) {
|
|
|
|
eof = true;
|
2016-01-09 23:43:34 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (r >= 0) {
|
|
|
|
pos += r;
|
|
|
|
if (pos > len) {
|
|
|
|
pos = len;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error FileAccessAndroid::get_error() const {
|
2021-07-11 03:39:31 +02:00
|
|
|
return eof ? ERR_FILE_EOF : OK; // not sure what else it may happen
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-09-22 07:56:02 +02:00
|
|
|
void FileAccessAndroid::flush() {
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void FileAccessAndroid::store_8(uint8_t p_dest) {
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool FileAccessAndroid::file_exists(const String &p_path) {
|
|
|
|
String path = fix_path(p_path).simplify_path();
|
2022-02-16 13:56:32 +01:00
|
|
|
if (path.begins_with("/")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(1, path.length());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (path.begins_with("res://")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(6, path.length());
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!at) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
AAsset_close(at);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccessAndroid::~FileAccessAndroid() {
|
2022-04-12 10:15:02 +02:00
|
|
|
_close();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|