// // FillGlobalConfig.cpp // auto_fill_jewel_v3 // // Created by Red on 2024/11/25. // #include "FillGlobalConfig.hpp" using namespace ArduinoJson; #include using std::ifstream; #include FillGlobalConfig* FillGlobalConfig::_s_instance = nullptr ; bool FillGlobalConfig::init(string filename) { _plateContours.clear(); _jewelContours.clear() ; ifstream ifs( filename.c_str() ); if( ifs.good()==false ) return false ; DynamicJsonBuffer buffer ; JsonObject& root = buffer.parse(ifs) ; string platedir = root["plate_contour_dir"].as() ; string jeweldir = root["jewel_contour_dir"].as() ; JsonArray& parr =root["plate_names"].as() ; JsonArray& jarr =root["jewel_names"].as() ; _plateContours.resize(parr.size()); _jewelContours.resize(jarr.size()); for(int i=0;i() ; string fullname1 = platedir + name1 ; _plateContours[i]._name = name1 ; _plateContours[i]._contour.readFromJsonFile(fullname1) ; } for(int i=0;i() ; string fullname1 = jeweldir + name1 ; _jewelContours[i]._name = name1 ; _jewelContours[i]._contour.readFromJsonFile(fullname1) ; } return true; } FillGlobalConfig* FillGlobalConfig::getInstance() { if( _s_instance == nullptr ) { _s_instance = new FillGlobalConfig ; _s_instance->init("config.json") ; _s_instance->initJewelItems("sg_jewel_items.csv") ; _s_instance->initLevelDatas("sg_level_data.csv") ; _s_instance->initPlateItems("sg_plate_items.csv") ; } return _s_instance ; } ContourData* FillGlobalConfig::getContourDataByPngName(string& pngname) { for(auto it = _jewelContours.begin(); it!=_jewelContours.end();++it ) { if( it->_name.find(pngname) != string::npos ) { return &(it->_contour) ; } } return nullptr ; } bool FillGlobalConfig::initJewelItems( string filename ) { _mapJewelItems.clear() ; ifstream ifs( filename.c_str() ) ; if( ifs.good()==false ) return false ; string line ; //跳过第一行 std::getline(ifs, line) ; vector tokens ; while( std::getline(ifs, line) ) { if( line.length() > 2 ) { tokens.clear() ; boost::split(tokens, line, boost::is_any_of(",")); if( tokens.size()>=13 ) { JewelItem ji ; ji._id = atof(tokens[0].c_str()) ; ji._category = tokens[1] ; ji._jewelName = tokens[2] ; ji._code1 = tokens[3] ; ji._code2 = tokens[4] ; ji._size = (tokens[5].compare("SM")==0)?FILLGLOBALCONFIG_JEWELSIZE_SM:((tokens[5].compare("MD")==0)?FILLGLOBALCONFIG_JEWELSIZE_MD:FILLGLOBALCONFIG_JEWELSIZE_LG) ; ji._scale = atof( tokens[6].c_str() ) ; ji._pngName = tokens[7] ; ji._yzName = tokens[8] ; ji._contourName = tokens[9] ; ji._bbWidth = atof( tokens[10].c_str() ) ; ji._bbHeight = atof( tokens[11].c_str() ) ; ji._etc = tokens[12] ; _mapJewelItems[ji._id] = ji ; } } } return true ; } bool FillGlobalConfig::initLevelDatas( string filename ) { _mapLevelDatas.clear() ; ifstream ifs( filename.c_str() ) ; if( ifs.good()==false ) return false ; string line ; //跳过第一行 std::getline(ifs, line) ; LevelData tempLevelData ; tempLevelData._id = -1 ;//初始化 vector tokens ; while( std::getline(ifs, line) ) { bool isDataLine = false; if( line.length() > 2 ) { tokens.clear() ; boost::split(tokens, line, boost::is_any_of(",")); if( tokens.size()>=6 ) { isDataLine = true ; int lid = atof(tokens[0].c_str()) ; int subid = atof(tokens[1].c_str()) ; int difficulty = atof(tokens[2].c_str()) ; int jewId = atof(tokens[3].c_str()) ; string jewSz = tokens[4]; //ignored int grpcnt = atof( tokens[5].c_str() ) ; if( tempLevelData._id == -1 ) { tempLevelData._id = lid ; tempLevelData._subId = subid ; tempLevelData._difficulty = difficulty ; } tempLevelData._jewelIds.push_back(jewId) ; tempLevelData._cnts.push_back(grpcnt*3) ; } } if( isDataLine==false ) { //出现空行, 记录临时关卡 if( tempLevelData._id>0 ) { _mapLevelDatas[tempLevelData._id].push_back(tempLevelData) ; } //清空临时关卡数据 tempLevelData._id = -1 ; tempLevelData._subId = -1 ; tempLevelData._difficulty = -1; tempLevelData._cnts.clear() ; tempLevelData._jewelIds.clear() ; } } //最后一个临时对象是否是有效关卡数据 if( tempLevelData._id>0 ) { _mapLevelDatas[tempLevelData._id].push_back(tempLevelData) ; tempLevelData._id = -1 ; tempLevelData._cnts.clear() ; tempLevelData._jewelIds.clear() ; } return true ; } bool FillGlobalConfig::initPlateItems( string filename ) { _mapPlateItems.clear() ; _mapPlateIdArray.clear() ; ifstream ifs( filename.c_str() ) ; if( ifs.good()==false ) return false ; string line ; //跳过第一行 std::getline(ifs, line) ; vector tokens ; while( std::getline(ifs, line) ) { if( line.length() > 2 ) { tokens.clear() ; boost::split(tokens, line, boost::is_any_of(",")); if( tokens.size()>=13 ) { PlateItem pi ; pi._id = atof(tokens[0].c_str()) ; pi._name = tokens[1] ; int sz = 0 ; string szStr = tokens[2] ; if( szStr.compare("SM")==0 ) sz = FILLGLOBALCONFIG_PLATESIZE_SM ; else if( szStr.compare("MD")==0 ) sz = FILLGLOBALCONFIG_PLATESIZE_MD; else if( szStr.compare("LG")==0 ) sz = FILLGLOBALCONFIG_PLATESIZE_LG; else continue; //无效盘子跳过 pi._size = sz ; pi._pngName = tokens[3] ; pi._contourName = tokens[4] ; pi._blx = atof( tokens[5].c_str() ) ;//bottom left pi._bly = atof( tokens[6].c_str() ) ; pi._trx = atof( tokens[7].c_str() ) ;//top right pi._try = atof( tokens[8].c_str() ) ; pi._bigJewCap = atof( tokens[9].c_str() ) ; pi._bbwid = atof( tokens[10].c_str()) ; pi._bbhei = atof( tokens[11].c_str()) ; pi._etc = tokens[12] ; _mapPlateItems[pi._id] = pi ; _mapPlateIdArray[sz].push_back(pi._id) ; } } } return true ; }