123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #include "stdafx.h"
- #include "tinyxml2/tinyxml2.h"
- #include "LuaCfgHelper.h"
- #include <iostream>
- #include <boost/algorithm/string.hpp>
- #include "M_GuideCFG.h"
- std::auto_ptr<M_GuideCFG> M_GuideCFG::msSingleton(nullptr);
- int M_GuideCFG::GetCount()
- {
- return (int)mMapData.size();
- }
- const M_GuideCFGData* M_GuideCFG::GetData(int ID)
- {
- auto it = mMapData.find(ID);
- if (it != mMapData.end())
- {
- return &it->second;
- }
- return NULL;
- }
- const std::map<int, M_GuideCFGData>& M_GuideCFG::GetMapData()
- {
- return mMapData;
- }
- void M_GuideCFG::Load()
- {
- tinyxml2::XMLDocument xmlDoc;
- std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_GuideCFG.xml");
- auto result = xmlDoc.Parse(content.c_str(), content.length());
- if (result != tinyxml2::XML_SUCCESS)
- {
- CCLOGERROR("Result:%d", result);
- CCASSERT(false, "result != tinyxml2::XML_SUCCESS");
- return;
- }
- auto root = xmlDoc.RootElement();
- if (root == NULL)
- {
- CCASSERT(false, "root == NULL");
- return;
- }
- auto element = root->FirstChildElement("Data");
- while (element != NULL)
- {
- M_GuideCFGData data;
- data.mID = element->IntAttribute("ID");
- data.mType = element->IntAttribute("Type");
- {
- const char* SelPos = element->Attribute("SelPos");
- std::vector<std::string> vecSelPos;
- boost::split(vecSelPos, SelPos, boost::is_any_of(","));
- int temp;
- for (unsigned int i = 0; i < vecSelPos.size(); i++)
- {
- if (tinyxml2::XMLUtil::ToInt(vecSelPos[i].c_str(), &temp))
- {
- data.mSelPos.push_back(temp);
- }
- }
- }
- {
- const char* SelSize = element->Attribute("SelSize");
- std::vector<std::string> vecSelSize;
- boost::split(vecSelSize, SelSize, boost::is_any_of(","));
- int temp;
- for (unsigned int i = 0; i < vecSelSize.size(); i++)
- {
- if (tinyxml2::XMLUtil::ToInt(vecSelSize[i].c_str(), &temp))
- {
- data.mSelSize.push_back(temp);
- }
- }
- }
- data.mSelPic = element->Attribute("SelPic");
- data.mTipsDescPic = element->Attribute("TipsDescPic");
- {
- const char* TipsPos = element->Attribute("TipsPos");
- std::vector<std::string> vecTipsPos;
- boost::split(vecTipsPos, TipsPos, boost::is_any_of(","));
- int temp;
- for (unsigned int i = 0; i < vecTipsPos.size(); i++)
- {
- if (tinyxml2::XMLUtil::ToInt(vecTipsPos[i].c_str(), &temp))
- {
- data.mTipsPos.push_back(temp);
- }
- }
- }
- {
- const char* TipsSize = element->Attribute("TipsSize");
- std::vector<std::string> vecTipsSize;
- boost::split(vecTipsSize, TipsSize, boost::is_any_of(","));
- int temp;
- for (unsigned int i = 0; i < vecTipsSize.size(); i++)
- {
- if (tinyxml2::XMLUtil::ToInt(vecTipsSize[i].c_str(), &temp))
- {
- data.mTipsSize.push_back(temp);
- }
- }
- }
- data.mTipsPic = element->Attribute("TipsPic");
- {
- const char* IconPos = element->Attribute("IconPos");
- std::vector<std::string> vecIconPos;
- boost::split(vecIconPos, IconPos, boost::is_any_of(","));
- int temp;
- for (unsigned int i = 0; i < vecIconPos.size(); i++)
- {
- if (tinyxml2::XMLUtil::ToInt(vecIconPos[i].c_str(), &temp))
- {
- data.mIconPos.push_back(temp);
- }
- }
- }
- {
- const char* FingerPos = element->Attribute("FingerPos");
- std::vector<std::string> vecFingerPos;
- boost::split(vecFingerPos, FingerPos, boost::is_any_of(","));
- int temp;
- for (unsigned int i = 0; i < vecFingerPos.size(); i++)
- {
- if (tinyxml2::XMLUtil::ToInt(vecFingerPos[i].c_str(), &temp))
- {
- data.mFingerPos.push_back(temp);
- }
- }
- }
- data.mName = element->Attribute("Name");
- if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
- CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
- mMapData.insert(std::make_pair(data.mID, data));
- element = element->NextSiblingElement();
- }
- CCLOG("M_GuideCFG Loaded. Load Data:%u", mMapData.size());
- }
- void M_GuideCFG::LoadLua()
- {
- LuaEngine::getInstance()->executeScriptFile("config/M_GuideCFG");
- lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
- lua_getglobal(L, "M_GuideCFG");
- CCASSERT(lua_istable(L, -1) == 1, "is not table");
- lua_pushstring(L, "datas");
- lua_gettable(L, -2);
- CCASSERT(lua_istable(L, -1) == 1, "is not table");
- lua_pushnil(L);
- while(lua_next(L, 2))
- {
- CCASSERT(lua_istable(L, -1) == 1, "is not table");
- M_GuideCFGData data;
- LuaCfgHelper::readInt(L, "ID", data.mID);
- LuaCfgHelper::readInt(L, "Type", data.mType);
- LuaCfgHelper::readVectorInt(L, "SelPos", data.mSelPos);
- LuaCfgHelper::readVectorInt(L, "SelSize", data.mSelSize);
- LuaCfgHelper::readString(L, "SelPic", data.mSelPic);
- LuaCfgHelper::readString(L, "TipsDescPic", data.mTipsDescPic);
- LuaCfgHelper::readVectorInt(L, "TipsPos", data.mTipsPos);
- LuaCfgHelper::readVectorInt(L, "TipsSize", data.mTipsSize);
- LuaCfgHelper::readString(L, "TipsPic", data.mTipsPic);
- LuaCfgHelper::readVectorInt(L, "IconPos", data.mIconPos);
- LuaCfgHelper::readVectorInt(L, "FingerPos", data.mFingerPos);
- LuaCfgHelper::readString(L, "Name", data.mName);
- if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
- CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
- mMapData.insert(std::make_pair(data.mID, data));
- lua_pop(L, 1);
- }
- lua_settop(L, 0);
- CCLOG("M_GuideCFG Loaded. Load Data:%u", mMapData.size());
- }
- void M_GuideCFG::Reload()
- {
- mMapData.clear();
- Load();
- }
- M_GuideCFG* M_GuideCFG::GetSingleton()
- {
- if (msSingleton.get() == nullptr)
- {
- msSingleton.reset(new M_GuideCFG());
- }
- return msSingleton.get();
- }
- void M_GuideCFG::Release()
- {
- msSingleton.reset(nullptr);
- }
|