|
|
@@ -25,36 +25,36 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
*/
|
|
|
|
|
|
#include <android/asset_manager_jni.h>
|
|
|
-
|
|
|
-#include "APKFile.h"
|
|
|
+#include "ApkFile.h"
|
|
|
#include "Assert.h"
|
|
|
-#include "OS.h"
|
|
|
-
|
|
|
-namespace crown
|
|
|
-{
|
|
|
|
|
|
-static AAssetManager* g_android_asset_manager = NULL;
|
|
|
+static AAssetManager* g_android_asset_manager = NULL;
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-APKFile::APKFile(const char* path, FileOpenMode mode)
|
|
|
+AAssetManager* get_android_asset_manager()
|
|
|
{
|
|
|
- // Android assets are always read-only
|
|
|
- (void) mode;
|
|
|
-
|
|
|
- m_mode = FOM_READ;
|
|
|
- m_asset = AAssetManager_open(get_android_asset_manager(), path, AASSET_MODE_RANDOM);
|
|
|
+ return g_android_asset_manager;
|
|
|
+}
|
|
|
|
|
|
- CE_ASSERT(m_asset != NULL, "Unable to open file: %s", path);
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env, jobject obj, jobject assetManager)
|
|
|
+{
|
|
|
+ g_android_asset_manager = AAssetManager_fromJava(env, assetManager);
|
|
|
}
|
|
|
|
|
|
+namespace crown
|
|
|
+{
|
|
|
+
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-APKFile::~APKFile()
|
|
|
+ApkFile::ApkFile(const char* path)
|
|
|
+ : File(FOM_READ)
|
|
|
{
|
|
|
- close();
|
|
|
+ m_asset = AAssetManager_open(get_android_asset_manager(), path, AASSET_MODE_RANDOM);
|
|
|
+ CE_ASSERT(m_asset != NULL, "Unable to open file: %s", path);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void APKFile::close()
|
|
|
+ApkFile::~ApkFile()
|
|
|
{
|
|
|
if (m_asset != NULL)
|
|
|
{
|
|
|
@@ -64,85 +64,94 @@ void APKFile::close()
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-bool APKFile::is_open() const
|
|
|
+void ApkFile::seek(size_t position)
|
|
|
{
|
|
|
- return m_asset != NULL;
|
|
|
+ off_t seek_result = AAsset_seek(m_asset, (off_t)position, SEEK_SET);
|
|
|
+ CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-FileOpenMode APKFile::mode() const
|
|
|
+void ApkFile::seek_to_end()
|
|
|
{
|
|
|
- return m_mode;
|
|
|
+ off_t seek_result = AAsset_seek(m_asset, 0, SEEK_END);
|
|
|
+ CE_ASSERT(seek_result != (off_t) -1, "Failed to seek to end");
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-size_t APKFile::size() const
|
|
|
+void ApkFile::skip(size_t bytes)
|
|
|
{
|
|
|
- return AAsset_getLength(m_asset);
|
|
|
+ off_t seek_result = AAsset_seek(m_asset, (off_t) bytes, SEEK_CUR);
|
|
|
+ CE_ASSERT(seek_result != (off_t) -1, "Failed to skip");
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-size_t APKFile::read(void* data, size_t size)
|
|
|
+void ApkFile::read(void* buffer, size_t size)
|
|
|
{
|
|
|
- CE_ASSERT(data != NULL, "Data must be != NULL");
|
|
|
+ CE_ASSERT_NOT_NULL(buffer);
|
|
|
|
|
|
- return (size_t)AAsset_read(m_asset, data, size);
|
|
|
+ size_t bytes_read = (size_t) AAsset_read(m_asset, buffer, size);
|
|
|
+ CE_ASSERT(bytes_read == size, "Failed to read from file: requested: %lu, read: %lu", size, bytes_read);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-size_t APKFile::write(const void* data, size_t /*size*/)
|
|
|
+void ApkFile::write(const void* /*buffer*/, size_t /*size*/)
|
|
|
{
|
|
|
- CE_ASSERT(data != NULL, "Data must be != NULL");
|
|
|
+ CE_ASSERT(false, "Attempt to write to android assets folder!");
|
|
|
+}
|
|
|
|
|
|
- os::printf("Android asset directory is read-only!");
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+bool ApkFile::copy_to(File& /*file*/, size_t /*size = 0*/)
|
|
|
+{
|
|
|
+ CE_ASSERT(false, "Not implemented yet :(");
|
|
|
+ return false;
|
|
|
+}
|
|
|
|
|
|
- return 0;
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+void ApkFile::flush()
|
|
|
+{
|
|
|
+ // Not needed
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void APKFile::seek(size_t position)
|
|
|
+bool ApkFile::is_valid() const
|
|
|
{
|
|
|
- off_t seek_result = AAsset_seek(m_asset, (off_t)position, SEEK_SET);
|
|
|
- CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
|
|
|
+ return m_asset != NULL;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void APKFile::seek_to_end()
|
|
|
+bool ApkFile::end_of_file() const
|
|
|
{
|
|
|
- off_t seek_result = AAsset_seek(m_asset, 0, SEEK_END);
|
|
|
- CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
|
|
|
+ return AAsset_getRemainingLength(m_asset) == 0;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void APKFile::skip(size_t bytes)
|
|
|
+size_t ApkFile::size() const
|
|
|
{
|
|
|
- off_t seek_result = AAsset_seek(m_asset, (off_t) bytes, SEEK_CUR);
|
|
|
- CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
|
|
|
+ return AAsset_getLength(m_asset);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-size_t APKFile::position() const
|
|
|
+size_t ApkFile::position() const
|
|
|
{
|
|
|
return (size_t) (AAsset_getLength(m_asset) - AAsset_getRemainingLength(m_asset));
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-bool APKFile::eof() const
|
|
|
+bool ApkFile::can_read() const
|
|
|
{
|
|
|
- return AAsset_getRemainingLength(m_asset) == 0;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-AAssetManager* get_android_asset_manager()
|
|
|
+bool ApkFile::can_write() const
|
|
|
{
|
|
|
- return g_android_asset_manager;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env, jobject obj, jobject assetManager)
|
|
|
+bool ApkFile::can_seek() const
|
|
|
{
|
|
|
- g_android_asset_manager = AAssetManager_fromJava(env, assetManager);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
-} // namespace crown
|
|
|
-
|
|
|
+} // namespace crown
|