mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-09 20:24:16 +00:00
chore: cleanup objectIdManager overloading and classes (#1391)
* objectIdManager fixes * Remove debug log
This commit is contained in:
45
dMasterServer/PersistentIDManager.cpp
Normal file
45
dMasterServer/PersistentIDManager.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "PersistentIDManager.h"
|
||||
|
||||
// Custom Classes
|
||||
#include "Database.h"
|
||||
#include "Logger.h"
|
||||
#include "Game.h"
|
||||
|
||||
namespace {
|
||||
uint32_t CurrentPersistentID = 1; //!< The highest current persistent ID in use
|
||||
};
|
||||
|
||||
//! Initializes the manager
|
||||
void PersistentIDManager::Initialize() {
|
||||
try {
|
||||
auto lastObjectId = Database::Get()->GetCurrentPersistentId();
|
||||
|
||||
if (!lastObjectId) {
|
||||
Database::Get()->InsertDefaultPersistentId();
|
||||
} else {
|
||||
CurrentPersistentID = lastObjectId.value();
|
||||
}
|
||||
|
||||
if (CurrentPersistentID <= 0) {
|
||||
LOG("Invalid persistent object ID in database. Aborting to prevent bad id generation.");
|
||||
throw std::runtime_error("Invalid persistent object ID in database. Aborting to prevent bad id generation.");
|
||||
}
|
||||
} catch (sql::SQLException& e) {
|
||||
LOG("Unable to fetch max persistent object ID in use. This will cause issues. Aborting to prevent collisions.");
|
||||
LOG("SQL error: %s", e.what());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
//! Generates a new persistent ID
|
||||
uint32_t PersistentIDManager::GeneratePersistentID() {
|
||||
uint32_t toReturn = ++CurrentPersistentID;
|
||||
|
||||
SaveToDatabase();
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
void PersistentIDManager::SaveToDatabase() {
|
||||
Database::Get()->UpdatePersistentId(CurrentPersistentID);
|
||||
}
|
Reference in New Issue
Block a user