From 6b9241fbc560b62a900a60f6cf5985d26c151aef Mon Sep 17 00:00:00 2001 From: Nathan Fulton Date: Sat, 29 Apr 2017 23:24:41 -0400 Subject: [PATCH] Add Android jni method for obtaining the AssetManager --- game/sdl/android/jni/ht/hosthelpers.cpp | 12 ++++++++++-- .../android/src/com/spiffcode/ht/GameActivity.java | 2 ++ game/sdl/android/src/com/spiffcode/ht/NativeLib.java | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/game/sdl/android/jni/ht/hosthelpers.cpp b/game/sdl/android/jni/ht/hosthelpers.cpp index ba04143..8998d4b 100644 --- a/game/sdl/android/jni/ht/hosthelpers.cpp +++ b/game/sdl/android/jni/ht/hosthelpers.cpp @@ -1,6 +1,8 @@ #include -#include #include +#include +#include +#include #include "game/sdl/hosthelpers.h" #include "base/thread.h" #include "game/sdl/sdlhttpservice.h" @@ -24,6 +26,7 @@ static jmethodID initiateAskMethod; static jmethodID initiateWebViewMethod; static jmethodID getAskStringMethod; static jmethodID getPlatformStringMethod; +static jmethodID getAssetManagerMethod; namespace wi { @@ -35,6 +38,7 @@ char gszSaveGamesDir[PATH_MAX]; // saved games char gszPrefsFilename[PATH_MAX]; // game prefs char gszCompletesDir[PATH_MAX]; // for "mission completed" tracking HttpService *gphttp; +AAssetManager *gassetmgr; IChatController *g_pchat; @@ -73,6 +77,7 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { getAskStringMethod = env->GetStaticMethodID(NativeLibClass, "getAskString", "()Ljava/lang/String;"); initiateAskMethod = env->GetStaticMethodID(NativeLibClass, "initiateAsk", "(Ljava/lang/String;ILjava/lang/String;II)V"); getPlatformStringMethod = env->GetStaticMethodID(NativeLibClass, "getPlatformString", "()Ljava/lang/String;"); + getAssetManagerMethod = env->GetStaticMethodID(NativeLibClass, "getAssetManager", "()Landroid/content/res/AssetManager;"); return JNI_VERSION_1_4; } @@ -109,7 +114,10 @@ bool HostHelpers::Init() { // Get the app's data path from Java jobject path = g_env->CallStaticObjectMethod(NativeLibClass, getDataDirMethod); const char* dataDir = g_env->GetStringUTFChars((jstring)path, NULL); - sprintf(gszMainDataDir, "%s", dataDir); + + // Get the asset manager + jobject jam = g_env->CallStaticObjectMethod(NativeLibClass, getAssetManagerMethod); + gassetmgr = AAssetManager_fromJava(g_env, jam); // Set the directory paths sprintf(gszTempDir, "%s/tmp", gszMainDataDir); diff --git a/game/sdl/android/src/com/spiffcode/ht/GameActivity.java b/game/sdl/android/src/com/spiffcode/ht/GameActivity.java index 208c1f0..def3457 100644 --- a/game/sdl/android/src/com/spiffcode/ht/GameActivity.java +++ b/game/sdl/android/src/com/spiffcode/ht/GameActivity.java @@ -57,6 +57,8 @@ public class GameActivity extends SDLActivity { NativeLib.screenHeight = metrics.heightPixels; NativeLib.screenDPI = metrics.densityDpi; + NativeLib.assetManager = getResources().getAssets(); + // App data path NativeLib.gameDataPath = getApplication().getApplicationContext().getFilesDir().getAbsolutePath(); diff --git a/game/sdl/android/src/com/spiffcode/ht/NativeLib.java b/game/sdl/android/src/com/spiffcode/ht/NativeLib.java index 528c545..d53d418 100644 --- a/game/sdl/android/src/com/spiffcode/ht/NativeLib.java +++ b/game/sdl/android/src/com/spiffcode/ht/NativeLib.java @@ -3,6 +3,7 @@ package com.spiffcode.ht; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; +import android.content.res.AssetManager; import android.net.Uri; import android.provider.Settings.Secure; import android.text.InputFilter; @@ -16,6 +17,7 @@ public class NativeLib { public static int screenWidth; // in pixels public static int screenHeight; // in pixels public static int screenDPI; // in dpi + public static AssetManager assetManager; public static String askString; // string from user input dialog public static GameActivity gameActivity; // A link back to our main gameActivity class instance public static ChatController chatc; @@ -46,6 +48,10 @@ public class NativeLib { return screenDPI; } + static AssetManager getAssetManager() { + return assetManager; + } + static String getAndroidID() { return Secure.getString(GameActivity.getContext().getContentResolver(), Secure.ANDROID_ID); }