diff --git a/tf2_bot_detector/Config/ConfigHelpers.cpp b/tf2_bot_detector/Config/ConfigHelpers.cpp index 64866641..e6e70ec6 100644 --- a/tf2_bot_detector/Config/ConfigHelpers.cpp +++ b/tf2_bot_detector/Config/ConfigHelpers.cpp @@ -238,6 +238,16 @@ mh::task ConfigFileBase::LoadFileAsync(const std::filesyst { const auto loadResult = co_await LoadFileInternalAsync(filename, client); + try + { + PostLoad(!loadResult); + } + catch (...) + { + LogException("Failed to run PostLoad() for {}", filename); + co_return ConfigErrorType::PostLoadFailed; + } + if (loadResult && loadResult != std::errc::no_such_file_or_directory) SaveConfigFileBackup(filename); @@ -491,6 +501,8 @@ const std::error_category& tf2_bot_detector::ConfigErrorCategory() return "Failed to validate $schema that was generated by the Serialize() function"; case ConfigErrorType::WriteFileFailed: return "Failed to write file"; + case ConfigErrorType::PostLoadFailed: + return "Exception thrown from PostLoad()"; default: assert(false); diff --git a/tf2_bot_detector/Config/ConfigHelpers.h b/tf2_bot_detector/Config/ConfigHelpers.h index 98e2d7f0..a8e40c76 100644 --- a/tf2_bot_detector/Config/ConfigHelpers.h +++ b/tf2_bot_detector/Config/ConfigHelpers.h @@ -38,6 +38,7 @@ namespace tf2_bot_detector SerializeFailed, SerializedSchemaValidationFailed, WriteFileFailed, + PostLoadFailed, }; const std::error_category& ConfigErrorCategory(); std::error_condition make_error_condition(ConfigErrorType e); @@ -98,6 +99,9 @@ namespace tf2_bot_detector std::optional m_Schema; std::string m_FileName; // Name of the file this was loaded from + protected: + virtual void PostLoad(bool deserialized) {} + private: mh::task LoadFileInternalAsync(std::filesystem::path filename, std::shared_ptr client); }; @@ -264,6 +268,7 @@ MH_ENUM_REFLECT_BEGIN(tf2_bot_detector::ConfigErrorType) MH_ENUM_REFLECT_VALUE(SerializeFailed) MH_ENUM_REFLECT_VALUE(SerializedSchemaValidationFailed) MH_ENUM_REFLECT_VALUE(WriteFileFailed) + MH_ENUM_REFLECT_VALUE(PostLoadFailed) MH_ENUM_REFLECT_END() namespace std diff --git a/tf2_bot_detector/Config/Settings.cpp b/tf2_bot_detector/Config/Settings.cpp index 4d6615a2..635f6da7 100644 --- a/tf2_bot_detector/Config/Settings.cpp +++ b/tf2_bot_detector/Config/Settings.cpp @@ -242,6 +242,15 @@ void Settings::ValidateSchema(const ConfigSchemaInfo& schema) const throw std::runtime_error(mh::format("Schema must be version {} (current version {})", SETTINGS_SCHEMA_VERSION, schema.m_Version)); } +void Settings::AddDefaultGotoProfileSites() +{ + m_GotoProfileSites.push_back({ "Steam Community", "https://steamcommunity.com/profiles/%SteamID64%" }); + m_GotoProfileSites.push_back({ "logs.tf", "https://logs.tf/profile/%SteamID64%" }); + m_GotoProfileSites.push_back({ "RGL", "https://rgl.gg/Public/PlayerProfile.aspx?p=%SteamID64%" }); + m_GotoProfileSites.push_back({ "SteamRep", "https://steamrep.com/profiles/%SteamID64%" }); + m_GotoProfileSites.push_back({ "UGC League", "https://www.ugcleague.com/players_page.cfm?player_id=%SteamID64%" }); +} + void Settings::Deserialize(const nlohmann::json& json) { if (auto found = json.find("general"); found != json.end()) @@ -276,15 +285,15 @@ void Settings::Deserialize(const nlohmann::json& json) try_get_to_defaulted(json, m_Discord, "discord"); try_get_to_defaulted(json, m_Theme, "theme"); try_get_to_defaulted(json, m_TF2Interface, "tf2_interface"); + if (!try_get_to_defaulted(json, m_GotoProfileSites, "goto_profile_sites")) - { - // Some defaults - m_GotoProfileSites.push_back({ "Steam Community", "https://steamcommunity.com/profiles/%SteamID64%" }); - m_GotoProfileSites.push_back({ "logs.tf", "https://logs.tf/profile/%SteamID64%" }); - m_GotoProfileSites.push_back({ "RGL", "https://rgl.gg/Public/PlayerProfile.aspx?p=%SteamID64%" }); - m_GotoProfileSites.push_back({ "SteamRep", "https://steamrep.com/profiles/%SteamID64%" }); - m_GotoProfileSites.push_back({ "UGC League", "https://www.ugcleague.com/players_page.cfm?player_id=%SteamID64%" }); - } + AddDefaultGotoProfileSites(); +} + +void Settings::PostLoad(bool deserialized) +{ + if (!deserialized) + AddDefaultGotoProfileSites(); } void Settings::Serialize(nlohmann::json& json) const diff --git a/tf2_bot_detector/Config/Settings.h b/tf2_bot_detector/Config/Settings.h index 354cbdff..a8edf04e 100644 --- a/tf2_bot_detector/Config/Settings.h +++ b/tf2_bot_detector/Config/Settings.h @@ -150,6 +150,9 @@ namespace tf2_bot_detector void ValidateSchema(const ConfigSchemaInfo& schema) const override; void Deserialize(const nlohmann::json& json) override; void Serialize(nlohmann::json& json) const override; + void PostLoad(bool deserialized) override; + + void AddDefaultGotoProfileSites(); mutable std::shared_ptr m_HTTPClient; };