From 8f80005cb1e09afb9ee0bd4eb8ad5a126ae11672 Mon Sep 17 00:00:00 2001 From: James Osborne Date: Mon, 8 Jun 2026 05:55:11 -0400 Subject: [PATCH] Move account sync implementation out of header --- CMakeLists.txt | 1 + src/AccountSync.cc | 123 +++++++++++++++++++++++++++++++++++++++++++++ src/AccountSync.hh | 111 ++++------------------------------------ 3 files changed, 134 insertions(+), 101 deletions(-) create mode 100644 src/AccountSync.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 874aaaa8..8c29c49d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ add_custom_target( set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/Revision.cc src/Account.cc + src/AccountSync.cc src/AddressTranslator.cc src/AFSArchive.cc src/AsyncHTTPServer.cc diff --git a/src/AccountSync.cc b/src/AccountSync.cc new file mode 100644 index 00000000..0123d529 --- /dev/null +++ b/src/AccountSync.cc @@ -0,0 +1,123 @@ +#include "AccountSync.hh" + +#include +#include + +namespace AccountSync { + +static std::mutex config_mutex; +static Config current_config; + +static Config get_config() { + std::lock_guard g(config_mutex); + return current_config; +} + +void configure(const Config& cfg) { + { + std::lock_guard g(config_mutex); + current_config = cfg; + } + + if (cfg.enabled) { + std::fprintf(stderr, + "[AccountSync] config enabled region=%s coordinator_url=%s login_locks=%s\n", + cfg.region.c_str(), + cfg.coordinator_url.c_str(), + cfg.enable_login_locks ? "true" : "false"); + } +} + +void configure_from_json(const phosg::JSON& json) { + Config cfg; + cfg.enabled = json.get_bool("Enabled", false); + cfg.region = json.get_string("Region", ""); + cfg.coordinator_url = json.get_string("CoordinatorURL", ""); + cfg.shared_secret = json.get_string("SharedSecret", ""); + cfg.request_timeout_usecs = json.get_int("RequestTimeoutUsecs", 3000000); + cfg.fail_open = json.get_bool("FailOpen", false); + cfg.notify_account_saves = json.get_bool("NotifyAccountSaves", true); + cfg.notify_player_saves = json.get_bool("NotifyPlayerSaves", true); + cfg.notify_backup_saves = json.get_bool("NotifyBackupSaves", true); + cfg.enable_login_locks = json.get_bool("EnableLoginLocks", false); + configure(cfg); +} + +void notify_account_saved(uint32_t account_id, const std::string& filename) { + auto cfg = get_config(); + if (!cfg.enabled || !cfg.notify_account_saves) { + return; + } + std::fprintf(stderr, + "[AccountSync] event=account_saved region=%s account_id=%010u filename=%s\n", + cfg.region.c_str(), + static_cast(account_id), + filename.c_str()); +} + +void notify_backup_saved(uint32_t account_id, size_t slot, const std::string& filename) { + auto cfg = get_config(); + if (!cfg.enabled || !cfg.notify_backup_saves) { + return; + } + std::fprintf(stderr, + "[AccountSync] event=backup_saved region=%s account_id=%010u slot=%zu filename=%s\n", + cfg.region.c_str(), + static_cast(account_id), + slot, + filename.c_str()); +} + +void notify_player_state_saved( + const char* reason, + uint32_t account_id, + const std::string& bb_username, + const std::string& filename) { + auto cfg = get_config(); + if (!cfg.enabled || !cfg.notify_player_saves) { + return; + } + std::fprintf(stderr, + "[AccountSync] event=player_state_saved region=%s reason=%s account_id=%010u bb_username=%s filename=%s\n", + cfg.region.c_str(), + reason, + static_cast(account_id), + bb_username.c_str(), + filename.c_str()); +} + +void notify_bb_login_start( + uint32_t account_id, + const std::string& bb_username, + int64_t character_slot, + uint8_t connection_phase) { + auto cfg = get_config(); + if (!cfg.enabled || !cfg.enable_login_locks) { + return; + } + std::fprintf(stderr, + "[AccountSync] event=bb_login_start region=%s account_id=%010u bb_username=%s character_slot=%lld connection_phase=%u\n", + cfg.region.c_str(), + static_cast(account_id), + bb_username.c_str(), + static_cast(character_slot), + static_cast(connection_phase)); +} + +void notify_bb_login_end( + uint32_t account_id, + const std::string& bb_username, + int64_t character_slot) { + auto cfg = get_config(); + if (!cfg.enabled || !cfg.enable_login_locks) { + return; + } + std::fprintf(stderr, + "[AccountSync] event=bb_login_end region=%s account_id=%010u bb_username=%s character_slot=%lld\n", + cfg.region.c_str(), + static_cast(account_id), + bb_username.c_str(), + static_cast(character_slot)); +} + +} // namespace AccountSync diff --git a/src/AccountSync.hh b/src/AccountSync.hh index 4eeb12a5..dd5ab6c4 100644 --- a/src/AccountSync.hh +++ b/src/AccountSync.hh @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -22,117 +21,27 @@ struct Config { bool enable_login_locks = false; }; -inline Config& mutable_config() { - static Config cfg; - return cfg; -} +void configure(const Config& cfg); +void configure_from_json(const phosg::JSON& json); -inline const Config& config() { - return mutable_config(); -} +void notify_account_saved(uint32_t account_id, const std::string& filename); +void notify_backup_saved(uint32_t account_id, size_t slot, const std::string& filename); -inline void configure(const Config& cfg) { - mutable_config() = cfg; - - if (cfg.enabled) { - std::fprintf(stderr, - "[AccountSync] config enabled region=%s coordinator_url=%s login_locks=%s\n", - cfg.region.c_str(), - cfg.coordinator_url.c_str(), - cfg.enable_login_locks ? "true" : "false"); - } -} - -inline void configure_from_json(const phosg::JSON& json) { - Config cfg; - cfg.enabled = json.get_bool("Enabled", false); - cfg.region = json.get_string("Region", ""); - cfg.coordinator_url = json.get_string("CoordinatorURL", ""); - cfg.shared_secret = json.get_string("SharedSecret", ""); - cfg.request_timeout_usecs = json.get_int("RequestTimeoutUsecs", 3000000); - cfg.fail_open = json.get_bool("FailOpen", false); - cfg.notify_account_saves = json.get_bool("NotifyAccountSaves", true); - cfg.notify_player_saves = json.get_bool("NotifyPlayerSaves", true); - cfg.notify_backup_saves = json.get_bool("NotifyBackupSaves", true); - cfg.enable_login_locks = json.get_bool("EnableLoginLocks", false); - configure(cfg); -} - -inline void notify_account_saved(uint32_t account_id, const std::string& filename) { - const auto& cfg = config(); - if (!cfg.enabled || !cfg.notify_account_saves) { - return; - } - std::fprintf(stderr, - "[AccountSync] event=account_saved region=%s account_id=%010u filename=%s\n", - cfg.region.c_str(), - static_cast(account_id), - filename.c_str()); -} - -inline void notify_backup_saved(uint32_t account_id, size_t slot, const std::string& filename) { - const auto& cfg = config(); - if (!cfg.enabled || !cfg.notify_backup_saves) { - return; - } - std::fprintf(stderr, - "[AccountSync] event=backup_saved region=%s account_id=%010u slot=%zu filename=%s\n", - cfg.region.c_str(), - static_cast(account_id), - slot, - filename.c_str()); -} - -inline void notify_player_state_saved( +void notify_player_state_saved( const char* reason, uint32_t account_id, const std::string& bb_username, - const std::string& filename) { - const auto& cfg = config(); - if (!cfg.enabled || !cfg.notify_player_saves) { - return; - } - std::fprintf(stderr, - "[AccountSync] event=player_state_saved region=%s reason=%s account_id=%010u bb_username=%s filename=%s\n", - cfg.region.c_str(), - reason, - static_cast(account_id), - bb_username.c_str(), - filename.c_str()); -} + const std::string& filename); -inline void notify_bb_login_start( +void notify_bb_login_start( uint32_t account_id, const std::string& bb_username, int64_t character_slot, - uint8_t connection_phase) { - const auto& cfg = config(); - if (!cfg.enabled || !cfg.enable_login_locks) { - return; - } - std::fprintf(stderr, - "[AccountSync] event=bb_login_start region=%s account_id=%010u bb_username=%s character_slot=%lld connection_phase=%u\n", - cfg.region.c_str(), - static_cast(account_id), - bb_username.c_str(), - static_cast(character_slot), - static_cast(connection_phase)); -} + uint8_t connection_phase); -inline void notify_bb_login_end( +void notify_bb_login_end( uint32_t account_id, const std::string& bb_username, - int64_t character_slot) { - const auto& cfg = config(); - if (!cfg.enabled || !cfg.enable_login_locks) { - return; - } - std::fprintf(stderr, - "[AccountSync] event=bb_login_end region=%s account_id=%010u bb_username=%s character_slot=%lld\n", - cfg.region.c_str(), - static_cast(account_id), - bb_username.c_str(), - static_cast(character_slot)); -} + int64_t character_slot); } // namespace AccountSync