Fish_FishChuBaoCFG.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "Fish_FishChuBaoCFG.h"
  7. std::auto_ptr<Fish_FishChuBaoCFG> Fish_FishChuBaoCFG::msSingleton(nullptr);
  8. int Fish_FishChuBaoCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Fish_FishChuBaoCFGData* Fish_FishChuBaoCFG::GetData(int ID)
  13. {
  14. auto it = mMapData.find(ID);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, Fish_FishChuBaoCFGData>& Fish_FishChuBaoCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Fish_FishChuBaoCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_FishChuBaoCFG.xml");
  29. auto result = xmlDoc.Parse(content.c_str(), content.length());
  30. if (result != tinyxml2::XML_SUCCESS)
  31. {
  32. CCLOGERROR("Result:%d", result);
  33. CCASSERT(false, "result != tinyxml2::XML_SUCCESS");
  34. return;
  35. }
  36. auto root = xmlDoc.RootElement();
  37. if (root == NULL)
  38. {
  39. CCASSERT(false, "root == NULL");
  40. return;
  41. }
  42. auto element = root->FirstChildElement("Data");
  43. while (element != NULL)
  44. {
  45. Fish_FishChuBaoCFGData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mFishId = element->IntAttribute("FishId");
  48. data.mTurretMin = element->IntAttribute("TurretMin");
  49. data.mTurretMax = element->IntAttribute("TurretMax");
  50. data.mScore = element->IntAttribute("Score");
  51. data.mWeight = element->IntAttribute("Weight");
  52. data.mGold = element->IntAttribute("Gold");
  53. {
  54. const char* GivenReward = element->Attribute("GivenReward");
  55. std::vector<std::string> vecGivenReward;
  56. boost::split(vecGivenReward, GivenReward, boost::is_any_of(","));
  57. int temp;
  58. for (unsigned int i = 0; i < vecGivenReward.size(); i++)
  59. {
  60. if (tinyxml2::XMLUtil::ToInt(vecGivenReward[i].c_str(), &temp))
  61. {
  62. data.mGivenReward.push_back(temp);
  63. }
  64. }
  65. }
  66. data.mGearName = element->Attribute("GearName");
  67. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  68. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  69. mMapData.insert(std::make_pair(data.mID, data));
  70. element = element->NextSiblingElement();
  71. }
  72. CCLOG("Fish_FishChuBaoCFG Loaded. Load Data:%u", mMapData.size());
  73. }
  74. void Fish_FishChuBaoCFG::LoadLua()
  75. {
  76. LuaEngine::getInstance()->executeScriptFile("config/Fish_FishChuBaoCFG");
  77. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  78. lua_getglobal(L, "Fish_FishChuBaoCFG");
  79. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  80. lua_pushstring(L, "datas");
  81. lua_gettable(L, -2);
  82. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  83. lua_pushnil(L);
  84. while(lua_next(L, 2))
  85. {
  86. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  87. Fish_FishChuBaoCFGData data;
  88. LuaCfgHelper::readInt(L, "ID", data.mID);
  89. LuaCfgHelper::readInt(L, "FishId", data.mFishId);
  90. LuaCfgHelper::readInt(L, "TurretMin", data.mTurretMin);
  91. LuaCfgHelper::readInt(L, "TurretMax", data.mTurretMax);
  92. LuaCfgHelper::readInt(L, "Score", data.mScore);
  93. LuaCfgHelper::readInt(L, "Weight", data.mWeight);
  94. LuaCfgHelper::readInt(L, "Gold", data.mGold);
  95. LuaCfgHelper::readVectorInt(L, "GivenReward", data.mGivenReward);
  96. LuaCfgHelper::readString(L, "GearName", data.mGearName);
  97. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  98. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  99. mMapData.insert(std::make_pair(data.mID, data));
  100. lua_pop(L, 1);
  101. }
  102. lua_settop(L, 0);
  103. CCLOG("Fish_FishChuBaoCFG Loaded. Load Data:%u", mMapData.size());
  104. }
  105. void Fish_FishChuBaoCFG::Reload()
  106. {
  107. mMapData.clear();
  108. Load();
  109. }
  110. Fish_FishChuBaoCFG* Fish_FishChuBaoCFG::GetSingleton()
  111. {
  112. if (msSingleton.get() == nullptr)
  113. {
  114. msSingleton.reset(new Fish_FishChuBaoCFG());
  115. }
  116. return msSingleton.get();
  117. }
  118. void Fish_FishChuBaoCFG::Release()
  119. {
  120. msSingleton.reset(nullptr);
  121. }