M_CDKEY_GiftCFG.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_CDKEY_GiftCFG.h"
  7. std::auto_ptr<M_CDKEY_GiftCFG> M_CDKEY_GiftCFG::msSingleton(nullptr);
  8. int M_CDKEY_GiftCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_CDKEY_GiftCFGData* M_CDKEY_GiftCFG::GetData(int GiftId)
  13. {
  14. auto it = mMapData.find(GiftId);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_CDKEY_GiftCFGData>& M_CDKEY_GiftCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_CDKEY_GiftCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_CDKEY_GiftCFG.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_CDKEY_GiftCFGData data;
  46. data.mGiftId = element->IntAttribute("GiftId");
  47. data.mGiftName = element->Attribute("GiftName");
  48. data.mGold = element->IntAttribute("Gold");
  49. data.mGem = element->IntAttribute("Gem");
  50. data.mDragbonBall = element->IntAttribute("DragbonBall");
  51. {
  52. const char* Item = element->Attribute("Item");
  53. std::vector<std::string> vecItem;
  54. boost::split(vecItem, Item, boost::is_any_of(","));
  55. int temp;
  56. for (unsigned int i = 0; i < vecItem.size(); i++)
  57. {
  58. if (tinyxml2::XMLUtil::ToInt(vecItem[i].c_str(), &temp))
  59. {
  60. data.mItem.push_back(temp);
  61. }
  62. }
  63. }
  64. if (mMapData.find(data.mGiftId) != mMapData.end())std::cout <<"data refind:" << data.mGiftId << std::endl;
  65. CCASSERT(mMapData.find(data.mGiftId) == mMapData.end(), "data.mGiftId is exists");
  66. mMapData.insert(std::make_pair(data.mGiftId, data));
  67. element = element->NextSiblingElement();
  68. }
  69. CCLOG("M_CDKEY_GiftCFG Loaded. Load Data:%u", mMapData.size());
  70. }
  71. void M_CDKEY_GiftCFG::LoadLua()
  72. {
  73. LuaEngine::getInstance()->executeScriptFile("config/M_CDKEY_GiftCFG");
  74. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  75. lua_getglobal(L, "M_CDKEY_GiftCFG");
  76. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  77. lua_pushstring(L, "datas");
  78. lua_gettable(L, -2);
  79. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  80. lua_pushnil(L);
  81. while(lua_next(L, 2))
  82. {
  83. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  84. M_CDKEY_GiftCFGData data;
  85. LuaCfgHelper::readInt(L, "GiftId", data.mGiftId);
  86. LuaCfgHelper::readString(L, "GiftName", data.mGiftName);
  87. LuaCfgHelper::readInt(L, "Gold", data.mGold);
  88. LuaCfgHelper::readInt(L, "Gem", data.mGem);
  89. LuaCfgHelper::readInt(L, "DragbonBall", data.mDragbonBall);
  90. LuaCfgHelper::readVectorInt(L, "Item", data.mItem);
  91. if (mMapData.find(data.mGiftId) != mMapData.end())std::cout <<"data refind:" << data.mGiftId << std::endl;
  92. CCASSERT(mMapData.find(data.mGiftId) == mMapData.end(), "data.mGiftId is exists");
  93. mMapData.insert(std::make_pair(data.mGiftId, data));
  94. lua_pop(L, 1);
  95. }
  96. lua_settop(L, 0);
  97. CCLOG("M_CDKEY_GiftCFG Loaded. Load Data:%u", mMapData.size());
  98. }
  99. void M_CDKEY_GiftCFG::Reload()
  100. {
  101. mMapData.clear();
  102. Load();
  103. }
  104. M_CDKEY_GiftCFG* M_CDKEY_GiftCFG::GetSingleton()
  105. {
  106. if (msSingleton.get() == nullptr)
  107. {
  108. msSingleton.reset(new M_CDKEY_GiftCFG());
  109. }
  110. return msSingleton.get();
  111. }
  112. void M_CDKEY_GiftCFG::Release()
  113. {
  114. msSingleton.reset(nullptr);
  115. }