PluginProtocol.mm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "PluginUtilsIOS.h"
  22. #include "PluginOCMacros.h"
  23. @protocol OCPluginProtocol
  24. @optional
  25. - (void)setDebugMode:(BOOL)debug;
  26. - (NSString *)getPluginVersion;
  27. - (NSString *)getSDKVersion;
  28. @end
  29. namespace cocos2d { namespace plugin {
  30. PluginProtocol::~PluginProtocol()
  31. {
  32. PluginUtilsIOS::erasePluginOCData(this);
  33. }
  34. std::string PluginProtocol::getPluginVersion()
  35. {
  36. std::string verName;
  37. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  38. if (pData) {
  39. id pOCObj = pData->obj;
  40. SEL selector = NSSelectorFromString(@"getPluginVersion");
  41. if ([pOCObj respondsToSelector:selector]) {
  42. NSString* strRet = [pOCObj getPluginVersion];
  43. if (strRet) {
  44. verName = [strRet UTF8String];
  45. }
  46. } else {
  47. PluginUtilsIOS::outputLog("Can't find function 'getPluginVersion' in class '%s'", pData->className.c_str());
  48. }
  49. } else {
  50. PluginUtilsIOS::outputLog("Plugin %p is not initialized", this);
  51. }
  52. return verName;
  53. }
  54. std::string PluginProtocol::getSDKVersion()
  55. {
  56. std::string verName;
  57. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  58. if (pData) {
  59. id pOCObj = pData->obj;
  60. SEL selector = NSSelectorFromString(@"getSDKVersion");
  61. if ([pOCObj respondsToSelector:selector]) {
  62. NSString* strRet = [pOCObj getSDKVersion];
  63. if (strRet) {
  64. verName = [strRet UTF8String];
  65. }
  66. } else {
  67. PluginUtilsIOS::outputLog("Can't find function 'getSDKVersion' in class '%s'", pData->className.c_str());
  68. }
  69. } else {
  70. PluginUtilsIOS::outputLog("Plugin %s is not initialized", this->getPluginName());
  71. }
  72. return verName;
  73. }
  74. void PluginProtocol::setDebugMode(bool isDebugMode)
  75. {
  76. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  77. if (pData) {
  78. id pOCObj = pData->obj;
  79. SEL selector = NSSelectorFromString(@"setDebugMode:");
  80. if ([pOCObj respondsToSelector:selector]) {
  81. [pOCObj setDebugMode:(BOOL)isDebugMode];
  82. } else {
  83. PluginUtilsIOS::outputLog("Can't find function 'setDebugMode' in class '%s'", pData->className.c_str());
  84. }
  85. } else {
  86. PluginUtilsIOS::outputLog("Plugin %s is not initialized", this->getPluginName());
  87. }
  88. }
  89. void PluginProtocol::callFuncWithParam(const char* funcName, PluginParam* param, ...)
  90. {
  91. std::vector<PluginParam*> allParams;
  92. if (NULL != param)
  93. {
  94. allParams.push_back(param);
  95. va_list argp;
  96. PluginParam* pArg = NULL;
  97. va_start( argp, param );
  98. while (1)
  99. {
  100. pArg = va_arg(argp, PluginParam*);
  101. if (pArg == NULL)
  102. break;
  103. allParams.push_back(pArg);
  104. }
  105. va_end(argp);
  106. }
  107. callFuncWithParam(funcName, allParams);
  108. }
  109. void PluginProtocol::callFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  110. {
  111. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  112. if (NULL == pData) {
  113. PluginUtilsIOS::outputLog("Can't find OC data for plugin : %s", this->getPluginName());
  114. return;
  115. }
  116. size_t nParamNum = params.size();
  117. if (0 == nParamNum)
  118. {
  119. PluginUtilsIOS::callOCFunctionWithName(this, funcName);
  120. } else
  121. {
  122. PluginParam* pRetParam = NULL;
  123. bool needDel = false;
  124. if (nParamNum == 1) {
  125. pRetParam = params[0];
  126. } else {
  127. std::map<std::string, PluginParam*> allParams;
  128. for (int i = 0; i < nParamNum; i++)
  129. {
  130. PluginParam* pArg = params[i];
  131. if (pArg == NULL)
  132. {
  133. break;
  134. }
  135. char strKey[8] = { 0 };
  136. sprintf(strKey, "Param%d", i + 1);
  137. allParams[strKey] = pArg;
  138. }
  139. pRetParam = new PluginParam(allParams);
  140. needDel = true;
  141. }
  142. id ocParam = PluginUtilsIOS::getOCObjFromParam(pRetParam);
  143. PluginUtilsIOS::callOCFunctionWithName_oneParam(this, funcName, ocParam);
  144. if (needDel && NULL != pRetParam) {
  145. delete pRetParam;
  146. pRetParam = NULL;
  147. }
  148. }
  149. }
  150. std::string PluginProtocol::callStringFuncWithParam(const char* funcName, PluginParam* param, ...)
  151. {
  152. CALL_OC_FUNC_WITH_VALIST(String)
  153. }
  154. std::string PluginProtocol::callStringFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  155. {
  156. CALL_OC_FUNC(std::string, "", String)
  157. }
  158. int PluginProtocol::callIntFuncWithParam(const char* funcName, PluginParam* param, ...)
  159. {
  160. CALL_OC_FUNC_WITH_VALIST(Int)
  161. }
  162. int PluginProtocol::callIntFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  163. {
  164. CALL_OC_FUNC(int, 0, Int)
  165. }
  166. bool PluginProtocol::callBoolFuncWithParam(const char* funcName, PluginParam* param, ...)
  167. {
  168. CALL_OC_FUNC_WITH_VALIST(Bool)
  169. }
  170. bool PluginProtocol::callBoolFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  171. {
  172. CALL_OC_FUNC(bool, false, Bool)
  173. }
  174. float PluginProtocol::callFloatFuncWithParam(const char* funcName, PluginParam* param, ...)
  175. {
  176. CALL_OC_FUNC_WITH_VALIST(Float)
  177. }
  178. float PluginProtocol::callFloatFuncWithParam(const char* funcName, std::vector<PluginParam*> params)
  179. {
  180. CALL_OC_FUNC(float, 0.0f, Float)
  181. }
  182. }} //namespace cocos2d { namespace plugin {