config_file.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*************************************************************************/
  2. /* config_file.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. PackedStringArray ConfigFile::_get_sections() const {
  35. List<String> s;
  36. get_sections(&s);
  37. PackedStringArray 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. PackedStringArray ConfigFile::_get_section_keys(const String &p_section) const {
  46. List<String> s;
  47. get_section_keys(p_section, &s);
  48. PackedStringArray 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. values[p_section].erase(p_key);
  62. if (values[p_section].empty()) {
  63. values.erase(p_section);
  64. }
  65. } else {
  66. if (!values.has(p_section)) {
  67. values[p_section] = OrderedHashMap<String, Variant>();
  68. }
  69. values[p_section][p_key] = p_value;
  70. }
  71. }
  72. Variant ConfigFile::get_value(const String &p_section, const String &p_key, Variant p_default) const {
  73. if (!values.has(p_section) || !values[p_section].has(p_key)) {
  74. ERR_FAIL_COND_V_MSG(p_default.get_type() == Variant::NIL, Variant(),
  75. vformat("Couldn't find the given section \"%s\" and key \"%s\", and no default was given.", p_section, p_key));
  76. return p_default;
  77. }
  78. return values[p_section][p_key];
  79. }
  80. bool ConfigFile::has_section(const String &p_section) const {
  81. return values.has(p_section);
  82. }
  83. bool ConfigFile::has_section_key(const String &p_section, const String &p_key) const {
  84. if (!values.has(p_section))
  85. return false;
  86. return values[p_section].has(p_key);
  87. }
  88. void ConfigFile::get_sections(List<String> *r_sections) const {
  89. for (OrderedHashMap<String, OrderedHashMap<String, Variant>>::ConstElement E = values.front(); E; E = E.next()) {
  90. r_sections->push_back(E.key());
  91. }
  92. }
  93. void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const {
  94. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot get keys from nonexistent section \"%s\".", p_section));
  95. for (OrderedHashMap<String, Variant>::ConstElement E = values[p_section].front(); E; E = E.next()) {
  96. r_keys->push_back(E.key());
  97. }
  98. }
  99. void ConfigFile::erase_section(const String &p_section) {
  100. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase nonexistent section \"%s\".", p_section));
  101. values.erase(p_section);
  102. }
  103. void ConfigFile::erase_section_key(const String &p_section, const String &p_key) {
  104. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase key \"%s\" from nonexistent section \"%s\".", p_key, p_section));
  105. ERR_FAIL_COND_MSG(!values[p_section].has(p_key), vformat("Cannot erase nonexistent key \"%s\" from section \"%s\".", p_key, p_section));
  106. values[p_section].erase(p_key);
  107. }
  108. Error ConfigFile::save(const String &p_path) {
  109. Error err;
  110. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  111. if (err) {
  112. if (file)
  113. memdelete(file);
  114. return err;
  115. }
  116. return _internal_save(file);
  117. }
  118. Error ConfigFile::save_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
  119. Error err;
  120. FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  121. if (err)
  122. return err;
  123. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  124. err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256);
  125. if (err) {
  126. memdelete(fae);
  127. memdelete(f);
  128. return err;
  129. }
  130. return _internal_save(fae);
  131. }
  132. Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) {
  133. Error err;
  134. FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  135. if (err)
  136. return err;
  137. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  138. err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256);
  139. if (err) {
  140. memdelete(fae);
  141. memdelete(f);
  142. return err;
  143. }
  144. return _internal_save(fae);
  145. }
  146. Error ConfigFile::_internal_save(FileAccess *file) {
  147. for (OrderedHashMap<String, OrderedHashMap<String, Variant>>::Element E = values.front(); E; E = E.next()) {
  148. if (E != values.front())
  149. file->store_string("\n");
  150. file->store_string("[" + E.key() + "]\n\n");
  151. for (OrderedHashMap<String, Variant>::Element F = E.get().front(); F; F = F.next()) {
  152. String vstr;
  153. VariantWriter::write_to_string(F.get(), vstr);
  154. file->store_string(F.key() + "=" + vstr + "\n");
  155. }
  156. }
  157. memdelete(file);
  158. return OK;
  159. }
  160. Error ConfigFile::load(const String &p_path) {
  161. Error err;
  162. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  163. if (!f)
  164. return err;
  165. return _internal_load(p_path, f);
  166. }
  167. Error ConfigFile::load_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
  168. Error err;
  169. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  170. if (err)
  171. return err;
  172. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  173. err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ);
  174. if (err) {
  175. memdelete(fae);
  176. memdelete(f);
  177. return err;
  178. }
  179. return _internal_load(p_path, fae);
  180. }
  181. Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) {
  182. Error err;
  183. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  184. if (err)
  185. return err;
  186. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  187. err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ);
  188. if (err) {
  189. memdelete(fae);
  190. memdelete(f);
  191. return err;
  192. }
  193. return _internal_load(p_path, fae);
  194. }
  195. Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) {
  196. VariantParser::StreamFile stream;
  197. stream.f = f;
  198. Error err = _parse(p_path, &stream);
  199. memdelete(f);
  200. return err;
  201. }
  202. Error ConfigFile::parse(const String &p_data) {
  203. VariantParser::StreamString stream;
  204. stream.s = p_data;
  205. return _parse("<string>", &stream);
  206. }
  207. Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) {
  208. String assign;
  209. Variant value;
  210. VariantParser::Tag next_tag;
  211. int lines = 0;
  212. String error_text;
  213. String section;
  214. while (true) {
  215. assign = Variant();
  216. next_tag.fields.clear();
  217. next_tag.name = String();
  218. Error err = VariantParser::parse_tag_assign_eof(p_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  219. if (err == ERR_FILE_EOF) {
  220. return OK;
  221. } else if (err != OK) {
  222. ERR_PRINT(vformat("ConfigFile parse error at %s:%d: %s.", p_path, lines, error_text));
  223. return err;
  224. }
  225. if (assign != String()) {
  226. set_value(section, assign, value);
  227. } else if (next_tag.name != String()) {
  228. section = next_tag.name;
  229. }
  230. }
  231. return OK;
  232. }
  233. void ConfigFile::_bind_methods() {
  234. ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value);
  235. ClassDB::bind_method(D_METHOD("get_value", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant()));
  236. ClassDB::bind_method(D_METHOD("has_section", "section"), &ConfigFile::has_section);
  237. ClassDB::bind_method(D_METHOD("has_section_key", "section", "key"), &ConfigFile::has_section_key);
  238. ClassDB::bind_method(D_METHOD("get_sections"), &ConfigFile::_get_sections);
  239. ClassDB::bind_method(D_METHOD("get_section_keys", "section"), &ConfigFile::_get_section_keys);
  240. ClassDB::bind_method(D_METHOD("erase_section", "section"), &ConfigFile::erase_section);
  241. ClassDB::bind_method(D_METHOD("erase_section_key", "section", "key"), &ConfigFile::erase_section_key);
  242. ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load);
  243. ClassDB::bind_method(D_METHOD("parse", "data"), &ConfigFile::parse);
  244. ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save);
  245. ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted);
  246. ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "password"), &ConfigFile::load_encrypted_pass);
  247. ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted);
  248. ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "password"), &ConfigFile::save_encrypted_pass);
  249. }