M_GiftRewardCFG.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_GiftRewardCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<M_GiftRewardCFG> M_GiftRewardCFG::msSingleton(nullptr);
  12. int M_GiftRewardCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const M_GiftRewardCFGData* M_GiftRewardCFG::GetData(int RewardType)
  17. {
  18. auto it = mMapData.find(RewardType);
  19. if (it != mMapData.end())
  20. {
  21. return &it->second;
  22. }
  23. return NULL;
  24. }
  25. boost::unordered_map<int, M_GiftRewardCFGData>& M_GiftRewardCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void M_GiftRewardCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void M_GiftRewardCFG::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_GiftRewardCFGData data;
  67. data.mRewardType = element->IntAttribute("RewardType");
  68. data.mStartTime = element->Attribute("StartTime");
  69. data.mEndTime = element->Attribute("EndTime");
  70. {
  71. const char* ItemId = element->Attribute("ItemId");
  72. std::vector<std::string> vecItemId;
  73. boost::split(vecItemId, ItemId, boost::is_any_of(","));
  74. int temp;
  75. for (unsigned int i = 0; i < vecItemId.size(); i++)
  76. {
  77. if (tinyxml2::XMLUtil::ToInt(vecItemId[i].c_str(), &temp))
  78. {
  79. data.mItemId.push_back(temp);
  80. }
  81. }
  82. }
  83. {
  84. const char* ItemCount = element->Attribute("ItemCount");
  85. std::vector<std::string> vecItemCount;
  86. boost::split(vecItemCount, ItemCount, boost::is_any_of(","));
  87. int temp;
  88. for (unsigned int i = 0; i < vecItemCount.size(); i++)
  89. {
  90. if (tinyxml2::XMLUtil::ToInt(vecItemCount[i].c_str(), &temp))
  91. {
  92. data.mItemCount.push_back(temp);
  93. }
  94. }
  95. }
  96. {
  97. const char* ItemId2 = element->Attribute("ItemId2");
  98. std::vector<std::string> vecItemId2;
  99. boost::split(vecItemId2, ItemId2, boost::is_any_of(","));
  100. int temp;
  101. for (unsigned int i = 0; i < vecItemId2.size(); i++)
  102. {
  103. if (tinyxml2::XMLUtil::ToInt(vecItemId2[i].c_str(), &temp))
  104. {
  105. data.mItemId2.push_back(temp);
  106. }
  107. }
  108. }
  109. {
  110. const char* ItemId3 = element->Attribute("ItemId3");
  111. std::vector<std::string> vecItemId3;
  112. boost::split(vecItemId3, ItemId3, boost::is_any_of(","));
  113. int temp;
  114. for (unsigned int i = 0; i < vecItemId3.size(); i++)
  115. {
  116. if (tinyxml2::XMLUtil::ToInt(vecItemId3[i].c_str(), &temp))
  117. {
  118. data.mItemId3.push_back(temp);
  119. }
  120. }
  121. }
  122. data.mPrice = element->IntAttribute("Price");
  123. {
  124. const char* TotalPrice = element->Attribute("TotalPrice");
  125. std::vector<std::string> vecTotalPrice;
  126. boost::split(vecTotalPrice, TotalPrice, boost::is_any_of(","));
  127. int temp;
  128. for (unsigned int i = 0; i < vecTotalPrice.size(); i++)
  129. {
  130. if (tinyxml2::XMLUtil::ToInt(vecTotalPrice[i].c_str(), &temp))
  131. {
  132. data.mTotalPrice.push_back(temp);
  133. }
  134. }
  135. }
  136. data.mVipLevel = element->IntAttribute("VipLevel");
  137. data.mGiftId = element->IntAttribute("GiftId");
  138. if (mMapData.find(data.mRewardType) != mMapData.end())std::cout <<"data refind:" << data.mRewardType << std::endl;
  139. assert(mMapData.find(data.mRewardType) == mMapData.end());
  140. mMapData.insert(std::make_pair(data.mRewardType, data));
  141. element = element->NextSiblingElement();
  142. }
  143. }
  144. void M_GiftRewardCFG::Load()
  145. {
  146. Load("../Config/M_GiftRewardCFG.xml");
  147. }
  148. M_GiftRewardCFG* M_GiftRewardCFG::GetSingleton()
  149. {
  150. if (msSingleton.get() == nullptr)
  151. {
  152. msSingleton.reset(new M_GiftRewardCFG());
  153. }
  154. return msSingleton.get();
  155. }