format codebase

This commit is contained in:
aronwk-aaron
2022-07-28 08:39:57 -05:00
parent 4f7aa11067
commit 19e77a38d8
881 changed files with 34700 additions and 38689 deletions

View File

@@ -25,65 +25,64 @@ InstanceManager::~InstanceManager() {
}
}
Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneID) {
mLogger->Log("InstanceManager", "Searching for an instance for mapID %i/%i", 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 -zone ";
#else
std::string cmd;
if (std::atoi(Game::config->GetValue("use_sudo_world").c_str())) {
if (std::atoi(Game::config->GetValue("use_sudo_world").c_str())) {
cmd = "sudo ./WorldServer -zone ";
} else {
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);
m_Instances.push_back(instance);
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!");
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) {
@@ -97,22 +96,22 @@ bool InstanceManager::IsPortInUse(uint32_t port) {
}
uint32_t InstanceManager::GetFreePort() {
uint32_t port = m_LastPort;
std::vector<uint32_t> usedPorts;
for (Instance* i : m_Instances) {
usedPorts.push_back(i->GetPort());
}
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());
std::sort(usedPorts.begin(), usedPorts.end());
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++;
}
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;
return port;
}
void InstanceManager::AddPlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID) {
@@ -135,8 +134,7 @@ void InstanceManager::RemovePlayer(SystemAddress systemAddr, LWOMAPID mapID, LWO
}
}
std::vector<Instance*> InstanceManager::GetInstances() const
{
std::vector<Instance*> InstanceManager::GetInstances() const {
return m_Instances;
}
@@ -147,10 +145,8 @@ void InstanceManager::AddInstance(Instance* 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);
@@ -164,14 +160,12 @@ 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)", request, zoneId.GetMapID(), zoneId.GetCloneID());
@@ -192,8 +186,7 @@ void InstanceManager::ReadyInstance(Instance* instance)
pending.clear();
}
void InstanceManager::RequestAffirmation(Instance* instance, const PendingInstanceRequest& request)
{
void InstanceManager::RequestAffirmation(Instance* instance, const PendingInstanceRequest& request) {
instance->GetPendingAffirmations().push_back(request);
CBITSTREAM;
@@ -210,12 +203,10 @@ void InstanceManager::RequestAffirmation(Instance* instance, const PendingInstan
);
}
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;
@@ -240,12 +231,10 @@ void InstanceManager::AffirmTransfer(Instance* instance, const uint64_t transfer
}
}
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
@@ -278,7 +267,7 @@ bool InstanceManager::IsInstanceFull(Instance* instance, bool isFriendTransfer)
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() && !i->GetIsShuttingDown()) {
return i;
@@ -288,9 +277,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() && !i->GetShutdownComplete() && !i->GetIsShuttingDown()) {
if (i && i->GetMapID() == mapID && i->GetInstanceID() == instanceID && !i->GetIsPrivate() && !i->GetShutdownComplete() && !i->GetIsShuttingDown()) {
return i;
}
}
@@ -298,12 +287,10 @@ 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;
}
@@ -312,29 +299,29 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
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);
@@ -344,21 +331,17 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
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", password.c_str(), instance->GetPassword().c_str(), password == instance->GetPassword());
if (instance->GetPassword() == password)
{
if (instance->GetPassword() == password) {
return instance;
}
}
@@ -392,18 +375,15 @@ 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);

View File

@@ -51,7 +51,7 @@ public:
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; }
@@ -60,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);*/
}
@@ -72,7 +72,7 @@ public:
void SetShutdownComplete(bool value);
bool GetShutdownComplete() const;
void Shutdown();
private:
@@ -90,7 +90,7 @@ private:
std::vector<PendingInstanceRequest> m_PendingAffirmations;
uint32_t m_AffirmationTimeout;
bool m_IsPrivate;
std::string m_Password;
@@ -107,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);

View File

@@ -104,8 +104,7 @@ int main(int argc, char** argv) {
Game::logger->Log("MigrationRunner", "Finished running migrations");
return EXIT_SUCCESS;
}
else {
} else {
//Check CDClient exists
const std::string cdclient_path = "./res/CDServer.sqlite";
@@ -229,8 +228,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);
@@ -274,8 +272,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:
@@ -294,8 +291,7 @@ int main(int argc, char** argv) {
delete stmt;
framesSinceLastSQLPing = 0;
}
else
} else
framesSinceLastSQLPing++;
//10m shutdown for universe kill command
@@ -303,8 +299,7 @@ int main(int argc, char** argv) {
if (framesSinceKillUniverseCommand >= 40000) {
//Break main loop and exit
break;
}
else
} else
framesSinceKillUniverseCommand++;
}
@@ -319,8 +314,7 @@ int main(int argc, char** argv) {
if (!instance->GetPendingAffirmations().empty()) {
affirmTimeout++;
}
else {
} else {
affirmTimeout = 0;
}
@@ -415,7 +409,7 @@ void HandlePacket(Packet* packet) {
}
case MSG_MASTER_REQUEST_ZONE_TRANSFER: {
Game::logger->Log("MasterServer","Received zone transfer req");
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;
@@ -431,7 +425,7 @@ 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",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
@@ -477,8 +471,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) {
@@ -562,8 +555,7 @@ void HandlePacket(Packet* packet) {
Game::im->FindInstance(theirZoneID, theirInstanceID);
if (instance) {
instance->AddPlayer(Player());
}
else {
} else {
printf("Instance missing? What?");
}
break;
@@ -633,7 +625,7 @@ void HandlePacket(Packet* packet) {
auto* instance = Game::im->FindPrivateInstance(password.c_str());
Game::logger->Log( "MasterServer", "Join private zone: %llu %d %s %p", 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;
@@ -641,7 +633,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;
}
@@ -656,12 +648,12 @@ void HandlePacket(Packet* packet) {
inStream.Read(zoneID);
inStream.Read(instanceID);
Game::logger->Log("MasterServer", "Got world ready %i %i",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");
Game::logger->Log("MasterServer", "Failed to find zone to ready");
return;
}
@@ -690,15 +682,15 @@ void HandlePacket(Packet* packet) {
inStream.Read(requestID);
Game::logger->Log("MasterServer","Got affirmation of transfer %llu",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",requestID);
Game::logger->Log("MasterServer", "Affirmation complete %llu", requestID);
break;
}
@@ -718,45 +710,43 @@ void HandlePacket(Packet* packet) {
}
case MSG_MASTER_SHUTDOWN_UNIVERSE: {
Game::logger->Log("MasterServer","Received shutdown universe command, shutting down in 10 minutes.");
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",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
}

View File

@@ -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.");
mLogger->Log("ObjectIDManager", "SQL error: %s", 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;
}

View File

@@ -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();
};