2013-04-25 17:33:42 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012-2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-04-07 16:58:26 +08:00
|
|
|
#include "PluginUtils.h"
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <map>
|
|
|
|
|
2013-05-23 17:57:55 +08:00
|
|
|
#define MAX_LOG_LEN 256
|
|
|
|
|
2013-04-07 16:58:26 +08:00
|
|
|
namespace cocos2d { namespace plugin {
|
|
|
|
|
|
|
|
#define JAVAVM cocos2d::PluginJniHelper::getJavaVM()
|
|
|
|
|
|
|
|
jobject PluginUtils::createJavaMapObject(PluginJniMethodInfo&t, std::map<std::string, std::string>* paramMap)
|
|
|
|
{
|
|
|
|
jclass class_Hashtable = t.env->FindClass("java/util/Hashtable");
|
|
|
|
jmethodID construct_method = t.env->GetMethodID( class_Hashtable, "<init>","()V");
|
|
|
|
jobject obj_Map = t.env->NewObject( class_Hashtable, construct_method, "");
|
|
|
|
if (paramMap != NULL)
|
|
|
|
{
|
|
|
|
jmethodID add_method= t.env->GetMethodID( class_Hashtable,"put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
|
|
|
|
for (std::map<std::string, std::string>::const_iterator it = paramMap->begin(); it != paramMap->end(); ++it)
|
|
|
|
{
|
|
|
|
t.env->CallObjectMethod(obj_Map, add_method, t.env->NewStringUTF(it->first.c_str()), t.env->NewStringUTF(it->second.c_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.env->DeleteLocalRef(class_Hashtable);
|
|
|
|
return obj_Map;
|
|
|
|
}
|
|
|
|
|
2013-05-24 17:34:01 +08:00
|
|
|
void PluginUtils::initJavaPlugin(PluginProtocol* pPlugin, jobject jObj, const char* className)
|
2013-04-07 16:58:26 +08:00
|
|
|
{
|
2013-05-24 17:34:01 +08:00
|
|
|
cocos2d::plugin::PluginJavaData* pUserData = new cocos2d::plugin::PluginJavaData();
|
|
|
|
pUserData->jobj = PluginUtils::getEnv()->NewGlobalRef(jObj);
|
|
|
|
pUserData->jclassName = className;
|
|
|
|
cocos2d::plugin::PluginUtils::setPluginJavaData(pPlugin, pUserData);
|
2013-04-07 16:58:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEnv* PluginUtils::getEnv()
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
JNIEnv* env = NULL;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (JAVAVM->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK)
|
|
|
|
{
|
2013-05-23 17:57:55 +08:00
|
|
|
outputLog("PluginUtils", "Failed to get the environment using GetEnv()");
|
2013-04-07 16:58:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JAVAVM->AttachCurrentThread(&env, 0) < 0)
|
|
|
|
{
|
2013-05-23 17:57:55 +08:00
|
|
|
outputLog("PluginUtils", "Failed to get the environment using AttachCurrentThread()");
|
2013-04-07 16:58:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (!bRet) {
|
|
|
|
env = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<PluginProtocol*, PluginJavaData*> s_PluginObjMap;
|
2013-05-16 11:45:46 +08:00
|
|
|
std::map<std::string, PluginProtocol*> s_JObjPluginMap;
|
2013-04-07 16:58:26 +08:00
|
|
|
|
|
|
|
typedef std::map<PluginProtocol*, PluginJavaData*>::iterator ObjMapIter;
|
2013-05-16 11:45:46 +08:00
|
|
|
typedef std::map<std::string, PluginProtocol*>::iterator JObjPluginMapIter;
|
2013-04-07 16:58:26 +08:00
|
|
|
|
|
|
|
PluginJavaData* PluginUtils::getPluginJavaData(PluginProtocol* pKeyObj)
|
|
|
|
{
|
|
|
|
PluginJavaData* ret = NULL;
|
|
|
|
ObjMapIter it = s_PluginObjMap.find(pKeyObj);
|
|
|
|
if (it != s_PluginObjMap.end()) {
|
|
|
|
ret = it->second;
|
|
|
|
}
|
|
|
|
|
2013-05-27 13:58:31 +08:00
|
|
|
if (NULL != ret)
|
|
|
|
{
|
|
|
|
outputLog("PluginUtils", "get plugin java data : %s, %p", ret->jclassName.c_str(), ret->jobj);
|
|
|
|
}
|
2013-04-07 16:58:26 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-16 11:45:46 +08:00
|
|
|
PluginProtocol* PluginUtils::getPluginPtr(std::string className)
|
2013-04-25 17:33:42 +08:00
|
|
|
{
|
|
|
|
PluginProtocol* ret = NULL;
|
2013-05-16 11:45:46 +08:00
|
|
|
JObjPluginMapIter it = s_JObjPluginMap.find(className);
|
2013-04-25 17:33:42 +08:00
|
|
|
if (it != s_JObjPluginMap.end()) {
|
|
|
|
ret = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-07 16:58:26 +08:00
|
|
|
void PluginUtils::setPluginJavaData(PluginProtocol* pKeyObj, PluginJavaData* pData)
|
|
|
|
{
|
|
|
|
erasePluginJavaData(pKeyObj);
|
|
|
|
s_PluginObjMap.insert(std::pair<PluginProtocol*, PluginJavaData*>(pKeyObj, pData));
|
2013-05-16 11:45:46 +08:00
|
|
|
s_JObjPluginMap.insert(std::pair<std::string, PluginProtocol*>(pData->jclassName, pKeyObj));
|
2013-04-07 16:58:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginUtils::erasePluginJavaData(PluginProtocol* pKeyObj)
|
|
|
|
{
|
|
|
|
ObjMapIter it = s_PluginObjMap.find(pKeyObj);
|
|
|
|
if (it != s_PluginObjMap.end()) {
|
|
|
|
PluginJavaData* pData = it->second;
|
|
|
|
if (pData != NULL)
|
|
|
|
{
|
|
|
|
jobject jobj = pData->jobj;
|
2013-04-25 17:33:42 +08:00
|
|
|
|
2013-05-16 11:45:46 +08:00
|
|
|
JObjPluginMapIter pluginIt = s_JObjPluginMap.find(pData->jclassName);
|
2013-04-25 17:33:42 +08:00
|
|
|
if (pluginIt != s_JObjPluginMap.end())
|
|
|
|
{
|
|
|
|
s_JObjPluginMap.erase(pluginIt);
|
|
|
|
}
|
|
|
|
|
2013-04-07 16:58:26 +08:00
|
|
|
JNIEnv* pEnv = getEnv();
|
2013-05-23 17:57:55 +08:00
|
|
|
outputLog("PluginUtils", "Delete global reference.");
|
2013-04-07 16:58:26 +08:00
|
|
|
pEnv->DeleteGlobalRef(jobj);
|
|
|
|
delete pData;
|
|
|
|
}
|
|
|
|
s_PluginObjMap.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:57:55 +08:00
|
|
|
void PluginUtils::outputLog(const char* logTag, const char* pFormat, ...)
|
|
|
|
{
|
|
|
|
char buf[MAX_LOG_LEN + 1];
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, pFormat);
|
|
|
|
vsnprintf(buf, MAX_LOG_LEN, pFormat, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
__android_log_print(ANDROID_LOG_DEBUG, logTag, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
jobject PluginUtils::getJObjFromParam(PluginParam* param)
|
|
|
|
{
|
|
|
|
if (NULL == param)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
jobject obj = NULL;
|
|
|
|
PluginJniMethodInfo t;
|
|
|
|
JNIEnv* env = PluginUtils::getEnv();
|
|
|
|
|
|
|
|
switch(param->getCurrentType())
|
|
|
|
{
|
|
|
|
case PluginParam::kParamTypeInt:
|
|
|
|
if (PluginJniHelper::getStaticMethodInfo(t, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"))
|
|
|
|
{
|
|
|
|
obj = t.env->CallStaticObjectMethod(t.classID, t.methodID, param->getIntValue());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PluginParam::kParamTypeFloat:
|
|
|
|
if (PluginJniHelper::getStaticMethodInfo(t, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"))
|
|
|
|
{
|
|
|
|
obj = t.env->CallStaticObjectMethod(t.classID, t.methodID, param->getFloatValue());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PluginParam::kParamTypeBool:
|
|
|
|
if (PluginJniHelper::getStaticMethodInfo(t, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;"))
|
|
|
|
{
|
|
|
|
obj = t.env->CallStaticObjectMethod(t.classID, t.methodID, param->getBoolValue());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PluginParam::kParamTypeString:
|
|
|
|
obj = env->NewStringUTF(param->getStringValue());
|
|
|
|
break;
|
|
|
|
case PluginParam::kParamTypeMap:
|
|
|
|
{
|
|
|
|
jclass cls = env->FindClass("org/json/JSONObject");
|
|
|
|
jmethodID mid = env->GetMethodID(cls,"<init>","()V");
|
|
|
|
obj = env->NewObject(cls,mid);
|
|
|
|
|
|
|
|
std::map<std::string, PluginParam*>::iterator it;
|
|
|
|
std::map<std::string, PluginParam*> mapParam = param->getMapValue();
|
|
|
|
for (it = mapParam.begin(); it != mapParam.end(); it++)
|
|
|
|
{
|
|
|
|
PluginJniMethodInfo tInfo;
|
|
|
|
if (PluginJniHelper::getMethodInfo(tInfo, "org/json/JSONObject", "put", "(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;"))
|
|
|
|
{
|
|
|
|
jstring strKey = env->NewStringUTF(it->first.c_str());
|
|
|
|
jobject objValue = PluginUtils::getJObjFromParam(it->second);
|
|
|
|
|
|
|
|
tInfo.env->CallObjectMethod(obj, tInfo.methodID, strKey, objValue);
|
|
|
|
tInfo.env->DeleteLocalRef(tInfo.classID);
|
|
|
|
|
|
|
|
PluginUtils::getEnv()->DeleteLocalRef(strKey);
|
|
|
|
PluginUtils::getEnv()->DeleteLocalRef(objValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-04-07 16:58:26 +08:00
|
|
|
}}// namespace cocos2d { namespace plugin {
|