// // 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 BaseConf::getConfAsFloatList(const string& confKey) { auto& str = getConf(confKey); if (str.size() < 1) { return std::list(); } // str使用,分割的一系列float类型 std::list 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 BaseConf::getConfAsFloatList(const string& group, const string& confKey) { auto& str = getConf(group, confKey); if (str.size() < 1) { return std::list(); } return redutils::parseAsFloatList(str); } bool BaseConf::isValid() { return _valid; } NS_RU_END