Files
DarkflameServer/dDatabase/GameDatabase/Database.cpp
Aaron Kimbrell 3a8838d463 feat(capture): replay bundles against a sandbox stack with a headless client
CaptureTool (built next to the servers) replays packet bundles and compares the answers:

- replay: per bundle a fresh sandbox folder with copied server binaries, rewritten settings
  (replay_sandbox=1, a new SQLite file inside the folder, ports from --port on, no dashboard),
  read back before anything starts; sandbox-setup runs inside it to apply migrations and make
  the replay account and the bundle's characters (setup mode); master is started, the stack is
  stopped as one process group and the folder deleted unless kept
- with replay_sandbox=1 every server refuses a database that isn't SQLite, isn't inside its
  own folder, or is replay_live_sqlite_path (Database::Connect); replay-target against a
  running server needs --i-know-this-is-not-a-sandbox
- the fake client splits the recording into connections, logs in and picks the character
  itself when the recording doesn't, fills in the target's account, session key and IDs,
  learns server-made object IDs from replica constructions by LOT, follows the recorded
  timing and waits for the answers a client waits for; the diff pairs answers by name (and
  constructions by LOT) and ignores fields that differ between runs
- import-live converts the 2014 live captures (folders of *_traffic.zip; pcaps and encrypted
  captures are left alone) into bundles, with secrets removed and CREATE_CHARACTER as setup
- anonymise makes local fixtures; docs/CaptureReplay.md describes capture, the bundle format,
  portability rules, the sandbox and the replay

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 22:34:34 -05:00

105 lines
3.3 KiB
C++

#include "Database.h"
#include "Game.h"
#include "dConfig.h"
#include "Logger.h"
#include "DluAssert.h"
#include "SQLiteDatabase.h"
#include "MySQLDatabase.h"
#include <filesystem>
#include <ranges>
#include <stdexcept>
#include "BinaryPathFinder.h"
#pragma warning (disable:4251) //Disables SQL warnings
namespace {
GameDatabase* database = nullptr;
/**
* A replay sandbox (replay_sandbox=1, docs/CaptureReplay.md) only ever uses its own SQLite file, inside its own
* folder, and never the live one: anything else and the server refuses to start.
*/
void CheckSandbox(const std::string& databaseType) {
if (!Game::config || Game::config->GetValue("replay_sandbox") != "1") return;
if (databaseType != "sqlite") throw std::runtime_error("A replay sandbox only runs on its own SQLite database (database_type=sqlite)");
namespace fs = std::filesystem;
const auto folder = fs::weakly_canonical(BinaryPathFinder::GetBinaryDir());
const auto path = fs::weakly_canonical(folder / Game::config->GetValue("sqlite_database_path"));
const auto relative = path.lexically_relative(folder);
if (Game::config->GetValue("sqlite_database_path").empty() || relative.empty() || *relative.begin() == "..") {
throw std::runtime_error("A replay sandbox's database must be inside its own folder (" + folder.string() + "), not " + path.string());
}
const auto live = Game::config->GetValue("replay_live_sqlite_path");
std::error_code ec;
if (!live.empty() && fs::exists(live, ec) && fs::equivalent(path, live, ec)) {
throw std::runtime_error("A replay sandbox must not use the live database " + live);
}
}
}
std::string Database::GetMigrationFolder() {
const std::set<std::string> validMysqlTypes = { "mysql", "mariadb", "maria" };
auto databaseType = Game::config->GetValue("database_type");
std::ranges::transform(databaseType, databaseType.begin(), ::tolower);
if (databaseType == "sqlite") return "sqlite";
else if (validMysqlTypes.contains(databaseType)) return "mysql";
else {
LOG("No database specified, using MySQL");
return "mysql";
}
}
void Database::Connect() {
if (database) {
LOG("Tried to connect to database when it's already connected!");
return;
}
const auto databaseType = GetMigrationFolder();
CheckSandbox(databaseType);
if (databaseType == "sqlite") database = new SQLiteDatabase();
else if (databaseType == "mysql") database = new MySQLDatabase();
else {
LOG("Invalid database type specified in config, using MySQL");
database = new MySQLDatabase();
}
database->Connect();
}
GameDatabase* Database::Get() {
if (!database) {
LOG("Tried to get database when it's not connected!");
Connect();
}
return database;
}
std::unique_ptr<GameDatabase> Database::CreateConnection() {
std::unique_ptr<GameDatabase> connection;
CheckSandbox(GetMigrationFolder());
if (GetMigrationFolder() == "sqlite") connection = std::make_unique<SQLiteDatabase>();
else connection = std::make_unique<MySQLDatabase>();
connection->Connect();
return connection;
}
void Database::Destroy(std::string source) {
if (database) {
database->Destroy(source);
delete database;
database = nullptr;
} else {
LOG("Trying to destroy database when it's not connected!");
}
}
void Database::_setDatabase(GameDatabase* const db) {
if (database) delete database;
database = db;
}