M_GuideCFG.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "stdafx.h"
  2. #include <cassert>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <iostream>
  6. #include <boost/smart_ptr.hpp>
  7. #include <boost/algorithm/string.hpp>
  8. #include "tinyxml2.h"
  9. #include "M_GuideCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<M_GuideCFG> M_GuideCFG::msSingleton(nullptr);
  12. int M_GuideCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const M_GuideCFGData* M_GuideCFG::GetData(int ID)
  17. {
  18. auto it = mMapData.find(ID);
  19. if (it != mMapData.end())
  20. {
  21. return &it->second;
  22. }
  23. return NULL;
  24. }
  25. boost::unordered_map<int, M_GuideCFGData>& M_GuideCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void M_GuideCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void M_GuideCFG::Load(const std::string& path)
  35. {
  36. std::ifstream readStream(path, std::ios::binary);
  37. if (!readStream.is_open())
  38. {
  39. assert(false);
  40. return;
  41. }
  42. readStream.seekg(0, std::ios::end);
  43. int fileSize = readStream.tellg();
  44. boost::shared_array<char> buffer(new char[fileSize+1]);
  45. buffer.get()[fileSize] = '\0';
  46. readStream.seekg(0, std::ios::beg);
  47. readStream.read(buffer.get(), fileSize);
  48. readStream.close();
  49. FileEncrypt::decryptBuffer( buffer, fileSize );
  50. tinyxml2::XMLDocument xmlDoc;
  51. auto result = xmlDoc.Parse(buffer.get(), fileSize);
  52. if (result != tinyxml2::XML_SUCCESS)
  53. {
  54. assert(false);
  55. return;
  56. }
  57. auto root = xmlDoc.RootElement();
  58. if (root == NULL)
  59. {
  60. assert(false);
  61. return;
  62. }
  63. auto element = root->FirstChildElement("Data");
  64. while (element != NULL)
  65. {
  66. M_GuideCFGData data;
  67. data.mID = element->IntAttribute("ID");
  68. data.mType = element->IntAttribute("Type");
  69. {
  70. const char* SelPos = element->Attribute("SelPos");
  71. std::vector<std::string> vecSelPos;
  72. boost::split(vecSelPos, SelPos, boost::is_any_of(","));
  73. int temp;
  74. for (unsigned int i = 0; i < vecSelPos.size(); i++)
  75. {
  76. if (tinyxml2::XMLUtil::ToInt(vecSelPos[i].c_str(), &temp))
  77. {
  78. data.mSelPos.push_back(temp);
  79. }
  80. }
  81. }
  82. {
  83. const char* SelSize = element->Attribute("SelSize");
  84. std::vector<std::string> vecSelSize;
  85. boost::split(vecSelSize, SelSize, boost::is_any_of(","));
  86. int temp;
  87. for (unsigned int i = 0; i < vecSelSize.size(); i++)
  88. {
  89. if (tinyxml2::XMLUtil::ToInt(vecSelSize[i].c_str(), &temp))
  90. {
  91. data.mSelSize.push_back(temp);
  92. }
  93. }
  94. }
  95. data.mSelPic = element->Attribute("SelPic");
  96. data.mTipsDescPic = element->Attribute("TipsDescPic");
  97. {
  98. const char* TipsPos = element->Attribute("TipsPos");
  99. std::vector<std::string> vecTipsPos;
  100. boost::split(vecTipsPos, TipsPos, boost::is_any_of(","));
  101. int temp;
  102. for (unsigned int i = 0; i < vecTipsPos.size(); i++)
  103. {
  104. if (tinyxml2::XMLUtil::ToInt(vecTipsPos[i].c_str(), &temp))
  105. {
  106. data.mTipsPos.push_back(temp);
  107. }
  108. }
  109. }
  110. {
  111. const char* TipsSize = element->Attribute("TipsSize");
  112. std::vector<std::string> vecTipsSize;
  113. boost::split(vecTipsSize, TipsSize, boost::is_any_of(","));
  114. int temp;
  115. for (unsigned int i = 0; i < vecTipsSize.size(); i++)
  116. {
  117. if (tinyxml2::XMLUtil::ToInt(vecTipsSize[i].c_str(), &temp))
  118. {
  119. data.mTipsSize.push_back(temp);
  120. }
  121. }
  122. }
  123. data.mTipsPic = element->Attribute("TipsPic");
  124. {
  125. const char* IconPos = element->Attribute("IconPos");
  126. std::vector<std::string> vecIconPos;
  127. boost::split(vecIconPos, IconPos, boost::is_any_of(","));
  128. int temp;
  129. for (unsigned int i = 0; i < vecIconPos.size(); i++)
  130. {
  131. if (tinyxml2::XMLUtil::ToInt(vecIconPos[i].c_str(), &temp))
  132. {
  133. data.mIconPos.push_back(temp);
  134. }
  135. }
  136. }
  137. {
  138. const char* FingerPos = element->Attribute("FingerPos");
  139. std::vector<std::string> vecFingerPos;
  140. boost::split(vecFingerPos, FingerPos, boost::is_any_of(","));
  141. int temp;
  142. for (unsigned int i = 0; i < vecFingerPos.size(); i++)
  143. {
  144. if (tinyxml2::XMLUtil::ToInt(vecFingerPos[i].c_str(), &temp))
  145. {
  146. data.mFingerPos.push_back(temp);
  147. }
  148. }
  149. }
  150. data.mName = element->Attribute("Name");
  151. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  152. assert(mMapData.find(data.mID) == mMapData.end());
  153. mMapData.insert(std::make_pair(data.mID, data));
  154. element = element->NextSiblingElement();
  155. }
  156. }
  157. void M_GuideCFG::Load()
  158. {
  159. Load("../Config/M_GuideCFG.xml");
  160. }
  161. M_GuideCFG* M_GuideCFG::GetSingleton()
  162. {
  163. if (msSingleton.get() == nullptr)
  164. {
  165. msSingleton.reset(new M_GuideCFG());
  166. }
  167. return msSingleton.get();
  168. }