Skip to content
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(ChaosMod CXX)

# Find latest version at https://github.com/OrfeasZ/ZHMModSDK/releases
# Set ZHMMODSDK_DIR variable to a local directory to use a local copy of the ZHMModSDK.
set(ZHMMODSDK_VER "v4.1.1")
set(ZHMMODSDK_VER "v4.2.1")
include(cmake/setup-zhmmodsdk.cmake)

# Set C++ standard to C++23.
Expand Down
25 changes: 21 additions & 4 deletions src/Effects/Player/ZAuthoritativeFigureEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <Globals.h>
#include <Logging.h>
#include <imgui.h>

#include "Registry.h"
#include "Helpers/Math.h"
Expand Down Expand Up @@ -41,8 +42,8 @@ void ZAuthoritativeFigureEffect::Start()
m_bOriginalWeaponsAllowed = m_SelectedOutfit.m_pInterfaceRef->m_bWeaponsAllowed;

// make outfit a authoritative figure
m_SelectedOutfit.m_pInterfaceRef->m_bAuthorityFigure = true;
m_SelectedOutfit.m_pInterfaceRef->m_bWeaponsAllowed = true;
// m_SelectedOutfit.m_pInterfaceRef->m_bAuthorityFigure = true;
// m_SelectedOutfit.m_pInterfaceRef->m_bWeaponsAllowed = true;
Comment thread
shadow578 marked this conversation as resolved.
Resolved
}

void ZAuthoritativeFigureEffect::Stop()
Expand All @@ -52,13 +53,29 @@ void ZAuthoritativeFigureEffect::Stop()
return;
}

m_SelectedOutfit.m_pInterfaceRef->m_bAuthorityFigure = m_bOriginalAuthorityFigure;
m_SelectedOutfit.m_pInterfaceRef->m_bWeaponsAllowed = m_bOriginalWeaponsAllowed;
// m_SelectedOutfit.m_pInterfaceRef->m_bAuthorityFigure = m_bOriginalAuthorityFigure;
// m_SelectedOutfit.m_pInterfaceRef->m_bWeaponsAllowed = m_bOriginalWeaponsAllowed;
}

bool ZAuthoritativeFigureEffect::Available() const
{
return IChaosEffect::Available() && Globals::ContentKitManager != nullptr;
}

void ZAuthoritativeFigureEffect::OnDrawDebugUI()
{
ImGui::TextUnformatted("Outfits:");
for (auto s_sOutfitName : m_aOutfitCommonNames)
{
if (ImGui::Button(s_sOutfitName.c_str()))
{
if (!Utils::SetPlayerOutfit(s_sOutfitName, m_SelectedOutfit))
{
Logger::Error(TAG "Failed to set outfit.");
return;
}
}
}
}

REGISTER_CHAOS_EFFECT(ZAuthoritativeFigureEffect);
1 change: 1 addition & 0 deletions src/Effects/Player/ZAuthoritativeFigureEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ZAuthoritativeFigureEffect : public IChaosEffect
void Start() override;
void Stop() override;
bool Available() const override;
void OnDrawDebugUI() override;

std::string GetDisplayName(const bool p_bVoting) const override
{
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Bindings/EntityBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct SEntityBinding

operator bool() const
{
return !!m_rEntity;
return m_rEntity && Utils::IsReasonableEntityRef(m_rEntity);
}
};

Expand Down
12 changes: 7 additions & 5 deletions src/Entity/Bindings/SSpeakEntitySounddefBinding.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#pragma once
#include "Entity/Bindings/EntityBinding.h"

class ZActor;

