2024-01-05 12:31:22 +00:00
|
|
|
#include "PersistentIDManager.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2024-01-05 12:31:22 +00:00
|
|
|
namespace {
|
|
|
|
uint32_t CurrentPersistentID = 1; //!< The highest current persistent ID in use
|
|
|
|
};
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//! Initializes the manager
|
2024-01-05 12:31:22 +00:00
|
|
|
void PersistentIDManager::Initialize() {
|
2021-12-05 17:54:36 +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();
|
|
|
|
} else {
|
2024-01-05 12:31:22 +00:00
|
|
|
CurrentPersistentID = lastObjectId.value();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 12:31:22 +00:00
|
|
|
if (CurrentPersistentID <= 0) {
|
2023-11-18 00:47:18 +00:00
|
|
|
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.");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2024-12-04 09:30:14 +00:00
|
|
|
} catch (std::exception& 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.");
|
2024-12-04 09:30:14 +00:00
|
|
|
LOG("Error: %s", e.what());
|
2024-01-05 12:31:22 +00:00
|
|
|
throw e;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Generates a new persistent ID
|
2024-01-05 12:31:22 +00:00
|
|
|
uint32_t PersistentIDManager::GeneratePersistentID() {
|
|
|
|
uint32_t toReturn = ++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
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2024-01-05 12:31:22 +00:00
|
|
|
void PersistentIDManager::SaveToDatabase() {
|
|
|
|
Database::Get()->UpdatePersistentId(CurrentPersistentID);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|