Fish_FishChuBaoCFG.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_FishChuBaoCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<Fish_FishChuBaoCFG> Fish_FishChuBaoCFG::msSingleton(nullptr);
  12. int Fish_FishChuBaoCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const Fish_FishChuBaoCFGData* Fish_FishChuBaoCFG::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_FishChuBaoCFGData>& Fish_FishChuBaoCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void Fish_FishChuBaoCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void Fish_FishChuBaoCFG::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_FishChuBaoCFGData data;
  67. data.mID = element->IntAttribute("ID");
  68. data.mFishId = element->IntAttribute("FishId");
  69. data.mTurretMin = element->IntAttribute("TurretMin");
  70. data.mTurretMax = element->IntAttribute("TurretMax");
  71. data.mScore = element->IntAttribute("Score");
  72. data.mWeight = element->IntAttribute("Weight");
  73. data.mGold = element->IntAttribute("Gold");
  74. {
  75. const char* GivenReward = element->Attribute("GivenReward");
  76. std::vector<std::string> vecGivenReward;
  77. boost::split(vecGivenReward, GivenReward, boost::is_any_of(","));
  78. int temp;
  79. for (unsigned int i = 0; i < vecGivenReward.size(); i++)
  80. {
  81. if (tinyxml2::XMLUtil::ToInt(vecGivenReward[i].c_str(), &temp))
  82. {
  83. data.mGivenReward.push_back(temp);
  84. }
  85. }
  86. }
  87. data.mGearName = element->Attribute("GearName");
  88. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  89. assert(mMapData.find(data.mID) == mMapData.end());
  90. mMapData.insert(std::make_pair(data.mID, data));
  91. element = element->NextSiblingElement();
  92. }
  93. }
  94. void Fish_FishChuBaoCFG::Load()
  95. {
  96. Load("../Config/Fish_FishChuBaoCFG.xml");
  97. }
  98. Fish_FishChuBaoCFG* Fish_FishChuBaoCFG::GetSingleton()
  99. {
  100. if (msSingleton.get() == nullptr)
  101. {
  102. msSingleton.reset(new Fish_FishChuBaoCFG());
  103. }
  104. return msSingleton.get();
  105. }