M_Global_Config.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_Global_Config.h"
  7. std::auto_ptr<M_Global_Config> M_Global_Config::msSingleton(nullptr);
  8. int M_Global_Config::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_Global_ConfigData* M_Global_Config::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_Global_ConfigData>& M_Global_Config::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_Global_Config::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_Global_Config.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_Global_ConfigData data;
  46. data.mKey = element->Attribute("Key");
  47. data.mDesc = element->Attribute("Desc");
  48. data.mIntValue = element->IntAttribute("IntValue");
  49. {
  50. const char* IntListValue = element->Attribute("IntListValue");
  51. std::vector<std::string> vecIntListValue;
  52. boost::split(vecIntListValue, IntListValue, boost::is_any_of(","));
  53. int temp;
  54. for (unsigned int i = 0; i < vecIntListValue.size(); i++)
  55. {
  56. if (tinyxml2::XMLUtil::ToInt(vecIntListValue[i].c_str(), &temp))
  57. {
  58. data.mIntListValue.push_back(temp);
  59. }
  60. }
  61. }
  62. data.mStrValue = element->Attribute("StrValue");
  63. {
  64. const char* StrListValue = element->Attribute("StrListValue");
  65. std::vector<std::string> vecStrListValue;
  66. boost::split(vecStrListValue, StrListValue, boost::is_any_of(","));
  67. for (unsigned int i = 0; i < vecStrListValue.size(); i++)
  68. {
  69. data.mStrListValue.push_back(vecStrListValue[i]);
  70. }
  71. }
  72. data.mBoolValue = element->BoolAttribute("BoolValue");
  73. if (mMapData.find(data.mKey) != mMapData.end())std::cout <<"data refind:" << data.mKey << std::endl;
  74. CCASSERT(mMapData.find(data.mKey) == mMapData.end(), "data.mKey is exists");
  75. mMapData.insert(std::make_pair(data.mKey, data));
  76. element = element->NextSiblingElement();
  77. }
  78. CCLOG("M_Global_Config Loaded. Load Data:%u", mMapData.size());
  79. }
  80. void M_Global_Config::LoadLua()
  81. {
  82. LuaEngine::getInstance()->executeScriptFile("config/M_Global_Config");
  83. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  84. lua_getglobal(L, "M_Global_Config");
  85. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  86. lua_pushstring(L, "datas");
  87. lua_gettable(L, -2);
  88. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  89. lua_pushnil(L);
  90. while(lua_next(L, 2))
  91. {
  92. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  93. M_Global_ConfigData data;
  94. LuaCfgHelper::readString(L, "Key", data.mKey);
  95. LuaCfgHelper::readString(L, "Desc", data.mDesc);
  96. LuaCfgHelper::readInt(L, "IntValue", data.mIntValue);
  97. LuaCfgHelper::readVectorInt(L, "IntListValue", data.mIntListValue);
  98. LuaCfgHelper::readString(L, "StrValue", data.mStrValue);
  99. LuaCfgHelper::readVectorString(L, "StrListValue", data.mStrListValue);
  100. LuaCfgHelper::readBool(L, "BoolValue", data.mBoolValue);
  101. if (mMapData.find(data.mKey) != mMapData.end())std::cout <<"data refind:" << data.mKey << std::endl;
  102. CCASSERT(mMapData.find(data.mKey) == mMapData.end(), "data.mKey is exists");
  103. mMapData.insert(std::make_pair(data.mKey, data));
  104. lua_pop(L, 1);
  105. }
  106. lua_settop(L, 0);
  107. CCLOG("M_Global_Config Loaded. Load Data:%u", mMapData.size());
  108. }
  109. void M_Global_Config::Reload()
  110. {
  111. mMapData.clear();
  112. Load();
  113. }
  114. M_Global_Config* M_Global_Config::GetSingleton()
  115. {
  116. if (msSingleton.get() == nullptr)
  117. {
  118. msSingleton.reset(new M_Global_Config());
  119. }
  120. return msSingleton.get();
  121. }
  122. void M_Global_Config::Release()
  123. {
  124. msSingleton.reset(nullptr);
  125. }