2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* file_access_android.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-02 05:01:57 +08:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 09:10:30 +08: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "file_access_android.h"
|
|
|
|
#include "print_string.h"
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
AAssetManager *FileAccessAndroid::asset_manager = NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2015-09-04 10:24:55 +08:00
|
|
|
/*void FileAccessAndroid::make_default() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
create_func=create_android;
|
2015-09-04 10:24:55 +08:00
|
|
|
}*/
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
FileAccess *FileAccessAndroid::create_android() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return memnew(FileAccessAndroid);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Error FileAccessAndroid::_open(const String &p_path, int p_mode_flags) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String path = fix_path(p_path).simplify_path();
|
2014-02-10 09:10:30 +08:00
|
|
|
if (path.begins_with("/"))
|
2017-03-05 23:44:50 +08:00
|
|
|
path = path.substr(1, path.length());
|
2014-02-10 09:10:30 +08:00
|
|
|
else if (path.begins_with("res://"))
|
2017-03-05 23:44:50 +08:00
|
|
|
path = path.substr(6, path.length());
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, ERR_UNAVAILABLE); //can't write on android..
|
|
|
|
a = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
|
2014-02-10 09:10:30 +08:00
|
|
|
if (!a)
|
|
|
|
return ERR_CANT_OPEN;
|
|
|
|
//ERR_FAIL_COND_V(!a,ERR_FILE_NOT_FOUND);
|
2017-03-05 23:44:50 +08:00
|
|
|
len = AAsset_getLength(a);
|
|
|
|
pos = 0;
|
|
|
|
eof = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccessAndroid::close() {
|
|
|
|
|
|
|
|
if (!a)
|
|
|
|
return;
|
|
|
|
AAsset_close(a);
|
2017-03-05 23:44:50 +08:00
|
|
|
a = NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
bool FileAccessAndroid::is_open() const {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
return a != NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccessAndroid::seek(size_t p_position) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!a);
|
2017-03-05 23:44:50 +08:00
|
|
|
AAsset_seek(a, p_position, SEEK_SET);
|
|
|
|
pos = p_position;
|
|
|
|
if (pos > len) {
|
|
|
|
pos = len;
|
|
|
|
eof = true;
|
2014-02-10 09:10:30 +08:00
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
eof = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void FileAccessAndroid::seek_end(int64_t p_position) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!a);
|
2017-03-05 23:44:50 +08:00
|
|
|
AAsset_seek(a, p_position, SEEK_END);
|
|
|
|
pos = len + p_position;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
size_t FileAccessAndroid::get_pos() const {
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
size_t FileAccessAndroid::get_len() const {
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileAccessAndroid::eof_reached() const {
|
|
|
|
|
|
|
|
return eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t FileAccessAndroid::get_8() const {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (pos >= len) {
|
|
|
|
eof = true;
|
2014-02-10 09:10:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-09 07:00:52 +08:00
|
|
|
uint8_t byte;
|
2017-03-05 23:44:50 +08:00
|
|
|
AAsset_read(a, &byte, 1);
|
2014-02-10 09:10:30 +08:00
|
|
|
pos++;
|
|
|
|
return byte;
|
|
|
|
}
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
int FileAccessAndroid::get_buffer(uint8_t *p_dst, int p_length) const {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
off_t r = AAsset_read(a, p_dst, p_length);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (pos + p_length > len) {
|
|
|
|
eof = true;
|
2016-01-10 06:43:34 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (r >= 0) {
|
2016-01-10 06:43:34 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
pos += r;
|
|
|
|
if (pos > len) {
|
|
|
|
pos = len;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error FileAccessAndroid::get_error() const {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
return eof ? ERR_FILE_EOF : OK; //not sure what else it may happen
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccessAndroid::store_8(uint8_t p_dest) {
|
|
|
|
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool FileAccessAndroid::file_exists(const String &p_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String path = fix_path(p_path).simplify_path();
|
2014-02-10 09:10:30 +08:00
|
|
|
if (path.begins_with("/"))
|
2017-03-05 23:44:50 +08:00
|
|
|
path = path.substr(1, path.length());
|
2014-02-10 09:10:30 +08:00
|
|
|
else if (path.begins_with("res://"))
|
2017-03-05 23:44:50 +08:00
|
|
|
path = path.substr(6, path.length());
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
if (!at)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AAsset_close(at);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileAccessAndroid::FileAccessAndroid() {
|
2017-03-05 23:44:50 +08:00
|
|
|
a = NULL;
|
|
|
|
eof = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
FileAccessAndroid::~FileAccessAndroid() {
|
2014-02-10 09:10:30 +08:00
|
|
|
close();
|
|
|
|
}
|