// [modules:/zspeakentity_sounddef.class].pc_entitytype
struct SSpeakEntitySounddefBinding : SEntityBinding
{
using SEntityBinding::SEntityBinding;

PROPERTY(ZEntityRef, m_rActor); // actor who will speak
PROPERTY_RO(ZEntityRef, m_rSpeakingActor); // while speaking == m_rActor, null when not speaking
PROPERTY(EGestureCategory, m_eGesture); // gesture to play while speaking
PROPERTY(EActorSoundDefs, m_eSoundDef); // sounddef to speak
PROPERTY(bool, m_bAllowInterrupt); // allow interrupting currently playing speech with this one
PROPERTY(ZEntityRef, m_rActor); // actor who will speak
PROPERTY_RO(TEntityRef<ZActor>, m_rSpeakingActor); // while speaking == m_rActor, null when not speaking
PROPERTY(EGestureCategory, m_eGesture); // gesture to play while speaking
PROPERTY(EActorSoundDefs, m_eSoundDef); // sounddef to speak
PROPERTY(bool, m_bAllowInterrupt); // allow interrupting currently playing speech with this one
PROPERTY(bool, m_bEnableCulling);

INPUT_PIN(Start); // start speaking
Expand Down
4 changes: 3 additions & 1 deletion src/Helpers/ActorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ std::vector<TEntityRef<ZActor>> Utils::GetActors(const bool p_bIncludeDead, cons

for (auto& s_rActor : Globals::ActorManager->m_activatedActors)
{
if (!s_rActor)
if (!s_rActor
|| !Utils::IsReasonableEntityRef(s_rActor)
|| !s_rActor.m_pInterfaceRef->m_bEnabled)
{
continue;
}
Expand Down
75 changes: 42 additions & 33 deletions src/Helpers/EntityUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <Logging.h>

#include <queue>
#include <limits>

#define TAG "[EntityUtils] "

Expand All @@ -33,48 +32,52 @@ std::vector<ZEntityRef> Utils::ZEntityFinder::Find(const size_t p_nMaxResults) c

for (const auto& s_Brick : s_pSceneCtx->m_aLoadedBricks)
{
const auto s_Entity = s_Brick.m_EntityRef;
if (!s_Entity)
const auto s_rEntity = s_Brick.m_EntityRef;
if (!Utils::IsReasonableEntityRef(s_rEntity))
{
continue;
}

const auto s_pBpFactory = s_Entity.GetBlueprintFactory();
if (!s_pBpFactory)
const auto s_pBpFactory = s_rEntity.GetBlueprintFactory();
if (!s_pBpFactory || !Utils::IsReasonablePointer(s_pBpFactory))
{
continue;
}

s_aNodeQueue.emplace_back(s_pBpFactory, s_Entity);
s_aNodeQueue.emplace_back(s_pBpFactory, s_rEntity);
}

while (s_nCurrentQueueIndex < s_aNodeQueue.size())
{
auto [s_pCurrentFactory, s_CurrentRoot] = s_aNodeQueue[s_nCurrentQueueIndex];
auto [s_pCurrentFactory, s_rCurrentRoot] = s_aNodeQueue[s_nCurrentQueueIndex];
s_nCurrentQueueIndex++;

// go through each sub-entity
const auto s_SubEntityCount = s_pCurrentFactory->GetSubEntitiesCount();
for (int i = 0; i < s_SubEntityCount; ++i)
const auto s_nSubEntityCount = s_pCurrentFactory->GetSubEntitiesCount();
for (int i = 0; i < s_nSubEntityCount; ++i)
{
const ZEntityRef s_SubEntity = s_pCurrentFactory->GetSubEntity(s_CurrentRoot.m_pObj, i);
const ZEntityRef s_rSubEntity = s_pCurrentFactory->GetSubEntity(s_rCurrentRoot.m_pObj, i);
const auto s_pSubEntityFactory = s_pCurrentFactory->GetSubEntityBlueprint(i);

if (!s_SubEntity.GetEntity() || !s_SubEntity->GetType())
if (!s_rSubEntity
|| !s_rSubEntity->GetType()
|| !Utils::IsReasonableEntityRef(s_rSubEntity))
Comment thread
shadow578 marked this conversation as resolved.
Resolved
{
continue;
}

// if the sub-entity has a factory with more sub-entities, add it to the queue.
if (s_pSubEntityFactory && s_pSubEntityFactory->GetSubEntitiesCount() > 0)
if (s_pSubEntityFactory
&& Utils::IsReasonablePointer(s_pSubEntityFactory)
&& s_pSubEntityFactory->GetSubEntitiesCount() > 0)
{
s_aNodeQueue.emplace_back(s_pSubEntityFactory, s_SubEntity);
s_aNodeQueue.emplace_back(s_pSubEntityFactory, s_rSubEntity);
}

// add if matches
if (Evaluate(s_SubEntity, s_pCurrentFactory, s_pSubEntityFactory, i))
if (Evaluate(s_rSubEntity, s_pCurrentFactory, s_pSubEntityFactory, i))
{
s_aFoundEntities.push_back(s_SubEntity);
s_aFoundEntities.push_back(s_rSubEntity);

// check max results limit
if (p_nMaxResults != 0 && s_aFoundEntities.size() >= p_nMaxResults)
Expand Down Expand Up @@ -134,7 +137,7 @@ bool Utils::ZEntityFinder::Evaluate(const ZEntityRef& p_rEntity, ZEntityBlueprin

std::string_view Utils::GetEntityName(const ZEntityRef& p_Entity, ZEntityBlueprintFactoryBase* p_pFactory, int p_nSubIndex)
{
if (!p_Entity)
if (!IsReasonableEntityRef(p_Entity))
{
return "";
}
Expand Down Expand Up @@ -187,7 +190,7 @@ std::string_view Utils::GetEntityName(const ZEntityRef& p_Entity, ZEntityBluepri

std::string_view Utils::GetEntityTypeName(const ZEntityRef& p_Entity)
{
if (!p_Entity)
if (!IsReasonableEntityRef(p_Entity))
{
return "";
}
Expand Down Expand Up @@ -216,28 +219,15 @@ std::string_view Utils::GetEntityTypeName(const ZEntityRef& p_Entity)
/// Heuristic checks for validity of blueprint factory pointers, checking for common failure cases after.
static bool IsValidBlueprintFactoryPointer(ZEntityBlueprintFactoryBase* p_pBpFactory)
{
// nullptr?
if (!p_pBpFactory)
if (!Utils::IsReasonablePointer(p_pBpFactory))
{
#if _DEBUG
Logger::Error(TAG "Blueprint factory pointer is null.");
Logger::Error(TAG "Got likely invalid blueprint factory pointer {:016X}.", reinterpret_cast<uintptr_t>(p_pBpFactory));
#endif

return false;
}

// seen invalid pointers with all bits set, check that too
std::uintptr_t s_nPtr = reinterpret_cast<std::uintptr_t>(p_pBpFactory);
if (s_nPtr == std::numeric_limits<std::uintptr_t>::max())
{
#if _DEBUG
Logger::Error(TAG "Blueprint factory pointer is 0xFFFF(...).");
#endif

return false;
}

// hopefully OK
return true;
}

Expand Down Expand Up @@ -307,7 +297,7 @@ ZEntityBlueprintFactoryBase* Utils::GetEntityBlueprintFactoryFor(ZEntityRef p_rE

ZEntityRef Utils::GetSubEntity(const ZEntityRef p_rParent, const uint64_t p_nSubEntityId, ZEntityBlueprintFactoryBase* p_pForcedBPFactory)
{
if (!p_rParent)
if (!IsReasonableEntityRef(p_rParent))
{
return {};
}
Expand All @@ -330,3 +320,22 @@ ZEntityRef Utils::GetSubEntity(const ZEntityRef p_rParent, const uint64_t p_nSub

return {};
}

bool Utils::IsReasonableEntityRef(const ZEntityRef& p_rEntity)
{
if (!p_rEntity
|| !Utils::IsReasonablePointer(p_rEntity.m_pObj)
|| !Utils::IsReasonablePointer(p_rEntity.GetEntity())
|| !Utils::IsReasonablePointer(p_rEntity->GetType()))
{
return false;
}

const auto s_nEid = p_rEntity->GetType()->m_nEntityID;
if (s_nEid == 0 /* || s_nEid == 0xFFFFFFFF */)
{
return false;
}

return true;
}
26 changes: 23 additions & 3 deletions src/Helpers/EntityUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <string_view>
#include <optional>

#include "Utils.h"

#define TAG "[EntityUtils] "

namespace Utils
Expand Down Expand Up @@ -121,6 +123,18 @@ namespace Utils
*/
ZEntityRef GetSubEntity(const ZEntityRef p_rParent, const uint64_t p_nSubEntityId, ZEntityBlueprintFactoryBase* p_pForcedBPFactory = nullptr);

/**
* Check if an entity reference is reasonable, i.e. not null and not likely to be invalid.
* @param p_rEntity The entity reference to check.
*/
bool IsReasonableEntityRef(const ZEntityRef& p_rEntity);
template <typename T>
inline bool IsReasonableEntityRef(const TEntityRef<T>& p_rEntity)
{
return IsReasonableEntityRef(p_rEntity.m_entityRef)
&& Utils::IsReasonablePointer(p_rEntity.m_pInterfaceRef);
}

/**
* safely get a property from an entity. Returns std::nullopt if failed or invalid.
* @template T The property type.
Expand All @@ -130,15 +144,21 @@ namespace Utils
template <typename T>
std::optional<T> GetProperty(const ZEntityRef& p_rEntity, const std::string& p_sProperty)
{
if (!p_rEntity)
if (!IsReasonableEntityRef(p_rEntity))
{
return std::nullopt;
}

auto s_PropValue = p_rEntity.GetProperty<T>(p_sProperty);
if (s_PropValue.IsEmpty())
{
Logger::Debug(TAG "Failed to get '{}' on EID {:016X}: was null.", p_sProperty, p_rEntity ? p_rEntity.GetEntity()->GetType()->m_nEntityID : 0);
return std::nullopt;
}

if (!s_PropValue.Is<T>())
{
Logger::Warn(TAG "Failed to get '{}' on EID {:016X}", p_sProperty, p_rEntity ? p_rEntity.GetEntity()->GetType()->m_nEntityID : 0);
Logger::Warn(TAG "Failed to get '{}' on EID {:016X}: type is mismatched!", p_sProperty, p_rEntity ? p_rEntity.GetEntity()->GetType()->m_nEntityID : 0);
return std::nullopt;
}

Expand All @@ -156,7 +176,7 @@ namespace Utils
template <typename T>
inline bool SetProperty(ZEntityRef& p_rEntity, const std::string& p_sProperty, const T& p_Value, bool p_bInvokeChangeHandlers = true)
{
if (!p_rEntity)
if (!IsReasonableEntityRef(p_rEntity))
{
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Helpers/PlayerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Glacier/ZSpatialEntity.h>
#include <Glacier/ZContentKitManager.h>

#include "Helpers/Utils.h"
#include "Helpers/EntityUtils.h"
#include "Entity/EntityIds.h"

Expand Down Expand Up @@ -66,21 +67,21 @@ bool Utils::TeleportPlayer(const SMatrix p_mTransform, SMatrix& p_mOldTransform,
bool Utils::SetPlayerOutfit(const std::string& p_sCommonName, TEntityRef<ZGlobalOutfitKit>& p_OutfitOut)
{
const auto s_Player = Utils::GetLocalPlayer();
if (!s_Player)
if (!Utils::IsReasonableEntityRef(s_Player))
{
return false;
}

const auto* s_pContentKitManager = Globals::ContentKitManager;
if (!s_pContentKitManager)
if (!Utils::IsReasonablePointer(s_pContentKitManager))
{
return false;
}

for (const auto& s_KV : s_pContentKitManager->m_repositoryGlobalOutfitKits)
{
const auto& s_OutfitKit = s_KV.second;
if (!s_OutfitKit || !s_OutfitKit.m_pInterfaceRef)
if (!Utils::IsReasonableEntityRef(s_OutfitKit))
{
continue;
}
Expand Down
Loading
Loading