Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup objectIdManager overloading and classes #1391

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dGame/EntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions dGame/TradingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions dGame/UserManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "Character.h"
#include <BitStream.h>
#include "PacketUtils.h"
#include "../dWorldServer/ObjectIDManager.h"
#include "ObjectIDManager.h"
#include "Logger.h"
#include "GeneralUtils.h"
#include "ZoneInstanceManager.h"
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions dGame/dBehaviors/ProjectileAttackBehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<LWOOBJID>(ObjectIDManager::Instance()->GenerateObjectID());
auto id = static_cast<LWOOBJID>(ObjectIDManager::GenerateObjectID());

GeneralUtils::SetBit(id, eObjectBits::SPAWNED);

Expand Down
8 changes: 4 additions & 4 deletions dGame/dComponents/InventoryComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand All @@ -86,7 +86,7 @@ InventoryComponent::InventoryComponent(Entity* parent, tinyxml2::XMLDocument* do
const auto proxyLOT = static_cast<LOT>(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++ });
Expand Down Expand Up @@ -1341,7 +1341,7 @@ void InventoryComponent::SetNPCItems(const std::vector<LOT>& 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);

Expand Down
4 changes: 2 additions & 2 deletions dGame/dComponents/PetComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions dGame/dComponents/PropertyManagementComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions dGame/dGameMessages/GameMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dGame/dInventory/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <sstream>

#include "../dWorldServer/ObjectIDManager.h"
#include "ObjectIDManager.h"
#include "GeneralUtils.h"
#include "GameMessages.h"
#include "Entity.h"
Expand Down
4 changes: 2 additions & 2 deletions dGame/dPropertyBehaviors/ControlBehaviors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.");
Expand Down
1 change: 1 addition & 0 deletions dGame/dUtilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(DGAME_DUTILITIES_SOURCES "BrickDatabase.cpp"
"GUID.cpp"
"Loot.cpp"
"Mail.cpp"
"ObjectIDManager.cpp"
"Preconditions.cpp"
"SlashCommandHandler.cpp"
"VanityUtilities.cpp")
Expand Down
51 changes: 51 additions & 0 deletions dGame/dUtilities/ObjectIDManager.cpp
Original file line number Diff line number Diff line change
@@ -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<void(uint32_t)>& callback) : requestID(requestID), callback(callback) {}
uint64_t requestID;

std::function<void(uint32_t)> callback;
};

namespace {
std::vector<PersistentIDRequest> 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<int> Uni(10000000, INT32_MAX);
};

//! Requests a persistent ID
void ObjectIDManager::RequestPersistentID(const std::function<void(uint32_t)> 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;
}
40 changes: 40 additions & 0 deletions dGame/dUtilities/ObjectIDManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

// C++
#include <functional>
#include <vector>
#include <stdint.h>

/*!
\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<void(uint32_t)> 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();
};
4 changes: 2 additions & 2 deletions dGame/dUtilities/VanityUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "EntityInfo.h"
#include "Spawner.h"
#include "dZoneManager.h"
#include "../dWorldServer/ObjectIDManager.h"
#include "ObjectIDManager.h"
#include "Level.h"

#include <fstream>
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dMasterServer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(DMASTERSERVER_SOURCES
"InstanceManager.cpp"
"ObjectIDManager.cpp"
"PersistentIDManager.cpp"
"Start.cpp"
)

Expand Down
Loading
Loading