Resolve the bug in the reflection of plugin.

This commit is contained in:
zhangbin 2013-06-09 17:20:05 +08:00
parent 2a241ad517
commit a2c059f0ed
7 changed files with 172 additions and 38 deletions

View File

@ -40,12 +40,25 @@ if (PluginJniHelper::getMethodInfo(t \
, funcName \
, paramCode)) \
{ \
if (NULL != param) \
{ \
ret = t.env->Call##retCode##Method(pData->jobj, t.methodID, param); \
} else { \
ret = t.env->Call##retCode##Method(pData->jobj, t.methodID); \
} \
ret = t.env->Call##retCode##Method(pData->jobj, t.methodID, param); \
t.env->DeleteLocalRef(t.classID); \
} \
return ret; \
#define CALL_BASERET_JAVA_FUNC(retType, paramCode, retCode, defaultRet) \
retType ret = defaultRet; \
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 \
, paramCode)) \
{ \
ret = t.env->Call##retCode##Method(pData->jobj, t.methodID); \
t.env->DeleteLocalRef(t.classID); \
} \
return ret; \
@ -88,7 +101,7 @@ if (0 == nParamNum)
{ \
paramCode = "()"; \
paramCode.append(jRetCode); \
ret = PluginUtils::callJava##retCode##FuncWithName_oneParam(this, funcName, paramCode.c_str(), NULL); \
ret = PluginUtils::callJava##retCode##FuncWithName(this, funcName); \
} else \
{ \
PluginParam* pRetParam = NULL; \

View File

@ -107,7 +107,7 @@ void PluginProtocol::callFuncWithParam(const char* funcName, std::vector<PluginP
int nParamNum = params.size();
if (nParamNum == 0)
{
PluginUtils::callJavaFunctionWithName_oneParam(this, funcName, "()V", NULL);
PluginUtils::callJavaFunctionWithName(this, funcName);
} else
{
PluginParam* pRetParam = NULL;

View File

@ -63,12 +63,23 @@ public:
, funcName
, paramCode))
{
if (param != NULL)
{
t.env->CallVoidMethod(pData->jobj, t.methodID, param);
} else {
t.env->CallVoidMethod(pData->jobj, t.methodID);
}
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);
t.env->DeleteLocalRef(t.classID);
}
}
@ -101,6 +112,25 @@ public:
}
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>
@ -108,6 +138,10 @@ public:
{
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>
@ -115,6 +149,10 @@ public:
{
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>
@ -122,6 +160,10 @@ public:
{
CALL_BASERET_JAVA_FUNC_WITH_PARAM(bool, paramCode, param, Boolean, false)
}
static bool callJavaBoolFuncWithName(PluginProtocol* thiz, const char* funcName)
{
CALL_BASERET_JAVA_FUNC(bool, "()Z", Boolean, false)
}
static void outputLog(const char* logTag, const char* pFormat, ...);
};

View File

@ -61,7 +61,7 @@ if (NULL == pData) {
int nParamNum = params.size(); \
if (0 == nParamNum) \
{ \
ret = PluginUtilsIOS::callOC##retCode##FunctionWithName_oneParam(this, funcName, NULL); \
ret = PluginUtilsIOS::callOC##retCode##FunctionWithName(this, funcName); \
} else \
{ \
PluginParam* pRetParam = NULL; \

View File

@ -117,7 +117,7 @@ void PluginProtocol::callFuncWithParam(const char* funcName, std::vector<PluginP
int nParamNum = params.size();
if (0 == nParamNum)
{
PluginUtilsIOS::callOCFunctionWithName_oneParam(this, funcName, NULL);
PluginUtilsIOS::callOCFunctionWithName(this, funcName);
} else
{
PluginParam* pRetParam = NULL;

View File

@ -57,31 +57,37 @@ public:
@brief method don't have return value
*/
static void callOCFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param);
static void callOCFunctionWithName(PluginProtocol* pPlugin, const char* funcName);
/**
@brief method return int value
*/
static int callOCIntFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param);
static int callOCIntFunctionWithName(PluginProtocol* pPlugin, const char* funcName);
/**
@brief method return float value
*/
static float callOCFloatFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param);
static float callOCFloatFunctionWithName(PluginProtocol* pPlugin, const char* funcName);
/**
@brief method return bool value
*/
static bool callOCBoolFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param);
static bool callOCBoolFunctionWithName(PluginProtocol* pPlugin, const char* funcName);
/**
@brief method return string value
*/
static const char* callOCStringFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param);
static const char* callOCStringFunctionWithName(PluginProtocol* pPlugin, const char* funcName);
static void outputLog(const char* pFormat, ...);
private:
static id callRetFunction(PluginProtocol* pPlugin, const char* funcName, id param);
static id callRetFunctionWithParam(PluginProtocol* pPlugin, const char* funcName, id param);
static id callRetFunction(PluginProtocol* pPlugin, const char* funcName);
};
}} // namespace cocos2d { namespace plugin {

View File

