axmol/plugin/protocols/platform/android/PluginUtils.h

168 lines
6.5 KiB
C
Raw Normal View History

/****************************************************************************
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"
#include "PluginJniMacros.h"
2013-04-07 16:58:26 +08:00
namespace cocos2d { namespace plugin {
class PluginProtocol;
2013-04-07 16:58:26 +08:00
class PluginUtils
{
public:
static jobject createJavaMapObject(std::map<std::string, std::string>* paramMap);
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);
static PluginProtocol* getPluginPtr(std::string className);
2013-05-23 17:57:55 +08:00
static jobject getJObjFromParam(PluginParam* param);
// 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);
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))
{
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
// 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
{
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);
return_val_if_fails(pData != NULL, ret);
2013-04-22 10:38:14 +08:00
PluginJniMethodInfo t;
if (PluginJniHelper::getMethodInfo(t
, pData->jclassName.c_str()
, funcName
, paramCode))
2013-04-22 10:38:14 +08:00
{
jstring strRet = (jstring)t.env->CallObjectMethod(pData->jobj, t.methodID, param);
ret = PluginJniHelper::jstring2string(strRet).c_str();
2013-04-22 10:38:14 +08:00
t.env->DeleteLocalRef(t.classID);
}
return ret;
}
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;
}
// 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)
}
static int callJavaIntFuncWithName(PluginProtocol* thiz, const char* funcName)
{
CALL_BASERET_JAVA_FUNC(int, "()I", Int, 0)
}
// 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)
}
static float callJavaFloatFuncWithName(PluginProtocol* thiz, const char* funcName)
{
CALL_BASERET_JAVA_FUNC(float, "()F", Float, 0.0f);
}
// 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
}
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__