From 870b56fe02f9dea5a226f3eaa5a5ce3de58831df Mon Sep 17 00:00:00 2001 From: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> Date: Fri, 5 Jan 2024 04:31:22 -0800 Subject: [PATCH] chore: cleanup objectIdManager overloading and classes (#1391) * objectIdManager fixes * Remove debug log --- dGame/EntityManager.cpp | 4 +- dGame/TradingManager.cpp | 4 +- dGame/UserManager.cpp | 4 +- dGame/dBehaviors/ProjectileAttackBehavior.cpp | 4 +- dGame/dComponents/InventoryComponent.cpp | 8 +- dGame/dComponents/PetComponent.cpp | 4 +- .../PropertyManagementComponent.cpp | 4 +- dGame/dGameMessages/GameMessages.cpp | 10 +-- dGame/dInventory/Item.cpp | 2 +- dGame/dPropertyBehaviors/ControlBehaviors.cpp | 4 +- dGame/dUtilities/CMakeLists.txt | 1 + dGame/dUtilities/ObjectIDManager.cpp | 51 +++++++++++++ dGame/dUtilities/ObjectIDManager.h | 40 ++++++++++ dGame/dUtilities/VanityUtilities.cpp | 4 +- dMasterServer/CMakeLists.txt | 2 +- dMasterServer/MasterServer.cpp | 19 ++--- dMasterServer/ObjectIDManager.h | 47 ------------ ...tIDManager.cpp => PersistentIDManager.cpp} | 27 +++---- dMasterServer/PersistentIDManager.h | 23 ++++++ .../ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp | 2 +- dWorldServer/CMakeLists.txt | 1 - dWorldServer/ObjectIDManager.cpp | 55 -------------- dWorldServer/ObjectIDManager.h | 75 ------------------- dWorldServer/WorldServer.cpp | 3 +- dZoneManager/dZoneManager.cpp | 4 +- 25 files changed, 166 insertions(+), 236 deletions(-) create mode 100644 dGame/dUtilities/ObjectIDManager.cpp create mode 100644 dGame/dUtilities/ObjectIDManager.h delete mode 100644 dMasterServer/ObjectIDManager.h rename dMasterServer/{ObjectIDManager.cpp => PersistentIDManager.cpp} (58%) create mode 100644 dMasterServer/PersistentIDManager.h delete mode 100644 dWorldServer/ObjectIDManager.cpp delete mode 100644 dWorldServer/ObjectIDManager.h diff --git a/dGame/EntityManager.cpp b/dGame/EntityManager.cpp index 493e910e..a098dbcf 100644 --- a/dGame/EntityManager.cpp +++ b/dGame/EntityManager.cpp @@ -2,7 +2,7 @@ #include "RakNetTypes.h" #include "Game.h" #include "User.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "Character.h" #include "GeneralUtils.h" #include "dServer.h" @@ -89,7 +89,7 @@ Entity* EntityManager::CreateEntity(EntityInfo info, User* user, Entity* parentE // Entities with no ID already set, often spawned entities, we'll generate a new sequencial ID if (info.id == 0) { - id = ObjectIDManager::Instance()->GenerateObjectID(); + id = ObjectIDManager::GenerateObjectID(); } // Entities with an ID already set, often level entities, we'll use that ID as a base diff --git a/dGame/TradingManager.cpp b/dGame/TradingManager.cpp index 7c3c9bb6..3d9a209c 100644 --- a/dGame/TradingManager.cpp +++ b/dGame/TradingManager.cpp @@ -2,7 +2,7 @@ #include "EntityManager.h" #include "GameMessages.h" #include "InventoryComponent.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "Game.h" #include "Logger.h" #include "Item.h" @@ -273,7 +273,7 @@ void TradingManager::CancelTrade(LWOOBJID tradeId) { } Trade* TradingManager::NewTrade(LWOOBJID participantA, LWOOBJID participantB) { - const LWOOBJID tradeId = ObjectIDManager::Instance()->GenerateObjectID(); + const LWOOBJID tradeId = ObjectIDManager::GenerateObjectID(); auto* trade = new Trade(tradeId, participantA, participantB); diff --git a/dGame/UserManager.cpp b/dGame/UserManager.cpp index 9afd38b2..1fed7907 100644 --- a/dGame/UserManager.cpp +++ b/dGame/UserManager.cpp @@ -12,7 +12,7 @@ #include "Character.h" #include #include "PacketUtils.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "Logger.h" #include "GeneralUtils.h" #include "ZoneInstanceManager.h" @@ -263,7 +263,7 @@ void UserManager::CreateCharacter(const SystemAddress& sysAddr, Packet* packet) } //Now that the name is ok, we can get an objectID from Master: - ObjectIDManager::Instance()->RequestPersistentID([=, this](uint32_t objectID) { + ObjectIDManager::RequestPersistentID([=, this](uint32_t objectID) { if (Database::Get()->GetCharacterInfo(objectID)) { LOG("Character object id unavailable, check object_id_tracker!"); WorldPackets::SendCharacterCreationResponse(sysAddr, eCharacterCreationResponse::OBJECT_ID_UNAVAILABLE); diff --git a/dGame/dBehaviors/ProjectileAttackBehavior.cpp b/dGame/dBehaviors/ProjectileAttackBehavior.cpp index c63a3d6f..732ff186 100644 --- a/dGame/dBehaviors/ProjectileAttackBehavior.cpp +++ b/dGame/dBehaviors/ProjectileAttackBehavior.cpp @@ -5,7 +5,7 @@ #include "Game.h" #include "Logger.h" #include "SkillComponent.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "eObjectBits.h" void ProjectileAttackBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) { @@ -106,7 +106,7 @@ void ProjectileAttackBehavior::Calculate(BehaviorContext* context, RakNet::BitSt const auto maxTime = this->m_maxDistance / this->m_projectileSpeed; for (auto i = 0u; i < this->m_projectileCount; ++i) { - auto id = static_cast(ObjectIDManager::Instance()->GenerateObjectID()); + auto id = static_cast(ObjectIDManager::GenerateObjectID()); GeneralUtils::SetBit(id, eObjectBits::SPAWNED); diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index fc379398..92b5171a 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -7,7 +7,7 @@ #include "Game.h" #include "Logger.h" #include "CDClientManager.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "MissionComponent.h" #include "GameMessages.h" #include "SkillComponent.h" @@ -68,7 +68,7 @@ InventoryComponent::InventoryComponent(Entity* parent, tinyxml2::XMLDocument* do continue; } - const LWOOBJID id = ObjectIDManager::Instance()->GenerateObjectID(); + const LWOOBJID id = ObjectIDManager::GenerateObjectID(); const auto& info = Inventory::FindItemComponent(item.itemid); @@ -86,7 +86,7 @@ InventoryComponent::InventoryComponent(Entity* parent, tinyxml2::XMLDocument* do const auto proxyLOT = static_cast(std::stoi(proxyLotAsString)); const auto& proxyInfo = Inventory::FindItemComponent(proxyLOT); - const LWOOBJID proxyId = ObjectIDManager::Instance()->GenerateObjectID(); + const LWOOBJID proxyId = ObjectIDManager::GenerateObjectID(); // Use item.count since we equip item.count number of the item this is a requested proxy of UpdateSlot(proxyInfo.equipLocation, { proxyId, proxyLOT, item.count, slot++ }); @@ -1341,7 +1341,7 @@ void InventoryComponent::SetNPCItems(const std::vector& items) { auto slot = 0u; for (const auto& item : items) { - const LWOOBJID id = ObjectIDManager::Instance()->GenerateObjectID(); + const LWOOBJID id = ObjectIDManager::GenerateObjectID(); const auto& info = Inventory::FindItemComponent(item); diff --git a/dGame/dComponents/PetComponent.cpp b/dGame/dComponents/PetComponent.cpp index 901d7485..902edffe 100644 --- a/dGame/dComponents/PetComponent.cpp +++ b/dGame/dComponents/PetComponent.cpp @@ -13,7 +13,7 @@ #include "DestroyableComponent.h" #include "dpWorld.h" #include "PetDigServer.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "eUnequippableActiveType.h" #include "eTerminateType.h" #include "ePetTamingNotifyType.h" @@ -562,7 +562,7 @@ void PetComponent::NotifyTamingBuildSuccess(NiPoint3 position) { return; } - LWOOBJID petSubKey = ObjectIDManager::Instance()->GenerateRandomObjectID(); + LWOOBJID petSubKey = ObjectIDManager::GenerateRandomObjectID(); GeneralUtils::SetBit(petSubKey, eObjectBits::CHARACTER); GeneralUtils::SetBit(petSubKey, eObjectBits::PERSISTENT); diff --git a/dGame/dComponents/PropertyManagementComponent.cpp b/dGame/dComponents/PropertyManagementComponent.cpp index de37308f..833b7d73 100644 --- a/dGame/dComponents/PropertyManagementComponent.cpp +++ b/dGame/dComponents/PropertyManagementComponent.cpp @@ -13,7 +13,7 @@ #include "Game.h" #include "Item.h" #include "Database.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "Player.h" #include "RocketLaunchpadControlComponent.h" #include "PropertyEntranceComponent.h" @@ -334,7 +334,7 @@ void PropertyManagementComponent::UpdateModelPosition(const LWOOBJID id, const N node->position = position; node->rotation = rotation; - ObjectIDManager::Instance()->RequestPersistentID([this, node, modelLOT, entity, position, rotation, originalRotation](uint32_t persistentId) { + ObjectIDManager::RequestPersistentID([this, node, modelLOT, entity, position, rotation, originalRotation](uint32_t persistentId) { SpawnerInfo info{}; info.templateID = modelLOT; diff --git a/dGame/dGameMessages/GameMessages.cpp b/dGame/dGameMessages/GameMessages.cpp index 5d3384cc..8e20e3a9 100644 --- a/dGame/dGameMessages/GameMessages.cpp +++ b/dGame/dGameMessages/GameMessages.cpp @@ -14,7 +14,7 @@ #include "EntityManager.h" #include "Database.h" #include "dServer.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "CppScripts.h" #include "UserManager.h" #include "ZoneInstanceManager.h" @@ -1045,7 +1045,7 @@ void GameMessages::SendDropClientLoot(Entity* entity, const LWOOBJID& sourceID, LWOOBJID owner = entity->GetObjectID(); if (item != LOT_NULL && item != 0) { - lootID = ObjectIDManager::Instance()->GenerateObjectID(); + lootID = ObjectIDManager::GenerateObjectID(); Loot::Info info; info.id = lootID; @@ -2565,12 +2565,12 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream* inStream, Entity* ent //But we don't want the server to go unresponsive, because then the client would disconnect. //We need to get a new ID for our model first: - ObjectIDManager::Instance()->RequestPersistentID([=](uint32_t newID) { + ObjectIDManager::RequestPersistentID([=](uint32_t newID) { LWOOBJID newIDL = newID; GeneralUtils::SetBit(newIDL, eObjectBits::CHARACTER); GeneralUtils::SetBit(newIDL, eObjectBits::PERSISTENT); - uint32_t blueprintIDSmall = ObjectIDManager::Instance()->GenerateRandomObjectID(); + uint32_t blueprintIDSmall = ObjectIDManager::GenerateRandomObjectID(); LWOOBJID blueprintID = blueprintIDSmall; GeneralUtils::SetBit(blueprintID, eObjectBits::CHARACTER); GeneralUtils::SetBit(blueprintID, eObjectBits::PERSISTENT); @@ -5565,7 +5565,7 @@ void GameMessages::HandleModularBuildFinish(RakNet::BitStream* inStream, Entity* } } - ObjectIDManager::Instance()->RequestPersistentID([=](uint32_t newId) { + ObjectIDManager::RequestPersistentID([=](uint32_t newId) { LOG("Build finished"); GameMessages::SendFinishArrangingWithItem(character, entity->GetObjectID()); // kick them from modular build GameMessages::SendModularBuildEnd(character); // i dont know if this does anything but DLUv2 did it diff --git a/dGame/dInventory/Item.cpp b/dGame/dInventory/Item.cpp index e68cdebc..4a13cd92 100644 --- a/dGame/dInventory/Item.cpp +++ b/dGame/dInventory/Item.cpp @@ -2,7 +2,7 @@ #include -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "GeneralUtils.h" #include "GameMessages.h" #include "Entity.h" diff --git a/dGame/dPropertyBehaviors/ControlBehaviors.cpp b/dGame/dPropertyBehaviors/ControlBehaviors.cpp index 248ce4e7..c541257c 100644 --- a/dGame/dPropertyBehaviors/ControlBehaviors.cpp +++ b/dGame/dPropertyBehaviors/ControlBehaviors.cpp @@ -5,7 +5,7 @@ #include "Game.h" #include "GameMessages.h" #include "ModelComponent.h" -#include "../../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "Logger.h" #include "BehaviorStates.h" #include "AssetManager.h" @@ -31,7 +31,7 @@ #include "UpdateStripUiMessage.h" void ControlBehaviors::RequestUpdatedID(ControlBehaviorContext& context) { - ObjectIDManager::Instance()->RequestPersistentID( + ObjectIDManager::RequestPersistentID( [context](uint32_t persistentId) { if (!context) { LOG("Model to update behavior ID for is null. Cannot update ID."); diff --git a/dGame/dUtilities/CMakeLists.txt b/dGame/dUtilities/CMakeLists.txt index b6bb6059..055cc706 100644 --- a/dGame/dUtilities/CMakeLists.txt +++ b/dGame/dUtilities/CMakeLists.txt @@ -3,6 +3,7 @@ set(DGAME_DUTILITIES_SOURCES "BrickDatabase.cpp" "GUID.cpp" "Loot.cpp" "Mail.cpp" + "ObjectIDManager.cpp" "Preconditions.cpp" "SlashCommandHandler.cpp" "VanityUtilities.cpp") diff --git a/dGame/dUtilities/ObjectIDManager.cpp b/dGame/dUtilities/ObjectIDManager.cpp new file mode 100644 index 00000000..a30ede05 --- /dev/null +++ b/dGame/dUtilities/ObjectIDManager.cpp @@ -0,0 +1,51 @@ +#include "ObjectIDManager.h" + +// Custom Classes +#include "MasterPackets.h" +#include "Database.h" +#include "Logger.h" +#include "Game.h" + + //! The persistent ID request +struct PersistentIDRequest { + PersistentIDRequest(const uint64_t& requestID, const std::function& callback) : requestID(requestID), callback(callback) {} + uint64_t requestID; + + std::function callback; +}; + +namespace { + std::vector Requests; //!< All outstanding persistent ID requests + uint64_t CurrentRequestID = 0; //!< The current request ID + uint32_t CurrentObjectID = uint32_t(1152921508165007067); //!< The current object ID + std::uniform_int_distribution Uni(10000000, INT32_MAX); +}; + +//! Requests a persistent ID +void ObjectIDManager::RequestPersistentID(const std::function callback) { + const auto& request = Requests.emplace_back(++CurrentRequestID, callback); + + MasterPackets::SendPersistentIDRequest(Game::server, request.requestID); +} + +//! Handles a persistent ID response +void ObjectIDManager::HandleRequestPersistentIDResponse(const uint64_t requestID, const uint32_t persistentID) { + auto it = std::find_if(Requests.begin(), Requests.end(), [requestID](const PersistentIDRequest& request) { + return request.requestID == requestID; + }); + + if (it == Requests.end()) return; + + it->callback(persistentID); + Requests.erase(it); +} + +//! 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() { + return ++CurrentObjectID; +} diff --git a/dGame/dUtilities/ObjectIDManager.h b/dGame/dUtilities/ObjectIDManager.h new file mode 100644 index 00000000..7650e4cc --- /dev/null +++ b/dGame/dUtilities/ObjectIDManager.h @@ -0,0 +1,40 @@ +#pragma once + +// C++ +#include +#include +#include + +/*! + \file ObjectIDManager.h + \brief A manager for handling object ID generation + */ + +//! The Object ID Manager +namespace ObjectIDManager { + //! Requests a persistent ID + /*! + \param callback The callback function + */ + void RequestPersistentID(const std::function callback); + + + //! Handles a persistent ID response + /*! + \param requestID The request ID + \param persistentID The persistent ID + */ + void HandleRequestPersistentIDResponse(const uint64_t requestID, const uint32_t persistentID); + + //! Generates an object ID server-sided + /*! + \return A generated object ID + */ + uint32_t GenerateObjectID(); + + //! Generates a random object ID server-sided + /*! + \return A generated object ID + */ + uint32_t GenerateRandomObjectID(); +}; diff --git a/dGame/dUtilities/VanityUtilities.cpp b/dGame/dUtilities/VanityUtilities.cpp index 6d5f996c..fa1a3eac 100644 --- a/dGame/dUtilities/VanityUtilities.cpp +++ b/dGame/dUtilities/VanityUtilities.cpp @@ -17,7 +17,7 @@ #include "EntityInfo.h" #include "Spawner.h" #include "dZoneManager.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "Level.h" #include @@ -182,7 +182,7 @@ LWOOBJID VanityUtilities::SpawnSpawner(LOT lot, const NiPoint3& position, const obj.lot = lot; // guratantee we have no collisions do { - obj.id = ObjectIDManager::Instance()->GenerateObjectID(); + obj.id = ObjectIDManager::GenerateObjectID(); } while(Game::zoneManager->GetSpawner(obj.id)); obj.position = position; obj.rotation = rotation; diff --git a/dMasterServer/CMakeLists.txt b/dMasterServer/CMakeLists.txt index 2ebdee37..108cddc2 100644 --- a/dMasterServer/CMakeLists.txt +++ b/dMasterServer/CMakeLists.txt @@ -1,6 +1,6 @@ set(DMASTERSERVER_SOURCES "InstanceManager.cpp" - "ObjectIDManager.cpp" + "PersistentIDManager.cpp" "Start.cpp" ) diff --git a/dMasterServer/MasterServer.cpp b/dMasterServer/MasterServer.cpp index 15d8db15..e5997b51 100644 --- a/dMasterServer/MasterServer.cpp +++ b/dMasterServer/MasterServer.cpp @@ -35,7 +35,7 @@ #include "Game.h" #include "InstanceManager.h" #include "MasterPackets.h" -#include "ObjectIDManager.h" +#include "PersistentIDManager.h" #include "PacketUtils.h" #include "FdbToSqlite.h" #include "BitStreamUtils.h" @@ -134,7 +134,7 @@ int main(int argc, char** argv) { if (!resServerPathExists) { LOG("%s does not exist, creating it.", (resServerPath).c_str()); - if(!std::filesystem::create_directories(resServerPath)){ + if (!std::filesystem::create_directories(resServerPath)) { LOG("Failed to create %s", (resServerPath).string().c_str()); return EXIT_FAILURE; } @@ -257,8 +257,8 @@ int main(int argc, char** argv) { //Create account try { Database::Get()->InsertNewAccount(username, std::string(hash, BCRYPT_HASHSIZE)); - } catch(sql::SQLException& e) { - LOG("A SQL error occurred!:\n %s", e.what()); + } catch (sql::SQLException& e) { + LOG("A SQL error occurred!:\n %s", e.what()); return EXIT_FAILURE; } @@ -288,7 +288,7 @@ int main(int argc, char** argv) { Database::Get()->SetMasterIp(master_server_ip, Game::server->GetPort()); //Create additional objects here: - ObjectIDManager::Instance()->Initialize(Game::logger); + PersistentIDManager::Initialize(); Game::im = new InstanceManager(Game::logger, Game::server->GetIP()); //Depending on the config, start up servers: @@ -464,7 +464,7 @@ void HandlePacket(Packet* packet) { uint64_t requestID = 0; inStream.Read(requestID); - uint32_t objID = ObjectIDManager::Instance()->GeneratePersistentID(); + uint32_t objID = PersistentIDManager::GeneratePersistentID(); MasterPackets::SendPersistentIDResponse(Game::server, packet->systemAddress, requestID, objID); break; } @@ -822,11 +822,8 @@ int ShutdownSequence(int32_t signal) { LOG("Triggered master shutdown"); } - auto* objIdManager = ObjectIDManager::TryInstance(); - if (objIdManager) { - objIdManager->SaveToDatabase(); - LOG("Saved ObjectIDTracker to DB"); - } + PersistentIDManager::SaveToDatabase(); + LOG("Saved ObjectIDTracker to DB"); // A server might not be finished spinning up yet, remove all of those here. for (auto* instance : Game::im->GetInstances()) { diff --git a/dMasterServer/ObjectIDManager.h b/dMasterServer/ObjectIDManager.h deleted file mode 100644 index cdb6dcdf..00000000 --- a/dMasterServer/ObjectIDManager.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -// C++ -#include - -class Logger; - -/*! - \file ObjectIDManager.hpp - \brief A manager that handles requests for object IDs - */ - - //! The Object ID Manager -class ObjectIDManager { -private: - Logger* mLogger; - 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(Logger* logger); - - //! Generates a new persistent ID - /*! - \return The new persistent ID - */ - uint32_t GeneratePersistentID(); - - void SaveToDatabase(); -}; diff --git a/dMasterServer/ObjectIDManager.cpp b/dMasterServer/PersistentIDManager.cpp similarity index 58% rename from dMasterServer/ObjectIDManager.cpp rename to dMasterServer/PersistentIDManager.cpp index 4739addf..3e56fadc 100644 --- a/dMasterServer/ObjectIDManager.cpp +++ b/dMasterServer/PersistentIDManager.cpp @@ -1,48 +1,45 @@ -#include "ObjectIDManager.h" +#include "PersistentIDManager.h" // Custom Classes #include "Database.h" #include "Logger.h" #include "Game.h" -// Static Variables -ObjectIDManager* ObjectIDManager::m_Address = nullptr; +namespace { + uint32_t CurrentPersistentID = 1; //!< The highest current persistent ID in use +}; //! Initializes the manager -void ObjectIDManager::Initialize(Logger* logger) { - this->mLogger = logger; - this->currentPersistentID = 1; - +void PersistentIDManager::Initialize() { try { auto lastObjectId = Database::Get()->GetCurrentPersistentId(); if (!lastObjectId) { Database::Get()->InsertDefaultPersistentId(); - return; } else { - this->currentPersistentID = lastObjectId.value(); + CurrentPersistentID = lastObjectId.value(); } - if (this->currentPersistentID <= 0) { + 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; + throw e; } } //! Generates a new persistent ID -uint32_t ObjectIDManager::GeneratePersistentID() { - uint32_t toReturn = ++this->currentPersistentID; +uint32_t PersistentIDManager::GeneratePersistentID() { + uint32_t toReturn = ++CurrentPersistentID; SaveToDatabase(); return toReturn; } -void ObjectIDManager::SaveToDatabase() { - Database::Get()->UpdatePersistentId(this->currentPersistentID); +void PersistentIDManager::SaveToDatabase() { + Database::Get()->UpdatePersistentId(CurrentPersistentID); } diff --git a/dMasterServer/PersistentIDManager.h b/dMasterServer/PersistentIDManager.h new file mode 100644 index 00000000..916ee33a --- /dev/null +++ b/dMasterServer/PersistentIDManager.h @@ -0,0 +1,23 @@ +#pragma once + +// C++ +#include + +/*! + \file PersistentIDManager.h + \brief A manager that handles requests for object IDs + */ + + //! The Object ID Manager +namespace PersistentIDManager { + //! Initializes the manager + void Initialize(); + + //! Generates a new persistent ID + /*! + \return The new persistent ID + */ + uint32_t GeneratePersistentID(); + + void SaveToDatabase(); +}; diff --git a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp index 55c82b95..1a298843 100644 --- a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp +++ b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp @@ -9,7 +9,7 @@ #include "CharacterComponent.h" #include "SimplePhysicsComponent.h" #include "MovementAIComponent.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" #include "MissionComponent.h" #include "Loot.h" #include "InventoryComponent.h" diff --git a/dWorldServer/CMakeLists.txt b/dWorldServer/CMakeLists.txt index a790487e..336a60f9 100644 --- a/dWorldServer/CMakeLists.txt +++ b/dWorldServer/CMakeLists.txt @@ -1,5 +1,4 @@ set(DWORLDSERVER_SOURCES - "ObjectIDManager.cpp" "PerformanceManager.cpp" ) diff --git a/dWorldServer/ObjectIDManager.cpp b/dWorldServer/ObjectIDManager.cpp deleted file mode 100644 index 44a45437..00000000 --- a/dWorldServer/ObjectIDManager.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#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 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 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; -} diff --git a/dWorldServer/ObjectIDManager.h b/dWorldServer/ObjectIDManager.h deleted file mode 100644 index deef89fd..00000000 --- a/dWorldServer/ObjectIDManager.h +++ /dev/null @@ -1,75 +0,0 @@ -#pragma once - -// C++ -#include -#include -#include - -/*! - \file ObjectIDManager.hpp - \brief A manager for handling object ID generation - */ - - //! The persistent ID request -struct PersistentIDRequest { - uint64_t requestID; - - std::function callback; -}; - -//! The Object ID Manager -class ObjectIDManager { -private: - static ObjectIDManager* m_Address; //!< The singleton instance - - std::vector requests; //!< All outstanding persistent ID requests - uint64_t currentRequestID; //!< The current request ID - - uint32_t currentObjectID; //!< The current object ID - -public: - - //! The singleton instance - static ObjectIDManager* Instance() { - if (m_Address == 0) { - m_Address = new ObjectIDManager; - } - - return m_Address; - } - - //! Initializes the manager - void Initialize(void); - - //! Requests a persistent ID - /*! - \param callback The callback function - */ - void RequestPersistentID(std::function callback); - - - //! Handles a persistent ID response - /*! - \param requestID The request ID - \param persistentID The persistent ID - */ - void HandleRequestPersistentIDResponse(uint64_t requestID, uint32_t persistentID); - - //! Generates an object ID server-sided - /*! - \return A generated object ID - */ - uint32_t GenerateObjectID(void); - - //! Generates a random object ID server-sided - /*! - \return A generated object ID - */ - static uint32_t GenerateRandomObjectID(); - - //! Generates a persistent object ID server-sided - /*! - \return A generated object ID - */ - uint32_t GeneratePersistentObjectID(void); -}; diff --git a/dWorldServer/WorldServer.cpp b/dWorldServer/WorldServer.cpp index c8866b13..f911f804 100644 --- a/dWorldServer/WorldServer.cpp +++ b/dWorldServer/WorldServer.cpp @@ -208,7 +208,6 @@ int main(int argc, char** argv) { masterPort = masterInfo->port; } - ObjectIDManager::Instance()->Initialize(); UserManager::Instance()->Initialize(); bool dontGenerateDCF = false; @@ -736,7 +735,7 @@ void HandlePacket(Packet* packet) { case eMasterMessageType::REQUEST_PERSISTENT_ID_RESPONSE: { uint64_t requestID = PacketUtils::ReadU64(8, packet); uint32_t objectID = PacketUtils::ReadU32(16, packet); - ObjectIDManager::Instance()->HandleRequestPersistentIDResponse(requestID, objectID); + ObjectIDManager::HandleRequestPersistentIDResponse(requestID, objectID); break; } diff --git a/dZoneManager/dZoneManager.cpp b/dZoneManager/dZoneManager.cpp index a6ce0944..3aa35485 100644 --- a/dZoneManager/dZoneManager.cpp +++ b/dZoneManager/dZoneManager.cpp @@ -15,7 +15,7 @@ #include "CDZoneTableTable.h" #include "AssetManager.h" -#include "../dWorldServer/ObjectIDManager.h" +#include "ObjectIDManager.h" void dZoneManager::Initialize(const LWOZONEID& zoneID) { LOG("Preparing zone: %i/%i/%i", zoneID.GetMapID(), zoneID.GetInstanceID(), zoneID.GetCloneID()); @@ -112,7 +112,7 @@ LWOOBJID dZoneManager::MakeSpawner(SpawnerInfo info) { auto objectId = info.spawnerID; if (objectId == LWOOBJID_EMPTY) { - objectId = ObjectIDManager::Instance()->GenerateObjectID(); + objectId = ObjectIDManager::GenerateObjectID(); GeneralUtils::SetBit(objectId, eObjectBits::CLIENT); info.spawnerID = objectId;