config_file.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**************************************************************************/
  2. /* config_file.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "config_file.h"
  31. #include "core/io/file_access_encrypted.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/variant_parser.h"
  34. PoolStringArray ConfigFile::_get_sections() const {
  35. List<String> s;
  36. get_sections(&s);
  37. PoolStringArray arr;
  38. arr.resize(s.size());
  39. int idx = 0;
  40. for (const List<String>::Element *E = s.front(); E; E = E->next()) {
  41. arr.set(idx++, E->get());
  42. }
  43. return arr;
  44. }
  45. PoolStringArray ConfigFile::_get_section_keys(const String &p_section) const {
  46. List<String> s;
  47. get_section_keys(p_section, &s);
  48. PoolStringArray arr;
  49. arr.resize(s.size());
  50. int idx = 0;
  51. for (const List<String>::Element *E = s.front(); E; E = E->next()) {
  52. arr.set(idx++, E->get());
  53. }
  54. return arr;
  55. }
  56. void ConfigFile::set_value(const String &p_section, const String &p_key, const Variant &p_value) {
  57. if (p_value.get_type() == Variant::NIL) {
  58. //erase
  59. if (!values.has(p_section)) {
  60. return; // ?
  61. }
  62. values[p_section].erase(p_key);
  63. if (values[p_section].empty()) {
  64. values.erase(p_section);
  65. }
  66. } else {
  67. if (!values.has(p_section)) {
  68. values[p_section] = OrderedHashMap<String, Variant>();
  69. }
  70. values[p_section][p_key] = p_value;
  71. }
  72. }
  73. Variant ConfigFile::get_value(const String &p_section, const String &p_key, Variant p_default) const {
  74. if (!values.has(p_section) || !values[p_section].has(p_key)) {
  75. ERR_FAIL_COND_V_MSG(p_default.get_type() == Variant::NIL, Variant(),
  76. vformat("Couldn't find the given section \"%s\" and key \"%s\", and no default was given.", p_section, p_key));
  77. return p_default;
  78. }
  79. return values[p_section][p_key];
  80. }
  81. bool ConfigFile::has_section(const String &p_section) const {
  82. return values.has(p_section);
  83. }
  84. bool ConfigFile::has_section_key(const String &p_section, const String &p_key) const {
  85. if (!values.has(p_section)) {
  86. return false;
  87. }
  88. return values[p_section].has(p_key);
  89. }
  90. void ConfigFile::get_sections(List<String> *r_sections) const {
  91. for (OrderedHashMap<String, OrderedHashMap<String, Variant>>::ConstElement E = values.front(); E; E = E.next()) {
  92. r_sections->push_back(E.key());
  93. }
  94. }
  95. void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const {
  96. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot get keys from nonexistent section \"%s\".", p_section));
  97. for (OrderedHashMap<String, Variant>::ConstElement E = values[p_section].front(); E; E = E.next()) {
  98. r_keys->push_back(E.key());
  99. }
  100. }
  101. void ConfigFile::erase_section(const String &p_section) {
  102. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase nonexistent section \"%s\".", p_section));
  103. values.erase(p_section);
  104. }
  105. void ConfigFile::erase_section_key(const String &p_section, const String &p_key) {
  106. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase key \"%s\" from nonexistent section \"%s\".", p_key, p_section));
  107. ERR_FAIL_COND_MSG(!values[p_section].has(p_key), vformat("Cannot erase nonexistent key \"%s\" from section \"%s\".", p_key, p_section));
  108. values[p_section].erase(p_key);
  109. }
  110. Error ConfigFile::save(const String &p_path) {
  111. Error err;
  112. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  113. if (err) {
  114. if (file) {
  115. memdelete(file);
  116. }
  117. return err;
  118. }
  119. return _internal_save(file);
  120. }
  121. Error ConfigFile::save_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
  122. Error err;
  123. FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  124. if (err) {
  125. return err;
  126. }
  127. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  128. err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256);
  129. if (err) {
  130. memdelete(fae);
  131. memdelete(f);
  132. return err;
  133. }
  134. return _internal_save(fae);
  135. }
  136. Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) {
  137. Error err;
  138. FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  139. if (err) {
  140. return err;
  141. }
  142. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  143. err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256);
  144. if (err) {
  145. memdelete(fae);
  146. memdelete(f);
  147. return err;
  148. }
  149. return _internal_save(fae);
  150. }
  151. Error ConfigFile::_internal_save(FileAccess *file) {
  152. for (OrderedHashMap<String, OrderedHashMap<String, Variant>>::Element E = values.front(); E; E = E.next()) {
  153. if (E != values.front()) {
  154. file->store_string("\n");
  155. }
  156. file->store_string("[" + E.key() + "]\n\n");
  157. for (OrderedHashMap<String, Variant>::Element F = E.get().front(); F; F = F.next()) {
  158. String vstr;
  159. VariantWriter::write_to_string(F.get(), vstr);
  160. file->store_string(F.key().property_name_encode() + "=" + vstr + "\n");
  161. }
  162. }
  163. memdelete(file);
  164. return OK;
  165. }
  166. Error ConfigFile::load(const String &p_path) {
  167. Error err;
  168. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  169. if (!f) {
  170. return err;
  171. }
  172. return _internal_load(p_path, f);
  173. }
  174. Error ConfigFile::load_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
  175. Error err;
  176. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  177. if (err) {
  178. return err;
  179. }
  180. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  181. err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ);
  182. if (err) {
  183. memdelete(fae);
  184. memdelete(f);
  185. return err;
  186. }
  187. return _internal_load(p_path, fae);
  188. }
  189. Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) {
  190. Error err;
  191. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  192. if (err) {
  193. return err;
  194. }
  195. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  196. err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ);
  197. if (err) {
  198. memdelete(fae);
  199. memdelete(f);
  200. return err;
  201. }
  202. return _internal_load(p_path, fae);
  203. }
  204. Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) {
  205. VariantParser::StreamFile stream;
  206. stream.f = f;
  207. Error err = _parse(p_path, &stream);
  208. memdelete(f);
  209. return err;
  210. }
  211. Error ConfigFile::parse(const String &p_data) {
  212. VariantParser::StreamString stream;
  213. stream.s = p_data;
  214. return _parse("<string>", &stream);
  215. }
  216. Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) {
  217. String assign;
  218. Variant value;
  219. VariantParser::Tag next_tag;
  220. int lines = 0;
  221. String error_text;
  222. String section;
  223. while (true) {
  224. assign = Variant();
  225. next_tag.fields.clear();
  226. next_tag.name = String();
  227. Error err = VariantParser::parse_tag_assign_eof(p_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  228. if (err == ERR_FILE_EOF) {
  229. return OK;
  230. } else if (err != OK) {
  231. ERR_PRINT(vformat("ConfigFile parse error at %s:%d: %s.", p_path, lines, error_text));
  232. return err;
  233. }
  234. if (assign != String()) {
  235. set_value(section, assign, value);
  236. } else if (next_tag.name != String()) {
  237. section = next_tag.name;
  238. }
  239. }
  240. return OK;
  241. }
  242. void ConfigFile::clear() {
  243. values.clear();
  244. }
  245. void ConfigFile::_bind_methods() {
  246. ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value);
  247. ClassDB::bind_method(D_METHOD("get_value", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant()));
  248. ClassDB::bind_method(D_METHOD("has_section", "section"), &ConfigFile::has_section);
  249. ClassDB::bind_method(D_METHOD("has_section_key", "section", "key"), &ConfigFile::has_section_key);
  250. ClassDB::bind_method(D_METHOD("get_sections"), &ConfigFile::_get_sections);
  251. ClassDB::bind_method(D_METHOD("get_section_keys", "section"), &ConfigFile::_get_section_keys);
  252. ClassDB::bind_method(D_METHOD("erase_section", "section"), &ConfigFile::erase_section);
  253. ClassDB::bind_method(D_METHOD("erase_section_key", "section", "key"), &ConfigFile::erase_section_key);
  254. ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load);
  255. ClassDB::bind_method(D_METHOD("parse", "data"), &ConfigFile::parse);
  256. ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save);
  257. ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted);
  258. ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "password"), &ConfigFile::load_encrypted_pass);
  259. ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted);
  260. ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "password"), &ConfigFile::save_encrypted_pass);
  261. ClassDB::bind_method(D_METHOD("clear"), &ConfigFile::clear);
  262. }