@ -172,16 +172,31 @@ void PluginUtilsIOS::callOCFunctionWithName_oneParam(PluginProtocol* pPlugin, co
id pOCObj = pData->obj;
NSString* strFuncName = [NSString stringWithUTF8String:funcName];
if (param != nil) {
strFuncName = [strFuncName stringByAppendingString:@":"];
}
strFuncName = [strFuncName stringByAppendingString:@":"];
SEL selector = NSSelectorFromString(strFuncName);
if ([pOCObj respondsToSelector:selector]) {
if (param == nil) {
[pOCObj performSelector:selector];
} else {
[pOCObj performSelector:selector withObject:param];
}
[pOCObj performSelector:selector withObject:param];
} else {
outputLog("Can't find function '%s' in class '%s'", [strFuncName UTF8String], pData->className.c_str());
}
} else {
PluginUtilsIOS::outputLog("Plugin %s not right initilized", pPlugin->getPluginName());
}
}
void PluginUtilsIOS::callOCFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
{
return_if_fails(funcName != NULL && strlen(funcName) > 0);
PluginOCData* pData = PluginUtilsIOS::getPluginOCData(pPlugin);
if (pData) {
id pOCObj = pData->obj;
NSString* strFuncName = [NSString stringWithUTF8String:funcName];
SEL selector = NSSelectorFromString(strFuncName);
if ([pOCObj respondsToSelector:selector]) {
[pOCObj performSelector:selector];
} else {
outputLog("Can't find function '%s' in class '%s'", [strFuncName UTF8String], pData->className.c_str());
}
@ -192,14 +207,33 @@ void PluginUtilsIOS::callOCFunctionWithName_oneParam(PluginProtocol* pPlugin, co
int PluginUtilsIOS::callOCIntFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
{
int ret = (NSInteger)callRetFunction(pPlugin, funcName, param);
NSNumber* num = (NSNumber*) callRetFunctionWithParam(pPlugin, funcName, param);
int ret = [num integerValue];
return ret;
}
int PluginUtilsIOS::callOCIntFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
{
NSNumber* num = (NSNumber*) callRetFunction(pPlugin, funcName);
int ret = [num integerValue];
return ret;
}
float PluginUtilsIOS::callOCFloatFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
{
float ret = 0.0f;
NSNumber* pRet = (NSNumber*)callRetFunction(pPlugin, funcName, param);
NSNumber* pRet = (NSNumber*)callRetFunctionWithParam(pPlugin, funcName, param);
if (nil != pRet) {
ret = [pRet floatValue];
}
return ret;
}
float PluginUtilsIOS::callOCFloatFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
{
float ret = 0.0f;
NSNumber* pRet = (NSNumber*)callRetFunction(pPlugin, funcName);
if (nil != pRet) {
ret = [pRet floatValue];
}
@ -210,7 +244,7 @@ float PluginUtilsIOS::callOCFloatFunctionWithName_oneParam(PluginProtocol* pPlug
bool PluginUtilsIOS::callOCBoolFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
{
bool ret = false;
NSNumber* pRet = (NSNumber*)callRetFunction(pPlugin, funcName, param);
NSNumber* pRet = (NSNumber*)callRetFunctionWithParam(pPlugin, funcName, param);
if (nil != pRet) {
ret = [pRet boolValue];
}
@ -218,18 +252,40 @@ bool PluginUtilsIOS::callOCBoolFunctionWithName_oneParam(PluginProtocol* pPlugin
return ret;
}
bool PluginUtilsIOS::callOCBoolFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
{
bool ret = false;
NSNumber* pRet = (NSNumber*)callRetFunction(pPlugin, funcName);
if (nil != pRet) {
ret = [pRet boolValue];
}
return ret;
}
const char* PluginUtilsIOS::callOCStringFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
{
const char* ret = "";
NSString* pRet = (NSString*)callRetFunction(pPlugin, funcName, param);
NSString* pRet = (NSString*)callRetFunctionWithParam(pPlugin, funcName, param);
if (nil != pRet) {
ret = [pRet UTF8String];
}
return ret;
}
const char* PluginUtilsIOS::callOCStringFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
{
const char* ret = "";
NSString* pRet = (NSString*)callRetFunction(pPlugin, funcName);
if (nil != pRet) {
ret = [pRet UTF8String];
}
return ret;
}
id PluginUtilsIOS::callRetFunction(PluginProtocol* pPlugin, const char* funcName, id param)
id PluginUtilsIOS::callRetFunction(PluginProtocol* pPlugin, const char* funcName)
{
id ret = nil;
return_val_if_fails(funcName != NULL && strlen(funcName) > 0, ret);
@ -239,16 +295,33 @@ id PluginUtilsIOS::callRetFunction(PluginProtocol* pPlugin, const char* funcName
id pOCObj = pData->obj;
NSString* strFuncName = [NSString stringWithUTF8String:funcName];
if (param != nil) {
strFuncName = [strFuncName stringByAppendingString:@":"];
}
SEL selector = NSSelectorFromString(strFuncName);
if ([pOCObj respondsToSelector:selector]) {
if (param == nil) {
ret = [pOCObj performSelector:selector];
} else {
ret = [pOCObj performSelector:selector withObject:param];
}
ret = [pOCObj performSelector:selector];
} else {
outputLog("Can't find function '%s' in class '%s'", [strFuncName UTF8String], pData->className.c_str());
}
} else {
PluginUtilsIOS::outputLog("Plugin %s not right initilized", pPlugin->getPluginName());
}
return ret;
}
id PluginUtilsIOS::callRetFunctionWithParam(PluginProtocol* pPlugin, const char* funcName, id param)
{
id ret = nil;
return_val_if_fails(funcName != NULL && strlen(funcName) > 0, ret);
PluginOCData* pData = PluginUtilsIOS::getPluginOCData(pPlugin);
if (pData) {
id pOCObj = pData->obj;
NSString* strFuncName = [NSString stringWithUTF8String:funcName];
strFuncName = [strFuncName stringByAppendingString:@":"];
SEL selector = NSSelectorFromString(strFuncName);
if ([pOCObj respondsToSelector:selector]) {
ret = [pOCObj performSelector:selector withObject:param];
} else {
outputLog("Can't find function '%s' in class '%s'", [strFuncName UTF8String], pData->className.c_str());
}