M_GiftGuideCFG.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_GiftGuideCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<M_GiftGuideCFG> M_GiftGuideCFG::msSingleton(nullptr);
  12. int M_GiftGuideCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const M_GiftGuideCFGData* M_GiftGuideCFG::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_GiftGuideCFGData>& M_GiftGuideCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void M_GiftGuideCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void M_GiftGuideCFG::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_GiftGuideCFGData data;
  67. data.mID = element->IntAttribute("ID");
  68. data.mName = element->Attribute("Name");
  69. data.mNeedLevel = element->IntAttribute("NeedLevel");
  70. data.mType = element->IntAttribute("Type");
  71. data.mRechangeID = element->IntAttribute("RechangeID");
  72. data.mContinueTime = element->IntAttribute("ContinueTime");
  73. {
  74. const char* AwardItem = element->Attribute("AwardItem");
  75. std::vector<std::string> vecAwardItem;
  76. boost::split(vecAwardItem, AwardItem, boost::is_any_of(","));
  77. int temp;
  78. for (unsigned int i = 0; i < vecAwardItem.size(); i++)
  79. {
  80. if (tinyxml2::XMLUtil::ToInt(vecAwardItem[i].c_str(), &temp))
  81. {
  82. data.mAwardItem.push_back(temp);
  83. }
  84. }
  85. }
  86. data.mParam = element->IntAttribute("Param");
  87. data.mTitleImage = element->Attribute("TitleImage");
  88. data.mLeftImage = element->Attribute("LeftImage");
  89. data.mRMBImage = element->Attribute("RMBImage");
  90. {
  91. const char* GemPrice = element->Attribute("GemPrice");
  92. std::vector<std::string> vecGemPrice;
  93. boost::split(vecGemPrice, GemPrice, boost::is_any_of(","));
  94. int temp;
  95. for (unsigned int i = 0; i < vecGemPrice.size(); i++)
  96. {
  97. if (tinyxml2::XMLUtil::ToInt(vecGemPrice[i].c_str(), &temp))
  98. {
  99. data.mGemPrice.push_back(temp);
  100. }
  101. }
  102. }
  103. data.mStartTime = element->Attribute("StartTime");
  104. data.mEndTime = element->Attribute("EndTime");
  105. data.mRefreshWeekDay = element->IntAttribute("RefreshWeekDay");
  106. data.mBuyCount = element->IntAttribute("BuyCount");
  107. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  108. assert(mMapData.find(data.mID) == mMapData.end());
  109. mMapData.insert(std::make_pair(data.mID, data));
  110. element = element->NextSiblingElement();
  111. }
  112. }
  113. void M_GiftGuideCFG::Load()
  114. {
  115. Load("../Config/M_GiftGuideCFG.xml");
  116. }
  117. M_GiftGuideCFG* M_GiftGuideCFG::GetSingleton()
  118. {
  119. if (msSingleton.get() == nullptr)
  120. {
  121. msSingleton.reset(new M_GiftGuideCFG());
  122. }
  123. return msSingleton.get();
  124. }