config_file.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/string/string_builder.h"
  34. #include "core/variant/variant_parser.h"
  35. PackedStringArray ConfigFile::_get_sections() const {
  36. List<String> s;
  37. get_sections(&s);
  38. PackedStringArray arr;
  39. arr.resize(s.size());
  40. int idx = 0;
  41. for (const String &E : s) {
  42. arr.set(idx++, E);
  43. }
  44. return arr;
  45. }
  46. PackedStringArray ConfigFile::_get_section_keys(const String &p_section) const {
  47. List<String> s;
  48. get_section_keys(p_section, &s);
  49. PackedStringArray arr;
  50. arr.resize(s.size());
  51. int idx = 0;
  52. for (const String &E : s) {
  53. arr.set(idx++, E);
  54. }
  55. return arr;
  56. }
  57. void ConfigFile::set_value(const String &p_section, const String &p_key, const Variant &p_value) {
  58. if (p_value.get_type() == Variant::NIL) { // Erase key.
  59. if (!values.has(p_section)) {
  60. return;
  61. }
  62. values[p_section].erase(p_key);
  63. if (values[p_section].is_empty()) {
  64. values.erase(p_section);
  65. }
  66. } else {
  67. if (!values.has(p_section)) {
  68. // Insert section-less keys at the beginning.
  69. values.insert(p_section, HashMap<String, Variant>(), p_section.is_empty());
  70. }
  71. values[p_section][p_key] = p_value;
  72. }
  73. }
  74. Variant ConfigFile::get_value(const String &p_section, const String &p_key, const Variant &p_default) const {
  75. if (!values.has(p_section) || !values[p_section].has(p_key)) {
  76. ERR_FAIL_COND_V_MSG(p_default.get_type() == Variant::NIL, Variant(),
  77. vformat("Couldn't find the given section \"%s\" and key \"%s\", and no default was given.", p_section, p_key));
  78. return p_default;
  79. }
  80. return values[p_section][p_key];
  81. }
  82. bool ConfigFile::has_section(const String &p_section) const {
  83. return values.has(p_section);
  84. }
  85. bool ConfigFile::has_section_key(const String &p_section, const String &p_key) const {
  86. if (!values.has(p_section)) {
  87. return false;
  88. }
  89. return values[p_section].has(p_key);
  90. }
  91. void ConfigFile::get_sections(List<String> *r_sections) const {
  92. for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
  93. r_sections->push_back(E.key);
  94. }
  95. }
  96. void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const {
  97. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot get keys from nonexistent section \"%s\".", p_section));
  98. for (const KeyValue<String, Variant> &E : values[p_section]) {
  99. r_keys->push_back(E.key);
  100. }
  101. }
  102. void ConfigFile::erase_section(const String &p_section) {
  103. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase nonexistent section \"%s\".", p_section));
  104. values.erase(p_section);
  105. }
  106. void ConfigFile::erase_section_key(const String &p_section, const String &p_key) {
  107. ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase key \"%s\" from nonexistent section \"%s\".", p_key, p_section));
  108. ERR_FAIL_COND_MSG(!values[p_section].has(p_key), vformat("Cannot erase nonexistent key \"%s\" from section \"%s\".", p_key, p_section));
  109. values[p_section].erase(p_key);
  110. if (values[p_section].is_empty()) {
  111. values.erase(p_section);
  112. }
  113. }
  114. String ConfigFile::encode_to_text() const {
  115. StringBuilder sb;
  116. bool first = true;
  117. for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
  118. if (first) {
  119. first = false;
  120. } else {
  121. sb.append("\n");
  122. }
  123. if (!E.key.is_empty()) {
  124. sb.append("[" + E.key + "]\n\n");
  125. }
  126. for (const KeyValue<String, Variant> &F : E.value) {
  127. String vstr;
  128. VariantWriter::write_to_string(F.value, vstr);
  129. sb.append(F.key.property_name_encode() + "=" + vstr + "\n");
  130. }
  131. }
  132. return sb.as_string();
  133. }
  134. Error ConfigFile::save(const String &p_path) {
  135. Error err;
  136. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  137. if (err) {
  138. return err;
  139. }
  140. return _internal_save(file);
  141. }
  142. Error ConfigFile::save_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
  143. Error err;
  144. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  145. if (err) {
  146. return err;
  147. }
  148. Ref<FileAccessEncrypted> fae;
  149. fae.instantiate();
  150. err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256);
  151. if (err) {
  152. return err;
  153. }
  154. return _internal_save(fae);
  155. }
  156. Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) {
  157. Error err;
  158. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  159. if (err) {
  160. return err;
  161. }
  162. Ref<FileAccessEncrypted> fae;
  163. fae.instantiate();
  164. err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256);
  165. if (err) {
  166. return err;
  167. }
  168. return _internal_save(fae);
  169. }
  170. Error ConfigFile::_internal_save(Ref<FileAccess> file) {
  171. bool first = true;
  172. for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
  173. if (first) {
  174. first = false;
  175. } else {
  176. file->store_string("\n");
  177. }
  178. if (!E.key.is_empty()) {
  179. file->store_string("[" + E.key.replace("]", "\\]") + "]\n\n");
  180. }
  181. for (const KeyValue<String, Variant> &F : E.value) {
  182. String vstr;
  183. VariantWriter::write_to_string(F.value, vstr);
  184. file->store_string(F.key.property_name_encode() + "=" + vstr + "\n");
  185. }
  186. }
  187. return OK;
  188. }
  189. Error ConfigFile::load(const String &p_path) {
  190. Error err;
  191. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  192. if (f.is_null()) {
  193. return err;
  194. }
  195. return _internal_load(p_path, f);
  196. }
  197. Error ConfigFile::load_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
  198. Error err;
  199. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  200. if (err) {
  201. return err;
  202. }
  203. Ref<FileAccessEncrypted> fae;
  204. fae.instantiate();
  205. err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ);
  206. if (err) {
  207. return err;
  208. }
  209. return _internal_load(p_path, fae);
  210. }
  211. Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) {
  212. Error err;
  213. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  214. if (err) {
  215. return err;
  216. }
  217. Ref<FileAccessEncrypted> fae;
  218. fae.instantiate();
  219. err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ);
  220. if (err) {
  221. return err;
  222. }
  223. return _internal_load(p_path, fae);
  224. }
  225. Error ConfigFile::_internal_load(const String &p_path, Ref<FileAccess> f) {
  226. VariantParser::StreamFile stream;
  227. stream.f = f;
  228. Error err = _parse(p_path, &stream);
  229. return err;
  230. }
  231. Error ConfigFile::parse(const String &p_data) {
  232. VariantParser::StreamString stream;
  233. stream.s = p_data;
  234. return _parse("<string>", &stream);
  235. }
  236. Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) {
  237. String assign;
  238. Variant value;
  239. VariantParser::Tag next_tag;
  240. int lines = 0;
  241. String error_text;
  242. String section;
  243. while (true) {
  244. assign = Variant();
  245. next_tag.fields.clear();
  246. next_tag.name = String();
  247. Error err = VariantParser::parse_tag_assign_eof(p_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  248. if (err == ERR_FILE_EOF) {
  249. return OK;
  250. } else if (err != OK) {
  251. ERR_PRINT(vformat("ConfigFile parse error at %s:%d: %s.", p_path, lines, error_text));
  252. return err;
  253. }
  254. if (!assign.is_empty()) {
  255. set_value(section, assign, value);
  256. } else if (!next_tag.name.is_empty()) {
  257. section = next_tag.name.replace("\\]", "]");
  258. }
  259. }
  260. return OK;
  261. }
  262. void ConfigFile::clear() {
  263. values.clear();
  264. }
  265. void ConfigFile::_bind_methods() {
  266. ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value);
  267. ClassDB::bind_method(D_METHOD("get_value", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant()));
  268. ClassDB::bind_method(D_METHOD("has_section", "section"), &ConfigFile::has_section);
  269. ClassDB::bind_method(D_METHOD("has_section_key", "section", "key"), &ConfigFile::has_section_key);
  270. ClassDB::bind_method(D_METHOD("get_sections"), &ConfigFile::_get_sections);
  271. ClassDB::bind_method(D_METHOD("get_section_keys", "section"), &ConfigFile::_get_section_keys);
  272. ClassDB::bind_method(D_METHOD("erase_section", "section"), &ConfigFile::erase_section);
  273. ClassDB::bind_method(D_METHOD("erase_section_key", "section", "key"), &ConfigFile::erase_section_key);
  274. ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load);
  275. ClassDB::bind_method(D_METHOD("parse", "data"), &ConfigFile::parse);
  276. ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save);
  277. ClassDB::bind_method(D_METHOD("encode_to_text"), &ConfigFile::encode_to_text);
  278. BIND_METHOD_ERR_RETURN_DOC("load", ERR_FILE_CANT_OPEN);
  279. ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted);
  280. ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "password"), &ConfigFile::load_encrypted_pass);
  281. ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted);
  282. ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "password"), &ConfigFile::save_encrypted_pass);
  283. ClassDB::bind_method(D_METHOD("clear"), &ConfigFile::clear);
  284. }