Config.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include "Config.h"
  2. #include "Logging.h"
  3. #include "Path.h"
  4. #include "FileSystem.h"
  5. #include "StringUtils.h"
  6. #include "Unicode.h"
  7. #include <cpptoml.h>
  8. #include <cstring>
  9. #include <unordered_map>
  10. #define GP_CONFIG_SETTING "config"
  11. #define GP_CONFIG_EXT ".toml"
  12. #define GP_CONFIG_FILE_DEFAULT "app.config.toml"
  13. #define GP_CONFIG_DEV_DIR "../../../config"
  14. namespace gameplay
  15. {
  16. struct Config::Impl
  17. {
  18. void replace_quotes(std::string& str);
  19. void parse_from_args(int argc, char** argv);
  20. void parse_arg(const std::string& arg);
  21. void apply_from_args();
  22. std::unordered_map<std::string, std::string> args;
  23. std::string lastKey;
  24. std::string configPath = "";
  25. std::shared_ptr<cpptoml::table> root = nullptr;
  26. };
  27. Config::Config()
  28. {
  29. _impl = new Config::Impl();
  30. }
  31. Config::~Config()
  32. {
  33. GP_SAFE_DELETE(_impl);
  34. }
  35. void Config::Impl::replace_quotes(std::string& str)
  36. {
  37. if (!str.empty() && str[0] == '\'' && str[str.size() - 1] == '\'')
  38. {
  39. str[0] = '"';
  40. str[str.size() - 1] = '"';
  41. }
  42. }
  43. void Config::Impl::parse_arg(const std::string& arg)
  44. {
  45. // check if there a key waiting to be filled with a value.
  46. if (!lastKey.empty())
  47. {
  48. if (StringUtils::starts_with(arg, "--"))
  49. {
  50. lastKey.clear();
  51. }
  52. else
  53. {
  54. std::string value = arg;
  55. replace_quotes(value);
  56. args[lastKey] = value;
  57. lastKey.clear();
  58. return;
  59. }
  60. }
  61. // process only keys starting with '--'
  62. static std::string prefix = "--";
  63. if (strncmp(arg.c_str(), prefix.c_str(), prefix.size()) == 0)
  64. {
  65. // split to key and value using the '=' sign
  66. std::size_t pos = arg.find("=");
  67. if (pos != std::string::npos)
  68. {
  69. const std::string key = arg.substr(prefix.size(), pos - prefix.size());
  70. std::string value = arg.substr(pos + 1, arg.size() - pos - 1);
  71. replace_quotes(value);
  72. args[key] = value;
  73. }
  74. else
  75. {
  76. // save key and wait for next value
  77. lastKey = arg.substr(prefix.size(), arg.size() - prefix.size());
  78. }
  79. }
  80. }
  81. void Config::Impl::parse_from_args(int argc, char** argv)
  82. {
  83. if (argc < 1 || argv == nullptr)
  84. {
  85. return;
  86. }
  87. for (int i = 1; i < argc; i++)
  88. {
  89. if (argv[i])
  90. {
  91. std::string arg = argv[i];
  92. parse_arg(arg);
  93. }
  94. }
  95. }
  96. void Config::load(int argc, char** argv)
  97. {
  98. // set the application
  99. Path appExecutablePath = Path(argv[0]).get_absolute();
  100. auto fs = App::get_app()->get_file_system();
  101. fs->set_app_executable_path(appExecutablePath.c_str());
  102. // parse args
  103. if (argc > 1)
  104. {
  105. _impl->parse_from_args(argc, argv);
  106. }
  107. // check for '--config' argument
  108. if (_impl->args.find(GP_CONFIG_SETTING) != _impl->args.end())
  109. {
  110. _impl->configPath = _impl->args[GP_CONFIG_SETTING];
  111. }
  112. // attempt to resolve the a config file in the following:
  113. //
  114. // 1st check the specified --config=<path>
  115. // 2nd check for app.config in the executable directory.
  116. // 3rd check the repo config path ../../../config/app.config
  117. bool configFound = true;
  118. std::string configExt = Path(_impl->configPath).get_extension();
  119. if (!fs->exists(_impl->configPath.c_str()) || configExt.compare(GP_CONFIG_EXT) != 0)
  120. {
  121. _impl->configPath = Path(appExecutablePath).get_parent().join(Path(GP_CONFIG_FILE_DEFAULT)).get_absolute();
  122. if (!fs->exists(_impl->configPath.c_str()))
  123. {
  124. _impl->configPath = Path(appExecutablePath).get_parent().join(Path(GP_CONFIG_DEV_DIR)).join(Path(GP_CONFIG_FILE_DEFAULT)).get_absolute();
  125. if (!fs->exists(_impl->configPath.c_str()))
  126. {
  127. configFound = false;
  128. }
  129. }
  130. }
  131. // if we found a config file that exists
  132. if (configFound)
  133. {
  134. try
  135. {
  136. _impl->root = cpptoml::parse_file(_impl->configPath);
  137. }
  138. catch(const cpptoml::parse_exception& e)
  139. {
  140. GP_LOG_ERROR("Failed to parse app .config file: {}, error: {}", _impl->configPath.c_str(), e.what());
  141. }
  142. }
  143. else
  144. {
  145. _impl->root = cpptoml::make_table();
  146. }
  147. // apply our settings from command line arguments
  148. if (argc > 0)
  149. {
  150. _impl->args.erase(GP_CONFIG_SETTING);
  151. _impl->apply_from_args();
  152. }
  153. }
  154. /*bool Config::save()
  155. {
  156. std::stringstream strBuffer;
  157. strBuffer << (*_table->root) << std::endl;
  158. auto fs = App::get_app()->get_file_system();
  159. std::string prefsPath = _configPath + GP_PREFS_EXT;
  160. auto file = fs->open_file(prefsPath.c_str(), FileMode::WRITE);
  161. if (!file)
  162. {
  163. GP_LOG_ERROR("Failed to save user .prefs file {}", prefsPath.c_str());
  164. return false;
  165. }
  166. std::string fileContents = strBuffer.str();
  167. size_t fileSize = fileContents.size() * sizeof(char);
  168. if (fs->write_file_chunk(file, fileContents.data(), fileSize) != fileSize)
  169. {
  170. GP_LOG_ERROR("Failed to write all data to settings file {}", prefsPath.c_str());
  171. return false;
  172. }
  173. fs->close_file(file);
  174. return true;
  175. }*/
  176. static std::pair<std::string, std::shared_ptr<cpptoml::table>> __get_key_and_table(std::shared_ptr<cpptoml::table> root, const char* key)
  177. {
  178. std::pair<std::string, std::shared_ptr<cpptoml::table>> ret;
  179. std::string keyStr = key;
  180. std::string::size_type pos = keyStr.find_last_of('.');
  181. if (pos != std::string::npos)
  182. {
  183. ret.first = keyStr.substr(pos+1);
  184. ret.second = root->get_table(keyStr.substr(0, pos));
  185. }
  186. else
  187. {
  188. ret.first = keyStr;
  189. ret.second = root;
  190. }
  191. return ret;
  192. }
  193. void Config::Impl::apply_from_args()
  194. {
  195. for (const auto& [k, value] : args)
  196. {
  197. if (strlen(k.c_str()) == 0)
  198. continue;
  199. auto kt = __get_key_and_table(root, k.c_str());
  200. auto key = kt.first;
  201. auto table = kt.second;
  202. if (value.compare("true") == 0)
  203. {
  204. table->insert(key, true);
  205. }
  206. else if (value.compare("false") == 0)
  207. {
  208. table->insert(key, false);
  209. }
  210. else if (value.at(0) > 47 && value.at(0) < 58)
  211. {
  212. // this is a number
  213. if (value.find(".") != std::string::npos)
  214. {
  215. // is a float number
  216. double v = std::stod(value);
  217. table->insert(key, v);
  218. }
  219. else
  220. {
  221. // is an int number
  222. int64_t v = std::stol(value);
  223. table->insert(key, v);
  224. }
  225. }
  226. else
  227. {
  228. table->insert(key, value);
  229. }
  230. }
  231. }
  232. size_t Config::get_array_size(const char* key)
  233. {
  234. auto arr = _impl->root->get_array_qualified(key);
  235. if (!arr)
  236. return 0;
  237. return arr->get().size();
  238. }
  239. std::string Config::get_string(const char* key, const char* altValue)
  240. {
  241. if(strchr(key, '.'))
  242. {
  243. return _impl->root->get_qualified_as<std::string>(key).value_or(altValue);
  244. }
  245. else
  246. {
  247. return _impl->root->get_as<std::string>(key).value_or(altValue);
  248. }
  249. }
  250. std::string Config::set_string(const char* key, const char* value)
  251. {
  252. std::string ret = value;
  253. auto kt = __get_key_and_table(_impl->root, key);
  254. if (kt.second && kt.second->contains(kt.first))
  255. {
  256. ret = kt.second->get_as<std::string>(kt.first).value_or(value);
  257. }
  258. else
  259. {
  260. kt.second->insert(kt.first, value);
  261. }
  262. return ret;
  263. }
  264. bool Config::get_bool(const char* key, bool altValue)
  265. {
  266. if(strchr(key, '.'))
  267. {
  268. return (int)_impl->root->get_qualified_as<bool>(key).value_or(altValue);
  269. }
  270. else
  271. {
  272. return (int)_impl->root->get_as<bool>(key).value_or(altValue);
  273. }
  274. }
  275. bool Config::set_bool(const char* key, bool value)
  276. {
  277. bool ret = value;
  278. auto kt = __get_key_and_table(_impl->root, key);
  279. if (kt.second && kt.second->contains(kt.first))
  280. {
  281. ret = kt.second->get_as<bool>(kt.first).value_or(value);
  282. }
  283. else
  284. {
  285. kt.second->insert(kt.first, value);
  286. }
  287. return ret;
  288. }
  289. int Config::get_int(const char* key, int altValue)
  290. {
  291. if(strchr(key, '.'))
  292. {
  293. return (int)_impl->root->get_qualified_as<int64_t>(key).value_or(altValue);
  294. }
  295. else
  296. {
  297. return (int)_impl->root->get_as<int64_t>(key).value_or(altValue);
  298. }
  299. }
  300. int Config::set_int(const char* key, int value)
  301. {
  302. int ret = value;
  303. auto kt = __get_key_and_table(_impl->root, key);
  304. if (kt.second && kt.second->contains(kt.first))
  305. {
  306. ret = (int)kt.second->get_as<int64_t>(kt.first).value_or(value);
  307. }
  308. else
  309. {
  310. kt.second->insert(kt.first, (int64_t)value);
  311. }
  312. return ret;
  313. }
  314. float Config::get_float(const char* key, float altValue)
  315. {
  316. if(strchr(key, '.'))
  317. {
  318. return (float)_impl->root->get_qualified_as<double>(key).value_or((double)altValue);
  319. }
  320. else
  321. {
  322. return (float)_impl->root->get_as<double>(key).value_or((double)altValue);
  323. }
  324. }
  325. float Config::set_float(const char* key, float value)
  326. {
  327. float ret = value;
  328. auto kt = __get_key_and_table(_impl->root, key);
  329. if (kt.second && kt.second->contains(kt.first))
  330. {
  331. ret = (float)kt.second->get_as<double>(kt.first).value_or(value);
  332. }
  333. else
  334. {
  335. kt.second->insert(kt.first, (double)value);
  336. }
  337. return ret;
  338. }
  339. void Config::get_string_array(const char* key, std::string* arr, size_t arrSize)
  340. {
  341. auto values = _impl->root->get_qualified_array_of<std::string>(key);
  342. GP_ASSERT(arr && arrSize >= values->size());
  343. size_t i = 0;
  344. for (const auto& value : *values)
  345. {
  346. arr[i] = value;
  347. i++;
  348. }
  349. }
  350. void Config::set_string_array(const char* key, const std::string* arr, size_t arrSize)
  351. {
  352. auto new_array = cpptoml::make_array();
  353. for (size_t i = 0; i < arrSize; i++)
  354. {
  355. new_array->push_back(arr[i]);
  356. }
  357. auto kt = __get_key_and_table(_impl->root, key);
  358. kt.second->insert(kt.first, new_array);
  359. }
  360. void Config::get_bool_array(const char* key, bool* arr, size_t arrSize)
  361. {
  362. auto values = _impl->root->get_qualified_array_of<bool>(key);
  363. GP_ASSERT(arr && arrSize >= values->size());
  364. size_t i = 0;
  365. for (const auto& value : *values)
  366. {
  367. arr[i] = value;
  368. i++;
  369. }
  370. }
  371. void Config::set_bool_array(const char* key, const bool* arr, size_t arrSize)
  372. {
  373. auto new_array = cpptoml::make_array();
  374. for (size_t i = 0; i < arrSize; i++)
  375. {
  376. new_array->push_back(arr[i]);
  377. }
  378. auto kt = __get_key_and_table(_impl->root, key);
  379. kt.second->insert(kt.first, new_array);
  380. }
  381. void Config::get_int_array(const char* key, int* arr, size_t arrSize)
  382. {
  383. auto values = _impl->root->get_qualified_array_of<int64_t>(key);
  384. GP_ASSERT(arr && arrSize >= values->size());
  385. size_t i = 0;
  386. for (const auto& value : *values)
  387. {
  388. arr[i] = (int)value;
  389. i++;
  390. }
  391. }
  392. void Config::set_int_array(const char* key, const int* arr, size_t arrSize)
  393. {
  394. auto new_array = cpptoml::make_array();
  395. for (size_t i = 0; i < arrSize; i++)
  396. {
  397. new_array->push_back((int64_t)arr[i]);
  398. }
  399. auto kt = __get_key_and_table(_impl->root, key);
  400. kt.second->insert(kt.first, new_array);
  401. }
  402. void Config::get_float_array(const char* key, float* arr, size_t arrSize)
  403. {
  404. auto values = _impl->root->get_qualified_array_of<double>(key);
  405. GP_ASSERT(arr && arrSize >= values->size());
  406. size_t i = 0;
  407. for (const auto& value : *values)
  408. {
  409. arr[i] = (float)value;
  410. i++;
  411. }
  412. }
  413. void Config::set_float_array(const char* key, const float* arr, size_t arrSize)
  414. {
  415. auto new_array = cpptoml::make_array();
  416. for (size_t i = 0; i < arrSize; i++)
  417. {
  418. new_array->push_back((float)arr[i]);
  419. }
  420. auto kt = __get_key_and_table(_impl->root, key);
  421. kt.second->insert(kt.first, new_array);
  422. }
  423. void Config::for_each_table(const char* key, Config::OnVisitTableFn fn, void* userPtr)
  424. {
  425. std::shared_ptr<cpptoml::table> root = _impl->root;
  426. auto table_arr = root->get_table_array_qualified(key);
  427. for (const auto& table : *table_arr)
  428. {
  429. _impl->root = table;
  430. if (!fn(this, userPtr))
  431. break;
  432. }
  433. _impl->root = root;
  434. }
  435. }