mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-11-30 05:08:14 +00:00
Merge branch 'main' into cdclient-rework
This commit is contained in:
@@ -25,28 +25,28 @@ InstanceManager::~InstanceManager() {
|
||||
}
|
||||
}
|
||||
|
||||
Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneID) {
|
||||
mLogger->Log("InstanceManager", "Searching for an instance for mapID %i/%i\n", mapID, cloneID);
|
||||
Instance* InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneID) {
|
||||
mLogger->Log("InstanceManager", "Searching for an instance for mapID %i/%i", mapID, cloneID);
|
||||
Instance* instance = FindInstance(mapID, isFriendTransfer, cloneID);
|
||||
if (instance) return instance;
|
||||
|
||||
//TODO: Update this so that the IP is read from a configuration file instead
|
||||
//TODO: Update this so that the IP is read from a configuration file instead
|
||||
|
||||
int softCap = 8;
|
||||
int maxPlayers = 12;
|
||||
int softCap = 8;
|
||||
int maxPlayers = 12;
|
||||
|
||||
if (mapID == 0) {
|
||||
softCap = 999;
|
||||
maxPlayers = softCap;
|
||||
} else {
|
||||
if (mapID == 0) {
|
||||
softCap = 999;
|
||||
maxPlayers = softCap;
|
||||
} else {
|
||||
softCap = GetSoftCap(mapID);
|
||||
maxPlayers = GetHardCap(mapID);
|
||||
}
|
||||
|
||||
uint32_t port = GetFreePort();
|
||||
instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, softCap, maxPlayers);
|
||||
uint32_t port = GetFreePort();
|
||||
instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, softCap, maxPlayers);
|
||||
|
||||
//Start the actual process:
|
||||
//Start the actual process:
|
||||
#ifdef _WIN32
|
||||
std::string cmd = "start ./WorldServer.exe";
|
||||
#else
|
||||
@@ -78,26 +78,25 @@ Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, L
|
||||
cmd.append(" -maxclients ");
|
||||
cmd.append(std::to_string(maxPlayers));
|
||||
|
||||
cmd.append(" -clone ");
|
||||
cmd.append(std::to_string(cloneID));
|
||||
cmd.append(" -clone ");
|
||||
cmd.append(std::to_string(cloneID));
|
||||
|
||||
#ifndef _WIN32
|
||||
cmd.append("&"); //Sends our next process to the background on Linux
|
||||
cmd.append("&"); //Sends our next process to the background on Linux
|
||||
#endif
|
||||
|
||||
Game::logger->Log("InstanceManager", "Starting instance %i with command: %s\n", instance->GetInstanceID(), cmd.c_str());
|
||||
|
||||
system(cmd.c_str());
|
||||
|
||||
m_Instances.push_back(instance);
|
||||
m_Instances.push_back(instance);
|
||||
|
||||
if (instance) {
|
||||
mLogger->Log("InstanceManager", "Created new instance: %i/%i/%i with min/max %i/%i\n", mapID, m_LastInstanceID, cloneID, softCap, maxPlayers);
|
||||
return instance;
|
||||
}
|
||||
else mLogger->Log("InstanceManager", "Failed to create a new instance!\n");
|
||||
if (instance) {
|
||||
mLogger->Log("InstanceManager", "Created new instance: %i/%i/%i with min/max %i/%i", mapID, m_LastInstanceID, cloneID, softCap, maxPlayers);
|
||||
return instance;
|
||||
} else mLogger->Log("InstanceManager", "Failed to create a new instance!");
|
||||
|
||||
return nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool InstanceManager::IsPortInUse(uint32_t port) {
|
||||
@@ -106,27 +105,27 @@ bool InstanceManager::IsPortInUse(uint32_t port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t InstanceManager::GetFreePort() {
|
||||
uint32_t port = m_LastPort;
|
||||
std::vector<uint32_t> usedPorts;
|
||||
for (Instance* i : m_Instances) {
|
||||
usedPorts.push_back(i->GetPort());
|
||||
}
|
||||
|
||||
std::sort(usedPorts.begin(), usedPorts.end());
|
||||
uint32_t port = m_LastPort;
|
||||
std::vector<uint32_t> usedPorts;
|
||||
for (Instance* i : m_Instances) {
|
||||
usedPorts.push_back(i->GetPort());
|
||||
}
|
||||
|
||||
int portIdx = 0;
|
||||
while (portIdx < usedPorts.size() && port == usedPorts[portIdx]) {
|
||||
//increment by 3 since each instance uses 3 ports (instance, world-server, world-chat)
|
||||
port += 3;
|
||||
portIdx++;
|
||||
}
|
||||
std::sort(usedPorts.begin(), usedPorts.end());
|
||||
|
||||
return port;
|
||||
int portIdx = 0;
|
||||
while (portIdx < usedPorts.size() && port == usedPorts[portIdx]) {
|
||||
//increment by 3 since each instance uses 3 ports (instance, world-server, world-chat)
|
||||
port += 3;
|
||||
portIdx++;
|
||||
}
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
void InstanceManager::AddPlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID) {
|
||||
@@ -149,26 +148,23 @@ void InstanceManager::RemovePlayer(SystemAddress systemAddr, LWOMAPID mapID, LWO
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Instance*> InstanceManager::GetInstances() const
|
||||
{
|
||||
std::vector<Instance*> InstanceManager::GetInstances() const {
|
||||
return m_Instances;
|
||||
}
|
||||
|
||||
void InstanceManager::AddInstance(Instance* instance) {
|
||||
if (instance == nullptr) return;
|
||||
|
||||
|
||||
m_Instances.push_back(instance);
|
||||
}
|
||||
|
||||
void InstanceManager::RemoveInstance(Instance* instance) {
|
||||
for (uint32_t i = 0; i < m_Instances.size(); ++i)
|
||||
{
|
||||
if (m_Instances[i] == instance)
|
||||
{
|
||||
for (uint32_t i = 0; i < m_Instances.size(); ++i) {
|
||||
if (m_Instances[i] == instance) {
|
||||
instance->SetShutdownComplete(true);
|
||||
|
||||
|
||||
RedirectPendingRequests(instance);
|
||||
|
||||
|
||||
delete m_Instances[i];
|
||||
|
||||
m_Instances.erase(m_Instances.begin() + i);
|
||||
@@ -178,17 +174,15 @@ void InstanceManager::RemoveInstance(Instance* instance) {
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceManager::ReadyInstance(Instance* instance)
|
||||
{
|
||||
void InstanceManager::ReadyInstance(Instance* instance) {
|
||||
instance->SetIsReady(true);
|
||||
|
||||
auto& pending = instance->GetPendingRequests();
|
||||
|
||||
for (const auto& request : pending)
|
||||
{
|
||||
for (const auto& request : pending) {
|
||||
const auto& zoneId = instance->GetZoneID();
|
||||
|
||||
Game::logger->Log("InstanceManager", "Responding to pending request %llu -> %i (%i)\n", request, zoneId.GetMapID(), zoneId.GetCloneID());
|
||||
Game::logger->Log("InstanceManager", "Responding to pending request %llu -> %i (%i)", request, zoneId.GetMapID(), zoneId.GetCloneID());
|
||||
|
||||
MasterPackets::SendZoneTransferResponse(
|
||||
Game::server,
|
||||
@@ -202,12 +196,11 @@ void InstanceManager::ReadyInstance(Instance* instance)
|
||||
instance->GetPort()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
pending.clear();
|
||||
}
|
||||
|
||||
void InstanceManager::RequestAffirmation(Instance* instance, const PendingInstanceRequest& request)
|
||||
{
|
||||
void InstanceManager::RequestAffirmation(Instance* instance, const PendingInstanceRequest& request) {
|
||||
instance->GetPendingAffirmations().push_back(request);
|
||||
|
||||
CBITSTREAM;
|
||||
@@ -215,23 +208,21 @@ void InstanceManager::RequestAffirmation(Instance* instance, const PendingInstan
|
||||
PacketUtils::WriteHeader(bitStream, MASTER, MSG_MASTER_AFFIRM_TRANSFER_REQUEST);
|
||||
|
||||
bitStream.Write(request.id);
|
||||
|
||||
|
||||
Game::server->Send(&bitStream, instance->GetSysAddr(), false);
|
||||
|
||||
Game::logger->Log("MasterServer", "Sent affirmation request %llu to %i/%i\n", request.id,
|
||||
|
||||
Game::logger->Log("MasterServer", "Sent affirmation request %llu to %i/%i", request.id,
|
||||
static_cast<int>(instance->GetZoneID().GetMapID()),
|
||||
static_cast<int>(instance->GetZoneID().GetCloneID())
|
||||
);
|
||||
}
|
||||
|
||||
void InstanceManager::AffirmTransfer(Instance* instance, const uint64_t transferID)
|
||||
{
|
||||
void InstanceManager::AffirmTransfer(Instance* instance, const uint64_t transferID) {
|
||||
auto& pending = instance->GetPendingAffirmations();
|
||||
|
||||
for (auto i = 0u; i < pending.size(); ++i)
|
||||
{
|
||||
for (auto i = 0u; i < pending.size(); ++i) {
|
||||
const auto& request = pending[i];
|
||||
|
||||
|
||||
if (request.id != transferID) continue;
|
||||
|
||||
const auto& zoneId = instance->GetZoneID();
|
||||
@@ -247,19 +238,17 @@ void InstanceManager::AffirmTransfer(Instance* instance, const uint64_t transfer
|
||||
instance->GetIP(),
|
||||
instance->GetPort()
|
||||
);
|
||||
|
||||
|
||||
pending.erase(pending.begin() + i);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InstanceManager::RedirectPendingRequests(Instance* instance)
|
||||
{
|
||||
void InstanceManager::RedirectPendingRequests(Instance* instance) {
|
||||
const auto& zoneId = instance->GetZoneID();
|
||||
|
||||
for (const auto& request : instance->GetPendingAffirmations())
|
||||
{
|
||||
|
||||
for (const auto& request : instance->GetPendingAffirmations()) {
|
||||
auto* in = Game::im->GetInstance(zoneId.GetMapID(), false, zoneId.GetCloneID());
|
||||
|
||||
if (!in->GetIsReady()) // Instance not ready, make a pending request
|
||||
@@ -284,17 +273,17 @@ Instance* InstanceManager::GetInstanceBySysAddr(SystemAddress& sysAddr) {
|
||||
}
|
||||
|
||||
bool InstanceManager::IsInstanceFull(Instance* instance, bool isFriendTransfer) {
|
||||
if (!isFriendTransfer && instance->GetSoftCap() > instance->GetCurrentClientCount())
|
||||
if (!isFriendTransfer && instance->GetSoftCap() > instance->GetCurrentClientCount())
|
||||
return false;
|
||||
else if (isFriendTransfer && instance->GetHardCap() > instance->GetCurrentClientCount())
|
||||
else if (isFriendTransfer && instance->GetHardCap() > instance->GetCurrentClientCount())
|
||||
return false;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Instance * InstanceManager::FindInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneId) {
|
||||
Instance* InstanceManager::FindInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneId) {
|
||||
for (Instance* i : m_Instances) {
|
||||
if (i && i->GetMapID() == mapID && i->GetCloneID() == cloneId && !IsInstanceFull(i, isFriendTransfer) && !i->GetIsPrivate() && !i->GetShutdownComplete()) {
|
||||
if (i && i->GetMapID() == mapID && i->GetCloneID() == cloneId && !IsInstanceFull(i, isFriendTransfer) && !i->GetIsPrivate() && !i->GetShutdownComplete() && !i->GetIsShuttingDown()) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -302,9 +291,9 @@ Instance * InstanceManager::FindInstance(LWOMAPID mapID, bool isFriendTransfer,
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Instance * InstanceManager::FindInstance(LWOMAPID mapID, LWOINSTANCEID instanceID) {
|
||||
Instance* InstanceManager::FindInstance(LWOMAPID mapID, LWOINSTANCEID instanceID) {
|
||||
for (Instance* i : m_Instances) {
|
||||
if (i && i->GetMapID() == mapID && i->GetInstanceID() == instanceID && !i->GetIsPrivate()) {
|
||||
if (i && i->GetMapID() == mapID && i->GetInstanceID() == instanceID && !i->GetIsPrivate() && !i->GetShutdownComplete() && !i->GetIsShuttingDown()) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -312,67 +301,61 @@ Instance * InstanceManager::FindInstance(LWOMAPID mapID, LWOINSTANCEID instanceI
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID cloneID, const std::string& password)
|
||||
{
|
||||
Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID cloneID, const std::string& password) {
|
||||
auto* instance = FindPrivateInstance(password);
|
||||
|
||||
if (instance != nullptr)
|
||||
{
|
||||
if (instance != nullptr) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
int maxPlayers = 999;
|
||||
|
||||
|
||||
uint32_t port = GetFreePort();
|
||||
instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, maxPlayers, maxPlayers, true, password);
|
||||
|
||||
//Start the actual process:
|
||||
std::string cmd = "start ./WorldServer.exe -zone ";
|
||||
//Start the actual process:
|
||||
std::string cmd = "start ./WorldServer.exe -zone ";
|
||||
|
||||
#ifndef _WIN32
|
||||
cmd = "./WorldServer -zone ";
|
||||
#endif
|
||||
|
||||
cmd.append(std::to_string(mapID));
|
||||
cmd.append(" -port ");
|
||||
cmd.append(std::to_string(port));
|
||||
cmd.append(" -instance ");
|
||||
cmd.append(std::to_string(m_LastInstanceID));
|
||||
cmd.append(" -maxclients ");
|
||||
cmd.append(std::to_string(maxPlayers));
|
||||
cmd.append(std::to_string(mapID));
|
||||
cmd.append(" -port ");
|
||||
cmd.append(std::to_string(port));
|
||||
cmd.append(" -instance ");
|
||||
cmd.append(std::to_string(m_LastInstanceID));
|
||||
cmd.append(" -maxclients ");
|
||||
cmd.append(std::to_string(maxPlayers));
|
||||
|
||||
cmd.append(" -clone ");
|
||||
cmd.append(std::to_string(cloneID));
|
||||
cmd.append(" -clone ");
|
||||
cmd.append(std::to_string(cloneID));
|
||||
|
||||
#ifndef WIN32
|
||||
cmd.append("&"); //Sends our next process to the background on Linux
|
||||
cmd.append("&"); //Sends our next process to the background on Linux
|
||||
#endif
|
||||
|
||||
system(cmd.c_str());
|
||||
system(cmd.c_str());
|
||||
|
||||
m_Instances.push_back(instance);
|
||||
|
||||
if (instance) return instance;
|
||||
else mLogger->Log("InstanceManager", "Failed to create a new instance!\n");
|
||||
else mLogger->Log("InstanceManager", "Failed to create a new instance!");
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
Instance* InstanceManager::FindPrivateInstance(const std::string& password)
|
||||
{
|
||||
for (auto* instance : m_Instances)
|
||||
{
|
||||
Instance* InstanceManager::FindPrivateInstance(const std::string& password) {
|
||||
for (auto* instance : m_Instances) {
|
||||
if (!instance) continue;
|
||||
|
||||
if (!instance->GetIsPrivate())
|
||||
{
|
||||
if (!instance->GetIsPrivate()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
mLogger->Log("InstanceManager", "Password: %s == %s => %d\n", password.c_str(), instance->GetPassword().c_str(), password == instance->GetPassword());
|
||||
mLogger->Log("InstanceManager", "Password: %s == %s => %d", password.c_str(), instance->GetPassword().c_str(), password == instance->GetPassword());
|
||||
|
||||
if (instance->GetPassword() == password)
|
||||
{
|
||||
if (instance->GetPassword() == password) {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -389,7 +372,7 @@ int InstanceManager::GetSoftCap(LWOMAPID mapID) {
|
||||
return zone->population_soft_cap;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 8;
|
||||
}
|
||||
|
||||
@@ -406,23 +389,20 @@ int InstanceManager::GetHardCap(LWOMAPID mapID) {
|
||||
return 12;
|
||||
}
|
||||
|
||||
void Instance::SetShutdownComplete(const bool value)
|
||||
{
|
||||
void Instance::SetShutdownComplete(const bool value) {
|
||||
m_Shutdown = value;
|
||||
}
|
||||
|
||||
bool Instance::GetShutdownComplete() const
|
||||
{
|
||||
bool Instance::GetShutdownComplete() const {
|
||||
return m_Shutdown;
|
||||
}
|
||||
|
||||
void Instance::Shutdown()
|
||||
{
|
||||
void Instance::Shutdown() {
|
||||
CBITSTREAM;
|
||||
|
||||
|
||||
PacketUtils::WriteHeader(bitStream, MASTER, MSG_MASTER_SHUTDOWN);
|
||||
|
||||
Game::server->Send(&bitStream, this->m_SysAddr, false);
|
||||
|
||||
Game::logger->Log("Instance", "Triggered world shutdown\n");
|
||||
|
||||
Game::logger->Log("Instance", "Triggered world shutdown");
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ public:
|
||||
m_PendingAffirmations = {};
|
||||
m_PendingRequests = {};
|
||||
m_Ready = false;
|
||||
m_IsShuttingDown = false;
|
||||
}
|
||||
|
||||
const std::string& GetIP() const { return m_IP; }
|
||||
@@ -46,9 +47,11 @@ public:
|
||||
|
||||
bool GetIsReady() const { return m_Ready; }
|
||||
void SetIsReady(bool value) { m_Ready = value; }
|
||||
bool GetIsShuttingDown() const { return m_IsShuttingDown; }
|
||||
void SetIsShuttingDown(bool value) { m_IsShuttingDown = value; }
|
||||
std::vector<PendingInstanceRequest>& GetPendingRequests() { return m_PendingRequests; }
|
||||
std::vector<PendingInstanceRequest>& GetPendingAffirmations() { return m_PendingAffirmations; }
|
||||
|
||||
|
||||
int GetHardCap() const { return m_MaxClientsHardCap; }
|
||||
int GetSoftCap() const { return m_MaxClientsSoftCap; }
|
||||
int GetCurrentClientCount() const { return m_CurrentClientCount; }
|
||||
@@ -57,10 +60,10 @@ public:
|
||||
uint32_t GetAffirmationTimeout() const { return m_AffirmationTimeout; }
|
||||
|
||||
void AddPlayer(Player player) { /*m_Players.push_back(player);*/ m_CurrentClientCount++; }
|
||||
void RemovePlayer(Player player) {
|
||||
void RemovePlayer(Player player) {
|
||||
m_CurrentClientCount--;
|
||||
if (m_CurrentClientCount < 0) m_CurrentClientCount = 0;
|
||||
/*for (size_t i = 0; i < m_Players.size(); ++i)
|
||||
/*for (size_t i = 0; i < m_Players.size(); ++i)
|
||||
if (m_Players[i].addr == player.addr) m_Players.erase(m_Players.begin() + i);*/
|
||||
}
|
||||
|
||||
@@ -69,7 +72,7 @@ public:
|
||||
|
||||
void SetShutdownComplete(bool value);
|
||||
bool GetShutdownComplete() const;
|
||||
|
||||
|
||||
void Shutdown();
|
||||
|
||||
private:
|
||||
@@ -82,11 +85,12 @@ private:
|
||||
std::vector<Player> m_Players;
|
||||
SystemAddress m_SysAddr;
|
||||
bool m_Ready;
|
||||
bool m_IsShuttingDown;
|
||||
std::vector<PendingInstanceRequest> m_PendingRequests;
|
||||
std::vector<PendingInstanceRequest> m_PendingAffirmations;
|
||||
|
||||
uint32_t m_AffirmationTimeout;
|
||||
|
||||
|
||||
bool m_IsPrivate;
|
||||
std::string m_Password;
|
||||
|
||||
@@ -103,7 +107,7 @@ public:
|
||||
Instance* GetInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneID); //Creates an instance if none found
|
||||
bool IsPortInUse(uint32_t port);
|
||||
uint32_t GetFreePort();
|
||||
|
||||
|
||||
void AddPlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID);
|
||||
void RemovePlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID);
|
||||
|
||||
|
||||
@@ -75,9 +75,9 @@ int main(int argc, char** argv) {
|
||||
Game::logger = SetupLogger();
|
||||
if (!Game::logger) return EXIT_FAILURE;
|
||||
|
||||
Game::logger->Log("MasterServer", "Starting Master server...\n");
|
||||
Game::logger->Log("MasterServer", "Version: %i.%i\n", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR);
|
||||
Game::logger->Log("MasterServer", "Compiled on: %s\n", __TIMESTAMP__);
|
||||
Game::logger->Log("MasterServer", "Starting Master server...");
|
||||
Game::logger->Log("MasterServer", "Version: %i.%i", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR);
|
||||
Game::logger->Log("MasterServer", "Compiled on: %s", __TIMESTAMP__);
|
||||
|
||||
//Read our config:
|
||||
dConfig config("masterconfig.ini");
|
||||
@@ -85,36 +85,6 @@ int main(int argc, char** argv) {
|
||||
Game::logger->SetLogToConsole(bool(std::stoi(config.GetValue("log_to_console"))));
|
||||
Game::logger->SetLogDebugStatements(config.GetValue("log_debug_statements") == "1");
|
||||
|
||||
//Check CDClient exists
|
||||
const std::string cdclient_path = "./res/CDServer.sqlite";
|
||||
std::ifstream cdclient_fd(cdclient_path);
|
||||
if (!cdclient_fd.good()) {
|
||||
Game::logger->Log("WorldServer", "%s could not be opened\n", cdclient_path.c_str());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
cdclient_fd.close();
|
||||
|
||||
//Connect to CDClient
|
||||
try {
|
||||
CDClientDatabase::Connect(cdclient_path);
|
||||
} catch (CppSQLite3Exception& e) {
|
||||
Game::logger->Log("WorldServer", "Unable to connect to CDServer SQLite Database\n");
|
||||
Game::logger->Log("WorldServer", "Error: %s\n", e.errorMessage());
|
||||
Game::logger->Log("WorldServer", "Error Code: %i\n", e.errorCode());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//Get CDClient initial information
|
||||
try {
|
||||
CDClientManager::Instance()->Initialize();
|
||||
} catch (CppSQLite3Exception& e) {
|
||||
Game::logger->Log("WorldServer", "Failed to initialize CDServer SQLite Database\n");
|
||||
Game::logger->Log("WorldServer", "May be caused by corrupted file: %s\n", cdclient_path.c_str());
|
||||
Game::logger->Log("WorldServer", "Error: %s\n", e.errorMessage());
|
||||
Game::logger->Log("WorldServer", "Error Code: %i\n", e.errorCode());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//Connect to the MySQL Database
|
||||
std::string mysql_host = config.GetValue("mysql_host");
|
||||
std::string mysql_database = config.GetValue("mysql_database");
|
||||
@@ -124,16 +94,42 @@ int main(int argc, char** argv) {
|
||||
try {
|
||||
Database::Connect(mysql_host, mysql_database, mysql_username, mysql_password);
|
||||
} catch (sql::SQLException& ex) {
|
||||
Game::logger->Log("MasterServer", "Got an error while connecting to the database: %s\n", ex.what());
|
||||
Game::logger->Log("MasterServer", "Got an error while connecting to the database: %s", ex.what());
|
||||
Game::logger->Log("MigrationRunner", "Migrations not run");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (argc > 1 && (strcmp(argv[1], "-m") == 0 || strcmp(argv[1], "--migrations") == 0)) {
|
||||
MigrationRunner::RunMigrations();
|
||||
Game::logger->Log("MigrationRunner", "Finished running migrations\n");
|
||||
MigrationRunner::RunMigrations();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
//Check CDClient exists
|
||||
const std::string cdclient_path = "./res/CDServer.sqlite";
|
||||
std::ifstream cdclient_fd(cdclient_path);
|
||||
if (!cdclient_fd.good()) {
|
||||
Game::logger->Log("WorldServer", "%s could not be opened", cdclient_path.c_str());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
cdclient_fd.close();
|
||||
|
||||
//Connect to CDClient
|
||||
try {
|
||||
CDClientDatabase::Connect(cdclient_path);
|
||||
} catch (CppSQLite3Exception& e) {
|
||||
Game::logger->Log("WorldServer", "Unable to connect to CDServer SQLite Database");
|
||||
Game::logger->Log("WorldServer", "Error: %s", e.errorMessage());
|
||||
Game::logger->Log("WorldServer", "Error Code: %i", e.errorCode());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//Get CDClient initial information
|
||||
try {
|
||||
CDClientManager::Instance()->Initialize();
|
||||
} catch (CppSQLite3Exception& e) {
|
||||
Game::logger->Log("WorldServer", "Failed to initialize CDServer SQLite Database");
|
||||
Game::logger->Log("WorldServer", "May be caused by corrupted file: %s", cdclient_path.c_str());
|
||||
Game::logger->Log("WorldServer", "Error: %s", e.errorMessage());
|
||||
Game::logger->Log("WorldServer", "Error Code: %i", e.errorCode());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//If the first command line argument is -a or --account then make the user
|
||||
//input a username and password, with the password being hidden.
|
||||
@@ -212,8 +208,7 @@ int main(int argc, char** argv) {
|
||||
updateStatement->setInt(3, result->getInt("id"));
|
||||
updateStatement->execute();
|
||||
delete updateStatement;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
//If we didn't find a server, create one.
|
||||
auto* insertStatement = Database::CreatePreppedStmt("INSERT INTO `servers` (`name`, `ip`, `port`, `state`, `version`) VALUES ('master', ?, ?, 0, 171023)");
|
||||
insertStatement->setString(1, master_server_ip);
|
||||
@@ -259,8 +254,7 @@ int main(int argc, char** argv) {
|
||||
if (framesSinceLastFlush >= 900) {
|
||||
Game::logger->Flush();
|
||||
framesSinceLastFlush = 0;
|
||||
}
|
||||
else
|
||||
} else
|
||||
framesSinceLastFlush++;
|
||||
|
||||
//Every 10 min we ping our sql server to keep it alive hopefully:
|
||||
@@ -279,8 +273,7 @@ int main(int argc, char** argv) {
|
||||
delete stmt;
|
||||
|
||||
framesSinceLastSQLPing = 0;
|
||||
}
|
||||
else
|
||||
} else
|
||||
framesSinceLastSQLPing++;
|
||||
|
||||
//10m shutdown for universe kill command
|
||||
@@ -288,8 +281,7 @@ int main(int argc, char** argv) {
|
||||
if (framesSinceKillUniverseCommand >= 40000) {
|
||||
//Break main loop and exit
|
||||
break;
|
||||
}
|
||||
else
|
||||
} else
|
||||
framesSinceKillUniverseCommand++;
|
||||
}
|
||||
|
||||
@@ -304,8 +296,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
if (!instance->GetPendingAffirmations().empty()) {
|
||||
affirmTimeout++;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
affirmTimeout = 0;
|
||||
}
|
||||
|
||||
@@ -313,7 +304,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
if (affirmTimeout == 1000) {
|
||||
instance->Shutdown();
|
||||
instance->SetShutdownComplete(true);
|
||||
instance->SetIsShuttingDown(true);
|
||||
|
||||
Game::im->RedirectPendingRequests(instance);
|
||||
}
|
||||
@@ -353,13 +344,14 @@ dLogger* SetupLogger() {
|
||||
|
||||
void HandlePacket(Packet* packet) {
|
||||
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION) {
|
||||
Game::logger->Log("MasterServer", "A server has disconnected\n");
|
||||
Game::logger->Log("MasterServer", "A server has disconnected");
|
||||
|
||||
//Since this disconnection is intentional, we'll just delete it as
|
||||
//we'll start a new one anyway if needed:
|
||||
Instance* instance =
|
||||
Game::im->GetInstanceBySysAddr(packet->systemAddress);
|
||||
if (instance) {
|
||||
Game::logger->Log("MasterServer", "Actually disconnected from zone %i clone %i instance %i port %i", instance->GetMapID(), instance->GetCloneID(), instance->GetInstanceID(), instance->GetPort());
|
||||
Game::im->RemoveInstance(instance); //Delete the old
|
||||
}
|
||||
|
||||
@@ -369,7 +361,7 @@ void HandlePacket(Packet* packet) {
|
||||
}
|
||||
|
||||
if (packet->data[0] == ID_CONNECTION_LOST) {
|
||||
Game::logger->Log("MasterServer", "A server has lost the connection\n");
|
||||
Game::logger->Log("MasterServer", "A server has lost the connection");
|
||||
|
||||
Instance* instance =
|
||||
Game::im->GetInstanceBySysAddr(packet->systemAddress);
|
||||
@@ -387,7 +379,7 @@ void HandlePacket(Packet* packet) {
|
||||
if (packet->data[1] == MASTER) {
|
||||
switch (packet->data[3]) {
|
||||
case MSG_MASTER_REQUEST_PERSISTENT_ID: {
|
||||
Game::logger->Log("MasterServer", "A persistent ID req\n");
|
||||
Game::logger->Log("MasterServer", "A persistent ID req");
|
||||
RakNet::BitStream inStream(packet->data, packet->length, false);
|
||||
uint64_t header = inStream.Read(header);
|
||||
uint64_t requestID = 0;
|
||||
@@ -399,7 +391,7 @@ void HandlePacket(Packet* packet) {
|
||||
}
|
||||
|
||||
case MSG_MASTER_REQUEST_ZONE_TRANSFER: {
|
||||
Game::logger->Log("MasterServer","Received zone transfer req\n");
|
||||
Game::logger->Log("MasterServer", "Received zone transfer req");
|
||||
RakNet::BitStream inStream(packet->data, packet->length, false);
|
||||
uint64_t header = inStream.Read(header);
|
||||
uint64_t requestID = 0;
|
||||
@@ -415,18 +407,18 @@ void HandlePacket(Packet* packet) {
|
||||
Instance* in = Game::im->GetInstance(zoneID, false, zoneClone);
|
||||
|
||||
for (auto* instance : Game::im->GetInstances()) {
|
||||
Game::logger->Log("MasterServer", "Instance: %i/%i/%i -> %i\n",instance->GetMapID(), instance->GetCloneID(),instance->GetInstanceID(), instance == in);
|
||||
Game::logger->Log("MasterServer", "Instance: %i/%i/%i -> %i", instance->GetMapID(), instance->GetCloneID(), instance->GetInstanceID(), instance == in);
|
||||
}
|
||||
|
||||
if (!in->GetIsReady()) //Instance not ready, make a pending request
|
||||
{
|
||||
in->GetPendingRequests().push_back({ requestID, static_cast<bool>(mythranShift), packet->systemAddress });
|
||||
Game::logger->Log("MasterServer", "Server not ready, adding pending request %llu %i %i\n", requestID, zoneID, zoneClone);
|
||||
Game::logger->Log("MasterServer", "Server not ready, adding pending request %llu %i %i", requestID, zoneID, zoneClone);
|
||||
break;
|
||||
}
|
||||
|
||||
//Instance is ready, transfer
|
||||
Game::logger->Log("MasterServer", "Responding to transfer request %llu for zone %i %i\n", requestID, zoneID, zoneClone);
|
||||
Game::logger->Log("MasterServer", "Responding to transfer request %llu for zone %i %i", requestID, zoneID, zoneClone);
|
||||
Game::im->RequestAffirmation(in, { requestID, static_cast<bool>(mythranShift), packet->systemAddress });
|
||||
break;
|
||||
}
|
||||
@@ -461,8 +453,7 @@ void HandlePacket(Packet* packet) {
|
||||
|
||||
in->SetSysAddr(copy);
|
||||
Game::im->AddInstance(in);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
auto instance = Game::im->FindInstance(
|
||||
theirZoneID, static_cast<uint16_t>(theirInstanceID));
|
||||
if (instance) {
|
||||
@@ -478,7 +469,7 @@ void HandlePacket(Packet* packet) {
|
||||
chatServerMasterPeerSysAddr = copy;
|
||||
}
|
||||
|
||||
Game::logger->Log("MasterServer", "Received server info, instance: %i port: %i\n", theirInstanceID, theirPort);
|
||||
Game::logger->Log("MasterServer", "Received server info, instance: %i port: %i", theirInstanceID, theirPort);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -510,7 +501,7 @@ void HandlePacket(Packet* packet) {
|
||||
}
|
||||
|
||||
activeSessions.insert(std::make_pair(sessionKey, username));
|
||||
Game::logger->Log("MasterServer", "Got sessionKey %i for user %s\n", sessionKey, username.c_str());
|
||||
Game::logger->Log("MasterServer", "Got sessionKey %i for user %s", sessionKey, username.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -546,9 +537,8 @@ void HandlePacket(Packet* packet) {
|
||||
Game::im->FindInstance(theirZoneID, theirInstanceID);
|
||||
if (instance) {
|
||||
instance->AddPlayer(Player());
|
||||
}
|
||||
else {
|
||||
printf("Instance missing? What?\n");
|
||||
} else {
|
||||
printf("Instance missing? What?");
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -606,7 +596,7 @@ void HandlePacket(Packet* packet) {
|
||||
|
||||
inStream.Read(requestID);
|
||||
inStream.Read(mythranShift);
|
||||
|
||||
|
||||
uint32_t len;
|
||||
inStream.Read<uint32_t>(len);
|
||||
|
||||
@@ -617,7 +607,7 @@ void HandlePacket(Packet* packet) {
|
||||
|
||||
auto* instance = Game::im->FindPrivateInstance(password.c_str());
|
||||
|
||||
Game::logger->Log( "MasterServer", "Join private zone: %llu %d %s %p\n", requestID, mythranShift, password.c_str(), instance);
|
||||
Game::logger->Log("MasterServer", "Join private zone: %llu %d %s %p", requestID, mythranShift, password.c_str(), instance);
|
||||
|
||||
if (instance == nullptr) {
|
||||
return;
|
||||
@@ -625,7 +615,7 @@ void HandlePacket(Packet* packet) {
|
||||
|
||||
const auto& zone = instance->GetZoneID();
|
||||
|
||||
MasterPackets::SendZoneTransferResponse(Game::server, packet->systemAddress, requestID,(bool)mythranShift, zone.GetMapID(),instance->GetInstanceID(), zone.GetCloneID(),instance->GetIP(), instance->GetPort());
|
||||
MasterPackets::SendZoneTransferResponse(Game::server, packet->systemAddress, requestID, (bool)mythranShift, zone.GetMapID(), instance->GetInstanceID(), zone.GetCloneID(), instance->GetIP(), instance->GetPort());
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -640,16 +630,16 @@ void HandlePacket(Packet* packet) {
|
||||
inStream.Read(zoneID);
|
||||
inStream.Read(instanceID);
|
||||
|
||||
Game::logger->Log("MasterServer", "Got world ready %i %i\n",zoneID, instanceID);
|
||||
Game::logger->Log("MasterServer", "Got world ready %i %i", zoneID, instanceID);
|
||||
|
||||
auto* instance = Game::im->FindInstance(zoneID, instanceID);
|
||||
|
||||
if (instance == nullptr) {
|
||||
Game::logger->Log("MasterServer","Failed to find zone to ready\n");
|
||||
Game::logger->Log("MasterServer", "Failed to find zone to ready");
|
||||
return;
|
||||
}
|
||||
|
||||
Game::logger->Log("MasterServer", "Ready zone %i\n", zoneID);
|
||||
Game::logger->Log("MasterServer", "Ready zone %i", zoneID);
|
||||
Game::im->ReadyInstance(instance);
|
||||
break;
|
||||
}
|
||||
@@ -661,7 +651,7 @@ void HandlePacket(Packet* packet) {
|
||||
int zoneID;
|
||||
inStream.Read(zoneID);
|
||||
|
||||
Game::logger->Log("MasterServer", "Prepping zone %i\n", zoneID);
|
||||
Game::logger->Log("MasterServer", "Prepping zone %i", zoneID);
|
||||
Game::im->GetInstance(zoneID, false, 0);
|
||||
break;
|
||||
}
|
||||
@@ -674,15 +664,15 @@ void HandlePacket(Packet* packet) {
|
||||
|
||||
inStream.Read(requestID);
|
||||
|
||||
Game::logger->Log("MasterServer","Got affirmation of transfer %llu\n",requestID);
|
||||
Game::logger->Log("MasterServer", "Got affirmation of transfer %llu", requestID);
|
||||
|
||||
auto* instance =Game::im->GetInstanceBySysAddr(packet->systemAddress);
|
||||
auto* instance = Game::im->GetInstanceBySysAddr(packet->systemAddress);
|
||||
|
||||
if (instance == nullptr)
|
||||
return;
|
||||
|
||||
Game::im->AffirmTransfer(instance, requestID);
|
||||
Game::logger->Log("MasterServer", "Affirmation complete %llu\n",requestID);
|
||||
Game::logger->Log("MasterServer", "Affirmation complete %llu", requestID);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -696,51 +686,49 @@ void HandlePacket(Packet* packet) {
|
||||
return;
|
||||
}
|
||||
|
||||
Game::logger->Log("MasterServer", "Got shutdown response\n");
|
||||
instance->SetShutdownComplete(true);
|
||||
Game::logger->Log("MasterServer", "Got shutdown response from zone %i clone %i instance %i port %i", instance->GetMapID(), instance->GetCloneID(), instance->GetInstanceID(), instance->GetPort());
|
||||
instance->SetIsShuttingDown(true);
|
||||
break;
|
||||
}
|
||||
|
||||
case MSG_MASTER_SHUTDOWN_UNIVERSE: {
|
||||
Game::logger->Log("MasterServer","Received shutdown universe command, shutting down in 10 minutes.\n");
|
||||
Game::logger->Log("MasterServer", "Received shutdown universe command, shutting down in 10 minutes.");
|
||||
shouldShutdown = true;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
Game::logger->Log("MasterServer","Unknown master packet ID from server: %i\n",packet->data[3]);
|
||||
Game::logger->Log("MasterServer", "Unknown master packet ID from server: %i", packet->data[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StartChatServer() {
|
||||
#ifdef __APPLE__
|
||||
//macOS doesn't need sudo to run on ports < 1024
|
||||
system("./ChatServer&");
|
||||
//macOS doesn't need sudo to run on ports < 1024
|
||||
system("./ChatServer&");
|
||||
#elif _WIN32
|
||||
system("start ./ChatServer.exe");
|
||||
system("start ./ChatServer.exe");
|
||||
#else
|
||||
if (std::atoi(Game::config->GetValue("use_sudo_chat").c_str())) {
|
||||
system("sudo ./ChatServer&");
|
||||
}
|
||||
else {
|
||||
system("./ChatServer&");
|
||||
}
|
||||
if (std::atoi(Game::config->GetValue("use_sudo_chat").c_str())) {
|
||||
system("sudo ./ChatServer&");
|
||||
} else {
|
||||
system("./ChatServer&");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void StartAuthServer() {
|
||||
#ifdef __APPLE__
|
||||
system("./AuthServer&");
|
||||
system("./AuthServer&");
|
||||
#elif _WIN32
|
||||
system("start ./AuthServer.exe");
|
||||
system("start ./AuthServer.exe");
|
||||
#else
|
||||
if (std::atoi(Game::config->GetValue("use_sudo_auth").c_str())) {
|
||||
system("sudo ./AuthServer&");
|
||||
}
|
||||
else {
|
||||
system("./AuthServer&");
|
||||
}
|
||||
if (std::atoi(Game::config->GetValue("use_sudo_auth").c_str())) {
|
||||
system("sudo ./AuthServer&");
|
||||
} else {
|
||||
system("./AuthServer&");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -764,7 +752,7 @@ void ShutdownSequence() {
|
||||
auto* objIdManager = ObjectIDManager::TryInstance();
|
||||
if (objIdManager != nullptr) {
|
||||
objIdManager->SaveToDatabase();
|
||||
Game::logger->Log("MasterServer", "Saved ObjectIDTracker to DB\n");
|
||||
Game::logger->Log("MasterServer", "Saved ObjectIDTracker to DB");
|
||||
}
|
||||
|
||||
auto t = std::chrono::high_resolution_clock::now();
|
||||
@@ -774,7 +762,7 @@ void ShutdownSequence() {
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
Game::logger->Log("MasterServer", "Attempting to shutdown instances, max 60 seconds...\n");
|
||||
Game::logger->Log("MasterServer", "Attempting to shutdown instances, max 60 seconds...");
|
||||
|
||||
while (true) {
|
||||
|
||||
@@ -784,7 +772,7 @@ void ShutdownSequence() {
|
||||
Game::server->DeallocatePacket(packet);
|
||||
packet = nullptr;
|
||||
}
|
||||
|
||||
|
||||
auto done = true;
|
||||
|
||||
for (auto* instance : Game::im->GetInstances()) {
|
||||
@@ -798,7 +786,7 @@ void ShutdownSequence() {
|
||||
}
|
||||
|
||||
if (done) {
|
||||
Game::logger->Log("MasterServer", "Finished shutting down MasterServer!\n");
|
||||
Game::logger->Log("MasterServer", "Finished shutting down MasterServer!");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -808,7 +796,7 @@ void ShutdownSequence() {
|
||||
ticks++;
|
||||
|
||||
if (ticks == 600 * 6) {
|
||||
Game::logger->Log("MasterServer", "Finished shutting down by timeout!\n");
|
||||
Game::logger->Log("MasterServer", "Finished shutting down by timeout!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -819,10 +807,10 @@ void ShutdownSequence() {
|
||||
int FinalizeShutdown() {
|
||||
//Delete our objects here:
|
||||
Database::Destroy("MasterServer");
|
||||
delete Game::im;
|
||||
delete Game::server;
|
||||
delete Game::logger;
|
||||
if (Game::im) delete Game::im;
|
||||
if (Game::server) delete Game::server;
|
||||
if (Game::logger) delete Game::logger;
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,67 +5,67 @@
|
||||
#include "dLogger.h"
|
||||
|
||||
// Static Variables
|
||||
ObjectIDManager *ObjectIDManager::m_Address = nullptr;
|
||||
ObjectIDManager* ObjectIDManager::m_Address = nullptr;
|
||||
|
||||
//! Initializes the manager
|
||||
void ObjectIDManager::Initialize(dLogger *logger) {
|
||||
this->mLogger = logger;
|
||||
this->currentPersistentID = 1;
|
||||
void ObjectIDManager::Initialize(dLogger* logger) {
|
||||
this->mLogger = logger;
|
||||
this->currentPersistentID = 1;
|
||||
|
||||
try {
|
||||
sql::PreparedStatement *stmt = Database::CreatePreppedStmt(
|
||||
"SELECT last_object_id FROM object_id_tracker");
|
||||
try {
|
||||
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
|
||||
"SELECT last_object_id FROM object_id_tracker");
|
||||
|
||||
sql::ResultSet *result = stmt->executeQuery();
|
||||
auto next = result->next();
|
||||
sql::ResultSet* result = stmt->executeQuery();
|
||||
auto next = result->next();
|
||||
|
||||
if (!next) {
|
||||
sql::PreparedStatement *insertStmt = Database::CreatePreppedStmt(
|
||||
"INSERT INTO object_id_tracker VALUES (1)");
|
||||
if (!next) {
|
||||
sql::PreparedStatement* insertStmt = Database::CreatePreppedStmt(
|
||||
"INSERT INTO object_id_tracker VALUES (1)");
|
||||
|
||||
insertStmt->execute();
|
||||
insertStmt->execute();
|
||||
|
||||
delete insertStmt;
|
||||
delete insertStmt;
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
while (next) {
|
||||
this->currentPersistentID =
|
||||
result->getInt(1) > 0 ? result->getInt(1) : 1;
|
||||
next = result->next();
|
||||
}
|
||||
while (next) {
|
||||
this->currentPersistentID =
|
||||
result->getInt(1) > 0 ? result->getInt(1) : 1;
|
||||
next = result->next();
|
||||
}
|
||||
|
||||
delete result;
|
||||
delete stmt;
|
||||
} catch (sql::SQLException &e) {
|
||||
mLogger->Log("ObjectIDManager", "Unable to fetch max persistent object "
|
||||
"ID in use. Defaulting to 1.\n");
|
||||
mLogger->Log("ObjectIDManager", "SQL error: %s\n", e.what());
|
||||
this->currentPersistentID = 1;
|
||||
}
|
||||
delete result;
|
||||
delete stmt;
|
||||
} catch (sql::SQLException& e) {
|
||||
mLogger->Log("ObjectIDManager", "Unable to fetch max persistent object "
|
||||
"ID in use. Defaulting to 1.");
|
||||
mLogger->Log("ObjectIDManager", "SQL error: %s", e.what());
|
||||
this->currentPersistentID = 1;
|
||||
}
|
||||
}
|
||||
|
||||
//! Generates a new persistent ID
|
||||
uint32_t ObjectIDManager::GeneratePersistentID(void) {
|
||||
uint32_t toReturn = ++this->currentPersistentID;
|
||||
uint32_t toReturn = ++this->currentPersistentID;
|
||||
|
||||
// So we peroidically save our ObjID to the database:
|
||||
if (toReturn % 25 == 0) { // TEMP: DISABLED FOR DEBUG / DEVELOPMENT!
|
||||
sql::PreparedStatement *stmt = Database::CreatePreppedStmt(
|
||||
"UPDATE object_id_tracker SET last_object_id=?");
|
||||
stmt->setUInt(1, toReturn);
|
||||
stmt->execute();
|
||||
delete stmt;
|
||||
}
|
||||
// So we peroidically save our ObjID to the database:
|
||||
if (toReturn % 25 == 0) { // TEMP: DISABLED FOR DEBUG / DEVELOPMENT!
|
||||
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
|
||||
"UPDATE object_id_tracker SET last_object_id=?");
|
||||
stmt->setUInt(1, toReturn);
|
||||
stmt->execute();
|
||||
delete stmt;
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
void ObjectIDManager::SaveToDatabase() {
|
||||
sql::PreparedStatement *stmt = Database::CreatePreppedStmt(
|
||||
"UPDATE object_id_tracker SET last_object_id=?");
|
||||
stmt->setUInt(1, currentPersistentID);
|
||||
stmt->execute();
|
||||
delete stmt;
|
||||
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
|
||||
"UPDATE object_id_tracker SET last_object_id=?");
|
||||
stmt->setUInt(1, currentPersistentID);
|
||||
stmt->execute();
|
||||
delete stmt;
|
||||
}
|
||||
|
||||
@@ -10,38 +10,38 @@ class dLogger;
|
||||
\brief A manager that handles requests for object IDs
|
||||
*/
|
||||
|
||||
//! The Object ID Manager
|
||||
//! The Object ID Manager
|
||||
class ObjectIDManager {
|
||||
private:
|
||||
dLogger* mLogger;
|
||||
static ObjectIDManager * m_Address; //!< The singleton instance
|
||||
|
||||
uint32_t currentPersistentID; //!< The highest current persistent ID in use
|
||||
|
||||
static ObjectIDManager* m_Address; //!< The singleton instance
|
||||
|
||||
uint32_t currentPersistentID; //!< The highest current persistent ID in use
|
||||
|
||||
public:
|
||||
|
||||
//! Return the singleton if it is initialized
|
||||
static ObjectIDManager* TryInstance() {
|
||||
return m_Address;
|
||||
}
|
||||
|
||||
//! The singleton method
|
||||
static ObjectIDManager * Instance() {
|
||||
if (m_Address == nullptr) {
|
||||
m_Address = new ObjectIDManager;
|
||||
}
|
||||
|
||||
return m_Address;
|
||||
}
|
||||
|
||||
//! Initializes the manager
|
||||
void Initialize(dLogger* logger);
|
||||
|
||||
//! Generates a new persistent ID
|
||||
/*!
|
||||
\return The new persistent ID
|
||||
*/
|
||||
uint32_t GeneratePersistentID(void);
|
||||
//! Return the singleton if it is initialized
|
||||
static ObjectIDManager* TryInstance() {
|
||||
return m_Address;
|
||||
}
|
||||
|
||||
void SaveToDatabase();
|
||||
//! The singleton method
|
||||
static ObjectIDManager* Instance() {
|
||||
if (m_Address == nullptr) {
|
||||
m_Address = new ObjectIDManager;
|
||||
}
|
||||
|
||||
return m_Address;
|
||||
}
|
||||
|
||||
//! Initializes the manager
|
||||
void Initialize(dLogger* logger);
|
||||
|
||||
//! Generates a new persistent ID
|
||||
/*!
|
||||
\return The new persistent ID
|
||||
*/
|
||||
uint32_t GeneratePersistentID(void);
|
||||
|
||||
void SaveToDatabase();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user