diff --git a/CMakeLists.txt b/CMakeLists.txt index cea75a3..fb918fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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. diff --git a/src/Effects/Player/ZAuthoritativeFigureEffect.cpp b/src/Effects/Player/ZAuthoritativeFigureEffect.cpp index 82e08ba..12292ef 100644 --- a/src/Effects/Player/ZAuthoritativeFigureEffect.cpp +++ b/src/Effects/Player/ZAuthoritativeFigureEffect.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "Registry.h" #include "Helpers/Math.h" @@ -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; } void ZAuthoritativeFigureEffect::Stop() @@ -52,8 +53,8 @@ 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 @@ -61,4 +62,20 @@ 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); diff --git a/src/Effects/Player/ZAuthoritativeFigureEffect.h b/src/Effects/Player/ZAuthoritativeFigureEffect.h index 33369b4..6f7b547 100644 --- a/src/Effects/Player/ZAuthoritativeFigureEffect.h +++ b/src/Effects/Player/ZAuthoritativeFigureEffect.h @@ -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 { diff --git a/src/Entity/Bindings/EntityBinding.h b/src/Entity/Bindings/EntityBinding.h index 7dd85b3..e6b51b0 100644 --- a/src/Entity/Bindings/EntityBinding.h +++ b/src/Entity/Bindings/EntityBinding.h @@ -37,7 +37,7 @@ struct SEntityBinding operator bool() const { - return !!m_rEntity; + return m_rEntity && Utils::IsReasonableEntityRef(m_rEntity); } }; diff --git a/src/Entity/Bindings/SSpeakEntitySounddefBinding.h b/src/Entity/Bindings/SSpeakEntitySounddefBinding.h index b625ce3..7d7dc6f 100644 --- a/src/Entity/Bindings/SSpeakEntitySounddefBinding.h +++ b/src/Entity/Bindings/SSpeakEntitySounddefBinding.h @@ -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, 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 diff --git a/src/Helpers/ActorUtils.cpp b/src/Helpers/ActorUtils.cpp index 808fb35..ddaffc7 100644 --- a/src/Helpers/ActorUtils.cpp +++ b/src/Helpers/ActorUtils.cpp @@ -23,7 +23,9 @@ std::vector> 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; } diff --git a/src/Helpers/EntityUtils.cpp b/src/Helpers/EntityUtils.cpp index f756944..72c7f90 100644 --- a/src/Helpers/EntityUtils.cpp +++ b/src/Helpers/EntityUtils.cpp @@ -7,7 +7,6 @@ #include #include -#include #define TAG "[EntityUtils] " @@ -33,48 +32,52 @@ std::vector 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)) { 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) @@ -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 ""; } @@ -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 ""; } @@ -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(p_pBpFactory)); #endif return false; } - // seen invalid pointers with all bits set, check that too - std::uintptr_t s_nPtr = reinterpret_cast(p_pBpFactory); - if (s_nPtr == std::numeric_limits::max()) - { -#if _DEBUG - Logger::Error(TAG "Blueprint factory pointer is 0xFFFF(...)."); -#endif - - return false; - } - - // hopefully OK return true; } @@ -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 {}; } @@ -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; +} diff --git a/src/Helpers/EntityUtils.h b/src/Helpers/EntityUtils.h index 5b66197..fac851c 100644 --- a/src/Helpers/EntityUtils.h +++ b/src/Helpers/EntityUtils.h @@ -10,6 +10,8 @@ #include #include +#include "Utils.h" + #define TAG "[EntityUtils] " namespace Utils @@ -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 + inline bool IsReasonableEntityRef(const TEntityRef& 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. @@ -130,15 +144,21 @@ namespace Utils template std::optional 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(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()) { - 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; } @@ -156,7 +176,7 @@ namespace Utils template 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; } diff --git a/src/Helpers/PlayerUtils.cpp b/src/Helpers/PlayerUtils.cpp index 51973d2..27a833e 100644 --- a/src/Helpers/PlayerUtils.cpp +++ b/src/Helpers/PlayerUtils.cpp @@ -6,6 +6,7 @@ #include #include +#include "Helpers/Utils.h" #include "Helpers/EntityUtils.h" #include "Entity/EntityIds.h" @@ -66,13 +67,13 @@ bool Utils::TeleportPlayer(const SMatrix p_mTransform, SMatrix& p_mOldTransform, bool Utils::SetPlayerOutfit(const std::string& p_sCommonName, TEntityRef& 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; } @@ -80,7 +81,7 @@ bool Utils::SetPlayerOutfit(const std::string& p_sCommonName, TEntityRefm_repositoryGlobalOutfitKits) { const auto& s_OutfitKit = s_KV.second; - if (!s_OutfitKit || !s_OutfitKit.m_pInterfaceRef) + if (!Utils::IsReasonableEntityRef(s_OutfitKit)) { continue; } diff --git a/src/Helpers/Utils.cpp b/src/Helpers/Utils.cpp index 0f01d18..c5664ad 100644 --- a/src/Helpers/Utils.cpp +++ b/src/Helpers/Utils.cpp @@ -54,3 +54,58 @@ std::string Utils::ToLower(const std::string& p_sInput) std::transform(s_Result.begin(), s_Result.end(), s_Result.begin(), [](unsigned char c) { return std::tolower(c); }); return s_Result; } + +bool Utils::IsReasonablePointer(const void* p_pPtr) +{ + // nullptr? + if (!p_pPtr) + { + return false; + } + + const auto s_nPtr = reinterpret_cast(p_pPtr); + + // common invalid pointers in Glacier2 / SDK + // - 0xFFFF'FFFF'FFFF'FFFF (64-bit all bits set) - seen in BP factory pointers + // - 0x0000'0000'FFFF'FFFF (64-bit lower 32 bits set) - seen for invalid entity refs + if (s_nPtr == 0xFFFFFFFFFFFFFFFF || s_nPtr == 0x00000000FFFFFFFF) + { + return false; + } + + // "The first 64KB of address space is never mapped in, so the lowest valid address you'd expect to see is 0x10000" + // https://learn.microsoft.com/en-us/archive/msdn-magazine/2006/may/x64-starting-out-in-64-bit-windows-systems-with-visual-c + if (s_nPtr < 0x10000) + { + return false; + } + + // "A 64-bit process on 64-bit Windows has a virtual address space within the 128-terabyte range 0x000'00000000 through 0x7FFF'FFFFFFFF" + // https://learn.microsoft.com/en-us/windows-hardware/drivers/gettingstarted/virtual-address-spaces + if (s_nPtr > 0x7FFFFFFFFFFF) + { + return false; + } + + // aligned to 4 byte boundary? + // not guranteed, but most pointers do follow this. + if (s_nPtr % 4 != 0) + { + return false; + } + + /* + // check against application address space range. + SYSTEM_INFO s_SysInfo; + GetSystemInfo(&s_SysInfo); + const auto s_nMinAppAddress = reinterpret_cast(s_SysInfo.lpMinimumApplicationAddress); + const auto s_nMaxAppAddress = reinterpret_cast(s_SysInfo.lpMaximumApplicationAddress); + if (s_nPtr < s_nMinAppAddress || s_nPtr > s_nMaxAppAddress) + { + return false; + } + */ + + // looks reasonable enough + return true; +} diff --git a/src/Helpers/Utils.h b/src/Helpers/Utils.h index 72df614..76acf1e 100644 --- a/src/Helpers/Utils.h +++ b/src/Helpers/Utils.h @@ -46,4 +46,14 @@ namespace Utils * Convert a string to lowercase, without modifying the input string. */ std::string ToLower(const std::string& p_sInput); + + /** + * Heuristic check if a pointer can be considered reasonable, checking + * for common failure cases observed in the engine. + * If this function returns false, the pointer is likely invalid and should not be dereferenced. + * TL;DR: this is basically a fancy nullptr check. + * + * @param p_pPtr Pointer to check. + */ + bool IsReasonablePointer(const void* p_pPtr); }; // namespace Utils \ No newline at end of file