ConfigSet.cpp 9.6 KB

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