M_RechangeCFG.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_RechangeCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<M_RechangeCFG> M_RechangeCFG::msSingleton(nullptr);
  12. int M_RechangeCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const M_RechangeCFGData* M_RechangeCFG::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_RechangeCFGData>& M_RechangeCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void M_RechangeCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void M_RechangeCFG::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_RechangeCFGData data;
  67. data.mID = element->IntAttribute("ID");
  68. data.mName = element->Attribute("Name");
  69. data.mDesc = element->Attribute("Desc");
  70. data.mActivityId = element->IntAttribute("ActivityId");
  71. data.mIcon = element->Attribute("Icon");
  72. data.mStartTime = element->Attribute("StartTime");
  73. data.mEndTime = element->Attribute("EndTime");
  74. data.mGroup = element->IntAttribute("Group");
  75. {
  76. const char* needlevel = element->Attribute("needlevel");
  77. std::vector<std::string> vecneedlevel;
  78. boost::split(vecneedlevel, needlevel, boost::is_any_of(","));
  79. int temp;
  80. for (unsigned int i = 0; i < vecneedlevel.size(); i++)
  81. {
  82. if (tinyxml2::XMLUtil::ToInt(vecneedlevel[i].c_str(), &temp))
  83. {
  84. data.mneedlevel.push_back(temp);
  85. }
  86. }
  87. }
  88. data.mNeedVIP = element->IntAttribute("NeedVIP");
  89. data.mType = element->IntAttribute("Type");
  90. data.mPrice = element->IntAttribute("Price");
  91. data.mDiamondPrice = element->IntAttribute("DiamondPrice");
  92. data.mNeedLevel = element->IntAttribute("NeedLevel");
  93. data.mFirstGold = element->IntAttribute("FirstGold");
  94. data.mFirstTicket = element->IntAttribute("FirstTicket");
  95. data.mVIPExp = element->IntAttribute("VIPExp");
  96. data.mIndex = element->IntAttribute("Index");
  97. data.mLastGift = element->IntAttribute("LastGift");
  98. data.mNextGift = element->IntAttribute("NextGift");
  99. data.mShopType = element->IntAttribute("ShopType");
  100. data.mCumulativeRecharge = element->IntAttribute("CumulativeRecharge");
  101. {
  102. const char* ItemID = element->Attribute("ItemID");
  103. std::vector<std::string> vecItemID;
  104. boost::split(vecItemID, ItemID, boost::is_any_of(","));
  105. int temp;
  106. for (unsigned int i = 0; i < vecItemID.size(); i++)
  107. {
  108. if (tinyxml2::XMLUtil::ToInt(vecItemID[i].c_str(), &temp))
  109. {
  110. data.mItemID.push_back(temp);
  111. }
  112. }
  113. }
  114. {
  115. const char* ItemCount = element->Attribute("ItemCount");
  116. std::vector<std::string> vecItemCount;
  117. boost::split(vecItemCount, ItemCount, boost::is_any_of(","));
  118. int temp;
  119. for (unsigned int i = 0; i < vecItemCount.size(); i++)
  120. {
  121. if (tinyxml2::XMLUtil::ToInt(vecItemCount[i].c_str(), &temp))
  122. {
  123. data.mItemCount.push_back(temp);
  124. }
  125. }
  126. }
  127. data.mCount = element->IntAttribute("Count");
  128. data.mDailyGiftPackage = element->IntAttribute("DailyGiftPackage");
  129. data.mTimeLimit = element->IntAttribute("TimeLimit");
  130. data.mCategory = element->IntAttribute("Category");
  131. data.mBuffID = element->IntAttribute("BuffID");
  132. data.mRebateValue = element->IntAttribute("RebateValue");
  133. data.mHotIcon = element->IntAttribute("HotIcon");
  134. {
  135. const char* PackageProbability = element->Attribute("PackageProbability");
  136. std::vector<std::string> vecPackageProbability;
  137. boost::split(vecPackageProbability, PackageProbability, boost::is_any_of(","));
  138. float temp;
  139. for (unsigned int i = 0; i < vecPackageProbability.size(); i++)
  140. {
  141. if (tinyxml2::XMLUtil::ToFloat(vecPackageProbability[i].c_str(), &temp))
  142. {
  143. data.mPackageProbability.push_back(temp);
  144. }
  145. }
  146. }
  147. data.mOriginalprice = element->IntAttribute("Originalprice");
  148. data.mGOLDOriginalprice = element->IntAttribute("GOLDOriginalprice");
  149. data.mDiamondsOriginalprice = element->IntAttribute("DiamondsOriginalprice");
  150. data.mGiftCeiling = element->IntAttribute("GiftCeiling");
  151. data.mLowerGiftLimit = element->IntAttribute("LowerGiftLimit");
  152. data.mHighestGain = element->IntAttribute("HighestGain");
  153. data.mTipsDisplay = element->IntAttribute("TipsDisplay");
  154. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  155. assert(mMapData.find(data.mID) == mMapData.end());
  156. mMapData.insert(std::make_pair(data.mID, data));
  157. element = element->NextSiblingElement();
  158. }
  159. }
  160. void M_RechangeCFG::Load()
  161. {
  162. Load("../Config/M_RechangeCFG.xml");
  163. }
  164. M_RechangeCFG* M_RechangeCFG::GetSingleton()
  165. {
  166. if (msSingleton.get() == nullptr)
  167. {
  168. msSingleton.reset(new M_RechangeCFG());
  169. }
  170. return msSingleton.get();
  171. }