Move account sync implementation out of header

This commit is contained in:
2026-06-08 05:55:11 -04:00
parent 70dd22ee8c
commit 8f80005cb1
3 changed files with 134 additions and 101 deletions
+1
View File
@@ -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
+123
View File
@@ -0,0 +1,123 @@
#include "AccountSync.hh"
#include <cstdio>
#include <mutex>
namespace AccountSync {
static std::mutex config_mutex;
static Config current_config;
static Config get_config() {
std::lock_guard<std::mutex> g(config_mutex);
return current_config;
}
void configure(const Config& cfg) {
{
std::lock_guard<std::mutex> 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<unsigned int>(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<unsigned int>(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<unsigned int>(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<unsigned int>(account_id),
bb_username.c_str(),
static_cast<long long>(character_slot),
static_cast<unsigned int>(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<unsigned int>(account_id),
bb_username.c_str(),
static_cast<long long>(character_slot));
}
} // namespace AccountSync
+10 -101
View File
@@ -2,7 +2,6 @@
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <string>
#include <phosg/JSON.hh>
@@ -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<unsigned int>(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<unsigned int>(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<unsigned int>(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<unsigned int>(account_id),
bb_username.c_str(),
static_cast<long long>(character_slot),
static_cast<unsigned int>(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<unsigned int>(account_id),
bb_username.c_str(),
static_cast<long long>(character_slot));
}
int64_t character_slot);
} // namespace AccountSync