PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/settings.h

https://gitlab.com/bsmr/voxelands
C Header | 699 lines | 502 code | 122 blank | 75 comment | 75 complexity | 81f716f8799e8355725fc1e721692652 MD5 | raw file
  1. /************************************************************************
  2. * Minetest-c55
  3. * Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. *
  5. * settings.h
  6. * voxelands - 3d voxel world sandbox game
  7. * Copyright (C) Lisa 'darkrose' Milne 2014 <lisa@ltmnet.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. * License updated from GPLv2 or later to GPLv3 or later by Lisa Milne
  23. * for Voxelands.
  24. ************************************************************************/
  25. #ifndef SETTINGS_HEADER
  26. #define SETTINGS_HEADER
  27. #include "common_irrlicht.h"
  28. #include <string>
  29. #include <jthread.h>
  30. #include <jmutex.h>
  31. #include <jmutexautolock.h>
  32. #include "strfnd.h"
  33. #include <iostream>
  34. #include <fstream>
  35. #include <sstream>
  36. #include "debug.h"
  37. #include "utility.h"
  38. #include "log.h"
  39. enum ValueType
  40. {
  41. VALUETYPE_STRING,
  42. VALUETYPE_FLAG // Doesn't take any arguments
  43. };
  44. struct ValueSpec
  45. {
  46. ValueSpec(ValueType a_type, const char *a_help=NULL)
  47. {
  48. type = a_type;
  49. help = a_help;
  50. }
  51. ValueType type;
  52. const char *help;
  53. };
  54. using namespace jthread;
  55. class Settings
  56. {
  57. public:
  58. Settings()
  59. {
  60. m_mutex.Init();
  61. }
  62. void writeLines(std::ostream &os)
  63. {
  64. JMutexAutoLock lock(m_mutex);
  65. for(core::map<std::string, std::string>::Iterator
  66. i = m_settings.getIterator();
  67. i.atEnd() == false; i++)
  68. {
  69. std::string name = i.getNode()->getKey();
  70. std::string value = i.getNode()->getValue();
  71. os<<name<<" = "<<value<<"\n";
  72. }
  73. }
  74. bool parseConfigLine(const std::string &line)
  75. {
  76. JMutexAutoLock lock(m_mutex);
  77. std::string trimmedline = trim(line);
  78. // Ignore comments
  79. if(trimmedline[0] == '#')
  80. return true;
  81. //infostream<<"trimmedline=\""<<trimmedline<<"\""<<std::endl;
  82. Strfnd sf(trim(line));
  83. std::string name = sf.next("=");
  84. name = trim(name);
  85. if(name == "")
  86. return true;
  87. std::string value = sf.next("\n");
  88. value = trim(value);
  89. /*infostream<<"Config name=\""<<name<<"\" value=\""
  90. <<value<<"\""<<std::endl;*/
  91. m_settings[name] = value;
  92. return true;
  93. }
  94. void parseConfigLines(std::istream &is, const std::string &endstring)
  95. {
  96. for(;;){
  97. if(is.eof())
  98. break;
  99. std::string line;
  100. std::getline(is, line);
  101. std::string trimmedline = trim(line);
  102. if(endstring != ""){
  103. if(trimmedline == endstring)
  104. break;
  105. }
  106. parseConfigLine(line);
  107. }
  108. }
  109. // Returns false on EOF
  110. bool parseConfigObject(std::istream &is)
  111. {
  112. if(is.eof())
  113. return false;
  114. /*
  115. NOTE: This function might be expanded to allow multi-line
  116. settings.
  117. */
  118. std::string line;
  119. std::getline(is, line);
  120. //infostream<<"got line: \""<<line<<"\""<<std::endl;
  121. return parseConfigLine(line);
  122. }
  123. /*
  124. Read configuration file
  125. Returns true on success
  126. */
  127. bool readConfigFile(const char *filename)
  128. {
  129. std::ifstream is(filename);
  130. if(is.good() == false)
  131. {
  132. errorstream<<"Error opening configuration file \""
  133. <<filename<<"\""<<std::endl;
  134. return false;
  135. }
  136. infostream<<"Parsing configuration file: \""
  137. <<filename<<"\""<<std::endl;
  138. while(parseConfigObject(is));
  139. return true;
  140. }
  141. /*
  142. Reads a configuration object from stream (usually a single line)
  143. and adds it to dst.
  144. Preserves comments and empty lines.
  145. Settings that were added to dst are also added to updated.
  146. key of updated is setting name, value of updated is dummy.
  147. Returns false on EOF
  148. */
  149. bool getUpdatedConfigObject(std::istream &is,
  150. core::list<std::string> &dst,
  151. core::map<std::string, bool> &updated)
  152. {
  153. JMutexAutoLock lock(m_mutex);
  154. if(is.eof())
  155. return false;
  156. // NOTE: This function will be expanded to allow multi-line settings
  157. std::string line;
  158. std::getline(is, line);
  159. std::string trimmedline = trim(line);
  160. std::string line_end = "";
  161. if(is.eof() == false)
  162. line_end = "\n";
  163. // Ignore comments
  164. if(trimmedline[0] == '#')
  165. {
  166. dst.push_back(line+line_end);
  167. return true;
  168. }
  169. Strfnd sf(trim(line));
  170. std::string name = sf.next("=");
  171. name = trim(name);
  172. if(name == "")
  173. {
  174. dst.push_back(line+line_end);
  175. return true;
  176. }
  177. std::string value = sf.next("\n");
  178. value = trim(value);
  179. if(m_settings.find(name))
  180. {
  181. std::string newvalue = m_settings[name];
  182. if(newvalue != value)
  183. {
  184. infostream<<"Changing value of \""<<name<<"\" = \""
  185. <<value<<"\" -> \""<<newvalue<<"\""
  186. <<std::endl;
  187. }
  188. dst.push_back(name + " = " + newvalue + line_end);
  189. updated[name] = true;
  190. }
  191. return true;
  192. }
  193. /*
  194. Updates configuration file
  195. Returns true on success
  196. */
  197. bool updateConfigFile(const char *filename)
  198. {
  199. infostream<<"Updating configuration file: \""
  200. <<filename<<"\""<<std::endl;
  201. core::list<std::string> objects;
  202. core::map<std::string, bool> updated;
  203. // Read and modify stuff
  204. {
  205. std::ifstream is(filename);
  206. if(is.good() == false)
  207. {
  208. infostream<<"updateConfigFile():"
  209. " Error opening configuration file"
  210. " for reading: \""
  211. <<filename<<"\""<<std::endl;
  212. }
  213. else
  214. {
  215. while(getUpdatedConfigObject(is, objects, updated));
  216. }
  217. }
  218. JMutexAutoLock lock(m_mutex);
  219. // Write stuff back
  220. {
  221. std::ofstream os(filename);
  222. if(os.good() == false)
  223. {
  224. errorstream<<"Error opening configuration file"
  225. " for writing: \""
  226. <<filename<<"\""<<std::endl;
  227. return false;
  228. }
  229. /*
  230. Write updated stuff
  231. */
  232. for(core::list<std::string>::Iterator
  233. i = objects.begin();
  234. i != objects.end(); i++)
  235. {
  236. os<<(*i);
  237. }
  238. /*
  239. Write stuff that was not already in the file
  240. */
  241. for(core::map<std::string, std::string>::Iterator
  242. i = m_settings.getIterator();
  243. i.atEnd() == false; i++)
  244. {
  245. if(updated.find(i.getNode()->getKey()))
  246. continue;
  247. std::string name = i.getNode()->getKey();
  248. std::string value = i.getNode()->getValue();
  249. infostream<<"Adding \""<<name<<"\" = \""<<value<<"\""
  250. <<std::endl;
  251. os<<name<<" = "<<value<<"\n";
  252. }
  253. }
  254. return true;
  255. }
  256. /*
  257. NOTE: Types of allowed_options are ignored
  258. returns true on success
  259. */
  260. bool parseCommandLine(int argc, char *argv[],
  261. core::map<std::string, ValueSpec> &allowed_options)
  262. {
  263. int i=1;
  264. for(;;)
  265. {
  266. if(i >= argc)
  267. break;
  268. std::string argname = argv[i];
  269. if(argname.substr(0, 2) != "--")
  270. {
  271. errorstream<<"Invalid command-line parameter \""
  272. <<argname<<"\": --<option> expected."<<std::endl;
  273. return false;
  274. }
  275. i++;
  276. std::string name = argname.substr(2);
  277. core::map<std::string, ValueSpec>::Node *n;
  278. n = allowed_options.find(name);
  279. if(n == NULL)
  280. {
  281. errorstream<<"Unknown command-line parameter \""
  282. <<argname<<"\""<<std::endl;
  283. return false;
  284. }
  285. ValueType type = n->getValue().type;
  286. std::string value = "";
  287. if(type == VALUETYPE_FLAG)
  288. {
  289. value = "true";
  290. }
  291. else
  292. {
  293. if(i >= argc)
  294. {
  295. errorstream<<"Invalid command-line parameter \""
  296. <<name<<"\": missing value"<<std::endl;
  297. return false;
  298. }
  299. value = argv[i];
  300. i++;
  301. }
  302. infostream<<"Valid command-line parameter: \""
  303. <<name<<"\" = \""<<value<<"\""
  304. <<std::endl;
  305. set(name, value);
  306. }
  307. return true;
  308. }
  309. virtual void set(std::string name, std::string value)
  310. {
  311. JMutexAutoLock lock(m_mutex);
  312. m_settings[name] = value;
  313. }
  314. virtual void set(std::string name, const char *value)
  315. {
  316. JMutexAutoLock lock(m_mutex);
  317. m_settings[name] = value;
  318. }
  319. void setDefault(std::string name, std::string value)
  320. {
  321. JMutexAutoLock lock(m_mutex);
  322. m_defaults[name] = value;
  323. }
  324. bool exists(std::string name)
  325. {
  326. JMutexAutoLock lock(m_mutex);
  327. return (m_settings.find(name) || m_defaults.find(name));
  328. }
  329. std::string get(std::string name)
  330. {
  331. JMutexAutoLock lock(m_mutex);
  332. core::map<std::string, std::string>::Node *n;
  333. n = m_settings.find(name);
  334. if(n == NULL)
  335. {
  336. n = m_defaults.find(name);
  337. if(n == NULL)
  338. {
  339. infostream<<"Settings: Setting not found: \""
  340. <<name<<"\""<<std::endl;
  341. throw SettingNotFoundException("Setting not found");
  342. }
  343. }
  344. return n->getValue();
  345. }
  346. bool getBool(std::string name)
  347. {
  348. return is_yes(get(name));
  349. }
  350. bool getFlag(std::string name)
  351. {
  352. try
  353. {
  354. return getBool(name);
  355. }
  356. catch(SettingNotFoundException &e)
  357. {
  358. return false;
  359. }
  360. }
  361. // Asks if empty
  362. bool getBoolAsk(std::string name, std::string question, bool def)
  363. {
  364. // If it is in settings
  365. if(exists(name))
  366. return getBool(name);
  367. std::string s;
  368. char templine[10];
  369. std::cout<<question<<" [y/N]: ";
  370. std::cin.getline(templine, 10);
  371. s = templine;
  372. if(s == "")
  373. return def;
  374. return is_yes(s);
  375. }
  376. float getFloat(std::string name)
  377. {
  378. return mystof(get(name));
  379. }
  380. u16 getU16(std::string name)
  381. {
  382. return mystoi(get(name), 0, 65535);
  383. }
  384. u16 getU16Ask(std::string name, std::string question, u16 def)
  385. {
  386. // If it is in settings
  387. if(exists(name))
  388. return getU16(name);
  389. std::string s;
  390. char templine[10];
  391. std::cout<<question<<" ["<<def<<"]: ";
  392. std::cin.getline(templine, 10);
  393. s = templine;
  394. if(s == "")
  395. return def;
  396. return mystoi(s, 0, 65535);
  397. }
  398. s16 getS16(std::string name)
  399. {
  400. return mystoi(get(name), -32768, 32767);
  401. }
  402. s32 getS32(std::string name)
  403. {
  404. return mystoi(get(name));
  405. }
  406. v3f getV3F(std::string name)
  407. {
  408. v3f value;
  409. Strfnd f(get(name));
  410. f.next("(");
  411. value.X = mystof(f.next(","));
  412. value.Y = mystof(f.next(","));
  413. value.Z = mystof(f.next(")"));
  414. return value;
  415. }
  416. v2f getV2F(std::string name)
  417. {
  418. v2f value;
  419. Strfnd f(get(name));
  420. f.next("(");
  421. value.X = mystof(f.next(","));
  422. value.Y = mystof(f.next(")"));
  423. return value;
  424. }
  425. uint64_t getU64(std::string name)
  426. {
  427. uint64_t value = 0;
  428. std::string s = get(name);
  429. std::istringstream ss(s);
  430. ss>>value;
  431. return value;
  432. }
  433. void setBool(std::string name, bool value)
  434. {
  435. if(value)
  436. set(name, "true");
  437. else
  438. set(name, "false");
  439. }
  440. void setS32(std::string name, s32 value)
  441. {
  442. set(name, itos(value));
  443. }
  444. void setFloat(std::string name, float value)
  445. {
  446. set(name, ftos(value));
  447. }
  448. void setV3F(std::string name, v3f value)
  449. {
  450. std::ostringstream os;
  451. os<<"("<<value.X<<","<<value.Y<<","<<value.Z<<")";
  452. set(name, os.str());
  453. }
  454. void setV2F(std::string name, v2f value)
  455. {
  456. std::ostringstream os;
  457. os<<"("<<value.X<<","<<value.Y<<")";
  458. set(name, os.str());
  459. }
  460. void setU64(std::string name, uint64_t value)
  461. {
  462. std::ostringstream os;
  463. os<<value;
  464. set(name, os.str());
  465. }
  466. void clear()
  467. {
  468. JMutexAutoLock lock(m_mutex);
  469. m_settings.clear();
  470. m_defaults.clear();
  471. }
  472. void updateValue(Settings &other, const std::string &name)
  473. {
  474. JMutexAutoLock lock(m_mutex);
  475. if(&other == this)
  476. return;
  477. try{
  478. std::string val = other.get(name);
  479. m_settings[name] = val;
  480. } catch(SettingNotFoundException &e){
  481. }
  482. return;
  483. }
  484. void update(Settings &other)
  485. {
  486. JMutexAutoLock lock(m_mutex);
  487. JMutexAutoLock lock2(other.m_mutex);
  488. if(&other == this)
  489. return;
  490. for(core::map<std::string, std::string>::Iterator
  491. i = other.m_settings.getIterator();
  492. i.atEnd() == false; i++)
  493. {
  494. m_settings[i.getNode()->getKey()] = i.getNode()->getValue();
  495. }
  496. for(core::map<std::string, std::string>::Iterator
  497. i = other.m_defaults.getIterator();
  498. i.atEnd() == false; i++)
  499. {
  500. m_defaults[i.getNode()->getKey()] = i.getNode()->getValue();
  501. }
  502. return;
  503. }
  504. Settings & operator+=(Settings &other)
  505. {
  506. JMutexAutoLock lock(m_mutex);
  507. JMutexAutoLock lock2(other.m_mutex);
  508. if(&other == this)
  509. return *this;
  510. for(core::map<std::string, std::string>::Iterator
  511. i = other.m_settings.getIterator();
  512. i.atEnd() == false; i++)
  513. {
  514. m_settings.insert(i.getNode()->getKey(),
  515. i.getNode()->getValue());
  516. }
  517. for(core::map<std::string, std::string>::Iterator
  518. i = other.m_defaults.getIterator();
  519. i.atEnd() == false; i++)
  520. {
  521. m_defaults.insert(i.getNode()->getKey(),
  522. i.getNode()->getValue());
  523. }
  524. return *this;
  525. }
  526. Settings & operator=(Settings &other)
  527. {
  528. JMutexAutoLock lock(m_mutex);
  529. JMutexAutoLock lock2(other.m_mutex);
  530. if(&other == this)
  531. return *this;
  532. clear();
  533. (*this) += other;
  534. return *this;
  535. }
  536. protected:
  537. core::map<std::string, std::string> m_settings;
  538. // All methods that access m_settings/m_defaults directly should lock this.
  539. JMutex m_mutex;
  540. private:
  541. core::map<std::string, std::string> m_defaults;
  542. };
  543. class GameSettings : public Settings
  544. {
  545. public:
  546. GameSettings()
  547. {
  548. Settings();
  549. }
  550. // you'll find this in defaultsettings.cpp
  551. void setGameDefaults(std::string mode);
  552. virtual void set(std::string name, std::string value)
  553. {
  554. {
  555. JMutexAutoLock lock(m_mutex);
  556. m_settings[name] = value;
  557. }
  558. if (name == "game_mode")
  559. setGameDefaults(value);
  560. }
  561. virtual void set(std::string name, const char *value)
  562. {
  563. {
  564. JMutexAutoLock lock(m_mutex);
  565. m_settings[name] = value;
  566. }
  567. if (name == "game_mode")
  568. setGameDefaults(value);
  569. }
  570. };
  571. #endif