123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- //
- // GameConf.cpp
- // CandyCookie
- //
- // Created by 徐炼新 on 2021/6/29.
- //
- #include "RUBaseConf.h"
- #include "RUUtils.h"
- NS_RU_BEGIN
- BaseConf::BaseConf(const char* fn) {
- string configErr = "";
- FileUtils* fileutils = FileUtils::getInstance();
- auto data = fileutils->getDataFromFile(fn);
- if (data.getSize() > 0) {
- string strConf((char*)data.getBytes(), data.getSize());
- _jConf = json11::Json::parse(strConf, configErr, JsonParse::COMMENTS);
- if (configErr.size() > 0) {
- CCLOG("error while parse json: %s", configErr.c_str());
- } else {
- _valid = true;
- }
- }
- }
- const string& BaseConf::getConf(const string& group, const string& confKey) {
- if (_valid && _jConf.object_items().find(group) != _jConf.object_items().end()) {
- auto objs = _jConf.object_items().at(group).object_items();
- int startPos = 0;
- int nPos = confKey.find(":");
- string conf = confKey.substr(startPos, nPos);
- while (nPos != string::npos) {
- if (objs.find(conf) == objs.end()) {
- return _nullV;
- }
- objs = objs.find(conf)->second.object_items();
- startPos = nPos + 1;
- nPos = confKey.find(":", startPos);
- conf = confKey.substr(startPos, nPos-startPos);
- }
- auto jConf = objs.find(conf);
- if (jConf != objs.end()) {
- return jConf->second.string_value();
- }
- }
- return _nullV;
- }
- std::string BaseConf::getGrpOptions(int userGrp, const char* optK) {
- std::string cfgK = "grp_" + Value(userGrp).asString();
- cfgK += ":options";
- cfgK += ":";
- cfgK += optK;
- return getConf("groups", cfgK);
- }
- const string& BaseConf::getConf(const string& confKey) {
- if (_valid) {
- auto objs = _jConf.object_items();
- auto jConf = objs.find(confKey);
- if (jConf != objs.end()) {
- return jConf->second.string_value();
- }
- }
- return _nullV;
- }
- float BaseConf::getConfAsFloat(const string& confKey) {
- auto& str = getConf(confKey);
- if (str.size() < 1) {
- return 0.0f;
- }
- return Value(str).asFloat();
- }
- float BaseConf::getConfAsFloat(const string& group, const string& confKey) {
- auto& str = getConf(group, confKey);
- if (str.size() < 1) {
- return 0.0f;
- }
- return Value(str).asFloat();
- }
- std::list<float> BaseConf::getConfAsFloatList(const string& confKey) {
- auto& str = getConf(confKey);
- if (str.size() < 1) {
- return std::list<float>();
- }
- // str使用,分割的一系列float类型
- std::list<float> ret;
- int startPos = 0;
- int nPos = str.find(",");
- string conf = str.substr(startPos, nPos);
- while (nPos != string::npos) {
- ret.push_back(Value(conf).asFloat());
- startPos = nPos + 1;
- nPos = str.find(",", startPos);
- conf = str.substr(startPos, nPos-startPos);
- }
- // 最后一个
- ret.push_back(Value(conf).asFloat());
- return ret;
- }
- std::list<float> BaseConf::getConfAsFloatList(const string& group, const string& confKey) {
- auto& str = getConf(group, confKey);
- if (str.size() < 1) {
- return std::list<float>();
- }
- return redutils::parseAsFloatList(str);
- }
- bool BaseConf::isValid() {
- return _valid;
- }
- NS_RU_END
|