From efba5dd40666596bd6c2e158bba42b0abaa9ab67 Mon Sep 17 00:00:00 2001 From: Robbe Bryssinck Date: Thu, 2 Dec 2021 19:22:03 +0100 Subject: [PATCH] fix: server id lookup bug --- .../Services/Generic/ActorValueService.cpp | 12 ++++++---- .../Services/Generic/EnvironmentService.cpp | 14 +++++++----- .../Services/Generic/InventoryService.cpp | 12 ++++++---- Code/client/Services/Generic/MagicService.cpp | 22 +++++++++++++------ Code/client/Utils.cpp | 9 +++++--- Code/client/Utils.h | 4 +++- 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/Code/client/Services/Generic/ActorValueService.cpp b/Code/client/Services/Generic/ActorValueService.cpp index 5a5967105..1680e00bf 100644 --- a/Code/client/Services/Generic/ActorValueService.cpp +++ b/Code/client/Services/Generic/ActorValueService.cpp @@ -182,10 +182,12 @@ void ActorValueService::OnHealthChange(const HealthChangeEvent& acEvent) noexcep return; } - uint32_t serverId = utils::GetServerId(*hitteeIt); - if (serverId == 0) + std::optional serverIdRes = utils::GetServerId(*hitteeIt); + if (!serverIdRes.has_value()) return; + uint32_t serverId = serverIdRes.value(); + if (acEvent.DeltaHealth > -1.0f && acEvent.DeltaHealth < 1.0f) { if (m_smallHealthChanges.find(serverId) == m_smallHealthChanges.end()) @@ -285,10 +287,12 @@ void ActorValueService::OnHealthChangeBroadcast(const NotifyHealthChangeBroadcas for (auto entity : view) { - uint32_t serverId = utils::GetServerId(entity); - if (serverId == 0) + std::optional serverIdRes = utils::GetServerId(entity); + if (!serverIdRes.has_value()) continue; + uint32_t serverId = serverIdRes.value(); + if (serverId == acMessage.Id) { const auto& formIdComponent = view.get(entity); diff --git a/Code/client/Services/Generic/EnvironmentService.cpp b/Code/client/Services/Generic/EnvironmentService.cpp index 85beee6c5..1eff2d22a 100644 --- a/Code/client/Services/Generic/EnvironmentService.cpp +++ b/Code/client/Services/Generic/EnvironmentService.cpp @@ -221,10 +221,12 @@ void EnvironmentService::OnActivate(const ActivateEvent& acEvent) noexcept if (pEntity == std::end(view)) return; - request.ActivatorId = utils::GetServerId(*pEntity); - if (request.ActivatorId == 0) + std::optional serverIdRes = utils::GetServerId(*pEntity); + if (!serverIdRes.has_value()) return; + request.ActivatorId = serverIdRes.value(); + m_transport.Send(request); } @@ -233,9 +235,11 @@ void EnvironmentService::OnActivateNotify(const NotifyActivate& acMessage) noexc auto view = m_world.view(); for (auto entity : view) { - uint32_t serverId = utils::GetServerId(entity); - if (serverId == 0) - return; + std::optional serverIdRes = utils::GetServerId(entity); + if (!serverIdRes.has_value()) + continue; + + uint32_t serverId = serverIdRes.value(); if (serverId == acMessage.ActivatorId) { diff --git a/Code/client/Services/Generic/InventoryService.cpp b/Code/client/Services/Generic/InventoryService.cpp index cfef99463..4d9ce2213 100644 --- a/Code/client/Services/Generic/InventoryService.cpp +++ b/Code/client/Services/Generic/InventoryService.cpp @@ -177,10 +177,12 @@ void InventoryService::RunCharacterInventoryUpdates() noexcept if (iter == std::end(view)) continue; - uint32_t serverId = utils::GetServerId(*iter); - if (serverId == 0) + std::optional serverIdRes = utils::GetServerId(*iter); + if (!serverIdRes.has_value()) continue; + uint32_t serverId = serverIdRes.value(); + const auto* pForm = TESForm::GetById(formId); auto* pActor = RTTI_CAST(pForm, TESForm, Actor); if (!pActor) @@ -246,10 +248,12 @@ void InventoryService::ApplyCachedCharacterInventoryChanges() noexcept auto view = m_world.view(); for (const auto entity : view) { - uint32_t serverId = utils::GetServerId(entity); - if (serverId == 0) + std::optional serverIdRes = utils::GetServerId(entity); + if (!serverIdRes.has_value()) continue; + uint32_t serverId = serverIdRes.value(); + const auto change = m_cachedCharacterInventoryChanges.find(serverId); if (change == m_cachedCharacterInventoryChanges.end()) diff --git a/Code/client/Services/Generic/MagicService.cpp b/Code/client/Services/Generic/MagicService.cpp index a5122a733..2c0230a61 100644 --- a/Code/client/Services/Generic/MagicService.cpp +++ b/Code/client/Services/Generic/MagicService.cpp @@ -58,9 +58,12 @@ void MagicService::OnUpdate(const UpdateEvent& acEvent) noexcept entt::entity entity = *it; AddTargetRequest request; - request.TargetId = utils::GetServerId(entity); - if (request.TargetId == 0) - return; + + std::optional serverIdRes = utils::GetServerId(entity); + if (!serverIdRes.has_value()) + continue; + + request.TargetId = serverIdRes.value(); if (!m_world.GetModSystem().GetServerModId(spellId, request.SpellId.ModId, request.SpellId.BaseId)) { @@ -267,10 +270,13 @@ void MagicService::OnAddTargetEvent(const AddTargetEvent& acEvent) noexcept entt::entity entity = *it; AddTargetRequest request; - request.TargetId = utils::GetServerId(entity); - if (request.TargetId == 0) + + std::optional serverIdRes = utils::GetServerId(entity); + if (!serverIdRes.has_value()) return; + request.TargetId = serverIdRes.value(); + if (!m_world.GetModSystem().GetServerModId(acEvent.SpellID, request.SpellId.ModId, request.SpellId.BaseId)) { spdlog::error("{s}: Could not find spell with form {:X}", __FUNCTION__, acEvent.SpellID); @@ -288,10 +294,12 @@ void MagicService::OnNotifyAddTarget(const NotifyAddTarget& acMessage) const noe for (auto entity : view) { - uint32_t serverId = utils::GetServerId(entity); - if (serverId == 0) + std::optional serverIdRes = utils::GetServerId(entity); + if (!serverIdRes.has_value()) continue; + uint32_t serverId = serverIdRes.value(); + if (serverId == acMessage.TargetId) { auto& formIdComponent = view.get(entity); diff --git a/Code/client/Utils.cpp b/Code/client/Utils.cpp index 6fa355603..62a2230b2 100644 --- a/Code/client/Utils.cpp +++ b/Code/client/Utils.cpp @@ -3,19 +3,22 @@ namespace utils { -uint32_t GetServerId(entt::entity aEntity) noexcept +std::optional GetServerId(entt::entity aEntity) noexcept { const auto* pLocalComponent = World::Get().try_get(aEntity); const auto* pRemoteComponent = World::Get().try_get(aEntity); - uint32_t serverId = 0; + uint32_t serverId = -1; if (pLocalComponent) serverId = pLocalComponent->Id; else if (pRemoteComponent) serverId = pRemoteComponent->Id; else + { spdlog::warn("This entity has neither a local or remote component: {:X}", aEntity); + return std::nullopt; + } - return serverId; + return {serverId}; } } diff --git a/Code/client/Utils.h b/Code/client/Utils.h index 17791d1e2..21725d892 100644 --- a/Code/client/Utils.h +++ b/Code/client/Utils.h @@ -1,5 +1,7 @@ #pragma once +#include + #if defined(TP_SKYRIM) && TP_PLATFORM_64 #define POINTER_SKYRIMSE(className, variableName, ...) static AutoPtr variableName(__VA_ARGS__) #else @@ -29,6 +31,6 @@ static void Assert(const char* apExpression, const char* apMessage) __debugbreak(); } -uint32_t GetServerId(entt::entity aEntity) noexcept; +std::optional GetServerId(entt::entity aEntity) noexcept; }