Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Convenience function: jnienv_find_native_method

  • Loading branch information...
commit 946598caaa44cab4b86f5458cde5db890549b3fe 1 parent 4312a92
@thp authored
View
2  config.mk
@@ -17,7 +17,7 @@ PLATFORM ?= harmattan
V ?= 0
# Default linker flags
-LDFLAGS += -fPIC -pthread -ldl -lz -lpng -ljpeg
+LDFLAGS += -fPIC -rdynamic -pthread -ldl -lz -lpng -ljpeg
ifeq ($(LATEHOOKS),1)
View
45 jni/jnienv.c
@@ -1914,6 +1914,50 @@ JNIEnv_SetDoubleArrayRegion(JNIEnv* p0, jdoubleArray p1, jsize p2, jsize p3, con
set_array_region(p0, p1, p2, p3, p4);
}
+struct NativeMethodEntry {
+ char *klass;
+ char *method;
+ void *function;
+ struct NativeMethodEntry *next;
+};
+
+static struct NativeMethodEntry *
+g_native_methods = NULL;
+
+void *
+jnienv_find_native_method(const char *klass, const char *method)
+{
+ struct NativeMethodEntry *cur = g_native_methods;
+
+ JNIENV_DEBUG_PRINTF("%s: %s %s\n", __func__, klass ? klass : "*", method);
+
+ while (cur) {
+ if ((klass == NULL || strcmp(cur->klass, klass) == 0) && strcmp(cur->method, method) == 0) {
+ JNIENV_DEBUG_PRINTF("%s: Found %x\n", __func__, cur->function);
+ return cur->function;
+ }
+
+ cur = cur->next;
+ }
+
+ JNIENV_DEBUG_PRINTF("%s: Not found.\n", __func__);
+ return NULL;
+}
+
+static void
+jnienv_register_native_method(const char *klass, const char *method, void *function)
+{
+ struct NativeMethodEntry *entry = (struct NativeMethodEntry *)
+ malloc(sizeof(struct NativeMethodEntry));
+
+ JNIENV_DEBUG_PRINTF("%s: Registering %s %s %x\n", __func__, klass, method, function);
+ entry->klass = strdup(klass);
+ entry->method = strdup(method);
+ entry->function = function;
+ entry->next = g_native_methods;
+
+ g_native_methods = entry;
+}
jint
JNIEnv_RegisterNatives(JNIEnv* p0, jclass p1, const JNINativeMethod* p2, jint p3)
@@ -1927,6 +1971,7 @@ JNIEnv_RegisterNatives(JNIEnv* p0, jclass p1, const JNINativeMethod* p2, jint p3
const JNINativeMethod *method = p2;
while (i<p3) {
JNIENV_DEBUG_PRINTF("\tName: %-20s Sig: %-10s Addr: %x\n", method->name, method->signature, method->fnPtr);
+ jnienv_register_native_method(clazz->name, method->name, method->fnPtr);
method++;
i++;
}
View
1  jni/jnienv.h
@@ -287,6 +287,7 @@ const char *JNIEnv_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCo
void JNIEnv_ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf) SOFTFP;
void jnienv_init(struct GlobalState *global) SOFTFP;
+void *jnienv_find_native_method(const char *klass, const char *method) SOFTFP;
jint JavaVM_DestroyJavaVM(JavaVM *vm) SOFTFP;
jint JavaVM_AttachCurrentThread(JavaVM *vm, JNIEnv **env, void *args) SOFTFP;
View
37 modules/petalsredux.c
@@ -43,7 +43,6 @@
#define ASSET_FILE_PETALS3D_TNX "assets/petals3d.tnx"
-jint RegisterNatives(JNIEnv *env, jclass klass, const JNINativeMethod *method, jint count) SOFTFP;
jobject CallStaticObjectMethodV(JNIEnv *env, jclass klass, jmethodID method, va_list args) SOFTFP;
typedef void (*petalsredux_init_t)(JNIEnv *env, jobject obj) SOFTFP;
@@ -78,31 +77,6 @@ struct SupportModulePriv {
};
static struct SupportModulePriv petalsredux_priv;
-#define REGISTER_METHOD(x, y) \
- if (strcmp(method[i].name, x) == 0) { \
- petalsredux_priv.y = method[i].fnPtr; \
- }
-
-jint
-RegisterNatives(JNIEnv *env, jclass klass, const JNINativeMethod *method, jint count)
-{
- int i;
-
- for (i = 0; i < count; i++) {
- REGISTER_METHOD("nativeStart", native_start);
- REGISTER_METHOD("nativePause", native_pause);
- REGISTER_METHOD("nativeResume", native_resume);
- REGISTER_METHOD("nativeBack", native_back);
- REGISTER_METHOD("nativeInit", native_init);
- REGISTER_METHOD("nativeResize", native_resize);
- REGISTER_METHOD("nativeRender", native_render);
- REGISTER_METHOD("nativeTouch", native_touch);
- REGISTER_METHOD("nativeMix", native_mix);
- }
-
- return 0;
-}
-
jobject
CallStaticObjectMethodV(JNIEnv *env, jclass klass, jmethodID method, va_list args)
{
@@ -125,7 +99,6 @@ petalsredux_try_init(struct SupportModule *self)
self->priv->JNI_OnLoad = (jni_onload_t)LOOKUP_M("JNI_OnLoad");
GLOBAL_M->foreach_file(ASSET_FILE_PETALS3D_TNX, check_petalsredux);
- self->override_env.RegisterNatives = RegisterNatives;
self->override_env.CallStaticObjectMethodV = CallStaticObjectMethodV;
return (self->priv->JNI_OnLoad != NULL && self->priv->is_petalsredux);
@@ -148,6 +121,16 @@ petalsredux_init(struct SupportModule *self, int width, int height, const char *
{
self->priv->JNI_OnLoad(VM_M, NULL);
+ self->priv->native_start = jnienv_find_native_method(NULL, "nativeStart");
+ self->priv->native_pause = jnienv_find_native_method(NULL, "nativePause");
+ self->priv->native_resume = jnienv_find_native_method(NULL, "nativeResume");
+ self->priv->native_back = jnienv_find_native_method(NULL, "nativeBack");
+ self->priv->native_init = jnienv_find_native_method(NULL, "nativeInit");
+ self->priv->native_resize = jnienv_find_native_method(NULL, "nativeResize");
+ self->priv->native_render = jnienv_find_native_method(NULL, "nativeRender");
+ self->priv->native_touch = jnienv_find_native_method(NULL, "nativeTouch");
+ self->priv->native_mix = jnienv_find_native_method(NULL, "nativeMix");
+
const char *locale = getenv("LANG");
if (!locale) {
locale = getenv("LC_MESSAGES");
View
53 modules/unity.c
@@ -197,7 +197,6 @@ unity_try_init(struct SupportModule *self)
self->override_env.GetObjectClass = JNIEnv_GetObjectClass;
self->override_env.CallObjectMethod = JNIEnv_CallObjectMethod;
self->override_env.CallStaticObjectMethod = JNIEnv_CallStaticObjectMethod;
- self->override_env.RegisterNatives = JNIEnv_RegisterNatives;
self->override_env.GetStringUTFChars = JNIEnv_GetStringUTFChars;
self->override_env.NewGlobalRef = JNIEnv_NewGlobalRef;
@@ -205,49 +204,6 @@ unity_try_init(struct SupportModule *self)
}
-#define UNITYPLAYER_CLASS_NAME "com/unity3d/player/UnityPlayer"
-#define PLAYERPREFS_CLASS_NAME "com/unity3d/player/PlayerPrefs"
-#define method_is(native) (strcmp(method->name,#native)==0)
-
-jint
-JNIEnv_RegisterNatives(JNIEnv* p0, jclass p1, const JNINativeMethod* p2, jint p3)
-{
- MODULE_DEBUG_PRINTF("JNIEnv_RegisterNatives()\n");
-
- struct dummy_jclass *clazz = (struct dummy_jclass*)p1;
- MODULE_DEBUG_PRINTF("\n\tClass: %s\n", clazz->name);
-
- int is_unity_player = strcmp(clazz->name,UNITYPLAYER_CLASS_NAME)==0;
- int is_player_prefs = strcmp(clazz->name,PLAYERPREFS_CLASS_NAME)==0;
-
- int i=0;
- const JNINativeMethod *method = p2;
- while (i<p3) {
- MODULE_DEBUG_PRINTF("\tName: %-20s Sig: %-10s Addr: %x\n", method->name, method->signature, method->fnPtr);
-
- if (is_unity_player) {
-
- if (method_is(nativeInit)) { unity_priv.nativeInit = (unity_nativeInit_t)method->fnPtr;} else
- if (method_is(nativeFile)) { unity_priv.nativeFile = (unity_nativeFile_t)method->fnPtr;} else
- if (method_is(nativeRender)) { unity_priv.nativeRender = (unity_nativeRender_t)method->fnPtr;} else
- if (method_is(initJni)) { unity_priv.initJni = (unity_initJni_t)method->fnPtr;} else
- if (method_is(unityAndroidInit)) { unity_priv.unityAndroidInit = (unity_androidinit_t)method->fnPtr;} else
- if (method_is(unityAndroidPrepareGameLoop)) {unity_priv.unityAndroidPrepareGameLoop = (unity_androidpreparegameloop_t)method->fnPtr;} else
- ;
- } else if (is_player_prefs) {
-
- if (method_is(InitPlayerPrefs)) { unity_priv.InitPlayerPrefs = (unity_InitPlayerPrefs_t)method->fnPtr;} else
- ;
- }
-
- method++;
- i++;
- }
- MODULE_DEBUG_PRINTF("\n");
-
- return 0;
-}
-
const char *
JNIEnv_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy)
{
@@ -265,6 +221,8 @@ JNIEnv_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy)
}
+#define UNITYPLAYER_CLASS_NAME "com/unity3d/player/UnityPlayer"
+#define PLAYERPREFS_CLASS_NAME "com/unity3d/player/PlayerPrefs"
static void
unity_init(struct SupportModule *self, int width, int height, const char *home)
@@ -277,6 +235,13 @@ unity_init(struct SupportModule *self, int width, int height, const char *home)
self->priv->JNI_OnLoad_libunity(VM_M,0);
MODULE_DEBUG_PRINTF("JNI_OnLoad done.\n");
+ self->priv->nativeInit = jnienv_find_native_method(UNITYPLAYER_CLASS_NAME, "nativeInit");
+ self->priv->nativeFile = jnienv_find_native_method(UNITYPLAYER_CLASS_NAME, "nativeFile");
+ self->priv->nativeRender = jnienv_find_native_method(UNITYPLAYER_CLASS_NAME, "nativeRender");
+ self->priv->initJni = jnienv_find_native_method(UNITYPLAYER_CLASS_NAME, "initJni");
+ self->priv->unityAndroidInit = jnienv_find_native_method(UNITYPLAYER_CLASS_NAME, "unityAndroidInit");
+ self->priv->unityAndroidPrepareGameLoop = jnienv_find_native_method(UNITYPLAYER_CLASS_NAME, "unityAndroidPrepareGameLoop");
+ self->priv->InitPlayerPrefs = jnienv_find_native_method(PLAYERPREFS_CLASS_NAME, "InitPlayerPrefs");
MODULE_DEBUG_PRINTF("nativeInit\n");
self->priv->nativeInit(ENV_M,GLOBAL_M,width,height);
Please sign in to comment.
Something went wrong with that request. Please try again.