M_ActivityMayDaySignCFG.cpp 4.6 KB

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