2021-12-05 17:54:36 +00:00
|
|
|
#include "ObjectIDManager.h"
|
|
|
|
|
|
|
|
// Custom Classes
|
|
|
|
#include "Database.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
|
|
|
#include "Game.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// Static Variables
|
2022-07-28 13:39:57 +00:00
|
|
|
ObjectIDManager* ObjectIDManager::m_Address = nullptr;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//! Initializes the manager
|
2023-10-21 23:31:55 +00:00
|
|
|
void ObjectIDManager::Initialize(Logger* logger) {
|
2022-07-28 13:39:57 +00:00
|
|
|
this->mLogger = logger;
|
|
|
|
this->currentPersistentID = 1;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
try {
|
2023-11-18 00:47:18 +00:00
|
|
|
auto lastObjectId = Database::Get()->GetCurrentPersistentId();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-18 00:47:18 +00:00
|
|
|
if (!lastObjectId) {
|
|
|
|
Database::Get()->InsertDefaultPersistentId();
|
2022-07-28 13:39:57 +00:00
|
|
|
return;
|
2023-11-18 00:47:18 +00:00
|
|
|
} else {
|
|
|
|
this->currentPersistentID = lastObjectId.value();
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-18 00:47:18 +00:00
|
|
|
if (this->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.");
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
} catch (sql::SQLException& e) {
|
2023-11-18 00:47:18 +00:00
|
|
|
LOG("Unable to fetch max persistent object ID in use. This will cause issues. Aborting to prevent collisions.");
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("SQL error: %s", e.what());
|
2023-11-18 00:47:18 +00:00
|
|
|
throw;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Generates a new persistent ID
|
2023-11-18 00:47:18 +00:00
|
|
|
uint32_t ObjectIDManager::GeneratePersistentID() {
|
2022-07-28 13:39:57 +00:00
|
|
|
uint32_t toReturn = ++this->currentPersistentID;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-18 00:47:18 +00:00
|
|
|
SaveToDatabase();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return toReturn;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectIDManager::SaveToDatabase() {
|
2023-11-18 00:47:18 +00:00
|
|
|
Database::Get()->UpdatePersistentId(this->currentPersistentID);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|