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
|
|
|
#ifndef __PLUGIN_UTILS_H__
|
|
|
|
#define __PLUGIN_UTILS_H__
|
|
|
|
|
|
|
|
#include "PluginJniHelper.h"
|
|
|
|
#include "PluginJavaData.h"
|
|
|
|
#include <map>
|
2013-05-23 17:57:55 +08:00
|
|
|
#include "PluginParam.h"
|
2013-05-28 18:10:09 +08:00
|
|
|
#include "PluginJniMacros.h"
|
2013-04-07 16:58:26 +08:00
|
|
|
|
|
|
|
namespace cocos2d { namespace plugin {
|
|
|
|
|
2013-06-04 10:06:16 +08:00
|
|
|
class PluginProtocol;
|
2013-04-07 16:58:26 +08:00
|
|
|
class PluginUtils
|
|
|
|
{
|
|
|
|
public:
|
2013-05-28 18:10:09 +08:00
|
|
|
static jobject createJavaMapObject(std::map<std::string, std::string>* paramMap);
|
2013-05-24 17:34:01 +08:00
|
|
|
static void initJavaPlugin(PluginProtocol* pPlugin, jobject jObj, const char* className);
|
2013-04-22 10:38:14 +08:00
|
|
|
static JNIEnv* getEnv();
|
2013-04-07 16:58:26 +08:00
|
|
|
|
|
|
|
static PluginJavaData* getPluginJavaData(PluginProtocol* pKeyObj);
|
|
|
|
static void setPluginJavaData(PluginProtocol* pKeyObj, PluginJavaData* pData);
|
|
|
|
static void erasePluginJavaData(PluginProtocol* pKeyObj);
|
|
|
|
|
2013-05-16 11:45:46 +08:00
|
|
|
static PluginProtocol* getPluginPtr(std::string className);
|
2013-04-25 17:33:42 +08:00
|
|
|
|
2013-05-23 17:57:55 +08:00
|
|
|
static jobject getJObjFromParam(PluginParam* param);
|
|
|
|
|
2013-05-28 18:10:09 +08:00
|
|
|
// methods have no return value
|
2013-04-07 16:58:26 +08:00
|
|
|
template <typename T>
|
2013-05-23 17:57:55 +08:00
|
|
|
static void callJavaFunctionWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
|
2013-04-07 16:58:26 +08:00
|
|
|
{
|
2013-04-22 10:38:14 +08:00
|
|
|
return_if_fails(funcName != NULL && strlen(funcName) > 0);
|
|
|
|
return_if_fails(paramCode != NULL && strlen(paramCode) > 0);
|
2013-04-07 16:58:26 +08:00
|
|
|
PluginJavaData* pData = PluginUtils::getPluginJavaData(thiz);
|
2013-05-27 13:58:31 +08:00
|
|
|
return_if_fails(pData != NULL);
|
|
|
|
|
2013-05-23 17:57:55 +08:00
|
|
|
PluginJniMethodInfo t;
|
2013-04-22 10:38:14 +08:00
|
|
|
if (PluginJniHelper::getMethodInfo(t
|
|
|
|
, pData->jclassName.c_str()
|
|
|
|
, funcName
|
|
|
|
, paramCode))
|
|
|
|
{
|
2013-06-09 17:20:05 +08:00
|
|
|
t.env->CallVoidMethod(pData->jobj, t.methodID, param);
|
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void callJavaFunctionWithName(PluginProtocol* thiz, const char* funcName)
|
|
|
|
{
|
|
|
|
return_if_fails(funcName != NULL && strlen(funcName) > 0);
|
|
|
|
PluginJavaData* pData = PluginUtils::getPluginJavaData(thiz);
|
|
|
|
return_if_fails(pData != NULL);
|
|
|
|
|
|
|
|
PluginJniMethodInfo t;
|
|
|
|
if (PluginJniHelper::getMethodInfo(t
|
|
|
|
, pData->jclassName.c_str()
|
|
|
|
, funcName
|
|
|
|
, "()V"))
|
|
|
|
{
|
|
|
|
t.env->CallVoidMethod(pData->jobj, t.methodID);
|
2013-04-22 10:38:14 +08:00
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
}
|
|
|
|
}
|
2013-05-23 17:57:55 +08:00
|
|
|
|
2013-05-28 18:10:09 +08:00
|
|
|
// methods return value is string
|
|
|
|
template <typename T>
|
|
|
|
static const char* callJavaStringFuncWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
|
2013-04-22 10:38:14 +08:00
|
|
|
{
|
2013-05-28 18:10:09 +08:00
|
|
|
const char* ret = "";
|
|
|
|
return_val_if_fails(funcName != NULL && strlen(funcName) > 0, ret);
|
|
|
|
return_val_if_fails(paramCode != NULL && strlen(paramCode) > 0, ret);
|
2013-04-22 10:38:14 +08:00
|
|
|
PluginJavaData* pData = PluginUtils::getPluginJavaData(thiz);
|
2013-05-28 18:10:09 +08:00
|
|
|
return_val_if_fails(pData != NULL, ret);
|
2013-05-27 13:58:31 +08:00
|
|
|
|
2013-04-22 10:38:14 +08:00
|
|
|
PluginJniMethodInfo t;
|
|
|
|
if (PluginJniHelper::getMethodInfo(t
|
|
|
|
, pData->jclassName.c_str()
|
|
|
|
, funcName
|
2013-05-28 18:10:09 +08:00
|
|
|
, paramCode))
|
2013-04-22 10:38:14 +08:00
|
|
|
{
|
2013-06-20 16:28:20 +08:00
|
|
|
jstring strRet = (jstring)t.env->CallObjectMethod(pData->jobj, t.methodID, param);
|
2013-05-28 18:10:09 +08:00
|
|
|
ret = PluginJniHelper::jstring2string(strRet).c_str();
|
2013-04-22 10:38:14 +08:00
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
}
|
2013-05-28 18:10:09 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2013-06-09 17:20:05 +08:00
|
|
|
static const char* callJavaStringFuncWithName(PluginProtocol* thiz, const char* funcName)
|
|
|
|
{
|
|
|
|
const char* ret = "";
|
|
|
|
return_val_if_fails(funcName != NULL && strlen(funcName) > 0, ret);
|
|
|
|
PluginJavaData* pData = PluginUtils::getPluginJavaData(thiz);
|
|
|
|
return_val_if_fails(pData != NULL, ret);
|
|
|
|
|
|
|
|
PluginJniMethodInfo t;
|
|
|
|
if (PluginJniHelper::getMethodInfo(t
|
|
|
|
, pData->jclassName.c_str()
|
|
|
|
, funcName
|
|
|
|
, "()Ljava/lang/String;"))
|
|
|
|
{
|
|
|
|
jstring strRet = (jstring) t.env->CallObjectMethod(pData->jobj, t.methodID);
|
|
|
|
ret = PluginJniHelper::jstring2string(strRet).c_str();
|
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-28 18:10:09 +08:00
|
|
|
|
|
|
|
// methods return value is int
|
|
|
|
template <typename T>
|
|
|
|
static int callJavaIntFuncWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
|
|
|
|
{
|
|
|
|
CALL_BASERET_JAVA_FUNC_WITH_PARAM(int, paramCode, param, Int, 0)
|
|
|
|
}
|
2013-06-09 17:20:05 +08:00
|
|
|
static int callJavaIntFuncWithName(PluginProtocol* thiz, const char* funcName)
|
|
|
|
{
|
|
|
|
CALL_BASERET_JAVA_FUNC(int, "()I", Int, 0)
|
|
|
|
}
|
2013-05-28 18:10:09 +08:00
|
|
|
|
|
|
|
// methods return value is float
|
|
|
|
template <typename T>
|
|
|
|
static float callJavaFloatFuncWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
|
|
|
|
{
|
|
|
|
CALL_BASERET_JAVA_FUNC_WITH_PARAM(float, paramCode, param, Float, 0.0f)
|
|
|
|
}
|
2013-06-09 17:20:05 +08:00
|
|
|
static float callJavaFloatFuncWithName(PluginProtocol* thiz, const char* funcName)
|
|
|
|
{
|
|
|
|
CALL_BASERET_JAVA_FUNC(float, "()F", Float, 0.0f);
|
|
|
|
}
|
2013-05-28 18:10:09 +08:00
|
|
|
|
|
|
|
// methods return value is bool
|
|
|
|
template <typename T>
|
|
|
|
static bool callJavaBoolFuncWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
|
|
|
|
{
|
|
|
|
CALL_BASERET_JAVA_FUNC_WITH_PARAM(bool, paramCode, param, Boolean, false)
|
2013-04-07 16:58:26 +08:00
|
|
|
}
|
2013-06-09 17:20:05 +08:00
|
|
|
static bool callJavaBoolFuncWithName(PluginProtocol* thiz, const char* funcName)
|
|
|
|
{
|
|
|
|
CALL_BASERET_JAVA_FUNC(bool, "()Z", Boolean, false)
|
|
|
|
}
|
2013-05-23 17:57:55 +08:00
|
|
|
|
|
|
|
static void outputLog(const char* logTag, const char* pFormat, ...);
|
2013-04-07 16:58:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}} // namespace cocos2d { namespace plugin {
|
|
|
|
|
|
|
|
#endif //__PLUGIN_UTILS_H__
|