ConfigSet.cpp 9.6 KB

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