M_RobotsCFG.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_RobotsCFG.h"
  7. std::auto_ptr<M_RobotsCFG> M_RobotsCFG::msSingleton(nullptr);
  8. int M_RobotsCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_RobotsCFGData* M_RobotsCFG::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_RobotsCFGData>& M_RobotsCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_RobotsCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_RobotsCFG.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_RobotsCFGData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mRankingTyep = element->IntAttribute("RankingTyep");
  48. {
  49. const char* Time = element->Attribute("Time");
  50. std::vector<std::string> vecTime;
  51. boost::split(vecTime, Time, boost::is_any_of(","));
  52. int temp;
  53. for (unsigned int i = 0; i < vecTime.size(); i++)
  54. {
  55. if (tinyxml2::XMLUtil::ToInt(vecTime[i].c_str(), &temp))
  56. {
  57. data.mTime.push_back(temp);
  58. }
  59. }
  60. }
  61. {
  62. const char* NumberOfRobots = element->Attribute("NumberOfRobots");
  63. std::vector<std::string> vecNumberOfRobots;
  64. boost::split(vecNumberOfRobots, NumberOfRobots, boost::is_any_of(","));
  65. int temp;
  66. for (unsigned int i = 0; i < vecNumberOfRobots.size(); i++)
  67. {
  68. if (tinyxml2::XMLUtil::ToInt(vecNumberOfRobots[i].c_str(), &temp))
  69. {
  70. data.mNumberOfRobots.push_back(temp);
  71. }
  72. }
  73. }
  74. data.mMin = element->IntAttribute("Min");
  75. data.mMax = element->IntAttribute("Max");
  76. data.mCreateHisCountMin = element->IntAttribute("CreateHisCountMin");
  77. data.mCreateHisCountMax = element->IntAttribute("CreateHisCountMax");
  78. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  79. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  80. mMapData.insert(std::make_pair(data.mID, data));
  81. element = element->NextSiblingElement();
  82. }
  83. CCLOG("M_RobotsCFG Loaded. Load Data:%u", mMapData.size());
  84. }
  85. void M_RobotsCFG::LoadLua()
  86. {
  87. LuaEngine::getInstance()->executeScriptFile("config/M_RobotsCFG");
  88. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  89. lua_getglobal(L, "M_RobotsCFG");
  90. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  91. lua_pushstring(L, "datas");
  92. lua_gettable(L, -2);
  93. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  94. lua_pushnil(L);
  95. while(lua_next(L, 2))
  96. {
  97. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  98. M_RobotsCFGData data;
  99. LuaCfgHelper::readInt(L, "ID", data.mID);
  100. LuaCfgHelper::readInt(L, "RankingTyep", data.mRankingTyep);
  101. LuaCfgHelper::readVectorInt(L, "Time", data.mTime);
  102. LuaCfgHelper::readVectorInt(L, "NumberOfRobots", data.mNumberOfRobots);
  103. LuaCfgHelper::readInt(L, "Min", data.mMin);
  104. LuaCfgHelper::readInt(L, "Max", data.mMax);
  105. LuaCfgHelper::readInt(L, "CreateHisCountMin", data.mCreateHisCountMin);
  106. LuaCfgHelper::readInt(L, "CreateHisCountMax", data.mCreateHisCountMax);
  107. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  108. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  109. mMapData.insert(std::make_pair(data.mID, data));
  110. lua_pop(L, 1);
  111. }
  112. lua_settop(L, 0);
  113. CCLOG("M_RobotsCFG Loaded. Load Data:%u", mMapData.size());
  114. }
  115. void M_RobotsCFG::Reload()
  116. {
  117. mMapData.clear();
  118. Load();
  119. }
  120. M_RobotsCFG* M_RobotsCFG::GetSingleton()
  121. {
  122. if (msSingleton.get() == nullptr)
  123. {
  124. msSingleton.reset(new M_RobotsCFG());
  125. }
  126. return msSingleton.get();
  127. }
  128. void M_RobotsCFG::Release()
  129. {
  130. msSingleton.reset(nullptr);
  131. }