Fish_SquamaExchangeCFG.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "Fish_SquamaExchangeCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<Fish_SquamaExchangeCFG> Fish_SquamaExchangeCFG::msSingleton(nullptr);
  12. int Fish_SquamaExchangeCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const Fish_SquamaExchangeCFGData* Fish_SquamaExchangeCFG::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, Fish_SquamaExchangeCFGData>& Fish_SquamaExchangeCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void Fish_SquamaExchangeCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void Fish_SquamaExchangeCFG::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. Fish_SquamaExchangeCFGData data;
  67. data.mID = element->IntAttribute("ID");
  68. data.mPrice = element->IntAttribute("Price");
  69. {
  70. const char* DiscountGift = element->Attribute("DiscountGift");
  71. std::vector<std::string> vecDiscountGift;
  72. boost::split(vecDiscountGift, DiscountGift, boost::is_any_of(","));
  73. int temp;
  74. for (unsigned int i = 0; i < vecDiscountGift.size(); i++)
  75. {
  76. if (tinyxml2::XMLUtil::ToInt(vecDiscountGift[i].c_str(), &temp))
  77. {
  78. data.mDiscountGift.push_back(temp);
  79. }
  80. }
  81. }
  82. {
  83. const char* SalePrice = element->Attribute("SalePrice");
  84. std::vector<std::string> vecSalePrice;
  85. boost::split(vecSalePrice, SalePrice, boost::is_any_of(","));
  86. int temp;
  87. for (unsigned int i = 0; i < vecSalePrice.size(); i++)
  88. {
  89. if (tinyxml2::XMLUtil::ToInt(vecSalePrice[i].c_str(), &temp))
  90. {
  91. data.mSalePrice.push_back(temp);
  92. }
  93. }
  94. }
  95. data.mFinalPrice = element->IntAttribute("FinalPrice");
  96. {
  97. const char* RewardList = element->Attribute("RewardList");
  98. std::vector<std::string> vecRewardList;
  99. boost::split(vecRewardList, RewardList, boost::is_any_of(","));
  100. int temp;
  101. for (unsigned int i = 0; i < vecRewardList.size(); i++)
  102. {
  103. if (tinyxml2::XMLUtil::ToInt(vecRewardList[i].c_str(), &temp))
  104. {
  105. data.mRewardList.push_back(temp);
  106. }
  107. }
  108. }
  109. {
  110. const char* RewardCount = element->Attribute("RewardCount");
  111. std::vector<std::string> vecRewardCount;
  112. boost::split(vecRewardCount, RewardCount, boost::is_any_of(","));
  113. int temp;
  114. for (unsigned int i = 0; i < vecRewardCount.size(); i++)
  115. {
  116. if (tinyxml2::XMLUtil::ToInt(vecRewardCount[i].c_str(), &temp))
  117. {
  118. data.mRewardCount.push_back(temp);
  119. }
  120. }
  121. }
  122. {
  123. const char* Rate = element->Attribute("Rate");
  124. std::vector<std::string> vecRate;
  125. boost::split(vecRate, Rate, boost::is_any_of(","));
  126. int temp;
  127. for (unsigned int i = 0; i < vecRate.size(); i++)
  128. {
  129. if (tinyxml2::XMLUtil::ToInt(vecRate[i].c_str(), &temp))
  130. {
  131. data.mRate.push_back(temp);
  132. }
  133. }
  134. }
  135. data.mGoldIcon = element->Attribute("GoldIcon");
  136. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  137. assert(mMapData.find(data.mID) == mMapData.end());
  138. mMapData.insert(std::make_pair(data.mID, data));
  139. element = element->NextSiblingElement();
  140. }
  141. }
  142. void Fish_SquamaExchangeCFG::Load()
  143. {
  144. Load("../Config/Fish_SquamaExchangeCFG.xml");
  145. }
  146. Fish_SquamaExchangeCFG* Fish_SquamaExchangeCFG::GetSingleton()
  147. {
  148. if (msSingleton.get() == nullptr)
  149. {
  150. msSingleton.reset(new Fish_SquamaExchangeCFG());
  151. }
  152. return msSingleton.get();
  153. }