Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/file_access_android.cpp
10277 views
1
/**************************************************************************/
2
/* file_access_android.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "file_access_android.h"
32
33
#include "core/string/print_string.h"
34
#include "thread_jandroid.h"
35
36
#include <android/asset_manager_jni.h>
37
38
AAssetManager *FileAccessAndroid::asset_manager = nullptr;
39
jobject FileAccessAndroid::j_asset_manager = nullptr;
40
41
String FileAccessAndroid::get_path() const {
42
return path_src;
43
}
44
45
String FileAccessAndroid::get_path_absolute() const {
46
return absolute_path;
47
}
48
49
Error FileAccessAndroid::open_internal(const String &p_path, int p_mode_flags) {
50
_close();
51
52
path_src = p_path;
53
String path = fix_path(p_path).simplify_path();
54
absolute_path = path;
55
if (path.begins_with("/")) {
56
path = path.substr(1);
57
} else if (path.begins_with("res://")) {
58
path = path.substr(6);
59
}
60
61
ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, ERR_UNAVAILABLE); //can't write on android..
62
asset = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
63
if (!asset) {
64
return ERR_CANT_OPEN;
65
}
66
len = AAsset_getLength(asset);
67
pos = 0;
68
eof = false;
69
70
return OK;
71
}
72
73
void FileAccessAndroid::_close() {
74
if (!asset) {
75
return;
76
}
77
AAsset_close(asset);
78
asset = nullptr;
79
}
80
81
bool FileAccessAndroid::is_open() const {
82
return asset != nullptr;
83
}
84
85
void FileAccessAndroid::seek(uint64_t p_position) {
86
ERR_FAIL_NULL(asset);
87
88
AAsset_seek(asset, p_position, SEEK_SET);
89
pos = p_position;
90
if (pos > len) {
91
pos = len;
92
eof = true;
93
} else {
94
eof = false;
95
}
96
}
97
98
void FileAccessAndroid::seek_end(int64_t p_position) {
99
ERR_FAIL_NULL(asset);
100
AAsset_seek(asset, p_position, SEEK_END);
101
pos = len + p_position;
102
}
103
104
uint64_t FileAccessAndroid::get_position() const {
105
return pos;
106
}
107
108
uint64_t FileAccessAndroid::get_length() const {
109
return len;
110
}
111
112
bool FileAccessAndroid::eof_reached() const {
113
return eof;
114
}
115
116
uint64_t FileAccessAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
117
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
118
119
int r = AAsset_read(asset, p_dst, p_length);
120
121
if (pos + p_length > len) {
122
eof = true;
123
}
124
125
if (r >= 0) {
126
pos += r;
127
if (pos > len) {
128
pos = len;
129
}
130
}
131
132
return r;
133
}
134
135
int64_t FileAccessAndroid::_get_size(const String &p_file) {
136
return AAsset_getLength64(asset);
137
}
138
139
Error FileAccessAndroid::get_error() const {
140
return eof ? ERR_FILE_EOF : OK; // not sure what else it may happen
141
}
142
143
void FileAccessAndroid::flush() {
144
ERR_FAIL();
145
}
146
147
bool FileAccessAndroid::store_buffer(const uint8_t *p_src, uint64_t p_length) {
148
ERR_FAIL_V(false);
149
}
150
151
bool FileAccessAndroid::file_exists(const String &p_path) {
152
String path = fix_path(p_path).simplify_path();
153
if (path.begins_with("/")) {
154
path = path.substr(1);
155
} else if (path.begins_with("res://")) {
156
path = path.substr(6);
157
}
158
159
AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
160
161
if (!at) {
162
return false;
163
}
164
165
AAsset_close(at);
166
return true;
167
}
168
169
void FileAccessAndroid::close() {
170
_close();
171
}
172
173
FileAccessAndroid::~FileAccessAndroid() {
174
_close();
175
}
176
177
void FileAccessAndroid::setup(jobject p_asset_manager) {
178
JNIEnv *env = get_jni_env();
179
j_asset_manager = env->NewGlobalRef(p_asset_manager);
180
asset_manager = AAssetManager_fromJava(env, j_asset_manager);
181
}
182
183
void FileAccessAndroid::terminate() {
184
JNIEnv *env = get_jni_env();
185
ERR_FAIL_NULL(env);
186
187
env->DeleteGlobalRef(j_asset_manager);
188
}
189
190