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