config_file.cpp 9.2 KB

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