config_file.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. 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 String &E : s) {
  41. arr.set(idx++, E);
  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 String &E : s) {
  52. arr.set(idx++, E);
  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) { // Erase key.
  58. if (!values.has(p_section)) {
  59. return;
  60. }
  61. values[p_section].erase(p_key);
  62. if (values[p_section].is_empty()) {
  63. values.erase(p_section);
  64. }
  65. } else {
  66. if (!values.has(p_section)) {
  67. // Insert section-less keys at the beginning.
  68. values.insert(p_section, HashMap<String, Variant>(), p_section.is_empty());
  69. }
  70. values[p_section][p_key] = p_value;
  71. }
  72. }
  73. Variant ConfigFile::get_value(const String &p_section, const String &p_key, const 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 (const KeyValue<String, HashMap<String, Variant>> &E : values) {
  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 (const KeyValue<String, Variant> &E : values[p_section]) {
  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. if (values[p_section].is_empty()) {
  110. values.erase(p_section);
  111. }
  112. }
  113. String ConfigFile::encode_to_text() const {
  114. StringBuilder sb;
  115. bool first = true;
  116. for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
  117. if (first) {
  118. first = false;
  119. } else {
  120. sb.append("\n");
  121. }
  122. if (!E.key.is_empty()) {
  123. sb.append("[" + E.key + "]\n\n");
  124. }
  125. for (const KeyValue<String, Variant> &F : E.value) {
  126. String vstr;
  127. VariantWriter::write_to_string(F.value, vstr);
  128. sb.append(F.key.property_name_encode() + "=" + vstr + "\n");
  129. }
  130. }
  131. return sb.as_string();
  132. }
  133. Error ConfigFile::save(const String &p_path) {
  134. Error err;
  135. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  136. if (err) {
  137. return err;
  138. }
  139. return _internal_save(file);
  140. }
  141. Error ConfigFile::save_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
  142. Error err;
  143. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  144. if (err) {
  145. return err;
  146. }
  147. Ref<FileAccessEncrypted> fae;
  148. fae.instantiate();
  149. err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256);
  150. if (err) {
  151. return err;
  152. }
  153. return _internal_save(fae);
  154. }
  155. Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) {
  156. Error err;
  157. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  158. if (err) {
  159. return err;
  160. }
  161. Ref<FileAccessEncrypted> fae;
  162. fae.instantiate();
  163. err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256);
  164. if (err) {
  165. return err;
  166. }
  167. return _internal_save(fae);
  168. }
  169. Error ConfigFile::_internal_save(Ref<FileAccess> file) {
  170. bool first = true;
  171. for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
  172. if (first) {
  173. first = false;
  174. } else {
  175. file->store_string("\n");
  176. }
  177. if (!E.key.is_empty()) {
  178. file->store_string("[" + E.key.replace("]", "\\]") + "]\n\n");
  179. }
  180. for (const KeyValue<String, Variant> &F : E.value) {
  181. String vstr;
  182. VariantWriter::write_to_string(F.value, vstr);
  183. file->store_string(F.key.property_name_encode() + "=" + vstr + "\n");
  184. }
  185. }
  186. return OK;
  187. }
  188. Error ConfigFile::load(const String &p_path) {
  189. Error err;
  190. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  191. if (f.is_null()) {
  192. return err;
  193. }
  194. return _internal_load(p_path, f);
  195. }
  196. Error ConfigFile::load_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
  197. Error err;
  198. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  199. if (err) {
  200. return err;
  201. }
  202. Ref<FileAccessEncrypted> fae;
  203. fae.instantiate();
  204. err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ);
  205. if (err) {
  206. return err;
  207. }
  208. return _internal_load(p_path, fae);
  209. }
  210. Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) {
  211. Error err;
  212. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  213. if (err) {
  214. return err;
  215. }
  216. Ref<FileAccessEncrypted> fae;
  217. fae.instantiate();
  218. err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ);
  219. if (err) {
  220. return err;
  221. }
  222. return _internal_load(p_path, fae);
  223. }
  224. Error ConfigFile::_internal_load(const String &p_path, Ref<FileAccess> f) {
  225. VariantParser::StreamFile stream;
  226. stream.f = f;
  227. Error err = _parse(p_path, &stream);
  228. return err;
  229. }
  230. Error ConfigFile::parse(const String &p_data) {
  231. VariantParser::StreamString stream;
  232. stream.s = p_data;
  233. return _parse("<string>", &stream);
  234. }
  235. Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) {
  236. String assign;
  237. Variant value;
  238. VariantParser::Tag next_tag;
  239. int lines = 0;
  240. String error_text;
  241. String section;
  242. while (true) {
  243. assign = Variant();
  244. next_tag.fields.clear();
  245. next_tag.name = String();
  246. Error err = VariantParser::parse_tag_assign_eof(p_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  247. if (err == ERR_FILE_EOF) {
  248. return OK;
  249. } else if (err != OK) {
  250. ERR_PRINT(vformat("ConfigFile parse error at %s:%d: %s.", p_path, lines, error_text));
  251. return err;
  252. }
  253. if (!assign.is_empty()) {
  254. set_value(section, assign, value);
  255. } else if (!next_tag.name.is_empty()) {
  256. section = next_tag.name.replace("\\]", "]");
  257. }
  258. }
  259. return OK;
  260. }
  261. void ConfigFile::clear() {
  262. values.clear();
  263. }
  264. void ConfigFile::_bind_methods() {
  265. ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value);
  266. ClassDB::bind_method(D_METHOD("get_value", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant()));
  267. ClassDB::bind_method(D_METHOD("has_section", "section"), &ConfigFile::has_section);
  268. ClassDB::bind_method(D_METHOD("has_section_key", "section", "key"), &ConfigFile::has_section_key);
  269. ClassDB::bind_method(D_METHOD("get_sections"), &ConfigFile::_get_sections);
  270. ClassDB::bind_method(D_METHOD("get_section_keys", "section"), &ConfigFile::_get_section_keys);
  271. ClassDB::bind_method(D_METHOD("erase_section", "section"), &ConfigFile::erase_section);
  272. ClassDB::bind_method(D_METHOD("erase_section_key", "section", "key"), &ConfigFile::erase_section_key);
  273. ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load);
  274. ClassDB::bind_method(D_METHOD("parse", "data"), &ConfigFile::parse);
  275. ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save);
  276. ClassDB::bind_method(D_METHOD("encode_to_text"), &ConfigFile::encode_to_text);
  277. BIND_METHOD_ERR_RETURN_DOC("load", ERR_FILE_CANT_OPEN);
  278. ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted);
  279. ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "password"), &ConfigFile::load_encrypted_pass);
  280. ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted);
  281. ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "password"), &ConfigFile::save_encrypted_pass);
  282. ClassDB::bind_method(D_METHOD("clear"), &ConfigFile::clear);
  283. }