M_RecordCFG.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_RecordCFG.h"
  7. std::auto_ptr<M_RecordCFG> M_RecordCFG::msSingleton(nullptr);
  8. int M_RecordCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_RecordCFGData* M_RecordCFG::GetData(int RecordID)
  13. {
  14. auto it = mMapData.find(RecordID);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_RecordCFGData>& M_RecordCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_RecordCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_RecordCFG.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_RecordCFGData data;
  46. data.mRecordID = element->IntAttribute("RecordID");
  47. data.mRecordName = element->Attribute("RecordName");
  48. {
  49. const char* RecordInfoList = element->Attribute("RecordInfoList");
  50. std::vector<std::string> vecRecordInfoList;
  51. boost::split(vecRecordInfoList, RecordInfoList, boost::is_any_of(","));
  52. for (unsigned int i = 0; i < vecRecordInfoList.size(); i++)
  53. {
  54. data.mRecordInfoList.push_back(vecRecordInfoList[i]);
  55. }
  56. }
  57. if (mMapData.find(data.mRecordID) != mMapData.end())std::cout <<"data refind:" << data.mRecordID << std::endl;
  58. CCASSERT(mMapData.find(data.mRecordID) == mMapData.end(), "data.mRecordID is exists");
  59. mMapData.insert(std::make_pair(data.mRecordID, data));
  60. element = element->NextSiblingElement();
  61. }
  62. CCLOG("M_RecordCFG Loaded. Load Data:%u", mMapData.size());
  63. }
  64. void M_RecordCFG::LoadLua()
  65. {
  66. LuaEngine::getInstance()->executeScriptFile("config/M_RecordCFG");
  67. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  68. lua_getglobal(L, "M_RecordCFG");
  69. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  70. lua_pushstring(L, "datas");
  71. lua_gettable(L, -2);
  72. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  73. lua_pushnil(L);
  74. while(lua_next(L, 2))
  75. {
  76. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  77. M_RecordCFGData data;
  78. LuaCfgHelper::readInt(L, "RecordID", data.mRecordID);
  79. LuaCfgHelper::readString(L, "RecordName", data.mRecordName);
  80. LuaCfgHelper::readVectorString(L, "RecordInfoList", data.mRecordInfoList);
  81. if (mMapData.find(data.mRecordID) != mMapData.end())std::cout <<"data refind:" << data.mRecordID << std::endl;
  82. CCASSERT(mMapData.find(data.mRecordID) == mMapData.end(), "data.mRecordID is exists");
  83. mMapData.insert(std::make_pair(data.mRecordID, data));
  84. lua_pop(L, 1);
  85. }
  86. lua_settop(L, 0);
  87. CCLOG("M_RecordCFG Loaded. Load Data:%u", mMapData.size());
  88. }
  89. void M_RecordCFG::Reload()
  90. {
  91. mMapData.clear();
  92. Load();
  93. }
  94. M_RecordCFG* M_RecordCFG::GetSingleton()
  95. {
  96. if (msSingleton.get() == nullptr)
  97. {
  98. msSingleton.reset(new M_RecordCFG());
  99. }
  100. return msSingleton.get();
  101. }
  102. void M_RecordCFG::Release()
  103. {
  104. msSingleton.reset(nullptr);
  105. }