1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#if defined(__ANDROID__)
#include <sys/types.h>
#include <unistd.h>
#include "common/str.h"
#include "common/stream.h"
#include "common/util.h"
#include "common/archive.h"
#include "common/debug.h"
#include "common/textconsole.h"
#include "backends/platform/android/jni.h"
#include "backends/platform/android/asset-archive.h"
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
class AssetInputStream : public Common::SeekableReadStream {
public:
AssetInputStream(AAssetManager *as, const Common::String &path);
virtual ~AssetInputStream();
virtual bool eos() const { return _eos; }
virtual void clearErr() {_eos = false; }
virtual uint32 read(void *dataPtr, uint32 dataSize);
virtual int32 pos() const { return _pos; }
virtual int32 size() const { return _len; }
virtual bool seek(int32 offset, int whence = SEEK_SET);
private:
void close();
AAsset *_asset;
uint32 _pos;
uint32 _len;
bool _eos;
};
AssetInputStream::AssetInputStream(AAssetManager *as, const Common::String &path) :
_eos(false), _pos(0) {
_asset = AAssetManager_open(as, path.c_str(), AASSET_MODE_RANDOM);
_len = AAsset_getLength(_asset);
}
AssetInputStream::~AssetInputStream() {
}
void AssetInputStream::close() {
AAsset_close(_asset);
}
uint32 AssetInputStream::read(void *dataPtr, uint32 dataSize) {
uint32 readlen = AAsset_read(_asset, dataPtr, dataSize);
_pos += readlen;
if (readlen != dataSize) {
_eos = true;
}
return readlen;
}
bool AssetInputStream::seek(int32 offset, int whence) {
int res = AAsset_seek(_asset, offset, whence);
if (res == -1) {
return false;
}
if (whence == SEEK_CUR) {
_pos += offset;
} else if (whence == SEEK_SET) {
_pos = offset;
} else if (whence == SEEK_END) {
_pos = _len + offset;
}
assert(_pos <= _len);
_eos = false;
return true;
}
AndroidAssetArchive::AndroidAssetArchive(jobject am) : _hasCached(false) {
JNIEnv *env = JNI::getEnv();
_am = AAssetManager_fromJava(env, am);
}
AndroidAssetArchive::~AndroidAssetArchive() {
}
bool AndroidAssetArchive::hasFile(const Common::String &name) const {
AAsset *asset = AAssetManager_open(_am, name.c_str(), AASSET_MODE_RANDOM);
bool exists = false;
if (asset != NULL) {
exists = true;
AAsset_close(asset);
}
return exists;
}
int AndroidAssetArchive::listMembers(Common::ArchiveMemberList &member_list) const {
if (_hasCached) {
member_list.insert(member_list.end(), _cachedMembers.begin(), _cachedMembers.end());
return _cachedMembers.size();
}
int count = 0;
AAssetDir *dir = AAssetManager_openDir(_am, "");
const char *file = AAssetDir_getNextFileName(dir);
while (file) {
member_list.push_back(getMember(file));
++count;
file = AAssetDir_getNextFileName(dir);
}
AAssetDir_close(dir);
_cachedMembers = Common::ArchiveMemberList(member_list);
_hasCached = true;
return count;
}
const Common::ArchiveMemberPtr AndroidAssetArchive::getMember(const Common::String &name) const {
return Common::ArchiveMemberPtr(new Common::GenericArchiveMember(name, this));
}
Common::SeekableReadStream *AndroidAssetArchive::createReadStreamForMember(const Common::String &path) const {
if (!hasFile(path)) {
return nullptr;
}
return new AssetInputStream(_am, path);
}
#endif
|