Fish_LuckyDrawCFG.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_LuckyDrawCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<Fish_LuckyDrawCFG> Fish_LuckyDrawCFG::msSingleton(nullptr);
  12. int Fish_LuckyDrawCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const Fish_LuckyDrawCFGData* Fish_LuckyDrawCFG::GetData(int Index)
  17. {
  18. auto it = mMapData.find(Index);
  19. if (it != mMapData.end())
  20. {
  21. return &it->second;
  22. }
  23. return NULL;
  24. }
  25. boost::unordered_map<int, Fish_LuckyDrawCFGData>& Fish_LuckyDrawCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void Fish_LuckyDrawCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void Fish_LuckyDrawCFG::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_LuckyDrawCFGData data;
  67. data.mIndex = element->IntAttribute("Index");
  68. {
  69. const char* PinValue = element->Attribute("PinValue");
  70. std::vector<std::string> vecPinValue;
  71. boost::split(vecPinValue, PinValue, boost::is_any_of(","));
  72. int temp;
  73. for (unsigned int i = 0; i < vecPinValue.size(); i++)
  74. {
  75. if (tinyxml2::XMLUtil::ToInt(vecPinValue[i].c_str(), &temp))
  76. {
  77. data.mPinValue.push_back(temp);
  78. }
  79. }
  80. }
  81. data.mStarLvl = element->IntAttribute("StarLvl");
  82. data.mItemType = element->IntAttribute("ItemType");
  83. data.mItemCount = element->IntAttribute("ItemCount");
  84. data.mItemValue = element->IntAttribute("ItemValue");
  85. {
  86. const char* NormalProb = element->Attribute("NormalProb");
  87. std::vector<std::string> vecNormalProb;
  88. boost::split(vecNormalProb, NormalProb, boost::is_any_of(","));
  89. int temp;
  90. for (unsigned int i = 0; i < vecNormalProb.size(); i++)
  91. {
  92. if (tinyxml2::XMLUtil::ToInt(vecNormalProb[i].c_str(), &temp))
  93. {
  94. data.mNormalProb.push_back(temp);
  95. }
  96. }
  97. }
  98. {
  99. const char* NoChipProb = element->Attribute("NoChipProb");
  100. std::vector<std::string> vecNoChipProb;
  101. boost::split(vecNoChipProb, NoChipProb, boost::is_any_of(","));
  102. int temp;
  103. for (unsigned int i = 0; i < vecNoChipProb.size(); i++)
  104. {
  105. if (tinyxml2::XMLUtil::ToInt(vecNoChipProb[i].c_str(), &temp))
  106. {
  107. data.mNoChipProb.push_back(temp);
  108. }
  109. }
  110. }
  111. if (mMapData.find(data.mIndex) != mMapData.end())std::cout <<"data refind:" << data.mIndex << std::endl;
  112. assert(mMapData.find(data.mIndex) == mMapData.end());
  113. mMapData.insert(std::make_pair(data.mIndex, data));
  114. element = element->NextSiblingElement();
  115. }
  116. }
  117. void Fish_LuckyDrawCFG::Load()
  118. {
  119. Load("../Config/Fish_LuckyDrawCFG.xml");
  120. }
  121. Fish_LuckyDrawCFG* Fish_LuckyDrawCFG::GetSingleton()
  122. {
  123. if (msSingleton.get() == nullptr)
  124. {
  125. msSingleton.reset(new Fish_LuckyDrawCFG());
  126. }
  127. return msSingleton.get();
  128. }