M_ActivitySailingSignCFG.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_ActivitySailingSignCFG.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<M_ActivitySailingSignCFG> M_ActivitySailingSignCFG::msSingleton(nullptr);
  12. int M_ActivitySailingSignCFG::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const M_ActivitySailingSignCFGData* M_ActivitySailingSignCFG::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_ActivitySailingSignCFGData>& M_ActivitySailingSignCFG::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void M_ActivitySailingSignCFG::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void M_ActivitySailingSignCFG::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_ActivitySailingSignCFGData data;
  67. data.mId = element->IntAttribute("Id");
  68. data.mType = element->IntAttribute("Type");
  69. data.mDesc = element->Attribute("Desc");
  70. data.mPlayerLvCond = element->IntAttribute("PlayerLvCond");
  71. data.mTurretLvCond = element->IntAttribute("TurretLvCond");
  72. {
  73. const char* AwardItemID = element->Attribute("AwardItemID");
  74. std::vector<std::string> vecAwardItemID;
  75. boost::split(vecAwardItemID, AwardItemID, boost::is_any_of(","));
  76. int temp;
  77. for (unsigned int i = 0; i < vecAwardItemID.size(); i++)
  78. {
  79. if (tinyxml2::XMLUtil::ToInt(vecAwardItemID[i].c_str(), &temp))
  80. {
  81. data.mAwardItemID.push_back(temp);
  82. }
  83. }
  84. }
  85. {
  86. const char* AwardItemCounts = element->Attribute("AwardItemCounts");
  87. std::vector<std::string> vecAwardItemCounts;
  88. boost::split(vecAwardItemCounts, AwardItemCounts, boost::is_any_of(","));
  89. int temp;
  90. for (unsigned int i = 0; i < vecAwardItemCounts.size(); i++)
  91. {
  92. if (tinyxml2::XMLUtil::ToInt(vecAwardItemCounts[i].c_str(), &temp))
  93. {
  94. data.mAwardItemCounts.push_back(temp);
  95. }
  96. }
  97. }
  98. data.mVipLevel = element->IntAttribute("VipLevel");
  99. data.mUnlockDay = element->IntAttribute("UnlockDay");
  100. if (mMapData.find(data.mId) != mMapData.end())std::cout <<"data refind:" << data.mId << std::endl;
  101. assert(mMapData.find(data.mId) == mMapData.end());
  102. mMapData.insert(std::make_pair(data.mId, data));
  103. element = element->NextSiblingElement();
  104. }
  105. }
  106. void M_ActivitySailingSignCFG::Load()
  107. {
  108. Load("../Config/M_ActivitySailingSignCFG.xml");
  109. }
  110. M_ActivitySailingSignCFG* M_ActivitySailingSignCFG::GetSingleton()
  111. {
  112. if (msSingleton.get() == nullptr)
  113. {
  114. msSingleton.reset(new M_ActivitySailingSignCFG());
  115. }
  116. return msSingleton.get();
  117. }