2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2023-12-08 00:13:39 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "platform/android/jni/JniHelper.h"
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/UTF8.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define LOG_TAG "JniHelper"
|
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
|
|
|
|
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
static pthread_key_t g_key;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jclass _getClassID(const char* className)
|
|
|
|
{
|
|
|
|
if (nullptr == className)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
JNIEnv* env = ax::JniHelper::getEnv();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
jstring _jstrClassName = env->NewStringUTF(className);
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
jclass _clazz = (jclass)env->CallObjectMethod(ax::JniHelper::classloader,
|
|
|
|
ax::JniHelper::loadclassMethod_methodID, _jstrClassName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr == _clazz)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
LOGE("Classloader failed to find class of %s", className);
|
|
|
|
env->ExceptionClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
env->DeleteLocalRef(_jstrClassName);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return _clazz;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void _detachCurrentThread(void* a)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::JniHelper::getJavaVM()->DetachCurrentThread();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JavaVM* JniHelper::_psJavaVM = nullptr;
|
|
|
|
jmethodID JniHelper::loadclassMethod_methodID = nullptr;
|
|
|
|
jobject JniHelper::classloader = nullptr;
|
|
|
|
std::function<void()> JniHelper::classloaderCallback = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jobject JniHelper::_activity = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JavaVM* JniHelper::getJavaVM()
|
|
|
|
{
|
|
|
|
// pthread_t thisthread = pthread_self();
|
|
|
|
// LOGD("JniHelper::getJavaVM(), pthread_self() = %ld", thisthread);
|
|
|
|
return _psJavaVM;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void JniHelper::setJavaVM(JavaVM* javaVM)
|
|
|
|
{
|
|
|
|
// pthread_t thisthread = pthread_self();
|
|
|
|
// LOGD("JniHelper::setJavaVM(%p), pthread_self() = %ld", javaVM, thisthread);
|
|
|
|
_psJavaVM = javaVM;
|
|
|
|
|
|
|
|
pthread_key_create(&g_key, _detachCurrentThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEnv* JniHelper::cacheEnv(JavaVM* jvm)
|
|
|
|
{
|
|
|
|
JNIEnv* _env = nullptr;
|
|
|
|
// get jni environment
|
|
|
|
jint ret = jvm->GetEnv((void**)&_env, JNI_VERSION_1_4);
|
|
|
|
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case JNI_OK:
|
|
|
|
// Success!
|
|
|
|
pthread_setspecific(g_key, _env);
|
|
|
|
return _env;
|
|
|
|
|
|
|
|
case JNI_EDETACHED:
|
|
|
|
// Thread not attached
|
|
|
|
if (jvm->AttachCurrentThread(&_env, nullptr) < 0)
|
|
|
|
{
|
|
|
|
LOGE("Failed to get the environment using AttachCurrentThread()");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Success : Attached and obtained JNIEnv!
|
2019-11-23 20:27:39 +08:00
|
|
|
pthread_setspecific(g_key, _env);
|
|
|
|
return _env;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case JNI_EVERSION:
|
|
|
|
// Cannot recover from this error
|
|
|
|
LOGE("JNI interface version 1.4 not supported");
|
|
|
|
default:
|
|
|
|
LOGE("Failed to get the environment using GetEnv()");
|
|
|
|
return nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JNIEnv* JniHelper::getEnv()
|
|
|
|
{
|
|
|
|
JNIEnv* _env = (JNIEnv*)pthread_getspecific(g_key);
|
|
|
|
if (_env == nullptr)
|
|
|
|
_env = JniHelper::cacheEnv(_psJavaVM);
|
|
|
|
return _env;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jobject JniHelper::getActivity()
|
|
|
|
{
|
|
|
|
return _activity;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool JniHelper::setClassLoaderFrom(jobject activityinstance)
|
|
|
|
{
|
|
|
|
JniMethodInfo _getclassloaderMethod;
|
|
|
|
if (!JniHelper::getMethodInfo_DefaultClassLoader(_getclassloaderMethod, "android/content/Context", "getClassLoader",
|
|
|
|
"()Ljava/lang/ClassLoader;"))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
jobject _c = ax::JniHelper::getEnv()->CallObjectMethod(activityinstance, _getclassloaderMethod.methodID);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr == _c)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JniMethodInfo _m;
|
|
|
|
if (!JniHelper::getMethodInfo_DefaultClassLoader(_m, "java/lang/ClassLoader", "loadClass",
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/Class;"))
|
|
|
|
{
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
JniHelper::classloader = ax::JniHelper::getEnv()->NewGlobalRef(_c);
|
2021-12-25 10:04:45 +08:00
|
|
|
JniHelper::loadclassMethod_methodID = _m.methodID;
|
2022-08-08 18:02:17 +08:00
|
|
|
JniHelper::_activity = ax::JniHelper::getEnv()->NewGlobalRef(activityinstance);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (JniHelper::classloaderCallback != nullptr)
|
|
|
|
{
|
|
|
|
JniHelper::classloaderCallback();
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool JniHelper::getStaticMethodInfo(JniMethodInfo& methodinfo,
|
|
|
|
const char* className,
|
|
|
|
const char* methodName,
|
|
|
|
const char* paramCode)
|
|
|
|
{
|
|
|
|
if ((nullptr == className) || (nullptr == methodName) || (nullptr == paramCode))
|
|
|
|
{
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JNIEnv* env = JniHelper::getEnv();
|
|
|
|
if (!env)
|
|
|
|
{
|
|
|
|
LOGE("Failed to get JNIEnv");
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jclass classID = _getClassID(className);
|
|
|
|
if (!classID)
|
|
|
|
{
|
|
|
|
LOGE("Failed to find class %s", className);
|
|
|
|
env->ExceptionClear();
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jmethodID methodID = env->GetStaticMethodID(classID, methodName, paramCode);
|
|
|
|
if (!methodID)
|
|
|
|
{
|
|
|
|
LOGE("Failed to find static method id of %s", methodName);
|
|
|
|
env->ExceptionClear();
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
methodinfo.classID = classID;
|
|
|
|
methodinfo.env = env;
|
|
|
|
methodinfo.methodID = methodID;
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool JniHelper::getMethodInfo_DefaultClassLoader(JniMethodInfo& methodinfo,
|
|
|
|
const char* className,
|
|
|
|
const char* methodName,
|
|
|
|
const char* paramCode)
|
|
|
|
{
|
|
|
|
if ((nullptr == className) || (nullptr == methodName) || (nullptr == paramCode))
|
|
|
|
{
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JNIEnv* env = JniHelper::getEnv();
|
|
|
|
if (!env)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jclass classID = env->FindClass(className);
|
|
|
|
if (!classID)
|
|
|
|
{
|
|
|
|
LOGE("Failed to find class %s", className);
|
|
|
|
env->ExceptionClear();
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jmethodID methodID = env->GetMethodID(classID, methodName, paramCode);
|
|
|
|
if (!methodID)
|
|
|
|
{
|
|
|
|
LOGE("Failed to find method id of %s", methodName);
|
|
|
|
env->ExceptionClear();
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
methodinfo.classID = classID;
|
|
|
|
methodinfo.env = env;
|
|
|
|
methodinfo.methodID = methodID;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool JniHelper::getMethodInfo(JniMethodInfo& methodinfo,
|
|
|
|
const char* className,
|
|
|
|
const char* methodName,
|
|
|
|
const char* paramCode)
|
|
|
|
{
|
|
|
|
if ((nullptr == className) || (nullptr == methodName) || (nullptr == paramCode))
|
|
|
|
{
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JNIEnv* env = JniHelper::getEnv();
|
|
|
|
if (!env)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jclass classID = _getClassID(className);
|
|
|
|
if (!classID)
|
|
|
|
{
|
|
|
|
LOGE("Failed to find class %s", className);
|
|
|
|
env->ExceptionClear();
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
jmethodID methodID = env->GetMethodID(classID, methodName, paramCode);
|
|
|
|
if (!methodID)
|
|
|
|
{
|
|
|
|
LOGE("Failed to find method id of %s", methodName);
|
|
|
|
env->ExceptionClear();
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
methodinfo.classID = classID;
|
|
|
|
methodinfo.env = env;
|
|
|
|
methodinfo.methodID = methodID;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string JniHelper::jstring2string(jstring jstr)
|
|
|
|
{
|
|
|
|
if (jstr == nullptr)
|
|
|
|
{
|
|
|
|
return "";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JNIEnv* env = JniHelper::getEnv();
|
|
|
|
if (!env)
|
|
|
|
{
|
|
|
|
return "";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
std::string strValue = ax::StringUtils::getStringUTFCharsJNI(env, jstr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return strValue;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
jstring JniHelper::convert(LocalRefMapType& localRefs, ax::JniMethodInfo& t, const char* x)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
jstring ret = ax::StringUtils::newStringUTFJNI(t.env, x ? x : "");
|
2022-08-08 13:18:33 +08:00
|
|
|
localRefs[t.env].emplace_back(ret);
|
2021-12-25 10:04:45 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
jstring JniHelper::convert(LocalRefMapType& localRefs, ax::JniMethodInfo& t, std::string_view x)
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
|
|
|
return convert(localRefs, t, x.data());
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
jstring JniHelper::convert(LocalRefMapType& localRefs, ax::JniMethodInfo& t, const std::string& x)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
return convert(localRefs, t, x.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void JniHelper::deleteLocalRefs(JNIEnv* env, LocalRefMapType& localRefs)
|
|
|
|
{
|
|
|
|
if (!env)
|
|
|
|
{
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& ref : localRefs[env])
|
|
|
|
{
|
|
|
|
env->DeleteLocalRef(ref);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
localRefs[env].clear();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void JniHelper::reportError(const char* className, const char* methodName, const char* signature)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
LOGE("Failed to find static java method. Class name: %s, method name: %s, signature: %s ", className, methodName,
|
|
|
|
signature);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-08-29 20:51:22 +08:00
|
|
|
NS_AX_END // namespace ax
|