compare language value of steam_settings/achievements.json case-insensitive

This commit is contained in:
otavepto 2024-06-26 23:17:29 +03:00
parent 21cab8b378
commit e35e2e5a4e
2 changed files with 28 additions and 23 deletions

View File

@ -110,7 +110,7 @@ private:
void save_achievements(); void save_achievements();
nlohmann::detail::iter_impl<nlohmann::json> defined_achievements_find(const std::string &key); nlohmann::detail::iter_impl<nlohmann::json> defined_achievements_find(const std::string &key);
std::string get_value_for_language(nlohmann::json &json, std::string key, std::string language); std::string get_value_for_language(const nlohmann::json &json, std::string_view key, std::string_view language);
std::vector<Steam_Leaderboard_Entry> load_leaderboard_entries(const std::string &name); std::vector<Steam_Leaderboard_Entry> load_leaderboard_entries(const std::string &name);
void save_my_leaderboard_entry(const Steam_Leaderboard &leaderboard); void save_my_leaderboard_entry(const Steam_Leaderboard &leaderboard);

View File

@ -138,36 +138,41 @@ nlohmann::detail::iter_impl<nlohmann::json> Steam_User_Stats::defined_achievemen
); );
} }
std::string Steam_User_Stats::get_value_for_language(nlohmann::json &json, std::string key, std::string language) std::string Steam_User_Stats::get_value_for_language(const nlohmann::json &json, std::string_view key, std::string_view language)
{ {
auto x = json.find(key); auto x = json.find(key); // find "displayName", or "description", etc ...
if (x == json.end()) return ""; if (json.end() == x) return "";
if (x.value().is_string()) {
if (x.value().is_string()) { // ex: "displayName": "some description"
return x.value().get<std::string>(); return x.value().get<std::string>();
} else if (x.value().is_object()) { } else if (x.value().is_object()) {
auto l = x.value().find(language); const auto &obj_kv_pairs = x.value().items();
if (l != x.value().end()) {
return l.value().get<std::string>(); // try to find target language
auto obj_itr = std::find_if(obj_kv_pairs.begin(), obj_kv_pairs.end(), [&]( decltype(*obj_kv_pairs.begin()) item ) {
return common_helpers::str_cmp_insensitive(item.key(), language);
});
if (obj_itr != obj_kv_pairs.end()) {
return obj_itr.value().get<std::string>();
} }
l = x.value().find("english"); // try to find english language
if (l != x.value().end()) { obj_itr = std::find_if(obj_kv_pairs.begin(), obj_kv_pairs.end(), [&]( decltype(*obj_kv_pairs.begin()) item ) {
return l.value().get<std::string>(); return common_helpers::str_cmp_insensitive(item.key(), "english");
});
if (obj_itr != obj_kv_pairs.end()) {
return obj_itr.value().get<std::string>();
} }
l = x.value().begin(); // try to find the first available language (not "token"),
if (l != x.value().end()) { // if not languages exist, try to find "token"
if (l.key() == "token") { for (bool search_for_token : { false, true }) {
std::string token_value = l.value().get<std::string>(); obj_itr = std::find_if(obj_kv_pairs.begin(), obj_kv_pairs.end(), [&]( decltype(*obj_kv_pairs.begin()) item ) {
l++; return common_helpers::str_cmp_insensitive(item.key(), "token") == search_for_token;
if (l != x.value().end()) { });
return l.value().get<std::string>(); if (obj_itr != obj_kv_pairs.end()) {
} return obj_itr.value().get<std::string>();
return token_value;
} }
return l.value().get<std::string>();
} }
} }