AgentManager.mm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. Copyright (c) 2014 Chukong Technologies Inc.
  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 <vector>
  21. #include "AgentManager.h"
  22. #include "PluginManager.h"
  23. #include "ProtocolUser.h"
  24. #include "ProtocolAnalytics.h"
  25. #include "PluginUtilsIOS.h"
  26. namespace cocos2d{ namespace plugin{
  27. static AgentManager* s_AgentManager = nullptr;
  28. AgentManager::AgentManager():pUser(nullptr), pShare(nullptr), pSocial(nullptr), pAds(nullptr), pAnalytics(nullptr), pIAP(nullptr)
  29. {
  30. }
  31. AgentManager::~AgentManager()
  32. {
  33. this->purge();
  34. }
  35. void AgentManager::purge()
  36. {
  37. delete pUser;
  38. delete pShare;
  39. delete pSocial;
  40. delete pAds;
  41. delete pAnalytics;
  42. delete pIAP;
  43. }
  44. AgentManager* AgentManager::getInstance()
  45. {
  46. if(nullptr == s_AgentManager)
  47. {
  48. s_AgentManager = new (std::nothrow) AgentManager();
  49. //s_AgentManager->init();
  50. }
  51. return s_AgentManager;
  52. }
  53. void AgentManager::destroyInstance()
  54. {
  55. if(s_AgentManager)
  56. {
  57. delete s_AgentManager;
  58. s_AgentManager = nullptr;
  59. }
  60. }
  61. bool AgentManager::initWithConfigureFile()
  62. {
  63. std::map<std::string, std::string> conf = getPluginConfigure();
  64. return init(conf);
  65. }
  66. bool AgentManager::init(std::map<std::string, std::string>& conf)
  67. {
  68. if(conf.empty())
  69. return false;
  70. for(std::map<std::string, std::string>::iterator iter = conf.begin(); iter != conf.end(); ++iter)
  71. {
  72. std::string pluginName = iter->first;
  73. if("PluginUser" == pluginName)
  74. {
  75. pUser = dynamic_cast<ProtocolUser *>(PluginManager::getInstance()->loadPlugin(iter->second.c_str()));
  76. }
  77. else if("PluginShare" == pluginName)
  78. {
  79. pShare = dynamic_cast<ProtocolShare *>(PluginManager::getInstance()->loadPlugin(iter->second.c_str()));
  80. }
  81. else if("PluginSocial" == pluginName)
  82. {
  83. pSocial = dynamic_cast<ProtocolSocial *>(PluginManager::getInstance()->loadPlugin(iter->second.c_str()));
  84. }
  85. else if("PluginAds" == pluginName)
  86. {
  87. pAds = dynamic_cast<ProtocolAds *>(PluginManager::getInstance()->loadPlugin(iter->second.c_str()));
  88. }
  89. else if("PluginAnalytics" == pluginName)
  90. {
  91. pAnalytics = dynamic_cast<ProtocolAnalytics *>(PluginManager::getInstance()->loadPlugin(iter->second.c_str()));
  92. }
  93. else if("PluginIAP" == pluginName)
  94. {
  95. pIAP = dynamic_cast<ProtocolIAP *>(PluginManager::getInstance()->loadPlugin(iter->second.c_str()));
  96. }
  97. }
  98. return true;
  99. }
  100. static std::vector<std::string> s_plugins = {"PluginUser", "PluginShare", "PluginSocial", "PluginAds", "PluginAnalytics", "PluginIAP"};
  101. std::map<std::string, std::string> AgentManager::getPluginConfigure()
  102. {
  103. std::map<std::string, std::string> configure;
  104. for(std::vector<std::string>::iterator iter = s_plugins.begin(); iter != s_plugins.end(); ++iter)
  105. {
  106. NSString *key = [NSString stringWithUTF8String:iter->c_str()];
  107. NSString *pluginName = [[NSBundle mainBundle] objectForInfoDictionaryKey:key];
  108. if (pluginName) {
  109. std::string name = [pluginName UTF8String];
  110. configure.emplace(*iter, name);
  111. }
  112. }
  113. return configure;
  114. }
  115. }}