M_ActivityCarnivalFullCFG.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_ActivityCarnivalFullCFG.h"
  7. std::auto_ptr<M_ActivityCarnivalFullCFG> M_ActivityCarnivalFullCFG::msSingleton(nullptr);
  8. int M_ActivityCarnivalFullCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_ActivityCarnivalFullCFGData* M_ActivityCarnivalFullCFG::GetData(int ID)
  13. {
  14. auto it = mMapData.find(ID);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_ActivityCarnivalFullCFGData>& M_ActivityCarnivalFullCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_ActivityCarnivalFullCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityCarnivalFullCFG.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_ActivityCarnivalFullCFGData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mDsc = element->Attribute("Dsc");
  48. data.mNeedRecharged = element->IntAttribute("NeedRecharged");
  49. data.mBigRedId = element->IntAttribute("BigRedId");
  50. {
  51. const char* BigRedIntegral = element->Attribute("BigRedIntegral");
  52. std::vector<std::string> vecBigRedIntegral;
  53. boost::split(vecBigRedIntegral, BigRedIntegral, boost::is_any_of(","));
  54. int temp;
  55. for (unsigned int i = 0; i < vecBigRedIntegral.size(); i++)
  56. {
  57. if (tinyxml2::XMLUtil::ToInt(vecBigRedIntegral[i].c_str(), &temp))
  58. {
  59. data.mBigRedIntegral.push_back(temp);
  60. }
  61. }
  62. }
  63. {
  64. const char* BigRedDisplay = element->Attribute("BigRedDisplay");
  65. std::vector<std::string> vecBigRedDisplay;
  66. boost::split(vecBigRedDisplay, BigRedDisplay, boost::is_any_of(","));
  67. for (unsigned int i = 0; i < vecBigRedDisplay.size(); i++)
  68. {
  69. data.mBigRedDisplay.push_back(vecBigRedDisplay[i]);
  70. }
  71. }
  72. {
  73. const char* AwardItemIDs = element->Attribute("AwardItemIDs");
  74. std::vector<std::string> vecAwardItemIDs;
  75. boost::split(vecAwardItemIDs, AwardItemIDs, boost::is_any_of(","));
  76. int temp;
  77. for (unsigned int i = 0; i < vecAwardItemIDs.size(); i++)
  78. {
  79. if (tinyxml2::XMLUtil::ToInt(vecAwardItemIDs[i].c_str(), &temp))
  80. {
  81. data.mAwardItemIDs.push_back(temp);
  82. }
  83. }
  84. }
  85. {
  86. const char* AwardItemCounts = element->Attribute("AwardItemCounts");
  87. std::vector<std::string> vecAwardItemCounts;
  88. boost::split(vecAwardItemCounts, AwardItemCounts, boost::is_any_of(","));
  89. int temp;
  90. for (unsigned int i = 0; i < vecAwardItemCounts.size(); i++)
  91. {
  92. if (tinyxml2::XMLUtil::ToInt(vecAwardItemCounts[i].c_str(), &temp))
  93. {
  94. data.mAwardItemCounts.push_back(temp);
  95. }
  96. }
  97. }
  98. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  99. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  100. mMapData.insert(std::make_pair(data.mID, data));
  101. element = element->NextSiblingElement();
  102. }
  103. CCLOG("M_ActivityCarnivalFullCFG Loaded. Load Data:%u", mMapData.size());
  104. }
  105. void M_ActivityCarnivalFullCFG::LoadLua()
  106. {
  107. LuaEngine::getInstance()->executeScriptFile("config/M_ActivityCarnivalFullCFG");
  108. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  109. lua_getglobal(L, "M_ActivityCarnivalFullCFG");
  110. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  111. lua_pushstring(L, "datas");
  112. lua_gettable(L, -2);
  113. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  114. lua_pushnil(L);
  115. while(lua_next(L, 2))
  116. {
  117. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  118. M_ActivityCarnivalFullCFGData data;
  119. LuaCfgHelper::readInt(L, "ID", data.mID);
  120. LuaCfgHelper::readString(L, "Dsc", data.mDsc);
  121. LuaCfgHelper::readInt(L, "NeedRecharged", data.mNeedRecharged);
  122. LuaCfgHelper::readInt(L, "BigRedId", data.mBigRedId);
  123. LuaCfgHelper::readVectorInt(L, "BigRedIntegral", data.mBigRedIntegral);
  124. LuaCfgHelper::readVectorString(L, "BigRedDisplay", data.mBigRedDisplay);
  125. LuaCfgHelper::readVectorInt(L, "AwardItemIDs", data.mAwardItemIDs);
  126. LuaCfgHelper::readVectorInt(L, "AwardItemCounts", data.mAwardItemCounts);
  127. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  128. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  129. mMapData.insert(std::make_pair(data.mID, data));
  130. lua_pop(L, 1);
  131. }
  132. lua_settop(L, 0);
  133. CCLOG("M_ActivityCarnivalFullCFG Loaded. Load Data:%u", mMapData.size());
  134. }
  135. void M_ActivityCarnivalFullCFG::Reload()
  136. {
  137. mMapData.clear();
  138. Load();
  139. }
  140. M_ActivityCarnivalFullCFG* M_ActivityCarnivalFullCFG::GetSingleton()
  141. {
  142. if (msSingleton.get() == nullptr)
  143. {
  144. msSingleton.reset(new M_ActivityCarnivalFullCFG());
  145. }
  146. return msSingleton.get();
  147. }
  148. void M_ActivityCarnivalFullCFG::Release()
  149. {
  150. msSingleton.reset(nullptr);
  151. }