ConfigSet.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Core/ConfigSet.h>
  6. #include <AnKi/Util/Xml.h>
  7. #include <AnKi/Util/Logger.h>
  8. #include <AnKi/Util/File.h>
  9. // Used by the config options
  10. #include <AnKi/Util/System.h>
  11. #include <AnKi/Renderer/ClusterBin.h>
  12. namespace anki
  13. {
  14. class ConfigSet::Option : public NonCopyable
  15. {
  16. public:
  17. enum Type
  18. {
  19. NONE,
  20. FLOAT,
  21. UNSIGNED,
  22. STRING
  23. };
  24. String m_name;
  25. String m_helpMsg;
  26. String m_str;
  27. F64 m_float = 0.0;
  28. F64 m_minFloat = 0.0;
  29. F64 m_maxFloat = 0.0;
  30. U64 m_unsigned = 0;
  31. U64 m_minUnsigned = 0;
  32. U64 m_maxUnsigned = 0;
  33. Type m_type = NONE;
  34. Option() = default;
  35. Option(Option&& b)
  36. : m_name(std::move(b.m_name))
  37. , m_helpMsg(std::move(b.m_helpMsg))
  38. , m_str(std::move(b.m_str))
  39. , m_float(b.m_float)
  40. , m_minFloat(b.m_minFloat)
  41. , m_maxFloat(b.m_maxFloat)
  42. , m_unsigned(b.m_unsigned)
  43. , m_minUnsigned(b.m_minUnsigned)
  44. , m_maxUnsigned(b.m_maxUnsigned)
  45. , m_type(b.m_type)
  46. {
  47. }
  48. ~Option() = default;
  49. Option& operator=(Option&& b) = delete;
  50. };
  51. ConfigSet::ConfigSet()
  52. {
  53. m_alloc = HeapAllocator<U8>(allocAligned, nullptr);
  54. #define ANKI_CONFIG_OPTION(name, ...) newOption(ANKI_STRINGIZE(name), __VA_ARGS__);
  55. #include <AnKi/Core/ConfigDefs.h>
  56. #include <AnKi/Resource/ConfigDefs.h>
  57. #include <AnKi/Renderer/ConfigDefs.h>
  58. #include <AnKi/Scene/ConfigDefs.h>
  59. #include <AnKi/Gr/ConfigDefs.h>
  60. #undef ANKI_CONFIG_OPTION
  61. }
  62. ConfigSet::~ConfigSet()
  63. {
  64. for(Option& o : m_options)
  65. {
  66. o.m_name.destroy(m_alloc);
  67. o.m_str.destroy(m_alloc);
  68. o.m_helpMsg.destroy(m_alloc);
  69. }
  70. m_options.destroy(m_alloc);
  71. }
  72. ConfigSet& ConfigSet::operator=(const ConfigSet& b)
  73. {
  74. m_alloc = b.m_alloc; // Not a copy but we are fine
  75. for(const Option& o : b.m_options)
  76. {
  77. Option newO;
  78. newO.m_name.create(m_alloc, o.m_name.toCString());
  79. if(o.m_type == Option::STRING)
  80. {
  81. newO.m_str.create(m_alloc, o.m_str.toCString());
  82. }
  83. newO.m_float = o.m_float;
  84. newO.m_minFloat = o.m_minFloat;
  85. newO.m_maxFloat = o.m_maxFloat;
  86. newO.m_unsigned = o.m_unsigned;
  87. newO.m_minUnsigned = o.m_minUnsigned;
  88. newO.m_maxUnsigned = o.m_maxUnsigned;
  89. newO.m_type = o.m_type;
  90. m_options.emplaceBack(m_alloc, std::move(newO));
  91. }
  92. return *this;
  93. }
  94. ConfigSet::Option* ConfigSet::tryFind(CString optionName)
  95. {
  96. for(List<Option>::Iterator it = m_options.getBegin(); it != m_options.getEnd(); ++it)
  97. {
  98. if((*it).m_name == optionName)
  99. {
  100. return &(*it);
  101. }
  102. }
  103. return nullptr;
  104. }
  105. const ConfigSet::Option* ConfigSet::tryFind(CString optionName) const
  106. {
  107. for(List<Option>::ConstIterator it = m_options.getBegin(); it != m_options.getEnd(); ++it)
  108. {
  109. if((*it).m_name == optionName)
  110. {
  111. return &(*it);
  112. }
  113. }
  114. return nullptr;
  115. }
  116. void ConfigSet::newOption(CString optionName, CString value, CString helpMsg)
  117. {
  118. ANKI_ASSERT(!tryFind(optionName));
  119. Option o;
  120. o.m_name.create(m_alloc, optionName);
  121. o.m_str.create(m_alloc, value);
  122. o.m_type = Option::STRING;
  123. if(!helpMsg.isEmpty())
  124. {
  125. o.m_helpMsg.create(m_alloc, helpMsg);
  126. }
  127. m_options.emplaceBack(m_alloc, std::move(o));
  128. }
  129. void ConfigSet::newOptionInternal(CString optionName, F64 value, F64 minValue, F64 maxValue, CString helpMsg)
  130. {
  131. ANKI_ASSERT(!tryFind(optionName));
  132. ANKI_ASSERT(value >= minValue && value <= maxValue && minValue <= maxValue);
  133. Option o;
  134. o.m_name.create(m_alloc, optionName);
  135. o.m_float = value;
  136. o.m_minFloat = minValue;
  137. o.m_maxFloat = maxValue;
  138. o.m_type = Option::FLOAT;
  139. if(!helpMsg.isEmpty())
  140. {
  141. o.m_helpMsg.create(m_alloc, helpMsg);
  142. }
  143. m_options.emplaceBack(m_alloc, std::move(o));
  144. }
  145. void ConfigSet::newOptionInternal(CString optionName, U64 value, U64 minValue, U64 maxValue, CString helpMsg)
  146. {
  147. ANKI_ASSERT(!tryFind(optionName));
  148. ANKI_ASSERT(value >= minValue && value <= maxValue && minValue <= maxValue);
  149. Option o;
  150. o.m_name.create(m_alloc, optionName);
  151. o.m_unsigned = value;
  152. o.m_minUnsigned = minValue;
  153. o.m_maxUnsigned = maxValue;
  154. o.m_type = Option::UNSIGNED;
  155. if(!helpMsg.isEmpty())
  156. {
  157. o.m_helpMsg.create(m_alloc, helpMsg);
  158. }
  159. m_options.emplaceBack(m_alloc, std::move(o));
  160. }
  161. void ConfigSet::set(CString optionName, CString value)
  162. {
  163. Option& o = find(optionName);
  164. ANKI_ASSERT(o.m_type == Option::STRING);
  165. o.m_str.destroy(m_alloc);
  166. o.m_str.create(m_alloc, value);
  167. }
  168. void ConfigSet::setInternal(CString optionName, F64 value)
  169. {
  170. Option& o = find(optionName);
  171. ANKI_ASSERT(o.m_type == Option::FLOAT);
  172. ANKI_ASSERT(value >= o.m_minFloat);
  173. ANKI_ASSERT(value <= o.m_maxFloat);
  174. o.m_float = value;
  175. }
  176. void ConfigSet::setInternal(CString optionName, U64 value)
  177. {
  178. Option& o = find(optionName);
  179. ANKI_ASSERT(o.m_type == Option::UNSIGNED);
  180. ANKI_ASSERT(value >= o.m_minUnsigned);
  181. ANKI_ASSERT(value <= o.m_maxUnsigned);
  182. o.m_unsigned = value;
  183. }
  184. F64 ConfigSet::getNumberF64(CString optionName) const
  185. {
  186. const Option& option = find(optionName);
  187. ANKI_ASSERT(option.m_type == Option::FLOAT);
  188. return option.m_float;
  189. }
  190. F32 ConfigSet::getNumberF32(CString optionName) const
  191. {
  192. return F32(getNumberF64(optionName));
  193. }
  194. U64 ConfigSet::getNumberU64(CString optionName) const
  195. {
  196. const Option& option = find(optionName);
  197. ANKI_ASSERT(option.m_type == Option::UNSIGNED);
  198. return option.m_unsigned;
  199. }
  200. U32 ConfigSet::getNumberU32(CString optionName) const
  201. {
  202. const U64 out = getNumberU64(optionName);
  203. if(out > MAX_U32)
  204. {
  205. ANKI_CORE_LOGW("Option is out of U32 range: %s", optionName.cstr());
  206. }
  207. return U32(out);
  208. }
  209. U16 ConfigSet::getNumberU16(CString optionName) const
  210. {
  211. const U64 out = getNumberU64(optionName);
  212. if(out > MAX_U16)
  213. {
  214. ANKI_CORE_LOGW("Option is out of U16 range: %s", optionName.cstr());
  215. }
  216. return U16(out);
  217. }
  218. U8 ConfigSet::getNumberU8(CString optionName) const
  219. {
  220. const U64 out = getNumberU64(optionName);
  221. if(out > MAX_U8)
  222. {
  223. ANKI_CORE_LOGW("Option is out of U8 range: %s", optionName.cstr());
  224. }
  225. return U8(out);
  226. }
  227. Bool ConfigSet::getBool(CString optionName) const
  228. {
  229. const U64 val = getNumberU64(optionName);
  230. if((val & ~U64(1)) != 0)
  231. {
  232. ANKI_CORE_LOGW("Expecting 0 or 1 for the config option \"%s\". Will mask out extra bits", optionName.cstr());
  233. }
  234. return val & 1;
  235. }
  236. CString ConfigSet::getString(CString optionName) const
  237. {
  238. const Option& o = find(optionName);
  239. ANKI_ASSERT(o.m_type == Option::STRING);
  240. return o.m_str.toCString();
  241. }
  242. Error ConfigSet::loadFromFile(CString filename)
  243. {
  244. ANKI_CORE_LOGI("Loading config file %s", filename.cstr());
  245. XmlDocument xml;
  246. ANKI_CHECK(xml.loadFile(filename, m_alloc));
  247. XmlElement rootel;
  248. ANKI_CHECK(xml.getChildElement("config", rootel));
  249. for(Option& option : m_options)
  250. {
  251. XmlElement el;
  252. ANKI_CHECK(rootel.getChildElementOptional(option.m_name.toCString(), el));
  253. if(el)
  254. {
  255. if(option.m_type == Option::FLOAT)
  256. {
  257. ANKI_CHECK(el.getNumber(option.m_float));
  258. }
  259. else if(option.m_type == Option::UNSIGNED)
  260. {
  261. ANKI_CHECK(el.getNumber(option.m_unsigned));
  262. }
  263. else
  264. {
  265. ANKI_ASSERT(option.m_type == Option::STRING);
  266. CString txt;
  267. ANKI_CHECK(el.getText(txt));
  268. option.m_str.destroy(m_alloc);
  269. option.m_str.create(m_alloc, txt);
  270. }
  271. }
  272. else
  273. {
  274. if(option.m_type == Option::FLOAT)
  275. {
  276. ANKI_CORE_LOGW("Missing option for \"%s\". Will use the default value: %f", &option.m_name[0],
  277. option.m_float);
  278. }
  279. else if(option.m_type == Option::UNSIGNED)
  280. {
  281. ANKI_CORE_LOGW("Missing option for \"%s\". Will use the default value: %" PRIu64, &option.m_name[0],
  282. option.m_unsigned);
  283. }
  284. else
  285. {
  286. ANKI_ASSERT(option.m_type == Option::STRING);
  287. ANKI_CORE_LOGW("Missing option for \"%s\". Will use the default value: %s", option.m_name.cstr(),
  288. option.m_str.cstr());
  289. }
  290. }
  291. }
  292. return Error::NONE;
  293. }
  294. Error ConfigSet::saveToFile(CString filename) const
  295. {
  296. ANKI_CORE_LOGI("Saving config file %s", &filename[0]);
  297. File file;
  298. ANKI_CHECK(file.open(filename, FileOpenFlag::WRITE));
  299. ANKI_CHECK(file.writeText("%s\n<config>\n", XmlDocument::XML_HEADER.cstr()));
  300. for(const Option& option : m_options)
  301. {
  302. if(!option.m_helpMsg.isEmpty())
  303. {
  304. ANKI_CHECK(file.writeText("\t<!-- %s -->\n", option.m_helpMsg.cstr()));
  305. }
  306. if(option.m_type == Option::FLOAT)
  307. {
  308. ANKI_CHECK(file.writeText("\t<%s>%f</%s>\n", option.m_name.cstr(), option.m_float, option.m_name.cstr()));
  309. }
  310. else if(option.m_type == Option::UNSIGNED)
  311. {
  312. ANKI_CHECK(file.writeText("\t<%s>%" PRIu64 "</%s>\n", option.m_name.cstr(), option.m_unsigned,
  313. option.m_name.cstr()));
  314. }
  315. else
  316. {
  317. ANKI_ASSERT(option.m_type == Option::STRING);
  318. ANKI_CHECK(file.writeText("\t<%s><![CDATA[%s]]></%s>\n", option.m_name.cstr(), option.m_str.cstr(),
  319. option.m_name.cstr()));
  320. }
  321. }
  322. ANKI_CHECK(file.writeText("</config>\n"));
  323. return Error::NONE;
  324. }
  325. Error ConfigSet::setFromCommandLineArguments(U32 cmdLineArgsCount, char* cmdLineArgs[])
  326. {
  327. for(U i = 0; i < cmdLineArgsCount; ++i)
  328. {
  329. const char* arg = cmdLineArgs[i];
  330. ANKI_ASSERT(arg);
  331. Option* option = tryFind(arg);
  332. if(option == nullptr)
  333. {
  334. ANKI_CORE_LOGW("Can't recognize command line argument: %s", arg);
  335. continue;
  336. }
  337. // Set the value
  338. ++i;
  339. if(i >= cmdLineArgsCount)
  340. {
  341. ANKI_CORE_LOGE("Expecting a command line argument after %s", arg);
  342. return Error::USER_DATA;
  343. }
  344. arg = cmdLineArgs[i];
  345. ANKI_ASSERT(arg);
  346. if(option->m_type == Option::STRING)
  347. {
  348. option->m_str.destroy(m_alloc);
  349. option->m_str.create(m_alloc, arg);
  350. }
  351. else if(option->m_type == Option::FLOAT)
  352. {
  353. CString val(arg);
  354. ANKI_CHECK(val.toNumber(option->m_float));
  355. }
  356. else
  357. {
  358. ANKI_ASSERT(option->m_type == Option::UNSIGNED);
  359. CString val(arg);
  360. ANKI_CHECK(val.toNumber(option->m_unsigned));
  361. }
  362. }
  363. return Error::NONE;
  364. }
  365. } // end namespace anki