Fish_FishRankCFG.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_FishRankCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<Fish_FishRankCFG> Fish_FishRankCFG::msSingleton(nullptr);
  12. int Fish_FishRankCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const Fish_FishRankCFGData* Fish_FishRankCFG::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_FishRankCFGData>& Fish_FishRankCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void Fish_FishRankCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void Fish_FishRankCFG::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_FishRankCFGData data;
  67. data.mID = element->IntAttribute("ID");
  68. data.mRankName = element->Attribute("RankName");
  69. data.mIntegral = element->Attribute("Integral");
  70. data.mStars1 = element->Attribute("Stars1");
  71. data.mStars2 = element->Attribute("Stars2");
  72. data.mStars3 = element->Attribute("Stars3");
  73. {
  74. const char* RankRewardId = element->Attribute("RankRewardId");
  75. std::vector<std::string> vecRankRewardId;
  76. boost::split(vecRankRewardId, RankRewardId, boost::is_any_of(","));
  77. int temp;
  78. for (unsigned int i = 0; i < vecRankRewardId.size(); i++)
  79. {
  80. if (tinyxml2::XMLUtil::ToInt(vecRankRewardId[i].c_str(), &temp))
  81. {
  82. data.mRankRewardId.push_back(temp);
  83. }
  84. }
  85. }
  86. {
  87. const char* RankRewardCount = element->Attribute("RankRewardCount");
  88. std::vector<std::string> vecRankRewardCount;
  89. boost::split(vecRankRewardCount, RankRewardCount, boost::is_any_of(","));
  90. int temp;
  91. for (unsigned int i = 0; i < vecRankRewardCount.size(); i++)
  92. {
  93. if (tinyxml2::XMLUtil::ToInt(vecRankRewardCount[i].c_str(), &temp))
  94. {
  95. data.mRankRewardCount.push_back(temp);
  96. }
  97. }
  98. }
  99. {
  100. const char* SeaGodRewardId = element->Attribute("SeaGodRewardId");
  101. std::vector<std::string> vecSeaGodRewardId;
  102. boost::split(vecSeaGodRewardId, SeaGodRewardId, boost::is_any_of(","));
  103. int temp;
  104. for (unsigned int i = 0; i < vecSeaGodRewardId.size(); i++)
  105. {
  106. if (tinyxml2::XMLUtil::ToInt(vecSeaGodRewardId[i].c_str(), &temp))
  107. {
  108. data.mSeaGodRewardId.push_back(temp);
  109. }
  110. }
  111. }
  112. {
  113. const char* SeaGodRewardCount = element->Attribute("SeaGodRewardCount");
  114. std::vector<std::string> vecSeaGodRewardCount;
  115. boost::split(vecSeaGodRewardCount, SeaGodRewardCount, boost::is_any_of(","));
  116. int temp;
  117. for (unsigned int i = 0; i < vecSeaGodRewardCount.size(); i++)
  118. {
  119. if (tinyxml2::XMLUtil::ToInt(vecSeaGodRewardCount[i].c_str(), &temp))
  120. {
  121. data.mSeaGodRewardCount.push_back(temp);
  122. }
  123. }
  124. }
  125. data.mGiftId = element->IntAttribute("GiftId");
  126. data.mResources = element->Attribute("Resources");
  127. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  128. assert(mMapData.find(data.mID) == mMapData.end());
  129. mMapData.insert(std::make_pair(data.mID, data));
  130. element = element->NextSiblingElement();
  131. }
  132. }
  133. void Fish_FishRankCFG::Load()
  134. {
  135. Load("../Config/Fish_FishRankCFG.xml");
  136. }
  137. Fish_FishRankCFG* Fish_FishRankCFG::GetSingleton()
  138. {
  139. if (msSingleton.get() == nullptr)
  140. {
  141. msSingleton.reset(new Fish_FishRankCFG());
  142. }
  143. return msSingleton.get();
  144. }