Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Fixed an issue causing goto_profile_sites in settings.json to not get…
Browse files Browse the repository at this point in the history
… populated with defaults on a fresh install.
  • Loading branch information
PazerOP committed Jan 9, 2021
1 parent 3152d3e commit 2664aa2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
12 changes: 12 additions & 0 deletions tf2_bot_detector/Config/ConfigHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ mh::task<std::error_condition> 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);

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions tf2_bot_detector/Config/ConfigHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -98,6 +99,9 @@ namespace tf2_bot_detector
std::optional<ConfigSchemaInfo> m_Schema;
std::string m_FileName; // Name of the file this was loaded from

protected:
virtual void PostLoad(bool deserialized) {}

private:
mh::task<std::error_condition> LoadFileInternalAsync(std::filesystem::path filename, std::shared_ptr<const HTTPClient> client);
};
Expand Down Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions tf2_bot_detector/Config/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tf2_bot_detector/Config/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPClient> m_HTTPClient;
};
Expand Down

0 comments on commit 2664aa2

Please sign in to comment.