25 using std::streamsize;
27 TypeHandle VirtualFileMountAndroidAsset::_type_handle;
32 VirtualFileMountAndroidAsset::
33 ~VirtualFileMountAndroidAsset() {
40 int VirtualFileMountAndroidAsset::
41 get_fd(
const Filename &file, off_t *start, off_t *length)
const {
43 asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
45 int fd = AAsset_openFileDescriptor(asset, start, length);
53 bool VirtualFileMountAndroidAsset::
54 has_file(
const Filename &file)
const {
62 bool VirtualFileMountAndroidAsset::
63 is_directory(
const Filename &file)
const {
79 bool VirtualFileMountAndroidAsset::
80 is_regular_file(
const Filename &file)
const {
83 asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
87 if (asset ==
nullptr) {
98 bool VirtualFileMountAndroidAsset::
99 read_file(
const Filename &file,
bool do_uncompress,
100 vector_uchar &result)
const {
113 asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_STREAMING);
114 if (asset ==
nullptr) {
116 <<
"Unable to read " << file <<
"\n";
121 off_t file_size = AAsset_getLength(asset);
122 if (file_size == 0) {
124 }
else if (file_size > 0) {
125 result.reserve((
size_t)file_size);
128 static const size_t buffer_size = 4096;
129 char buffer[buffer_size];
131 int count = AAsset_read(asset, buffer, buffer_size);
133 thread_consider_yield();
134 result.insert(result.end(), buffer, buffer + count);
135 count = AAsset_read(asset, buffer, buffer_size);
147 std::istream *VirtualFileMountAndroidAsset::
148 open_read_file(
const Filename &file)
const {
150 asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
151 if (asset ==
nullptr) {
155 AssetStream *stream =
new AssetStream(asset);
156 return (std::istream *) stream;
164 streamsize VirtualFileMountAndroidAsset::
165 get_file_size(
const Filename &file, std::istream *in)
const {
167 const AssetStreamBuf *buf = (
const AssetStreamBuf *) in->rdbuf();
168 off_t length = AAsset_getLength(buf->_asset);
176 streamsize VirtualFileMountAndroidAsset::
177 get_file_size(
const Filename &file)
const {
178 AAsset* asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
179 off_t length = AAsset_getLength(asset);
194 time_t VirtualFileMountAndroidAsset::
195 get_timestamp(
const Filename &file)
const {
207 bool VirtualFileMountAndroidAsset::
210 int fd = get_fd(file, &start, &length);
214 struct stat st1, st2;
215 nassertr(fstat(fd, &st1) == 0,
false);
216 nassertr(stat(_apk_path.c_str(), &st2) == 0,
false);
217 nassertr(st1.st_dev == st2.st_dev,
false);
218 nassertr(st1.st_ino == st2.st_ino,
false);
233 bool VirtualFileMountAndroidAsset::
234 scan_directory(vector_string &contents,
const Filename &dir)
const {
235 AAssetDir *asset_dir = AAssetManager_openDir(_asset_mgr, dir.c_str());
236 if (asset_dir ==
nullptr) {
241 const char *fullpath = AAssetDir_getNextFileName(asset_dir);
243 while (fullpath !=
nullptr) {
244 express_cat.error() << fullpath <<
"\n";
247 contents.push_back(fname.get_basename());
248 fullpath = AAssetDir_getNextFileName(asset_dir);
257 VirtualFileMountAndroidAsset::AssetStream::
265 VirtualFileMountAndroidAsset::AssetStreamBuf::
266 AssetStreamBuf(AAsset *asset) :
269 #ifdef PHAVE_IOSTREAM 270 char *buf =
new char[4096];
271 char *ebuf = buf + 4096;
272 setg(buf, ebuf, ebuf);
276 setg(base(), ebuf(), ebuf());
283 VirtualFileMountAndroidAsset::AssetStreamBuf::
285 AAsset_close(_asset);
291 streampos VirtualFileMountAndroidAsset::AssetStreamBuf::
292 seekoff(streamoff off, ios_seekdir dir, ios_openmode which) {
293 size_t n = egptr() - gptr();
297 case std::ios_base::beg:
300 case std::ios_base::cur:
303 return AAsset_seek(_asset, 0, SEEK_CUR) - n;
305 }
else if (gptr() + off >= eback() && gptr() + off < egptr()) {
308 return AAsset_seek(_asset, 0, SEEK_CUR) - n + off;
312 case std::ios_base::end:
319 return AAsset_seek(_asset, off, whence);
331 streampos VirtualFileMountAndroidAsset::AssetStreamBuf::
332 seekpos(streampos pos, ios_openmode which) {
333 size_t n = egptr() - gptr();
335 return AAsset_seek(_asset, pos, SEEK_SET);
342 int VirtualFileMountAndroidAsset::AssetStreamBuf::
345 if (gptr() >= egptr()) {
347 size_t buffer_size = egptr() - eback();
348 gbump(-(
int)buffer_size);
350 streamsize read_count;
351 read_count = AAsset_read(_asset, gptr(), buffer_size);
353 if (read_count != buffer_size) {
355 if (read_count == 0) {
361 nassertr(read_count < buffer_size, EOF);
362 size_t delta = buffer_size - read_count;
363 memmove(gptr() + delta, gptr(), read_count);
368 return (
unsigned char)*gptr();
The name of a file, such as a texture file or an Egg file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool is_regular_file(const Filename &filename) const
Convenience function; returns true if the named file exists and is a regular file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class records a particular byte sub-range within an existing file on disk.
TypeHandle is the identifier used to differentiate C++ class types.
virtual bool read_file(const Filename &file, bool do_uncompress, vector_uchar &result) const
Fills up the indicated pvector with the contents of the file, if it is a regular file.