PluginProtocol.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /****************************************************************************
  2. Copyright (c) 2012-2013 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "PluginProtocol.h"
  21. #include "PluginUtils.h"
  22. #define LOG_TAG "PluginProtocol"
  23. namespace cocos2d { namespace plugin {
  24. PluginProtocol::~PluginProtocol()
  25. {
  26. PluginUtils::erasePluginJavaData(this);
  27. }
  28. std::string PluginProtocol::getPluginVersion()
  29. {
  30. return PluginUtils::callJavaStringFuncWithName(this, "getPluginVersion");
  31. }
  32. std::string PluginProtocol::getSDKVersion()
  33. {
  34. return PluginUtils::callJavaStringFuncWithName(this, "getSDKVersion");
  35. }
  36. void PluginProtocol::setDebugMode(bool isDebugMode)
  37. {
  38. PluginUtils::callJavaFunctionWithName_oneParam(this, "setDebugMode", "(Z)V", isDebugMode);
  39. }
  40. void PluginProtocol::callFuncWithParam(const char* funcName, PluginParam* param, ...)
  41. {
  42. std::vector<PluginParam*> allParams;
  43. if (NULL != param)
  44. {
  45. allParams.push_back(param);
  46. va_list argp;
  47. PluginParam* pArg = NULL;
  48. va_start( argp, param );
  49. while (1)
  50. {
  51. pArg = va_arg(argp, PluginParam*);
  52. if (pArg == NULL)
  53. break;
  54. allParams.push_back(pArg);
  55. }
  56. va_end(argp);
  57. }
  58. callFuncWithParam(funcName, allParams);
  59. }
  60. void PluginProtocol::callFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  61. {
  62. PluginJavaData* pData = PluginUtils::getPluginJavaData(this);
  63. if (NULL == pData) {
  64. PluginUtils::outputLog(LOG_TAG, "Can't find java data for plugin : %s", this->getPluginName());
  65. return;
  66. }
  67. int nParamNum = params.size();
  68. if (nParamNum == 0)
  69. {
  70. PluginUtils::callJavaFunctionWithName(this, funcName);
  71. } else
  72. {
  73. PluginParam* pRetParam = NULL;
  74. bool needDel = false;
  75. if (nParamNum == 1) {
  76. pRetParam = params[0];
  77. } else {
  78. std::map<std::string, PluginParam*> allParams;
  79. for (int i = 0; i < nParamNum; i++)
  80. {
  81. PluginParam* pArg = params[i];
  82. if (pArg == NULL)
  83. {
  84. break;
  85. }
  86. char strKey[8] = { 0 };
  87. sprintf(strKey, "Param%d", i + 1);
  88. allParams[strKey] = pArg;
  89. }
  90. pRetParam = new PluginParam(allParams);
  91. needDel = true;
  92. }
  93. switch(pRetParam->getCurrentType())
  94. {
  95. case PluginParam::kParamTypeInt:
  96. PluginUtils::callJavaFunctionWithName_oneParam(this, funcName, "(I)V", pRetParam->getIntValue());
  97. break;
  98. case PluginParam::kParamTypeFloat:
  99. PluginUtils::callJavaFunctionWithName_oneParam(this, funcName, "(F)V", pRetParam->getFloatValue());
  100. break;
  101. case PluginParam::kParamTypeBool:
  102. PluginUtils::callJavaFunctionWithName_oneParam(this, funcName, "(Z)V", pRetParam->getBoolValue());
  103. break;
  104. case PluginParam::kParamTypeString:
  105. {
  106. jstring jstr = PluginUtils::getEnv()->NewStringUTF(pRetParam->getStringValue());
  107. PluginUtils::callJavaFunctionWithName_oneParam(this, funcName, "(Ljava/lang/String;)V", jstr);
  108. PluginUtils::getEnv()->DeleteLocalRef(jstr);
  109. }
  110. break;
  111. case PluginParam::kParamTypeStringMap:
  112. case PluginParam::kParamTypeMap:
  113. {
  114. jobject jMap = PluginUtils::getJObjFromParam(pRetParam);
  115. PluginUtils::callJavaFunctionWithName_oneParam(this, funcName, "(Lorg/json/JSONObject;)V", jMap);
  116. PluginUtils::getEnv()->DeleteLocalRef(jMap);
  117. }
  118. break;
  119. default:
  120. break;
  121. }
  122. if (needDel && pRetParam != NULL)
  123. {
  124. delete pRetParam;
  125. pRetParam = NULL;
  126. }
  127. }
  128. }
  129. std::string PluginProtocol::callStringFuncWithParam(const char* funcName, PluginParam* param, ...)
  130. {
  131. CALL_JAVA_FUNC_WITH_VALIST(String)
  132. }
  133. std::string PluginProtocol::callStringFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  134. {
  135. CALL_JAVA_FUNC(std::string, String, "", "Ljava/lang/String;")
  136. }
  137. int PluginProtocol::callIntFuncWithParam(const char* funcName, PluginParam* param, ...)
  138. {
  139. CALL_JAVA_FUNC_WITH_VALIST(Int)
  140. }
  141. int PluginProtocol::callIntFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  142. {
  143. CALL_JAVA_FUNC(int, Int, 0, "I")
  144. }
  145. bool PluginProtocol::callBoolFuncWithParam(const char* funcName, PluginParam* param, ...)
  146. {
  147. CALL_JAVA_FUNC_WITH_VALIST(Bool)
  148. }
  149. bool PluginProtocol::callBoolFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  150. {
  151. CALL_JAVA_FUNC(bool, Bool, false, "Z")
  152. }
  153. float PluginProtocol::callFloatFuncWithParam(const char* funcName, PluginParam* param, ...)
  154. {
  155. CALL_JAVA_FUNC_WITH_VALIST(Float)
  156. }
  157. float PluginProtocol::callFloatFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  158. {
  159. CALL_JAVA_FUNC(float, Float, 0.0f, "F")
  160. }
  161. }} //namespace cocos2d { namespace plugin {