config_file.cpp 11 KB

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