M_equipment.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_equipment.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<M_equipment> M_equipment::msSingleton(nullptr);
  12. int M_equipment::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const M_equipmentData* M_equipment::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_equipmentData>& M_equipment::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void M_equipment::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void M_equipment::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_equipmentData data;
  67. data.mID = element->IntAttribute("ID");
  68. data.mitemType = element->IntAttribute("itemType");
  69. data.mleve = element->IntAttribute("leve");
  70. data.mquality = element->Attribute("quality");
  71. {
  72. const char* cost1 = element->Attribute("cost1");
  73. std::vector<std::string> veccost1;
  74. boost::split(veccost1, cost1, boost::is_any_of(","));
  75. int temp;
  76. for (unsigned int i = 0; i < veccost1.size(); i++)
  77. {
  78. if (tinyxml2::XMLUtil::ToInt(veccost1[i].c_str(), &temp))
  79. {
  80. data.mcost1.push_back(temp);
  81. }
  82. }
  83. }
  84. {
  85. const char* cost2 = element->Attribute("cost2");
  86. std::vector<std::string> veccost2;
  87. boost::split(veccost2, cost2, boost::is_any_of(","));
  88. int temp;
  89. for (unsigned int i = 0; i < veccost2.size(); i++)
  90. {
  91. if (tinyxml2::XMLUtil::ToInt(veccost2[i].c_str(), &temp))
  92. {
  93. data.mcost2.push_back(temp);
  94. }
  95. }
  96. }
  97. {
  98. const char* reward = element->Attribute("reward");
  99. std::vector<std::string> vecreward;
  100. boost::split(vecreward, reward, boost::is_any_of(","));
  101. int temp;
  102. for (unsigned int i = 0; i < vecreward.size(); i++)
  103. {
  104. if (tinyxml2::XMLUtil::ToInt(vecreward[i].c_str(), &temp))
  105. {
  106. data.mreward.push_back(temp);
  107. }
  108. }
  109. }
  110. data.matk = element->IntAttribute("atk");
  111. data.mhp = element->IntAttribute("hp");
  112. data.matkpct = element->IntAttribute("atkpct");
  113. data.mhppct = element->IntAttribute("hppct");
  114. data.mcrtpct = element->IntAttribute("crtpct");
  115. data.mmovepct = element->IntAttribute("movepct");
  116. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  117. assert(mMapData.find(data.mID) == mMapData.end());
  118. mMapData.insert(std::make_pair(data.mID, data));
  119. element = element->NextSiblingElement();
  120. }
  121. }
  122. void M_equipment::Load()
  123. {
  124. Load("../Config/M_equipment.xml");
  125. }
  126. M_equipment* M_equipment::GetSingleton()
  127. {
  128. if (msSingleton.get() == nullptr)
  129. {
  130. msSingleton.reset(new M_equipment());
  131. }
  132. return msSingleton.get();
  133. }