17 #include "virtualFileMountAndroidAsset.h" 18 #include "virtualFileSystem.h" 24 TypeHandle VirtualFileMountAndroidAsset::_type_handle;
31 VirtualFileMountAndroidAsset::
32 ~VirtualFileMountAndroidAsset() {
42 int VirtualFileMountAndroidAsset::
43 get_fd(
const Filename &file, off_t *start, off_t *length)
const {
45 asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
47 int fd = AAsset_openFileDescriptor(asset, start, length);
58 bool VirtualFileMountAndroidAsset::
59 has_file(
const Filename &file)
const {
60 return (file.empty() || is_regular_file(file));
69 bool VirtualFileMountAndroidAsset::
70 is_directory(
const Filename &file)
const {
82 return !is_regular_file(file);
91 bool VirtualFileMountAndroidAsset::
92 is_regular_file(
const Filename &file)
const {
95 asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
97 express_cat.error() <<
"is_regular_file " << file <<
" - " << asset <<
"\n";
113 bool VirtualFileMountAndroidAsset::
114 read_file(
const Filename &file,
bool do_uncompress,
128 asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_STREAMING);
129 if (asset == (AAsset *)NULL) {
131 <<
"Unable to read " << file <<
"\n";
136 off_t file_size = AAsset_getLength(asset);
137 if (file_size == 0) {
139 }
else if (file_size > 0) {
140 result.reserve((
size_t)file_size);
143 static const size_t buffer_size = 4096;
144 char buffer[buffer_size];
146 int count = AAsset_read(asset, buffer, buffer_size);
148 thread_consider_yield();
149 result.insert(result.end(), buffer, buffer + count);
150 count = AAsset_read(asset, buffer, buffer_size);
165 istream *VirtualFileMountAndroidAsset::
166 open_read_file(
const Filename &file)
const {
168 asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
169 if (asset == (AAsset *)NULL) {
173 AssetStream *stream =
new AssetStream(asset);
174 return (istream *) stream;
186 streamsize VirtualFileMountAndroidAsset::
187 get_file_size(
const Filename &file, istream *in)
const {
189 const AssetStreamBuf *buf = (
const AssetStreamBuf *) in->rdbuf();
190 off_t length = AAsset_getLength(buf->_asset);
200 streamsize VirtualFileMountAndroidAsset::
201 get_file_size(
const Filename &file)
const {
202 AAsset* asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
203 off_t length = AAsset_getLength(asset);
222 time_t VirtualFileMountAndroidAsset::
223 get_timestamp(
const Filename &file)
const {
238 bool VirtualFileMountAndroidAsset::
241 int fd = get_fd(file, &start, &length);
245 struct stat st1, st2;
246 nassertr(fstat(fd, &st1) == 0,
false);
247 nassertr(stat(_apk_path.c_str(), &st2) == 0,
false);
248 nassertr(st1.st_dev == st2.st_dev,
false);
249 nassertr(st1.st_ino == st2.st_ino,
false);
267 bool VirtualFileMountAndroidAsset::
268 scan_directory(vector_string &contents,
const Filename &dir)
const {
269 AAssetDir *asset_dir = AAssetManager_openDir(_asset_mgr, dir.c_str());
270 if (asset_dir == NULL) {
275 const char *fullpath = AAssetDir_getNextFileName(asset_dir);
277 while (fullpath != NULL) {
278 express_cat.error() << fullpath <<
"\n";
281 contents.push_back(fname.get_basename());
282 fullpath = AAssetDir_getNextFileName(asset_dir);
293 VirtualFileMountAndroidAsset::AssetStream::
303 VirtualFileMountAndroidAsset::AssetStreamBuf::
304 AssetStreamBuf(AAsset *asset) :
307 #ifdef PHAVE_IOSTREAM 308 char *buf =
new char[4096];
309 char *ebuf = buf + 4096;
310 setg(buf, ebuf, ebuf);
314 setg(base(), ebuf(), ebuf());
323 VirtualFileMountAndroidAsset::AssetStreamBuf::
325 AAsset_close(_asset);
333 streampos VirtualFileMountAndroidAsset::AssetStreamBuf::
334 seekoff(streamoff off, ios_seekdir dir, ios_openmode which) {
335 size_t n = egptr() - gptr();
346 return AAsset_seek(_asset, 0, SEEK_CUR) - n;
348 }
else if (gptr() + off >= eback() && gptr() + off < egptr()) {
351 return AAsset_seek(_asset, 0, SEEK_CUR) - n + off;
362 return AAsset_seek(_asset, off, whence);
379 streampos VirtualFileMountAndroidAsset::AssetStreamBuf::
380 seekpos(streampos pos, ios_openmode which) {
381 size_t n = egptr() - gptr();
383 return AAsset_seek(_asset, pos, SEEK_SET);
392 int VirtualFileMountAndroidAsset::AssetStreamBuf::
395 if (gptr() >= egptr()) {
397 size_t buffer_size = egptr() - eback();
398 gbump(-(
int)buffer_size);
400 streamsize read_count;
401 read_count = AAsset_read(_asset, gptr(), buffer_size);
403 if (read_count != buffer_size) {
405 if (read_count == 0) {
411 nassertr(read_count < buffer_size, EOF);
412 size_t delta = buffer_size - read_count;
413 memmove(gptr() + delta, gptr(), read_count);
418 return (
unsigned char)*gptr();
virtual bool read_file(const Filename &file, bool do_uncompress, pvector< unsigned char > &result) const
Fills up the indicated pvector with the contents of the file, if it is a regular file.
The name of a file, such as a texture file or an Egg file.
This class records a particular byte sub-range within an existing file on disk.
TypeHandle is the identifier used to differentiate C++ class types.