Skip to content

Commit f8a8d6c

Browse files
committed
Merge pull request #105700 from dementive/config-file-get-improvement
Improve ConfigFile get_sections and get_section_keys by returning Vector<String>
2 parents 74fc4da + b8e44a0 commit f8a8d6c

20 files changed

+58
-105
lines changed

core/config/project_settings.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,8 +1410,7 @@ void ProjectSettings::load_scene_groups_cache() {
14101410
Ref<ConfigFile> cf;
14111411
cf.instantiate();
14121412
if (cf->load(get_scene_groups_cache_path()) == OK) {
1413-
List<String> scene_paths;
1414-
cf->get_sections(&scene_paths);
1413+
Vector<String> scene_paths = cf->get_sections();
14151414
for (const String &E : scene_paths) {
14161415
Array scene_groups = cf->get_value(E, "groups", Array());
14171416
HashSet<StringName> cache;

core/extension/gdextension_library_loader.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
Vector<SharedObject> GDExtensionLibraryLoader::find_extension_dependencies(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature) {
3939
Vector<SharedObject> dependencies_shared_objects;
4040
if (p_config->has_section("dependencies")) {
41-
List<String> config_dependencies;
42-
p_config->get_section_keys("dependencies", &config_dependencies);
41+
Vector<String> config_dependencies = p_config->get_section_keys("dependencies");
4342

4443
for (const String &dependency : config_dependencies) {
4544
Vector<String> dependency_tags = dependency.split(".");
@@ -73,8 +72,7 @@ Vector<SharedObject> GDExtensionLibraryLoader::find_extension_dependencies(const
7372
String GDExtensionLibraryLoader::find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags) {
7473
// First, check the explicit libraries.
7574
if (p_config->has_section("libraries")) {
76-
List<String> libraries;
77-
p_config->get_section_keys("libraries", &libraries);
75+
Vector<String> libraries = p_config->get_section_keys("libraries");
7876

7977
// Iterate the libraries, finding the best matching tags.
8078
String best_library_path;
@@ -378,8 +376,7 @@ Error GDExtensionLibraryLoader::parse_gdextension_file(const String &p_path) {
378376

379377
// Handle icons if any are specified.
380378
if (config->has_section("icons")) {
381-
List<String> keys;
382-
config->get_section_keys("icons", &keys);
379+
Vector<String> keys = config->get_section_keys("icons");
383380
for (const String &key : keys) {
384381
String icon_path = config->get_value("icons", key);
385382
if (icon_path.is_relative_path()) {

core/io/config_file.cpp

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,6 @@
3434
#include "core/string/string_builder.h"
3535
#include "core/variant/variant_parser.h"
3636

37-
PackedStringArray ConfigFile::_get_sections() const {
38-
List<String> s;
39-
get_sections(&s);
40-
PackedStringArray arr;
41-
arr.resize(s.size());
42-
int idx = 0;
43-
for (const String &E : s) {
44-
arr.set(idx++, E);
45-
}
46-
47-
return arr;
48-
}
49-
50-
PackedStringArray ConfigFile::_get_section_keys(const String &p_section) const {
51-
List<String> s;
52-
get_section_keys(p_section, &s);
53-
PackedStringArray arr;
54-
arr.resize(s.size());
55-
int idx = 0;
56-
for (const String &E : s) {
57-
arr.set(idx++, E);
58-
}
59-
60-
return arr;
61-
}
62-
6337
void ConfigFile::set_value(const String &p_section, const String &p_key, const Variant &p_value) {
6438
if (p_value.get_type() == Variant::NIL) { // Erase key.
6539
if (!values.has(p_section)) {
@@ -101,18 +75,33 @@ bool ConfigFile::has_section_key(const String &p_section, const String &p_key) c
10175
return values[p_section].has(p_key);
10276
}
10377

104-
void ConfigFile::get_sections(List<String> *r_sections) const {
78+
Vector<String> ConfigFile::get_sections() const {
79+
Vector<String> sections;
80+
sections.resize(values.size());
81+
82+
int i = 0;
83+
String *sections_write = sections.ptrw();
10584
for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
106-
r_sections->push_back(E.key);
85+
sections_write[i++] = E.key;
10786
}
87+
88+
return sections;
10889
}
10990

110-
void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const {
111-
ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot get keys from nonexistent section \"%s\".", p_section));
91+
Vector<String> ConfigFile::get_section_keys(const String &p_section) const {
92+
Vector<String> keys;
93+
ERR_FAIL_COND_V_MSG(!values.has(p_section), keys, vformat("Cannot get keys from nonexistent section \"%s\".", p_section));
94+
95+
const HashMap<String, Variant> &keys_map = values[p_section];
96+
keys.resize(keys_map.size());
11297

113-
for (const KeyValue<String, Variant> &E : values[p_section]) {
114-
r_keys->push_back(E.key);
98+
int i = 0;
99+
String *keys_write = keys.ptrw();
100+
for (const KeyValue<String, Variant> &E : keys_map) {
101+
keys_write[i++] = E.key;
115102
}
103+
104+
return keys;
116105
}
117106

118107
void ConfigFile::erase_section(const String &p_section) {
@@ -325,8 +314,8 @@ void ConfigFile::_bind_methods() {
325314
ClassDB::bind_method(D_METHOD("has_section", "section"), &ConfigFile::has_section);
326315
ClassDB::bind_method(D_METHOD("has_section_key", "section", "key"), &ConfigFile::has_section_key);
327316

328-
ClassDB::bind_method(D_METHOD("get_sections"), &ConfigFile::_get_sections);
329-
ClassDB::bind_method(D_METHOD("get_section_keys", "section"), &ConfigFile::_get_section_keys);
317+
ClassDB::bind_method(D_METHOD("get_sections"), &ConfigFile::get_sections);
318+
ClassDB::bind_method(D_METHOD("get_section_keys", "section"), &ConfigFile::get_section_keys);
330319

331320
ClassDB::bind_method(D_METHOD("erase_section", "section"), &ConfigFile::erase_section);
332321
ClassDB::bind_method(D_METHOD("erase_section_key", "section", "key"), &ConfigFile::erase_section_key);

core/io/config_file.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ class ConfigFile : public RefCounted {
4040

4141
HashMap<String, HashMap<String, Variant>> values;
4242

43-
PackedStringArray _get_sections() const;
44-
PackedStringArray _get_section_keys(const String &p_section) const;
4543
Error _internal_load(const String &p_path, Ref<FileAccess> f);
4644
Error _internal_save(Ref<FileAccess> file);
4745

@@ -57,8 +55,8 @@ class ConfigFile : public RefCounted {
5755
bool has_section(const String &p_section) const;
5856
bool has_section_key(const String &p_section, const String &p_key) const;
5957

60-
void get_sections(List<String> *r_sections) const;
61-
void get_section_keys(const String &p_section, List<String> *r_keys) const;
58+
Vector<String> get_sections() const;
59+
Vector<String> get_section_keys(const String &p_section) const;
6260

6361
void erase_section(const String &p_section);
6462
void erase_section_key(const String &p_section, const String &p_key);

editor/debugger/editor_file_server.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ void EditorFileServer::_scan_files_changed(EditorFileSystemDirectory *efd, const
6969
continue;
7070
}
7171

72-
List<String> remaps;
73-
cf->get_section_keys("remap", &remaps);
72+
Vector<String> remaps = cf->get_section_keys("remap");
7473

7574
for (const String &remap : remaps) {
7675
if (remap == "path") {

editor/editor_file_system.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,8 +2585,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
25852585
}
25862586

25872587
if (config->has_section("params")) {
2588-
List<String> sk;
2589-
config->get_section_keys("params", &sk);
2588+
Vector<String> sk = config->get_section_keys("params");
25902589
for (const String &param : sk) {
25912590
Variant value = config->get_value("params", param);
25922591
//override with whatever is in file
@@ -2769,8 +2768,7 @@ Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<Strin
27692768
Error err = cf->load(p_file + ".import");
27702769
if (err == OK) {
27712770
if (cf->has_section("params")) {
2772-
List<String> sk;
2773-
cf->get_section_keys("params", &sk);
2771+
Vector<String> sk = cf->get_section_keys("params");
27742772
for (const String &E : sk) {
27752773
if (!params.has(E)) {
27762774
params[E] = cf->get_value("params", E);
@@ -3408,8 +3406,7 @@ void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const S
34083406
config->set_value("remap", "group_file", p_new_location);
34093407
}
34103408

3411-
List<String> sk;
3412-
config->get_section_keys("params", &sk);
3409+
Vector<String> sk = config->get_section_keys("params");
34133410
for (const String &param : sk) {
34143411
//not very clean, but should work
34153412
String value = config->get_value("params", param);

editor/editor_layouts_dialog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ void EditorLayoutsDialog::_post_popup() {
9595
return;
9696
}
9797

98-
List<String> layouts;
99-
config.ptr()->get_sections(&layouts);
98+
Vector<String> layouts = config->get_sections();
10099

101100
for (const String &E : layouts) {
102101
layout_names->add_item(E);

editor/editor_node.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,8 +1649,7 @@ void EditorNode::_load_editor_plugin_states_from_config(const Ref<ConfigFile> &p
16491649
return;
16501650
}
16511651

1652-
List<String> esl;
1653-
p_config_file->get_section_keys("editor_states", &esl);
1652+
Vector<String> esl = p_config_file->get_section_keys("editor_states");
16541653

16551654
Dictionary md;
16561655
for (const String &E : esl) {
@@ -2379,8 +2378,7 @@ void EditorNode::_dialog_action(String p_file) {
23792378
}
23802379

23812380
// Erase key values.
2382-
List<String> keys;
2383-
config->get_section_keys(p_file, &keys);
2381+
Vector<String> keys = config->get_section_keys(p_file);
23842382
for (const String &key : keys) {
23852383
config->set_value(p_file, key, Variant());
23862384
}
@@ -5778,8 +5776,7 @@ void EditorNode::_update_layouts_menu() {
57785776
return; // No config.
57795777
}
57805778

5781-
List<String> layouts;
5782-
config.ptr()->get_sections(&layouts);
5779+
Vector<String> layouts = config->get_sections();
57835780

57845781
for (const String &layout : layouts) {
57855782
if (layout == TTR("Default")) {

editor/editor_settings.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
10721072
}
10731073

10741074
if (p_extra_config->has_section("presets")) {
1075-
List<String> keys;
1076-
p_extra_config->get_section_keys("presets", &keys);
1075+
Vector<String> keys = p_extra_config->get_section_keys("presets");
10771076

10781077
for (const String &key : keys) {
10791078
Variant val = p_extra_config->get_value("presets", key);
@@ -1660,8 +1659,7 @@ void EditorSettings::load_favorites_and_recent_dirs() {
16601659
Ref<ConfigFile> cf;
16611660
cf.instantiate();
16621661
if (cf->load(favorite_properties_file) == OK) {
1663-
List<String> secs;
1664-
cf->get_sections(&secs);
1662+
Vector<String> secs = cf->get_sections();
16651663

16661664
for (String &E : secs) {
16671665
PackedStringArray properties = PackedStringArray(cf->get_value(E, "properties"));
@@ -1730,8 +1728,7 @@ void EditorSettings::load_text_editor_theme() {
17301728
return;
17311729
}
17321730

1733-
List<String> keys;
1734-
cf->get_section_keys("color_theme", &keys);
1731+
Vector<String> keys = cf->get_section_keys("color_theme");
17351732

17361733
for (const String &key : keys) {
17371734
String val = cf->get_value("color_theme", key);
@@ -2150,8 +2147,7 @@ void EditorSettings::get_argument_options(const StringName &p_function, int p_id
21502147
r_options->push_back(E.key.quote());
21512148
}
21522149
} else if (pf == "get_project_metadata" && project_metadata.is_valid()) {
2153-
List<String> sections;
2154-
project_metadata->get_sections(&sections);
2150+
Vector<String> sections = project_metadata->get_sections();
21552151
for (const String &section : sections) {
21562152
r_options->push_back(section.quote());
21572153
}

editor/export/editor_export.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,15 @@ void EditorExport::load_config() {
330330

331331
String option_section = "preset." + itos(index) + ".options";
332332

333-
List<String> options;
334-
config->get_section_keys(option_section, &options);
333+
Vector<String> options = config->get_section_keys(option_section);
335334

336335
for (const String &E : options) {
337336
Variant value = config->get_value(option_section, E);
338337
preset->set(E, value);
339338
}
340339

341340
if (credentials->has_section(option_section)) {
342-
options.clear();
343-
credentials->get_section_keys(option_section, &options);
341+
options = credentials->get_section_keys(option_section);
344342

345343
for (const String &E : options) {
346344
// Drop values for secret properties that no longer exist, or during the next save they would end up in the regular config file.

0 commit comments

Comments
 (0)