M_game_battle_field.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_game_battle_field.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<M_game_battle_field> M_game_battle_field::msSingleton(nullptr);
  12. int M_game_battle_field::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const M_game_battle_fieldData* M_game_battle_field::GetData(int64_t 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<int64_t, M_game_battle_fieldData>& M_game_battle_field::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void M_game_battle_field::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void M_game_battle_field::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_game_battle_fieldData data;
  67. {
  68. int64_t temp = 0;
  69. const char* str = element->Attribute("ID");
  70. tinyxml2::XMLUtil::ToInt64(str, &temp);
  71. data.mID = temp;
  72. }
  73. {
  74. int64_t temp = 0;
  75. const char* str = element->Attribute("monster_id");
  76. tinyxml2::XMLUtil::ToInt64(str, &temp);
  77. data.mmonster_id = temp;
  78. }
  79. data.mscene_type = element->IntAttribute("scene_type");
  80. data.mweight = element->IntAttribute("weight");
  81. data.mmax_num = element->IntAttribute("max_num");
  82. {
  83. const char* kill_count = element->Attribute("kill_count");
  84. std::vector<std::string> veckill_count;
  85. boost::split(veckill_count, kill_count, boost::is_any_of(","));
  86. int temp;
  87. for (unsigned int i = 0; i < veckill_count.size(); i++)
  88. {
  89. if (tinyxml2::XMLUtil::ToInt(veckill_count[i].c_str(), &temp))
  90. {
  91. data.mkill_count.push_back(temp);
  92. }
  93. }
  94. }
  95. data.maward_drop_prob = element->FloatAttribute("award_drop_prob");
  96. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  97. assert(mMapData.find(data.mID) == mMapData.end());
  98. mMapData.insert(std::make_pair(data.mID, data));
  99. element = element->NextSiblingElement();
  100. }
  101. }
  102. void M_game_battle_field::Load()
  103. {
  104. Load("../Config/M_game_battle_field.xml");
  105. }
  106. M_game_battle_field* M_game_battle_field::GetSingleton()
  107. {
  108. if (msSingleton.get() == nullptr)
  109. {
  110. msSingleton.reset(new M_game_battle_field());
  111. }
  112. return msSingleton.get();
  113. }