DarkflameServer/dWorldServer/ObjectIDManager.cpp
David Markowitz 5942182486
feat: Abstract Logger and simplify code (#1207)
* Logger: Rename logger to Logger from dLogger

* Logger: Add compile time filename

Fix include issues
Add writers
Add macros
Add macro to force compilation

* Logger: Replace calls with macros

Allows for filename and line number to be logged

* Logger: Add comments

and remove extra define

Logger: Replace with unique_ptr

also flush console at exit. regular file writer should be flushed on file close.

Logger: Remove constexpr on variable

* Logger: Simplify code

* Update Logger.cpp
2023-10-21 16:31:55 -07:00

56 lines
1.6 KiB
C++

#include "ObjectIDManager.h"
// Custom Classes
#include "MasterPackets.h"
#include "Database.h"
#include "Logger.h"
#include "Game.h"
// Static Variables
ObjectIDManager* ObjectIDManager::m_Address = nullptr;
static std::uniform_int_distribution<int> uni(10000000, INT32_MAX);
//! Initializes the manager
void ObjectIDManager::Initialize(void) {
//this->currentRequestID = 0;
this->currentObjectID = uint32_t(1152921508165007067); //Initial value for this server's objectIDs
}
//! Requests a persistent ID
void ObjectIDManager::RequestPersistentID(std::function<void(uint32_t)> callback) {
PersistentIDRequest* request = new PersistentIDRequest();
request->requestID = ++this->currentRequestID;
request->callback = callback;
this->requests.push_back(request);
MasterPackets::SendPersistentIDRequest(Game::server, request->requestID);
}
//! Handles a persistent ID response
void ObjectIDManager::HandleRequestPersistentIDResponse(uint64_t requestID, uint32_t persistentID) {
for (uint32_t i = 0; i < this->requests.size(); ++i) {
if (this->requests[i]->requestID == requestID) {
// Call the callback function
this->requests[i]->callback(persistentID);
// Then delete the request
delete this->requests[i];
this->requests.erase(this->requests.begin() + i);
return;
}
}
}
//! Handles cases where we have to get a unique object ID synchronously
uint32_t ObjectIDManager::GenerateRandomObjectID() {
return uni(Game::randomEngine);
}
//! Generates an object ID server-sided (used for regular entities like smashables)
uint32_t ObjectIDManager::GenerateObjectID(void) {
return ++this->currentObjectID;
}