M_CommodityCFG.cpp 4.0 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 "M_CommodityCFG.h"
  7. std::auto_ptr<M_CommodityCFG> M_CommodityCFG::msSingleton(nullptr);
  8. int M_CommodityCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_CommodityCFGData* M_CommodityCFG::GetData(int CommodityId)
  13. {
  14. auto it = mMapData.find(CommodityId);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_CommodityCFGData>& M_CommodityCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_CommodityCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_CommodityCFG.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_CommodityCFGData data;
  46. data.mCommodityId = element->IntAttribute("CommodityId");
  47. data.mName = element->Attribute("Name");
  48. data.mCommodityType = element->IntAttribute("CommodityType");
  49. data.mPriceType = element->IntAttribute("PriceType");
  50. data.mPrice = element->IntAttribute("Price");
  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. data.mIsCanBuy = element->BoolAttribute("IsCanBuy");
  65. {
  66. const char* Count = element->Attribute("Count");
  67. std::vector<std::string> vecCount;
  68. boost::split(vecCount, Count, boost::is_any_of(","));
  69. int temp;
  70. for (unsigned int i = 0; i < vecCount.size(); i++)
  71. {
  72. if (tinyxml2::XMLUtil::ToInt(vecCount[i].c_str(), &temp))
  73. {
  74. data.mCount.push_back(temp);
  75. }
  76. }
  77. }
  78. if (mMapData.find(data.mCommodityId) != mMapData.end())std::cout <<"data refind:" << data.mCommodityId << std::endl;
  79. CCASSERT(mMapData.find(data.mCommodityId) == mMapData.end(), "data.mCommodityId is exists");
  80. mMapData.insert(std::make_pair(data.mCommodityId, data));
  81. element = element->NextSiblingElement();
  82. }
  83. CCLOG("M_CommodityCFG Loaded. Load Data:%u", mMapData.size());
  84. }
  85. void M_CommodityCFG::LoadLua()
  86. {
  87. LuaEngine::getInstance()->executeScriptFile("config/M_CommodityCFG");
  88. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  89. lua_getglobal(L, "M_CommodityCFG");
  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. M_CommodityCFGData data;
  99. LuaCfgHelper::readInt(L, "CommodityId", data.mCommodityId);
  100. LuaCfgHelper::readString(L, "Name", data.mName);
  101. LuaCfgHelper::readInt(L, "CommodityType", data.mCommodityType);
  102. LuaCfgHelper::readInt(L, "PriceType", data.mPriceType);
  103. LuaCfgHelper::readInt(L, "Price", data.mPrice);
  104. LuaCfgHelper::readVectorInt(L, "Item", data.mItem);
  105. LuaCfgHelper::readBool(L, "IsCanBuy", data.mIsCanBuy);
  106. LuaCfgHelper::readVectorInt(L, "Count", data.mCount);
  107. if (mMapData.find(data.mCommodityId) != mMapData.end())std::cout <<"data refind:" << data.mCommodityId << std::endl;
  108. CCASSERT(mMapData.find(data.mCommodityId) == mMapData.end(), "data.mCommodityId is exists");
  109. mMapData.insert(std::make_pair(data.mCommodityId, data));
  110. lua_pop(L, 1);
  111. }
  112. lua_settop(L, 0);
  113. CCLOG("M_CommodityCFG Loaded. Load Data:%u", mMapData.size());
  114. }
  115. void M_CommodityCFG::Reload()
  116. {
  117. mMapData.clear();
  118. Load();
  119. }
  120. M_CommodityCFG* M_CommodityCFG::GetSingleton()
  121. {
  122. if (msSingleton.get() == nullptr)
  123. {
  124. msSingleton.reset(new M_CommodityCFG());
  125. }
  126. return msSingleton.get();
  127. }
  128. void M_CommodityCFG::Release()
  129. {
  130. msSingleton.reset(nullptr);
  131. }