Skip to content

Commit

Permalink
fix: server id lookup bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed Dec 2, 2021
1 parent dc300fe commit efba5dd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
12 changes: 8 additions & 4 deletions Code/client/Services/Generic/ActorValueService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ void ActorValueService::OnHealthChange(const HealthChangeEvent& acEvent) noexcep
return;
}

uint32_t serverId = utils::GetServerId(*hitteeIt);
if (serverId == 0)
std::optional<uint32_t> 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())
Expand Down Expand Up @@ -285,10 +287,12 @@ void ActorValueService::OnHealthChangeBroadcast(const NotifyHealthChangeBroadcas

for (auto entity : view)
{
uint32_t serverId = utils::GetServerId(entity);
if (serverId == 0)
std::optional<uint32_t> serverIdRes = utils::GetServerId(entity);
if (!serverIdRes.has_value())
continue;

uint32_t serverId = serverIdRes.value();

if (serverId == acMessage.Id)
{
const auto& formIdComponent = view.get<FormIdComponent>(entity);
Expand Down
14 changes: 9 additions & 5 deletions Code/client/Services/Generic/EnvironmentService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> serverIdRes = utils::GetServerId(*pEntity);
if (!serverIdRes.has_value())
return;

request.ActivatorId = serverIdRes.value();

m_transport.Send(request);
}

Expand All @@ -233,9 +235,11 @@ void EnvironmentService::OnActivateNotify(const NotifyActivate& acMessage) noexc
auto view = m_world.view<FormIdComponent>();
for (auto entity : view)
{
uint32_t serverId = utils::GetServerId(entity);
if (serverId == 0)
return;
std::optional<uint32_t> serverIdRes = utils::GetServerId(entity);
if (!serverIdRes.has_value())
continue;

uint32_t serverId = serverIdRes.value();

if (serverId == acMessage.ActivatorId)
{
Expand Down
12 changes: 8 additions & 4 deletions Code/client/Services/Generic/InventoryService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> 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)
Expand Down Expand Up @@ -246,10 +248,12 @@ void InventoryService::ApplyCachedCharacterInventoryChanges() noexcept
auto view = m_world.view<FormIdComponent>();
for (const auto entity : view)
{
uint32_t serverId = utils::GetServerId(entity);
if (serverId == 0)
std::optional<uint32_t> 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())
Expand Down
22 changes: 15 additions & 7 deletions Code/client/Services/Generic/MagicService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> 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))
{
Expand Down Expand Up @@ -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<uint32_t> 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);
Expand All @@ -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<uint32_t> serverIdRes = utils::GetServerId(entity);
if (!serverIdRes.has_value())
continue;

uint32_t serverId = serverIdRes.value();

if (serverId == acMessage.TargetId)
{
auto& formIdComponent = view.get<FormIdComponent>(entity);
Expand Down
9 changes: 6 additions & 3 deletions Code/client/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@

namespace utils
{
uint32_t GetServerId(entt::entity aEntity) noexcept
std::optional<uint32_t> GetServerId(entt::entity aEntity) noexcept
{
const auto* pLocalComponent = World::Get().try_get<LocalComponent>(aEntity);
const auto* pRemoteComponent = World::Get().try_get<RemoteComponent>(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};
}
}
4 changes: 3 additions & 1 deletion Code/client/Utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <optional>

#if defined(TP_SKYRIM) && TP_PLATFORM_64
#define POINTER_SKYRIMSE(className, variableName, ...) static AutoPtr<className> variableName(__VA_ARGS__)
#else
Expand Down Expand Up @@ -29,6 +31,6 @@ static void Assert(const char* apExpression, const char* apMessage)
__debugbreak();
}

uint32_t GetServerId(entt::entity aEntity) noexcept;
std::optional<uint32_t> GetServerId(entt::entity aEntity) noexcept;
}

0 comments on commit efba5dd

Please sign in to comment.