Fish_TorpedoRankRewardCFG.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_TorpedoRankRewardCFG.h"
  7. std::auto_ptr<Fish_TorpedoRankRewardCFG> Fish_TorpedoRankRewardCFG::msSingleton(nullptr);
  8. int Fish_TorpedoRankRewardCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Fish_TorpedoRankRewardCFGData* Fish_TorpedoRankRewardCFG::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_TorpedoRankRewardCFGData>& Fish_TorpedoRankRewardCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Fish_TorpedoRankRewardCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_TorpedoRankRewardCFG.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_TorpedoRankRewardCFGData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mRankType = element->IntAttribute("RankType");
  48. data.mTorpedoId = element->IntAttribute("TorpedoId");
  49. data.mStartRank = element->IntAttribute("StartRank");
  50. data.mEndRank = element->IntAttribute("EndRank");
  51. {
  52. const char* RewardList = element->Attribute("RewardList");
  53. std::vector<std::string> vecRewardList;
  54. boost::split(vecRewardList, RewardList, boost::is_any_of(","));
  55. int temp;
  56. for (unsigned int i = 0; i < vecRewardList.size(); i++)
  57. {
  58. if (tinyxml2::XMLUtil::ToInt(vecRewardList[i].c_str(), &temp))
  59. {
  60. data.mRewardList.push_back(temp);
  61. }
  62. }
  63. }
  64. {
  65. const char* RewardCount = element->Attribute("RewardCount");
  66. std::vector<std::string> vecRewardCount;
  67. boost::split(vecRewardCount, RewardCount, boost::is_any_of(","));
  68. int temp;
  69. for (unsigned int i = 0; i < vecRewardCount.size(); i++)
  70. {
  71. if (tinyxml2::XMLUtil::ToInt(vecRewardCount[i].c_str(), &temp))
  72. {
  73. data.mRewardCount.push_back(temp);
  74. }
  75. }
  76. }
  77. data.mType = element->IntAttribute("Type");
  78. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  79. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  80. mMapData.insert(std::make_pair(data.mID, data));
  81. element = element->NextSiblingElement();
  82. }
  83. CCLOG("Fish_TorpedoRankRewardCFG Loaded. Load Data:%u", mMapData.size());
  84. }
  85. void Fish_TorpedoRankRewardCFG::LoadLua()
  86. {
  87. LuaEngine::getInstance()->executeScriptFile("config/Fish_TorpedoRankRewardCFG");
  88. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  89. lua_getglobal(L, "Fish_TorpedoRankRewardCFG");
  90. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  91. lua_pushstring(L, "datas");
  92. lua_gettable(L, -2);
  93. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  94. lua_pushnil(L);
  95. while(lua_next(L, 2))
  96. {
  97. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  98. Fish_TorpedoRankRewardCFGData data;
  99. LuaCfgHelper::readInt(L, "ID", data.mID);
  100. LuaCfgHelper::readInt(L, "RankType", data.mRankType);
  101. LuaCfgHelper::readInt(L, "TorpedoId", data.mTorpedoId);
  102. LuaCfgHelper::readInt(L, "StartRank", data.mStartRank);
  103. LuaCfgHelper::readInt(L, "EndRank", data.mEndRank);
  104. LuaCfgHelper::readVectorInt(L, "RewardList", data.mRewardList);
  105. LuaCfgHelper::readVectorInt(L, "RewardCount", data.mRewardCount);
  106. LuaCfgHelper::readInt(L, "Type", data.mType);
  107. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  108. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  109. mMapData.insert(std::make_pair(data.mID, data));
  110. lua_pop(L, 1);
  111. }
  112. lua_settop(L, 0);
  113. CCLOG("Fish_TorpedoRankRewardCFG Loaded. Load Data:%u", mMapData.size());
  114. }
  115. void Fish_TorpedoRankRewardCFG::Reload()
  116. {
  117. mMapData.clear();
  118. Load();
  119. }
  120. Fish_TorpedoRankRewardCFG* Fish_TorpedoRankRewardCFG::GetSingleton()
  121. {
  122. if (msSingleton.get() == nullptr)
  123. {
  124. msSingleton.reset(new Fish_TorpedoRankRewardCFG());
  125. }
  126. return msSingleton.get();
  127. }
  128. void Fish_TorpedoRankRewardCFG::Release()
  129. {
  130. msSingleton.reset(nullptr);
  131. }