PluginUtils.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifndef __PLUGIN_UTILS_H__
  21. #define __PLUGIN_UTILS_H__
  22. #include "PluginJniHelper.h"
  23. #include "PluginJavaData.h"
  24. #include <map>
  25. #include "PluginParam.h"
  26. #include "PluginJniMacros.h"
  27. #include <android_native_app_glue.h>
  28. namespace cocos2d { namespace plugin {
  29. class PluginProtocol;
  30. class PluginUtils
  31. {
  32. public:
  33. static void initPluginWrapper(android_app* app);
  34. static jobject createJavaMapObject(std::map<std::string, std::string>* paramMap);
  35. static void initJavaPlugin(PluginProtocol* pPlugin, jobject jObj, const char* className);
  36. static JNIEnv* getEnv();
  37. static PluginJavaData* getPluginJavaData(PluginProtocol* pKeyObj);
  38. static void setPluginJavaData(PluginProtocol* pKeyObj, PluginJavaData* pData);
  39. static void erasePluginJavaData(PluginProtocol* pKeyObj);
  40. static PluginProtocol* getPluginPtr(std::string className);
  41. static jobject getJObjFromParam(PluginParam* param);
  42. // methods have no return value
  43. template <typename T>
  44. static void callJavaFunctionWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
  45. {
  46. return_if_fails(funcName != NULL && strlen(funcName) > 0);
  47. return_if_fails(paramCode != NULL && strlen(paramCode) > 0);
  48. PluginJavaData* pData = PluginUtils::getPluginJavaData(thiz);
  49. return_if_fails(pData != NULL);
  50. PluginJniMethodInfo t;
  51. if (PluginJniHelper::getMethodInfo(t
  52. , pData->jclassName.c_str()
  53. , funcName
  54. , paramCode))
  55. {
  56. t.env->CallVoidMethod(pData->jobj, t.methodID, param);
  57. t.env->DeleteLocalRef(t.classID);
  58. }
  59. }
  60. static void callJavaFunctionWithName(PluginProtocol* thiz, const char* funcName)
  61. {
  62. return_if_fails(funcName != NULL && strlen(funcName) > 0);
  63. PluginJavaData* pData = PluginUtils::getPluginJavaData(thiz);
  64. return_if_fails(pData != NULL);
  65. PluginJniMethodInfo t;
  66. if (PluginJniHelper::getMethodInfo(t
  67. , pData->jclassName.c_str()
  68. , funcName
  69. , "()V"))
  70. {
  71. t.env->CallVoidMethod(pData->jobj, t.methodID);
  72. t.env->DeleteLocalRef(t.classID);
  73. }
  74. }
  75. // methods return value is string
  76. template <typename T>
  77. static std::string callJavaStringFuncWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
  78. {
  79. std::string ret = "";
  80. return_val_if_fails(funcName != NULL && strlen(funcName) > 0, ret);
  81. return_val_if_fails(paramCode != NULL && strlen(paramCode) > 0, ret);
  82. PluginJavaData* pData = PluginUtils::getPluginJavaData(thiz);
  83. return_val_if_fails(pData != NULL, ret);
  84. PluginJniMethodInfo t;
  85. if (PluginJniHelper::getMethodInfo(t
  86. , pData->jclassName.c_str()
  87. , funcName
  88. , paramCode))
  89. {
  90. jstring strRet = (jstring)t.env->CallObjectMethod(pData->jobj, t.methodID, param);
  91. ret = PluginJniHelper::jstring2string(strRet);
  92. t.env->DeleteLocalRef(t.classID);
  93. }
  94. return ret;
  95. }
  96. static std::string callJavaStringFuncWithName(PluginProtocol* thiz, const char* funcName)
  97. {
  98. std::string ret = "";
  99. return_val_if_fails(funcName != NULL && strlen(funcName) > 0, ret);
  100. PluginJavaData* pData = PluginUtils::getPluginJavaData(thiz);
  101. return_val_if_fails(pData != NULL, ret);
  102. PluginJniMethodInfo t;
  103. if (PluginJniHelper::getMethodInfo(t
  104. , pData->jclassName.c_str()
  105. , funcName
  106. , "()Ljava/lang/String;"))
  107. {
  108. jstring strRet = (jstring) t.env->CallObjectMethod(pData->jobj, t.methodID);
  109. ret = PluginJniHelper::jstring2string(strRet);
  110. t.env->DeleteLocalRef(t.classID);
  111. }
  112. return ret;
  113. }
  114. // methods return value is int
  115. template <typename T>
  116. static int callJavaIntFuncWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
  117. {
  118. CALL_BASERET_JAVA_FUNC_WITH_PARAM(int, paramCode, param, Int, 0)
  119. }
  120. static int callJavaIntFuncWithName(PluginProtocol* thiz, const char* funcName)
  121. {
  122. CALL_BASERET_JAVA_FUNC(int, "()I", Int, 0)
  123. }
  124. // methods return value is float
  125. template <typename T>
  126. static float callJavaFloatFuncWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
  127. {
  128. CALL_BASERET_JAVA_FUNC_WITH_PARAM(float, paramCode, param, Float, 0.0f)
  129. }
  130. static float callJavaFloatFuncWithName(PluginProtocol* thiz, const char* funcName)
  131. {
  132. CALL_BASERET_JAVA_FUNC(float, "()F", Float, 0.0f);
  133. }
  134. // methods return value is bool
  135. template <typename T>
  136. static bool callJavaBoolFuncWithName_oneParam(PluginProtocol* thiz, const char* funcName, const char* paramCode, T param)
  137. {
  138. CALL_BASERET_JAVA_FUNC_WITH_PARAM(bool, paramCode, param, Boolean, false)
  139. }
  140. static bool callJavaBoolFuncWithName(PluginProtocol* thiz, const char* funcName)
  141. {
  142. CALL_BASERET_JAVA_FUNC(bool, "()Z", Boolean, false)
  143. }
  144. static void outputLog(const char* logTag, const char* pFormat, ...);
  145. };
  146. }} // namespace cocos2d { namespace plugin {
  147. #endif //__PLUGIN_UTILS_H__