M_GuideCFG.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_GuideCFG.h"
  7. std::auto_ptr<M_GuideCFG> M_GuideCFG::msSingleton(nullptr);
  8. int M_GuideCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_GuideCFGData* M_GuideCFG::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_GuideCFGData>& M_GuideCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_GuideCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_GuideCFG.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_GuideCFGData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mType = element->IntAttribute("Type");
  48. {
  49. const char* SelPos = element->Attribute("SelPos");
  50. std::vector<std::string> vecSelPos;
  51. boost::split(vecSelPos, SelPos, boost::is_any_of(","));
  52. int temp;
  53. for (unsigned int i = 0; i < vecSelPos.size(); i++)
  54. {
  55. if (tinyxml2::XMLUtil::ToInt(vecSelPos[i].c_str(), &temp))
  56. {
  57. data.mSelPos.push_back(temp);
  58. }
  59. }
  60. }
  61. {
  62. const char* SelSize = element->Attribute("SelSize");
  63. std::vector<std::string> vecSelSize;
  64. boost::split(vecSelSize, SelSize, boost::is_any_of(","));
  65. int temp;
  66. for (unsigned int i = 0; i < vecSelSize.size(); i++)
  67. {
  68. if (tinyxml2::XMLUtil::ToInt(vecSelSize[i].c_str(), &temp))
  69. {
  70. data.mSelSize.push_back(temp);
  71. }
  72. }
  73. }
  74. data.mSelPic = element->Attribute("SelPic");
  75. data.mTipsDescPic = element->Attribute("TipsDescPic");
  76. {
  77. const char* TipsPos = element->Attribute("TipsPos");
  78. std::vector<std::string> vecTipsPos;
  79. boost::split(vecTipsPos, TipsPos, boost::is_any_of(","));
  80. int temp;
  81. for (unsigned int i = 0; i < vecTipsPos.size(); i++)
  82. {
  83. if (tinyxml2::XMLUtil::ToInt(vecTipsPos[i].c_str(), &temp))
  84. {
  85. data.mTipsPos.push_back(temp);
  86. }
  87. }
  88. }
  89. {
  90. const char* TipsSize = element->Attribute("TipsSize");
  91. std::vector<std::string> vecTipsSize;
  92. boost::split(vecTipsSize, TipsSize, boost::is_any_of(","));
  93. int temp;
  94. for (unsigned int i = 0; i < vecTipsSize.size(); i++)
  95. {
  96. if (tinyxml2::XMLUtil::ToInt(vecTipsSize[i].c_str(), &temp))
  97. {
  98. data.mTipsSize.push_back(temp);
  99. }
  100. }
  101. }
  102. data.mTipsPic = element->Attribute("TipsPic");
  103. {
  104. const char* IconPos = element->Attribute("IconPos");
  105. std::vector<std::string> vecIconPos;
  106. boost::split(vecIconPos, IconPos, boost::is_any_of(","));
  107. int temp;
  108. for (unsigned int i = 0; i < vecIconPos.size(); i++)
  109. {
  110. if (tinyxml2::XMLUtil::ToInt(vecIconPos[i].c_str(), &temp))
  111. {
  112. data.mIconPos.push_back(temp);
  113. }
  114. }
  115. }
  116. {
  117. const char* FingerPos = element->Attribute("FingerPos");
  118. std::vector<std::string> vecFingerPos;
  119. boost::split(vecFingerPos, FingerPos, boost::is_any_of(","));
  120. int temp;
  121. for (unsigned int i = 0; i < vecFingerPos.size(); i++)
  122. {
  123. if (tinyxml2::XMLUtil::ToInt(vecFingerPos[i].c_str(), &temp))
  124. {
  125. data.mFingerPos.push_back(temp);
  126. }
  127. }
  128. }
  129. data.mName = element->Attribute("Name");
  130. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  131. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  132. mMapData.insert(std::make_pair(data.mID, data));
  133. element = element->NextSiblingElement();
  134. }
  135. CCLOG("M_GuideCFG Loaded. Load Data:%u", mMapData.size());
  136. }
  137. void M_GuideCFG::LoadLua()
  138. {
  139. LuaEngine::getInstance()->executeScriptFile("config/M_GuideCFG");
  140. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  141. lua_getglobal(L, "M_GuideCFG");
  142. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  143. lua_pushstring(L, "datas");
  144. lua_gettable(L, -2);
  145. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  146. lua_pushnil(L);
  147. while(lua_next(L, 2))
  148. {
  149. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  150. M_GuideCFGData data;
  151. LuaCfgHelper::readInt(L, "ID", data.mID);
  152. LuaCfgHelper::readInt(L, "Type", data.mType);
  153. LuaCfgHelper::readVectorInt(L, "SelPos", data.mSelPos);
  154. LuaCfgHelper::readVectorInt(L, "SelSize", data.mSelSize);
  155. LuaCfgHelper::readString(L, "SelPic", data.mSelPic);
  156. LuaCfgHelper::readString(L, "TipsDescPic", data.mTipsDescPic);
  157. LuaCfgHelper::readVectorInt(L, "TipsPos", data.mTipsPos);
  158. LuaCfgHelper::readVectorInt(L, "TipsSize", data.mTipsSize);
  159. LuaCfgHelper::readString(L, "TipsPic", data.mTipsPic);
  160. LuaCfgHelper::readVectorInt(L, "IconPos", data.mIconPos);
  161. LuaCfgHelper::readVectorInt(L, "FingerPos", data.mFingerPos);
  162. LuaCfgHelper::readString(L, "Name", data.mName);
  163. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  164. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  165. mMapData.insert(std::make_pair(data.mID, data));
  166. lua_pop(L, 1);
  167. }
  168. lua_settop(L, 0);
  169. CCLOG("M_GuideCFG Loaded. Load Data:%u", mMapData.size());
  170. }
  171. void M_GuideCFG::Reload()
  172. {
  173. mMapData.clear();
  174. Load();
  175. }
  176. M_GuideCFG* M_GuideCFG::GetSingleton()
  177. {
  178. if (msSingleton.get() == nullptr)
  179. {
  180. msSingleton.reset(new M_GuideCFG());
  181. }
  182. return msSingleton.get();
  183. }
  184. void M_GuideCFG::Release()
  185. {
  186. msSingleton.reset(nullptr);
  187. }