M_Global_Config.cpp 3.8 KB

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