123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // FillGlobalConfig.cpp
- // Test
- //
- // Created by 高慕白 on 2024/12/3.
- //
- #include "FillGlobalConfig.hpp"
- using namespace ArduinoJson;
- #include <fstream>
- using std::ifstream;
- #include <boost/algorithm/string.hpp>
- 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<char*>() ;
- string jeweldir = root["jewel_contour_dir"].as<char*>() ;
- JsonArray& parr =root["plate_names"].as<JsonArray>() ;
- JsonArray& jarr =root["jewel_names"].as<JsonArray>() ;
- _plateContours.resize(parr.size());
- _jewelContours.resize(jarr.size());
-
- for(int i=0;i<parr.size();++i ) {
- string name1 = parr[i].as<char*>() ;
- string fullname1 = platedir + name1 ;
- _plateContours[i]._name = name1 ;
- _plateContours[i]._contour.readFromJsonFile(fullname1) ;
- }
- for(int i=0;i<jarr.size();++i ) {
- string name1 = jarr[i].as<char*>() ;
- 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<string> 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<string> 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<string> 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 ;
- }
|