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