From d4372a6a1e53edcb0eaf2651b65bff71218d0059 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 12 Mar 2013 15:54:36 +0000 Subject: [PATCH] android: open files through the asset manager, meaning Lua works. --- build/android/LolActivity.java | 18 +++++++++++++----- src/platform/android/androidapp.cpp | 7 ++++++- src/sys/file.cpp | 19 +++++++++++++++++++ src/sys/init.cpp | 20 +++++++++++++------- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/build/android/LolActivity.java b/build/android/LolActivity.java index 6c32bc33..62f53b4d 100644 --- a/build/android/LolActivity.java +++ b/build/android/LolActivity.java @@ -10,7 +10,8 @@ package net.lolengine; -import android.app.NativeActivity; +import android.app.NativeActivity; /* NativeActivity */ +import android.os.Bundle; /* Bundle */ import android.content.res.AssetManager; /* getAssets() */ import android.graphics.Bitmap; @@ -26,12 +27,19 @@ public class LolActivity extends NativeActivity System.loadLibrary("@PROGRAM@"); } - public LolActivity() + @Override + protected void onCreate(Bundle saved_instance) { - nativeInit(); + super.onCreate(saved_instance); + + m_assets = getAssets(); + + nativeInit(m_assets); } - private native void nativeInit(); + private native void nativeInit(AssetManager assets); + + private AssetManager m_assets; /* * Bitmap loading helpers @@ -41,7 +49,7 @@ public class LolActivity extends NativeActivity { try { - return BitmapFactory.decodeStream(getAssets().open(name)); + return BitmapFactory.decodeStream(m_assets.open(name)); } catch (Exception e) { } return null; diff --git a/src/platform/android/androidapp.cpp b/src/platform/android/androidapp.cpp index ce26421c..7f15c9b6 100644 --- a/src/platform/android/androidapp.cpp +++ b/src/platform/android/androidapp.cpp @@ -15,6 +15,7 @@ #if defined __ANDROID__ #include +#include #include #include @@ -33,6 +34,7 @@ namespace lol { JavaVM *g_vm; jobject g_activity; +AAssetManager *g_assets; }; /* namespace lol */ extern "C" jint @@ -44,11 +46,14 @@ JNI_OnLoad(JavaVM* vm, void* reserved) } extern "C" void -Java_net_lolengine_LolActivity_nativeInit(JNIEnv* env, jobject thiz) +Java_net_lolengine_LolActivity_nativeInit(JNIEnv* env, jobject thiz, + jobject assets) { Log::Info("Java layer initialising activity 0x%08lx", (long)thiz); env->NewGlobalRef(thiz); /* FIXME: never released! */ g_activity = thiz; + env->NewGlobalRef(assets); /* FIXME: never released! */ + g_assets = AAssetManager_fromJava(env, assets); } /* One of these wrappers will be overridden by the user's version */ diff --git a/src/sys/file.cpp b/src/sys/file.cpp index 7598af6a..d0d6b7ae 100644 --- a/src/sys/file.cpp +++ b/src/sys/file.cpp @@ -15,6 +15,8 @@ #if __CELLOS_LV2__ # include # include +#elif __ANDROID__ +# include #endif #include "core.h" @@ -22,6 +24,10 @@ namespace lol { +#if __ANDROID__ +extern AAssetManager *g_assets; +#endif + class FileData { friend class File; @@ -34,6 +40,8 @@ class FileData &m_fd, NULL, 0); if (err != CELL_FS_SUCCEEDED) m_fd = -1; +#elif __ANDROID__ + m_asset = AAssetManager_open(g_assets, file.C(), AASSET_MODE_UNKNOWN); #elif HAVE_STDIO_H /* FIXME: no modes, no error checking, no nothing */ m_fd = fopen(file.C(), "r"); @@ -44,6 +52,8 @@ class FileData { #if __CELLOS_LV2__ return m_fd > -1; +#elif __ANDROID__ + return !!m_asset; #elif HAVE_STDIO_H return !!m_fd; #else @@ -61,6 +71,8 @@ class FileData return -1; return (int)done; +#elif __ANDROID__ + return AAsset_read(m_asset, buf, count); #elif HAVE_STDIO_H size_t done = fread(buf, 1, count, m_fd); if (done <= 0) @@ -98,6 +110,11 @@ class FileData #if __CELLOS_LV2__ if (m_fd >= 0) cellFsClose(m_fd); + m_fd = -1; +#elif __ANDROID__ + if (m_asset) + AAsset_close(m_asset); + m_asset = nullptr; #elif HAVE_STDIO_H if (m_fd) fclose(m_fd); @@ -107,6 +124,8 @@ class FileData #if __CELLOS_LV2__ int m_fd; +#elif __ANDROID__ + AAsset *m_asset; #elif HAVE_STDIO_H FILE *m_fd; #endif diff --git a/src/sys/init.cpp b/src/sys/init.cpp index 7a144669..305cbb34 100644 --- a/src/sys/init.cpp +++ b/src/sys/init.cpp @@ -48,15 +48,21 @@ void Init(int argc, char *argv[], * Retrieve binary directory, defaulting to current dir. */ -#if defined HAVE_GETCWD +#if __ANDROID__ + /* Android assets are accessed using no prefix at all. */ + String binarydir = ""; +#elif defined HAVE_GETCWD char *cwd = getcwd(nullptr, 0); + String binarydir = String(cwd ? cwd : ".") + SEPARATOR; + free(cwd); #elif defined HAVE__GETCWD || (defined _WIN32 && !defined _XBOX) char *cwd = _getcwd(nullptr, 0); -#else - char *cwd = nullptr; -#endif String binarydir = String(cwd ? cwd : ".") + SEPARATOR; free(cwd); +#else + String binarydir = "./"; +#endif + if (argc > 0) { char const *last_sep = strrchr(argv[0], SEPARATOR); @@ -99,7 +105,7 @@ void Init(int argc, char *argv[], if (!got_rootdir) { String rootdir = binarydir; - if (rootdir.Last() != SEPARATOR) + if (rootdir.Count() && rootdir.Last() != SEPARATOR) rootdir += SEPARATOR; for (int i = 1; i < sourcesubdir.Count(); ++i) { @@ -117,9 +123,9 @@ void Init(int argc, char *argv[], got_rootdir = true; } - Log::Debug("binary dir: %s\n", binarydir.C()); + Log::Debug("binary dir: ā€œ%sā€\n", binarydir.C()); for (int i = 0; i < data_dir.Count(); ++i) - Log::Debug("data dir %d/%d: %s\n", i + 1, data_dir.Count(), + Log::Debug("data dir %d/%d: ā€œ%sā€\n", i + 1, data_dir.Count(), data_dir[i].C()); }