ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

asset-archive.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #if defined(__ANDROID__)
00024 
00025 #include <sys/types.h>
00026 #include <unistd.h>
00027 
00028 #include "common/str.h"
00029 #include "common/stream.h"
00030 #include "common/util.h"
00031 #include "common/archive.h"
00032 #include "common/debug.h"
00033 #include "common/textconsole.h"
00034 
00035 #include "backends/platform/android/jni.h"
00036 #include "backends/platform/android/asset-archive.h"
00037 
00038 #include <android/asset_manager.h>
00039 #include <android/asset_manager_jni.h>
00040 
00041 class AssetInputStream : public Common::SeekableReadStream {
00042 public:
00043     AssetInputStream(AAssetManager *as, const Common::String &path);
00044     virtual ~AssetInputStream();
00045 
00046     virtual bool eos() const { return _eos; }
00047 
00048     virtual void clearErr() {_eos = false; }
00049 
00050     virtual uint32 read(void *dataPtr, uint32 dataSize);
00051 
00052     virtual int32 pos() const { return _pos; }
00053 
00054     virtual int32 size() const { return _len; }
00055 
00056     virtual bool seek(int32 offset, int whence = SEEK_SET);
00057 
00058 private:
00059     void close();
00060     AAsset *_asset;
00061 
00062     uint32 _pos;
00063     uint32 _len;
00064     bool _eos;
00065 };
00066 
00067 AssetInputStream::AssetInputStream(AAssetManager *as, const Common::String &path) :
00068     _eos(false), _pos(0) {
00069     _asset = AAssetManager_open(as, path.c_str(), AASSET_MODE_RANDOM);
00070     _len = AAsset_getLength(_asset);
00071 }
00072 
00073 AssetInputStream::~AssetInputStream() {
00074 }
00075 
00076 void AssetInputStream::close() {
00077     AAsset_close(_asset);
00078 }
00079 
00080 uint32 AssetInputStream::read(void *dataPtr, uint32 dataSize) {
00081     uint32 readlen = AAsset_read(_asset, dataPtr, dataSize);
00082     _pos += readlen;
00083     if (readlen != dataSize) {
00084         _eos = true;
00085     }
00086     return readlen;
00087 }
00088 
00089 bool AssetInputStream::seek(int32 offset, int whence) {
00090     int res = AAsset_seek(_asset, offset, whence);
00091     if (res == -1) {
00092         return false;
00093     }
00094     if (whence == SEEK_CUR) {
00095         _pos += offset;
00096     } else if (whence == SEEK_SET) {
00097         _pos = offset;
00098     } else if (whence == SEEK_END) {
00099         _pos = _len + offset;
00100     }
00101     assert(_pos < _len);
00102     _eos = false;
00103     return true;
00104 }
00105 
00106 
00107 AndroidAssetArchive::AndroidAssetArchive(jobject am) {
00108     JNIEnv *env = JNI::getEnv();
00109 
00110     _am = AAssetManager_fromJava(env, am);
00111     _cachedMembers = NULL;
00112 }
00113 
00114 AndroidAssetArchive::~AndroidAssetArchive() {
00115     delete _cachedMembers;
00116     _cachedMembers = NULL;
00117 }
00118 
00119 bool AndroidAssetArchive::hasFile(const Common::String &name) const {
00120     AAsset *asset = AAssetManager_open(_am, name.c_str(), AASSET_MODE_RANDOM);
00121     bool exists = false;
00122     if (asset != NULL) {
00123         exists = true;
00124         AAsset_close(asset);
00125     }
00126     return exists;
00127 }
00128 
00129 int AndroidAssetArchive::listMembers(Common::ArchiveMemberList &member_list) const {
00130     if (_cachedMembers) {
00131         member_list.insert(member_list.end(), _cachedMembers->begin(), _cachedMembers->end());
00132         return _cachedMembers->size();
00133     }
00134 
00135     Common::List<Common::String> dirs;
00136     dirs.push_back("");
00137     dirs.push_back("shaders");
00138     int count = 0;
00139     while (!dirs.empty()) {
00140         Common::String currentDir = dirs.front();
00141         dirs.pop_front();
00142         AAssetDir *dir = AAssetManager_openDir(_am, currentDir.c_str());
00143         const char *file = AAssetDir_getNextFileName(dir);
00144 
00145         while (file) {
00146             Common::String f(file);
00147             f = currentDir + f;
00148             member_list.push_back(getMember(f));
00149             ++count;
00150             file = AAssetDir_getNextFileName(dir);
00151         }
00152         AAssetDir_close(dir);
00153     }
00154 
00155     _cachedMembers = new Common::ArchiveMemberList(member_list);
00156 
00157     return count;
00158 }
00159 
00160 const Common::ArchiveMemberPtr AndroidAssetArchive::getMember(const Common::String &name) const {
00161     return Common::ArchiveMemberPtr(new Common::GenericArchiveMember(name, this));
00162 }
00163 
00164 Common::SeekableReadStream *AndroidAssetArchive::createReadStreamForMember(const Common::String &path) const {
00165     if (!hasFile(path)) {
00166         return nullptr;
00167     }
00168     return new AssetInputStream(_am, path);
00169 }
00170 
00171 #endif


Generated on Sat May 18 2019 05:00:55 for ResidualVM by doxygen 1.7.1
curved edge   curved edge