handle unknown preferences.

This commit is contained in:
pentarctagon 2024-06-24 10:05:07 -05:00 committed by Pentarctagon
parent e9f13b1958
commit bf512a5e1a
2 changed files with 49 additions and 0 deletions

View file

@ -260,6 +260,29 @@ void prefs::load_preferences()
preferences_.merge_with(default_prefs);
preferences_.merge_with(unsynced_prefs);
preferences_.merge_with(synced_prefs);
// check for any unknown preferences
for(const auto& attr : synced_prefs.attribute_range()) {
if(std::find(synced_attributes_.begin(), synced_attributes_.end(), attr.first) == synced_attributes_.end()) {
unknown_synced_attributes_.insert(attr.first);
}
}
for(const auto& attr : unsynced_prefs.attribute_range()) {
if(std::find(unsynced_attributes_.begin(), unsynced_attributes_.end(), attr.first) == unsynced_attributes_.end()) {
unknown_unsynced_attributes_.insert(attr.first);
}
}
for(const auto& child : synced_prefs.all_children_range()) {
if(std::find(synced_children_.begin(), synced_children_.end(), child.key) == synced_children_.end()) {
unknown_synced_children_.insert(child.key);
}
}
for(const auto& child : unsynced_prefs.all_children_range()) {
if(std::find(unsynced_children_.begin(), unsynced_children_.end(), child.key) == unsynced_children_.end()) {
unknown_unsynced_children_.insert(child.key);
}
}
} catch(const config::error& e) {
ERR_CFG << "Error loading preference, message: " << e.what();
}
@ -345,6 +368,27 @@ void prefs::write_preferences()
}
}
// write any unknown preferences back out
for(const std::string& attr : unknown_synced_attributes_) {
synced[attr] = preferences_[attr];
}
for(const std::string& attr : unknown_synced_children_) {
for(const auto& child : preferences_.child_range(attr)) {
config& ch = synced.add_child(attr);
ch.append_children(child);
}
}
for(const std::string& attr : unknown_unsynced_attributes_) {
unsynced[attr] = preferences_[attr];
}
for(const std::string& attr : unknown_unsynced_children_) {
for(const auto& child : preferences_.child_range(attr)) {
config& ch = unsynced.add_child(attr);
ch.append_children(child);
}
}
try {
filesystem::scoped_ostream synced_prefs_file = filesystem::ostream_file(filesystem::get_prefs_file());
write(*synced_prefs_file, synced);

View file

@ -871,6 +871,11 @@ class prefs
preferences::secure_buffer escape(const preferences::secure_buffer& text);
preferences::secure_buffer unescape(const preferences::secure_buffer& text);
std::set<std::string> unknown_synced_attributes_;
std::set<std::string> unknown_unsynced_attributes_;
std::set<std::string> unknown_synced_children_;
std::set<std::string> unknown_unsynced_children_;
// a bit verbose, but it being a compile time error if a preference hasn't been added is nice
static constexpr std::array synced_attributes_{
prefs_list::player_joins_sound,