From 8069dc02c6efe2bb9fccb215a596ce3e7cebc618 Mon Sep 17 00:00:00 2001 From: Jairo Luiz Date: Wed, 12 Feb 2014 20:56:11 -0300 Subject: [PATCH] Now, we have a filetype association!!!! :) --- AndroidManifest.xml | 13 ++++++++++ jni/love/src/common/android.cpp | 24 +++++++++++++++++++ jni/love/src/common/android.h | 5 ++++ .../modules/filesystem/physfs/Filesystem.cpp | 23 ++++++++++++------ src/org/love2d/android/GameLauncher.java | 24 +++++++++++++++++++ 5 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 src/org/love2d/android/GameLauncher.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8cc92484..6a3d22e7 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -24,6 +24,19 @@ + + + + + + + + + + diff --git a/jni/love/src/common/android.cpp b/jni/love/src/common/android.cpp index 8c7adcbe..3412069f 100644 --- a/jni/love/src/common/android.cpp +++ b/jni/love/src/common/android.cpp @@ -52,6 +52,30 @@ double getScreenScale() return result; } +const char* getSelectedGameFile() +{ + static const char *path; + + if (!path) { + JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv(); + jclass activity = env->FindClass("org/love2d/android/GameLauncher"); + + jmethodID getGamePath = env->GetStaticMethodID(activity, "getGamePath", "()Ljava/lang/String;"); + jstring gamePath = (jstring) env->CallStaticObjectMethod(activity, getGamePath); + const char *utf = env->GetStringUTFChars(gamePath, 0); + if (utf) + { + path = SDL_strdup(utf); + env->ReleaseStringUTFChars(gamePath, utf); + } + + env->DeleteLocalRef (gamePath); + env->DeleteLocalRef (activity); + } + + return path; +} + } // android } // love diff --git a/jni/love/src/common/android.h b/jni/love/src/common/android.h index 9a81d194..6e8fd5f1 100644 --- a/jni/love/src/common/android.h +++ b/jni/love/src/common/android.h @@ -36,6 +36,11 @@ namespace android **/ double getScreenScale(); +/** + * Gets the selected love file in the device filesystem. + **/ +const char* getSelectedGameFile(); + } // android } // love diff --git a/jni/love/src/modules/filesystem/physfs/Filesystem.cpp b/jni/love/src/modules/filesystem/physfs/Filesystem.cpp index c0a39020..dec3fedc 100644 --- a/jni/love/src/modules/filesystem/physfs/Filesystem.cpp +++ b/jni/love/src/modules/filesystem/physfs/Filesystem.cpp @@ -35,6 +35,7 @@ #include #include #include +#include "common/android.h" #endif namespace @@ -65,18 +66,18 @@ namespace delete[] game_love_data; } - bool androidMountAssetGame () { - SDL_Log ("Trying to mount assets/game.love"); - SDL_RWops *asset_game_file = SDL_RWFromFile("game.love", "rb"); + bool androidMountGame (const char *filename) { + SDL_Log ("Trying to mount %s", filename); + SDL_RWops *asset_game_file = SDL_RWFromFile(filename, "rb"); if (!asset_game_file) { - SDL_Log ("Could not find assets/game.love"); + SDL_Log ("Could not find %s", filename); return false; } Sint64 file_size = asset_game_file->size(asset_game_file); if (file_size == 0) { - SDL_Log ("Could not find valid game.love in assets. File has zero size.", (int) file_size); + SDL_Log ("Could not find valid %s. File has zero size.", filename); return false; } @@ -106,6 +107,14 @@ namespace return result; } + bool androidMountAssetGame () { + return androidMountGame ("game.love"); + } + + bool androidMountSelectedGame () { + return androidMountGame (love::android::getSelectedGameFile()); + } + bool androidDirectoryExists(const char* path) { SDL_Log ("Checking directory exists for %s", path); struct stat s; @@ -289,7 +298,7 @@ bool Filesystem::setSource(const char *source) SDL_Log ("Error creating storage directories!"); } - if (!androidMountAssetGame()) { + if (!androidMountSelectedGame() && !androidMountAssetGame()) { SDL_RWops *sdcard_main = SDL_RWFromFile("/sdcard/lovegame/main.lua", "rb"); if (sdcard_main) { @@ -306,7 +315,7 @@ bool Filesystem::setSource(const char *source) // sucessfully, therefore simply fail. return false; } - } + } #else // Add the directory. if (!PHYSFS_addToSearchPath(new_search_path.c_str(), 1)) { diff --git a/src/org/love2d/android/GameLauncher.java b/src/org/love2d/android/GameLauncher.java new file mode 100644 index 00000000..e8f06a00 --- /dev/null +++ b/src/org/love2d/android/GameLauncher.java @@ -0,0 +1,24 @@ +package org.love2d.android; + +import android.net.Uri; +import android.os.Bundle; +import android.util.Log; + +public class GameLauncher extends GameActivity { + + private static String gamePath; + + @Override + protected void onCreate(Bundle bundle) { + Uri game = this.getIntent().getData(); + if (game != null) { + gamePath = game.getPath(); + Log.d("GameLauncher", "Selected the file: " + gamePath); + } + super.onCreate(bundle); + } + + public static String getGamePath() { + return gamePath; + } +}