Odds2_Room.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "Odds2_Room.h"
  10. #include "FileEncrypt.h"
  11. std::auto_ptr<Odds2_Room> Odds2_Room::msSingleton(nullptr);
  12. int Odds2_Room::GetCount()
  13. {
  14. return (int)mMapData.size();
  15. }
  16. const Odds2_RoomData* Odds2_Room::GetData(int RoomID)
  17. {
  18. auto it = mMapData.find(RoomID);
  19. if (it != mMapData.end())
  20. {
  21. return &it->second;
  22. }
  23. return NULL;
  24. }
  25. boost::unordered_map<int, Odds2_RoomData>& Odds2_Room::GetMapData()
  26. {
  27. return mMapData;
  28. }
  29. void Odds2_Room::Reload()
  30. {
  31. mMapData.clear();
  32. Load();
  33. }
  34. void Odds2_Room::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. Odds2_RoomData data;
  67. data.mRoomID = element->IntAttribute("RoomID");
  68. data.mLaserAdd = element->IntAttribute("LaserAdd");
  69. data.mLaserReduce = element->IntAttribute("LaserReduce");
  70. data.mFormationAdd = element->IntAttribute("FormationAdd");
  71. data.mFormationReduce = element->IntAttribute("FormationReduce");
  72. if (mMapData.find(data.mRoomID) != mMapData.end())std::cout <<"data refind:" << data.mRoomID << std::endl;
  73. assert(mMapData.find(data.mRoomID) == mMapData.end());
  74. mMapData.insert(std::make_pair(data.mRoomID, data));
  75. element = element->NextSiblingElement();
  76. }
  77. }
  78. void Odds2_Room::Load()
  79. {
  80. Load("../Config/Odds2_Room.xml");
  81. }
  82. Odds2_Room* Odds2_Room::GetSingleton()
  83. {
  84. if (msSingleton.get() == nullptr)
  85. {
  86. msSingleton.reset(new Odds2_Room());
  87. }
  88. return msSingleton.get();
  89. }