RUBaseConf.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // GameConf.cpp
  3. // CandyCookie
  4. //
  5. // Created by 徐炼新 on 2021/6/29.
  6. //
  7. #include "RUBaseConf.h"
  8. #include "RUUtils.h"
  9. NS_RU_BEGIN
  10. BaseConf::BaseConf(const char* fn) {
  11. string configErr = "";
  12. FileUtils* fileutils = FileUtils::getInstance();
  13. auto data = fileutils->getDataFromFile(fn);
  14. if (data.getSize() > 0) {
  15. string strConf((char*)data.getBytes(), data.getSize());
  16. _jConf = json11::Json::parse(strConf, configErr, JsonParse::COMMENTS);
  17. if (configErr.size() > 0) {
  18. CCLOG("error while parse json: %s", configErr.c_str());
  19. } else {
  20. _valid = true;
  21. }
  22. }
  23. }
  24. const string& BaseConf::getConf(const string& group, const string& confKey) {
  25. if (_valid && _jConf.object_items().find(group) != _jConf.object_items().end()) {
  26. auto objs = _jConf.object_items().at(group).object_items();
  27. int startPos = 0;
  28. int nPos = confKey.find(":");
  29. string conf = confKey.substr(startPos, nPos);
  30. while (nPos != string::npos) {
  31. if (objs.find(conf) == objs.end()) {
  32. return _nullV;
  33. }
  34. objs = objs.find(conf)->second.object_items();
  35. startPos = nPos + 1;
  36. nPos = confKey.find(":", startPos);
  37. conf = confKey.substr(startPos, nPos-startPos);
  38. }
  39. auto jConf = objs.find(conf);
  40. if (jConf != objs.end()) {
  41. return jConf->second.string_value();
  42. }
  43. }
  44. return _nullV;
  45. }
  46. std::string BaseConf::getGrpOptions(int userGrp, const char* optK) {
  47. std::string cfgK = "grp_" + Value(userGrp).asString();
  48. cfgK += ":options";
  49. cfgK += ":";
  50. cfgK += optK;
  51. return getConf("groups", cfgK);
  52. }
  53. const string& BaseConf::getConf(const string& confKey) {
  54. if (_valid) {
  55. auto objs = _jConf.object_items();
  56. auto jConf = objs.find(confKey);
  57. if (jConf != objs.end()) {
  58. return jConf->second.string_value();
  59. }
  60. }
  61. return _nullV;
  62. }
  63. float BaseConf::getConfAsFloat(const string& confKey) {
  64. auto& str = getConf(confKey);
  65. if (str.size() < 1) {
  66. return 0.0f;
  67. }
  68. return Value(str).asFloat();
  69. }
  70. float BaseConf::getConfAsFloat(const string& group, const string& confKey) {
  71. auto& str = getConf(group, confKey);
  72. if (str.size() < 1) {
  73. return 0.0f;
  74. }
  75. return Value(str).asFloat();
  76. }
  77. std::list<float> BaseConf::getConfAsFloatList(const string& confKey) {
  78. auto& str = getConf(confKey);
  79. if (str.size() < 1) {
  80. return std::list<float>();
  81. }
  82. // str使用,分割的一系列float类型
  83. std::list<float> ret;
  84. int startPos = 0;
  85. int nPos = str.find(",");
  86. string conf = str.substr(startPos, nPos);
  87. while (nPos != string::npos) {
  88. ret.push_back(Value(conf).asFloat());
  89. startPos = nPos + 1;
  90. nPos = str.find(",", startPos);
  91. conf = str.substr(startPos, nPos-startPos);
  92. }
  93. // 最后一个
  94. ret.push_back(Value(conf).asFloat());
  95. return ret;
  96. }
  97. std::list<float> BaseConf::getConfAsFloatList(const string& group, const string& confKey) {
  98. auto& str = getConf(group, confKey);
  99. if (str.size() < 1) {
  100. return std::list<float>();
  101. }
  102. return redutils::parseAsFloatList(str);
  103. }
  104. bool BaseConf::isValid() {
  105. return _valid;
  106. }
  107. NS_RU_END