M_PopupCFG.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "M_PopupCFG.h"
  7. std::auto_ptr<M_PopupCFG> M_PopupCFG::msSingleton(nullptr);
  8. int M_PopupCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_PopupCFGData* M_PopupCFG::GetData(std::string Key)
  13. {
  14. auto it = mMapData.find(Key);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<std::string, M_PopupCFGData>& M_PopupCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_PopupCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_PopupCFG.xml");
  29. auto result = xmlDoc.Parse(content.c_str(), content.length());
  30. if (result != tinyxml2::XML_SUCCESS)
  31. {
  32. CCLOGERROR("Result:%d", result);
  33. CCASSERT(false, "result != tinyxml2::XML_SUCCESS");
  34. return;
  35. }
  36. auto root = xmlDoc.RootElement();
  37. if (root == NULL)
  38. {
  39. CCASSERT(false, "root == NULL");
  40. return;
  41. }
  42. auto element = root->FirstChildElement("Data");
  43. while (element != NULL)
  44. {
  45. M_PopupCFGData data;
  46. data.mKey = element->Attribute("Key");
  47. data.mScene = element->Attribute("Scene");
  48. data.mRewardCon = element->IntAttribute("RewardCon");
  49. data.mOtherDes = element->Attribute("OtherDes");
  50. data.mOder = element->IntAttribute("Oder");
  51. {
  52. const char* Channel = element->Attribute("Channel");
  53. std::vector<std::string> vecChannel;
  54. boost::split(vecChannel, Channel, boost::is_any_of(","));
  55. for (unsigned int i = 0; i < vecChannel.size(); i++)
  56. {
  57. data.mChannel.push_back(vecChannel[i]);
  58. }
  59. }
  60. if (mMapData.find(data.mKey) != mMapData.end())std::cout <<"data refind:" << data.mKey << std::endl;
  61. CCASSERT(mMapData.find(data.mKey) == mMapData.end(), "data.mKey is exists");
  62. mMapData.insert(std::make_pair(data.mKey, data));
  63. element = element->NextSiblingElement();
  64. }
  65. CCLOG("M_PopupCFG Loaded. Load Data:%u", mMapData.size());
  66. }
  67. void M_PopupCFG::LoadLua()
  68. {
  69. LuaEngine::getInstance()->executeScriptFile("config/M_PopupCFG");
  70. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  71. lua_getglobal(L, "M_PopupCFG");
  72. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  73. lua_pushstring(L, "datas");
  74. lua_gettable(L, -2);
  75. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  76. lua_pushnil(L);
  77. while(lua_next(L, 2))
  78. {
  79. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  80. M_PopupCFGData data;
  81. LuaCfgHelper::readString(L, "Key", data.mKey);
  82. LuaCfgHelper::readString(L, "Scene", data.mScene);
  83. LuaCfgHelper::readInt(L, "RewardCon", data.mRewardCon);
  84. LuaCfgHelper::readString(L, "OtherDes", data.mOtherDes);
  85. LuaCfgHelper::readInt(L, "Oder", data.mOder);
  86. LuaCfgHelper::readVectorString(L, "Channel", data.mChannel);
  87. if (mMapData.find(data.mKey) != mMapData.end())std::cout <<"data refind:" << data.mKey << std::endl;
  88. CCASSERT(mMapData.find(data.mKey) == mMapData.end(), "data.mKey is exists");
  89. mMapData.insert(std::make_pair(data.mKey, data));
  90. lua_pop(L, 1);
  91. }
  92. lua_settop(L, 0);
  93. CCLOG("M_PopupCFG Loaded. Load Data:%u", mMapData.size());
  94. }
  95. void M_PopupCFG::Reload()
  96. {
  97. mMapData.clear();
  98. Load();
  99. }
  100. M_PopupCFG* M_PopupCFG::GetSingleton()
  101. {
  102. if (msSingleton.get() == nullptr)
  103. {
  104. msSingleton.reset(new M_PopupCFG());
  105. }
  106. return msSingleton.get();
  107. }
  108. void M_PopupCFG::Release()
  109. {
  110. msSingleton.reset(nullptr);
  111. }