M_ExchangeCFG.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_ExchangeCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<M_ExchangeCFG> M_ExchangeCFG::msSingleton(nullptr);
  12. int M_ExchangeCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const M_ExchangeCFGData* M_ExchangeCFG::GetData(int ChangeId)
  17. {
  18. auto it = mMapData.find(ChangeId);
  19. if (it != mMapData.end())
  20. {
  21. return &it->second;
  22. }
  23. return NULL;
  24. }
  25. boost::unordered_map<int, M_ExchangeCFGData>& M_ExchangeCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void M_ExchangeCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void M_ExchangeCFG::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_ExchangeCFGData data;
  67. data.mChangeId = element->IntAttribute("ChangeId");
  68. data.mFunction = element->IntAttribute("Function");
  69. data.moder = element->IntAttribute("oder");
  70. data.mItemName = element->Attribute("ItemName");
  71. data.mItemDesc = element->Attribute("ItemDesc");
  72. data.mVipCondition = element->IntAttribute("VipCondition");
  73. data.mExchangeType = element->IntAttribute("ExchangeType");
  74. {
  75. const char* NeedItemId = element->Attribute("NeedItemId");
  76. std::vector<std::string> vecNeedItemId;
  77. boost::split(vecNeedItemId, NeedItemId, boost::is_any_of(","));
  78. int temp;
  79. for (unsigned int i = 0; i < vecNeedItemId.size(); i++)
  80. {
  81. if (tinyxml2::XMLUtil::ToInt(vecNeedItemId[i].c_str(), &temp))
  82. {
  83. data.mNeedItemId.push_back(temp);
  84. }
  85. }
  86. }
  87. {
  88. const char* NeedItemCount = element->Attribute("NeedItemCount");
  89. std::vector<std::string> vecNeedItemCount;
  90. boost::split(vecNeedItemCount, NeedItemCount, boost::is_any_of(","));
  91. int temp;
  92. for (unsigned int i = 0; i < vecNeedItemCount.size(); i++)
  93. {
  94. if (tinyxml2::XMLUtil::ToInt(vecNeedItemCount[i].c_str(), &temp))
  95. {
  96. data.mNeedItemCount.push_back(temp);
  97. }
  98. }
  99. }
  100. {
  101. const char* ItemId = element->Attribute("ItemId");
  102. std::vector<std::string> vecItemId;
  103. boost::split(vecItemId, ItemId, boost::is_any_of(","));
  104. int temp;
  105. for (unsigned int i = 0; i < vecItemId.size(); i++)
  106. {
  107. if (tinyxml2::XMLUtil::ToInt(vecItemId[i].c_str(), &temp))
  108. {
  109. data.mItemId.push_back(temp);
  110. }
  111. }
  112. }
  113. {
  114. const char* ItemCount = element->Attribute("ItemCount");
  115. std::vector<std::string> vecItemCount;
  116. boost::split(vecItemCount, ItemCount, boost::is_any_of(","));
  117. int temp;
  118. for (unsigned int i = 0; i < vecItemCount.size(); i++)
  119. {
  120. if (tinyxml2::XMLUtil::ToInt(vecItemCount[i].c_str(), &temp))
  121. {
  122. data.mItemCount.push_back(temp);
  123. }
  124. }
  125. }
  126. data.mIcon = element->Attribute("Icon");
  127. data.mTimeType = element->IntAttribute("TimeType");
  128. data.mExchangeMaxLimit = element->IntAttribute("ExchangeMaxLimit");
  129. data.mType = element->IntAttribute("Type");
  130. data.mManageMent = element->IntAttribute("ManageMent");
  131. data.mManageMentName = element->Attribute("ManageMentName");
  132. data.mDiscount = element->IntAttribute("Discount");
  133. data.mReturnContribution = element->IntAttribute("ReturnContribution");
  134. if (mMapData.find(data.mChangeId) != mMapData.end())std::cout <<"data refind:" << data.mChangeId << std::endl;
  135. assert(mMapData.find(data.mChangeId) == mMapData.end());
  136. mMapData.insert(std::make_pair(data.mChangeId, data));
  137. element = element->NextSiblingElement();
  138. }
  139. }
  140. void M_ExchangeCFG::Load()
  141. {
  142. Load("../Config/M_ExchangeCFG.xml");
  143. }
  144. M_ExchangeCFG* M_ExchangeCFG::GetSingleton()
  145. {
  146. if (msSingleton.get() == nullptr)
  147. {
  148. msSingleton.reset(new M_ExchangeCFG());
  149. }
  150. return msSingleton.get();
  151. }