Fish_LargeFishCFG5.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_LargeFishCFG5.h"
  7. std::auto_ptr<Fish_LargeFishCFG5> Fish_LargeFishCFG5::msSingleton(nullptr);
  8. int Fish_LargeFishCFG5::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Fish_LargeFishCFG5Data* Fish_LargeFishCFG5::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_LargeFishCFG5Data>& Fish_LargeFishCFG5::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Fish_LargeFishCFG5::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_LargeFishCFG5.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_LargeFishCFG5Data data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mName = element->Attribute("Name");
  48. data.mGeneratorID = element->IntAttribute("GeneratorID");
  49. data.mFishID = element->IntAttribute("FishID");
  50. data.mImageInfo = element->Attribute("ImageInfo");
  51. data.mIsBoss = element->BoolAttribute("IsBoss");
  52. data.mBackSound = element->Attribute("BackSound");
  53. data.mDuration = element->IntAttribute("Duration");
  54. data.mWarnTime = element->IntAttribute("WarnTime");
  55. data.mFlashTime = element->IntAttribute("FlashTime");
  56. data.mDieTime = element->IntAttribute("DieTime");
  57. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  58. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  59. mMapData.insert(std::make_pair(data.mID, data));
  60. element = element->NextSiblingElement();
  61. }
  62. CCLOG("Fish_LargeFishCFG5 Loaded. Load Data:%u", mMapData.size());
  63. }
  64. void Fish_LargeFishCFG5::LoadLua()
  65. {
  66. LuaEngine::getInstance()->executeScriptFile("config/Fish_LargeFishCFG5");
  67. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  68. lua_getglobal(L, "Fish_LargeFishCFG5");
  69. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  70. lua_pushstring(L, "datas");
  71. lua_gettable(L, -2);
  72. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  73. lua_pushnil(L);
  74. while(lua_next(L, 2))
  75. {
  76. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  77. Fish_LargeFishCFG5Data data;
  78. LuaCfgHelper::readInt(L, "ID", data.mID);
  79. LuaCfgHelper::readString(L, "Name", data.mName);
  80. LuaCfgHelper::readInt(L, "GeneratorID", data.mGeneratorID);
  81. LuaCfgHelper::readInt(L, "FishID", data.mFishID);
  82. LuaCfgHelper::readString(L, "ImageInfo", data.mImageInfo);
  83. LuaCfgHelper::readBool(L, "IsBoss", data.mIsBoss);
  84. LuaCfgHelper::readString(L, "BackSound", data.mBackSound);
  85. LuaCfgHelper::readInt(L, "Duration", data.mDuration);
  86. LuaCfgHelper::readInt(L, "WarnTime", data.mWarnTime);
  87. LuaCfgHelper::readInt(L, "FlashTime", data.mFlashTime);
  88. LuaCfgHelper::readInt(L, "DieTime", data.mDieTime);
  89. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  90. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  91. mMapData.insert(std::make_pair(data.mID, data));
  92. lua_pop(L, 1);
  93. }
  94. lua_settop(L, 0);
  95. CCLOG("Fish_LargeFishCFG5 Loaded. Load Data:%u", mMapData.size());
  96. }
  97. void Fish_LargeFishCFG5::Reload()
  98. {
  99. mMapData.clear();
  100. Load();
  101. }
  102. Fish_LargeFishCFG5* Fish_LargeFishCFG5::GetSingleton()
  103. {
  104. if (msSingleton.get() == nullptr)
  105. {
  106. msSingleton.reset(new Fish_LargeFishCFG5());
  107. }
  108. return msSingleton.get();
  109. }
  110. void Fish_LargeFishCFG5::Release()
  111. {
  112. msSingleton.reset(nullptr);
  113. }