PluginUtilsIOS.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "PluginUtilsIOS.h"
  21. #include <map>
  22. #import <objc/runtime.h>
  23. #include "PluginOCMacros.h"
  24. #define MAX_LOG_LEN 256
  25. namespace cocos2d { namespace plugin {
  26. void PluginUtilsIOS::initOCPlugin(PluginProtocol* pPlugin, id ocObj, const char* className)
  27. {
  28. PluginOCData* pData = new PluginOCData();
  29. pData->obj = ocObj;
  30. pData->className = className;
  31. PluginUtilsIOS::setPluginOCData(pPlugin, pData);
  32. }
  33. std::map<PluginProtocol*, PluginOCData*> s_PluginOCObjMap;
  34. std::map<id, PluginProtocol*> s_OCObjPluginMap;
  35. typedef std::map<PluginProtocol*, PluginOCData*>::iterator OCObjMapIter;
  36. typedef std::map<id, PluginProtocol*>::iterator OCObjPluginMapIter;
  37. PluginOCData* PluginUtilsIOS::getPluginOCData(PluginProtocol* pKeyObj)
  38. {
  39. PluginOCData* ret = NULL;
  40. OCObjMapIter it = s_PluginOCObjMap.find(pKeyObj);
  41. if (it != s_PluginOCObjMap.end()) {
  42. ret = it->second;
  43. }
  44. return ret;
  45. }
  46. PluginProtocol* PluginUtilsIOS::getPluginPtr(id obj)
  47. {
  48. PluginProtocol* ret = NULL;
  49. OCObjPluginMapIter it = s_OCObjPluginMap.find(obj);
  50. if (it != s_OCObjPluginMap.end()) {
  51. ret = it->second;
  52. }
  53. return ret;
  54. }
  55. id PluginUtilsIOS::getOCObjFromParam(PluginParam* param)
  56. {
  57. if (NULL == param)
  58. {
  59. return nil;
  60. }
  61. id obj = nil;
  62. switch(param->getCurrentType())
  63. {
  64. case PluginParam::kParamTypeInt:
  65. obj = [NSNumber numberWithInt:param->getIntValue()];
  66. break;
  67. case PluginParam::kParamTypeFloat:
  68. obj = [NSNumber numberWithFloat:param->getFloatValue()];
  69. break;
  70. case PluginParam::kParamTypeBool:
  71. obj = [NSNumber numberWithBool:param->getBoolValue()];
  72. break;
  73. case PluginParam::kParamTypeString:
  74. obj = [NSString stringWithUTF8String:param->getStringValue()];
  75. break;
  76. case PluginParam::kParamTypeStringMap:
  77. {
  78. std::map<std::string, std::string> mapValue = param->getStrMapValue();
  79. obj = createDictFromMap(&mapValue);
  80. }
  81. break;
  82. case PluginParam::kParamTypeMap:
  83. {
  84. obj = [NSMutableDictionary dictionary];
  85. std::map<std::string, PluginParam*> paramMap = param->getMapValue();
  86. std::map<std::string, PluginParam*>::const_iterator it;
  87. for (it = paramMap.begin(); it != paramMap.end(); ++it)
  88. {
  89. NSString* pKey = [NSString stringWithUTF8String:it->first.c_str()];
  90. id objValue = PluginUtilsIOS::getOCObjFromParam(it->second);
  91. [obj setValue:objValue forKey:pKey];
  92. }
  93. }
  94. break;
  95. default:
  96. break;
  97. }
  98. return obj;
  99. }
  100. void PluginUtilsIOS::setPluginOCData(PluginProtocol* pKeyObj, PluginOCData* pData)
  101. {
  102. erasePluginOCData(pKeyObj);
  103. s_PluginOCObjMap.insert(std::pair<PluginProtocol*, PluginOCData*>(pKeyObj, pData));
  104. s_OCObjPluginMap.insert(std::pair<id, PluginProtocol*>(pData->obj, pKeyObj));
  105. }
  106. void PluginUtilsIOS::erasePluginOCData(PluginProtocol* pKeyObj)
  107. {
  108. OCObjMapIter it = s_PluginOCObjMap.find(pKeyObj);
  109. if (it != s_PluginOCObjMap.end()) {
  110. PluginOCData* pData = it->second;
  111. if (pData != NULL)
  112. {
  113. id jobj = pData->obj;
  114. OCObjPluginMapIter pluginIt = s_OCObjPluginMap.find(jobj);
  115. if (pluginIt != s_OCObjPluginMap.end())
  116. {
  117. s_OCObjPluginMap.erase(pluginIt);
  118. }
  119. [jobj release];
  120. delete pData;
  121. }
  122. s_PluginOCObjMap.erase(it);
  123. }
  124. }
  125. NSMutableDictionary* PluginUtilsIOS::createDictFromMap(std::map<std::string, std::string>* paramMap)
  126. {
  127. if (NULL == paramMap)
  128. {
  129. return nil;
  130. }
  131. NSMutableDictionary* dict = [NSMutableDictionary dictionary];
  132. std::map<std::string, std::string>::const_iterator it;
  133. for (it = paramMap->begin(); it != paramMap->end(); ++it)
  134. {
  135. NSString* pKey = [NSString stringWithUTF8String:it->first.c_str()];
  136. NSString* pValue = [NSString stringWithUTF8String:it->second.c_str()];
  137. [dict setValue:pValue forKey:pKey];
  138. }
  139. return dict;
  140. }
  141. void PluginUtilsIOS::callOCFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
  142. {
  143. return_if_fails(funcName != NULL && strlen(funcName) > 0);
  144. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(pPlugin);
  145. if (pData) {
  146. id pOCObj = pData->obj;
  147. NSString* strFuncName = [NSString stringWithUTF8String:funcName];
  148. strFuncName = [strFuncName stringByAppendingString:@":"];
  149. SEL selector = NSSelectorFromString(strFuncName);
  150. if ([pOCObj respondsToSelector:selector]) {
  151. [pOCObj performSelector:selector withObject:param];
  152. } else {
  153. outputLog("Can't find function '%s' in class '%s'", [strFuncName UTF8String], pData->className.c_str());
  154. }
  155. } else {
  156. PluginUtilsIOS::outputLog("Plugin %s not right initilized", pPlugin->getPluginName());
  157. }
  158. }
  159. void PluginUtilsIOS::callOCFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
  160. {
  161. return_if_fails(funcName != NULL && strlen(funcName) > 0);
  162. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(pPlugin);
  163. if (pData) {
  164. id pOCObj = pData->obj;
  165. NSString* strFuncName = [NSString stringWithUTF8String:funcName];
  166. SEL selector = NSSelectorFromString(strFuncName);
  167. if ([pOCObj respondsToSelector:selector]) {
  168. [pOCObj performSelector:selector];
  169. } else {
  170. outputLog("Can't find function '%s' in class '%s'", [strFuncName UTF8String], pData->className.c_str());
  171. }
  172. } else {
  173. PluginUtilsIOS::outputLog("Plugin %s not right initilized", pPlugin->getPluginName());
  174. }
  175. }
  176. int PluginUtilsIOS::callOCIntFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
  177. {
  178. NSNumber* num = (NSNumber*) callRetFunctionWithParam(pPlugin, funcName, param);
  179. int ret = [num intValue];
  180. return ret;
  181. }
  182. int PluginUtilsIOS::callOCIntFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
  183. {
  184. NSNumber* num = (NSNumber*) callRetFunction(pPlugin, funcName);
  185. int ret = [num intValue];
  186. return ret;
  187. }
  188. float PluginUtilsIOS::callOCFloatFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
  189. {
  190. float ret = 0.0f;
  191. NSNumber* pRet = (NSNumber*)callRetFunctionWithParam(pPlugin, funcName, param);
  192. if (nil != pRet) {
  193. ret = [pRet floatValue];
  194. }
  195. return ret;
  196. }
  197. float PluginUtilsIOS::callOCFloatFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
  198. {
  199. float ret = 0.0f;
  200. NSNumber* pRet = (NSNumber*)callRetFunction(pPlugin, funcName);
  201. if (nil != pRet) {
  202. ret = [pRet floatValue];
  203. }
  204. return ret;
  205. }
  206. bool PluginUtilsIOS::callOCBoolFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
  207. {
  208. bool ret = false;
  209. BOOL* pRet = (BOOL*)callRetFunctionWithParam(pPlugin, funcName, param);
  210. if (nil != pRet) {
  211. ret = pRet;
  212. }
  213. return ret;
  214. }
  215. bool PluginUtilsIOS::callOCBoolFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
  216. {
  217. bool ret = false;
  218. BOOL* pRet = (BOOL*)callRetFunction(pPlugin, funcName);
  219. if (nil != pRet) {
  220. ret = pRet;
  221. }
  222. return ret;
  223. }
  224. std::string PluginUtilsIOS::callOCStringFunctionWithName_oneParam(PluginProtocol* pPlugin, const char* funcName, id param)
  225. {
  226. std::string ret = "";
  227. NSString* pRet = (NSString*)callRetFunctionWithParam(pPlugin, funcName, param);
  228. if (nil != pRet) {
  229. ret = [pRet UTF8String];
  230. }
  231. return ret;
  232. }
  233. std::string PluginUtilsIOS::callOCStringFunctionWithName(PluginProtocol* pPlugin, const char* funcName)
  234. {
  235. std::string ret = "";
  236. NSString* pRet = (NSString*)callRetFunction(pPlugin, funcName);
  237. if (nil != pRet) {
  238. ret = [pRet UTF8String];
  239. }
  240. return ret;
  241. }
  242. id PluginUtilsIOS::callRetFunction(PluginProtocol* pPlugin, const char* funcName)
  243. {
  244. id ret = nil;
  245. return_val_if_fails(funcName != NULL && strlen(funcName) > 0, ret);
  246. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(pPlugin);
  247. if (pData) {
  248. id pOCObj = pData->obj;
  249. NSString* strFuncName = [NSString stringWithUTF8String:funcName];
  250. SEL selector = NSSelectorFromString(strFuncName);
  251. if ([pOCObj respondsToSelector:selector]) {
  252. ret = [pOCObj performSelector:selector];
  253. } else {
  254. outputLog("Can't find function '%s' in class '%s'", [strFuncName UTF8String], pData->className.c_str());
  255. }
  256. } else {
  257. PluginUtilsIOS::outputLog("Plugin %s not right initilized", pPlugin->getPluginName());
  258. }
  259. return ret;
  260. }
  261. id PluginUtilsIOS::callRetFunctionWithParam(PluginProtocol* pPlugin, const char* funcName, id param)
  262. {
  263. id ret = nil;
  264. return_val_if_fails(funcName != NULL && strlen(funcName) > 0, ret);
  265. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(pPlugin);
  266. if (pData) {
  267. id pOCObj = pData->obj;
  268. NSString* strFuncName = [NSString stringWithUTF8String:funcName];
  269. strFuncName = [strFuncName stringByAppendingString:@":"];
  270. SEL selector = NSSelectorFromString(strFuncName);
  271. if ([pOCObj respondsToSelector:selector]) {
  272. ret = [pOCObj performSelector:selector withObject:param];
  273. } else {
  274. outputLog("Can't find function '%s' in class '%s'", [strFuncName UTF8String], pData->className.c_str());
  275. }
  276. } else {
  277. PluginUtilsIOS::outputLog("Plugin %s not right initilized", pPlugin->getPluginName());
  278. }
  279. return ret;
  280. }
  281. void PluginUtilsIOS::outputLog(const char* pFormat, ...)
  282. {
  283. printf("Plugin-x: ");
  284. char szBuf[MAX_LOG_LEN+1] = {0};
  285. va_list ap;
  286. va_start(ap, pFormat);
  287. vsnprintf(szBuf, MAX_LOG_LEN, pFormat, ap);
  288. va_end(ap);
  289. printf("%s", szBuf);
  290. printf("\n");
  291. }
  292. }}// namespace cocos2d { namespace plugin {