Fish_DiceFishCFG.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_DiceFishCFG.h"
  7. std::auto_ptr<Fish_DiceFishCFG> Fish_DiceFishCFG::msSingleton(nullptr);
  8. int Fish_DiceFishCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Fish_DiceFishCFGData* Fish_DiceFishCFG::GetData(int Rotation)
  13. {
  14. auto it = mMapData.find(Rotation);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, Fish_DiceFishCFGData>& Fish_DiceFishCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Fish_DiceFishCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_DiceFishCFG.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_DiceFishCFGData data;
  46. data.mRotation = element->IntAttribute("Rotation");
  47. data.mMagnification = element->IntAttribute("Magnification");
  48. data.mProbability = element->IntAttribute("Probability");
  49. if (mMapData.find(data.mRotation) != mMapData.end())std::cout <<"data refind:" << data.mRotation << std::endl;
  50. CCASSERT(mMapData.find(data.mRotation) == mMapData.end(), "data.mRotation is exists");
  51. mMapData.insert(std::make_pair(data.mRotation, data));
  52. element = element->NextSiblingElement();
  53. }
  54. CCLOG("Fish_DiceFishCFG Loaded. Load Data:%u", mMapData.size());
  55. }
  56. void Fish_DiceFishCFG::LoadLua()
  57. {
  58. LuaEngine::getInstance()->executeScriptFile("config/Fish_DiceFishCFG");
  59. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  60. lua_getglobal(L, "Fish_DiceFishCFG");
  61. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  62. lua_pushstring(L, "datas");
  63. lua_gettable(L, -2);
  64. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  65. lua_pushnil(L);
  66. while(lua_next(L, 2))
  67. {
  68. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  69. Fish_DiceFishCFGData data;
  70. LuaCfgHelper::readInt(L, "Rotation", data.mRotation);
  71. LuaCfgHelper::readInt(L, "Magnification", data.mMagnification);
  72. LuaCfgHelper::readInt(L, "Probability", data.mProbability);
  73. if (mMapData.find(data.mRotation) != mMapData.end())std::cout <<"data refind:" << data.mRotation << std::endl;
  74. CCASSERT(mMapData.find(data.mRotation) == mMapData.end(), "data.mRotation is exists");
  75. mMapData.insert(std::make_pair(data.mRotation, data));
  76. lua_pop(L, 1);
  77. }
  78. lua_settop(L, 0);
  79. CCLOG("Fish_DiceFishCFG Loaded. Load Data:%u", mMapData.size());
  80. }
  81. void Fish_DiceFishCFG::Reload()
  82. {
  83. mMapData.clear();
  84. Load();
  85. }
  86. Fish_DiceFishCFG* Fish_DiceFishCFG::GetSingleton()
  87. {
  88. if (msSingleton.get() == nullptr)
  89. {
  90. msSingleton.reset(new Fish_DiceFishCFG());
  91. }
  92. return msSingleton.get();
  93. }
  94. void Fish_DiceFishCFG::Release()
  95. {
  96. msSingleton.reset(nullptr);
  97. }