M_item.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_item.h"
  7. std::auto_ptr<M_item> M_item::msSingleton(nullptr);
  8. int M_item::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_itemData* M_item::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_itemData>& M_item::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_item::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_item.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_itemData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mname = element->Attribute("name");
  48. data.mdesc = element->Attribute("desc");
  49. data.mtype = element->IntAttribute("type");
  50. data.mquality = element->IntAttribute("quality");
  51. data.micon = element->Attribute("icon");
  52. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  53. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  54. mMapData.insert(std::make_pair(data.mID, data));
  55. element = element->NextSiblingElement();
  56. }
  57. CCLOG("M_item Loaded. Load Data:%u", mMapData.size());
  58. }
  59. void M_item::LoadLua()
  60. {
  61. LuaEngine::getInstance()->executeScriptFile("config/M_item");
  62. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  63. lua_getglobal(L, "M_item");
  64. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  65. lua_pushstring(L, "datas");
  66. lua_gettable(L, -2);
  67. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  68. lua_pushnil(L);
  69. while(lua_next(L, 2))
  70. {
  71. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  72. M_itemData data;
  73. LuaCfgHelper::readInt(L, "ID", data.mID);
  74. LuaCfgHelper::readString(L, "name", data.mname);
  75. LuaCfgHelper::readString(L, "desc", data.mdesc);
  76. LuaCfgHelper::readInt(L, "type", data.mtype);
  77. LuaCfgHelper::readInt(L, "quality", data.mquality);
  78. LuaCfgHelper::readString(L, "icon", data.micon);
  79. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  80. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  81. mMapData.insert(std::make_pair(data.mID, data));
  82. lua_pop(L, 1);
  83. }
  84. lua_settop(L, 0);
  85. CCLOG("M_item Loaded. Load Data:%u", mMapData.size());
  86. }
  87. void M_item::Reload()
  88. {
  89. mMapData.clear();
  90. Load();
  91. }
  92. M_item* M_item::GetSingleton()
  93. {
  94. if (msSingleton.get() == nullptr)
  95. {
  96. msSingleton.reset(new M_item());
  97. }
  98. return msSingleton.get();
  99. }
  100. void M_item::Release()
  101. {
  102. msSingleton.reset(nullptr);
  103. }