M_ActivityFishingCeleExchangeCFG.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "M_ActivityFishingCeleExchangeCFG.h"
  7. std::auto_ptr<M_ActivityFishingCeleExchangeCFG> M_ActivityFishingCeleExchangeCFG::msSingleton(nullptr);
  8. int M_ActivityFishingCeleExchangeCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_ActivityFishingCeleExchangeCFGData* M_ActivityFishingCeleExchangeCFG::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, M_ActivityFishingCeleExchangeCFGData>& M_ActivityFishingCeleExchangeCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_ActivityFishingCeleExchangeCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityFishingCeleExchangeCFG.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. M_ActivityFishingCeleExchangeCFGData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mType = element->IntAttribute("Type");
  48. data.mNeedItem = element->IntAttribute("NeedItem");
  49. data.mNeedCount = element->IntAttribute("NeedCount");
  50. data.mDiscountCount = element->IntAttribute("DiscountCount");
  51. data.mItemId = element->IntAttribute("ItemId");
  52. data.mItemCount = element->IntAttribute("ItemCount");
  53. data.mLimit = element->IntAttribute("Limit");
  54. data.mProbItem = element->IntAttribute("ProbItem");
  55. data.mProbCount = element->IntAttribute("ProbCount");
  56. data.mProb = element->IntAttribute("Prob");
  57. data.mGiftDiscount = element->IntAttribute("GiftDiscount");
  58. data.mLvLimit = element->IntAttribute("LvLimit");
  59. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  60. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  61. mMapData.insert(std::make_pair(data.mID, data));
  62. element = element->NextSiblingElement();
  63. }
  64. CCLOG("M_ActivityFishingCeleExchangeCFG Loaded. Load Data:%u", mMapData.size());
  65. }
  66. void M_ActivityFishingCeleExchangeCFG::LoadLua()
  67. {
  68. LuaEngine::getInstance()->executeScriptFile("config/M_ActivityFishingCeleExchangeCFG");
  69. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  70. lua_getglobal(L, "M_ActivityFishingCeleExchangeCFG");
  71. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  72. lua_pushstring(L, "datas");
  73. lua_gettable(L, -2);
  74. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  75. lua_pushnil(L);
  76. while(lua_next(L, 2))
  77. {
  78. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  79. M_ActivityFishingCeleExchangeCFGData data;
  80. LuaCfgHelper::readInt(L, "ID", data.mID);
  81. LuaCfgHelper::readInt(L, "Type", data.mType);
  82. LuaCfgHelper::readInt(L, "NeedItem", data.mNeedItem);
  83. LuaCfgHelper::readInt(L, "NeedCount", data.mNeedCount);
  84. LuaCfgHelper::readInt(L, "DiscountCount", data.mDiscountCount);
  85. LuaCfgHelper::readInt(L, "ItemId", data.mItemId);
  86. LuaCfgHelper::readInt(L, "ItemCount", data.mItemCount);
  87. LuaCfgHelper::readInt(L, "Limit", data.mLimit);
  88. LuaCfgHelper::readInt(L, "ProbItem", data.mProbItem);
  89. LuaCfgHelper::readInt(L, "ProbCount", data.mProbCount);
  90. LuaCfgHelper::readInt(L, "Prob", data.mProb);
  91. LuaCfgHelper::readInt(L, "GiftDiscount", data.mGiftDiscount);
  92. LuaCfgHelper::readInt(L, "LvLimit", data.mLvLimit);
  93. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  94. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  95. mMapData.insert(std::make_pair(data.mID, data));
  96. lua_pop(L, 1);
  97. }
  98. lua_settop(L, 0);
  99. CCLOG("M_ActivityFishingCeleExchangeCFG Loaded. Load Data:%u", mMapData.size());
  100. }
  101. void M_ActivityFishingCeleExchangeCFG::Reload()
  102. {
  103. mMapData.clear();
  104. Load();
  105. }
  106. M_ActivityFishingCeleExchangeCFG* M_ActivityFishingCeleExchangeCFG::GetSingleton()
  107. {
  108. if (msSingleton.get() == nullptr)
  109. {
  110. msSingleton.reset(new M_ActivityFishingCeleExchangeCFG());
  111. }
  112. return msSingleton.get();
  113. }
  114. void M_ActivityFishingCeleExchangeCFG::Release()
  115. {
  116. msSingleton.reset(nullptr);
  117. }