From 0aa3b5c86777e75907aac451e4d619077a0b6147 Mon Sep 17 00:00:00 2001 From: pv Date: Sun, 3 Sep 2023 12:54:12 +0300 Subject: [PATCH] Refactoring --- lib/Red/Utils/Resources.hpp | 12 +++++++++-- .../Extensions/GarmentOverride/Dynamic.cpp | 15 +++++++++++++ .../Extensions/GarmentOverride/Dynamic.hpp | 4 ++++ src/App/Extensions/GarmentOverride/Module.cpp | 10 +++++---- src/App/Extensions/GarmentOverride/Module.hpp | 2 +- src/App/Extensions/GarmentOverride/States.cpp | 21 +++++++++---------- src/App/Extensions/GarmentOverride/States.hpp | 8 +++---- .../Extensions/GarmentOverride/Wrapper.cpp | 20 +++++++++--------- .../Extensions/GarmentOverride/Wrapper.hpp | 4 ++-- src/App/Project.hpp | 2 +- src/App/Version.rc | 8 +++---- src/Red/ResourcePath.hpp | 11 +++++++++- src/pch.hpp | 1 + xmake.lua | 2 +- 14 files changed, 79 insertions(+), 41 deletions(-) diff --git a/lib/Red/Utils/Resources.hpp b/lib/Red/Utils/Resources.hpp index 148e72e..7a3c0a8 100644 --- a/lib/Red/Utils/Resources.hpp +++ b/lib/Red/Utils/Resources.hpp @@ -77,7 +77,11 @@ inline void WaitForResource(const R& aResource, const W& aTimeout) }); cv.wait_for(lock, aTimeout); - mutex.unlock(); + + if (!lock.owns_lock()) + { + mutex.unlock(); + } } } @@ -103,7 +107,11 @@ inline void WaitForResources(const V& aResources, const W& aTimeout) }); cv.wait_for(lock, aTimeout); - mutex.unlock(); + + if (!lock.owns_lock()) + { + mutex.unlock(); + } } } } diff --git a/src/App/Extensions/GarmentOverride/Dynamic.cpp b/src/App/Extensions/GarmentOverride/Dynamic.cpp index d00c507..7838de7 100644 --- a/src/App/Extensions/GarmentOverride/Dynamic.cpp +++ b/src/App/Extensions/GarmentOverride/Dynamic.cpp @@ -570,6 +570,16 @@ bool App::DynamicAppearanceController::IsDynamicValue(const std::string_view& aV return aValue.starts_with(DynamicValueMarker); } +bool App::DynamicAppearanceController::IsDynamicValue(const Red::StringView& aValue) const +{ + return aValue.size > 0 && IsDynamicValue(aValue.data); +} + +bool App::DynamicAppearanceController::IsDynamicValue(Red::CName aValue) const +{ + return IsDynamicValue(aValue.ToString()); +} + void App::DynamicAppearanceController::RegisterPath(Red::ResourcePath aPath, const char* aPathStr) { m_paths.insert_or_assign(aPath, aPathStr); @@ -585,6 +595,11 @@ void App::DynamicAppearanceController::RegisterPath(Red::ResourcePath aPath, con m_paths.insert_or_assign(aPath, aPathStr); } +void App::DynamicAppearanceController::RegisterPath(Red::ResourcePath aPath, const Red::StringView& aPathStr) +{ + m_paths.insert_or_assign(aPath, std::string(aPathStr.data, aPathStr.size)); +} + const std::string& App::DynamicAppearanceController::GetPathStr(Red::ResourcePath aPath) const { if (!aPath) diff --git a/src/App/Extensions/GarmentOverride/Dynamic.hpp b/src/App/Extensions/GarmentOverride/Dynamic.hpp index 0ae0e69..3adfcd9 100644 --- a/src/App/Extensions/GarmentOverride/Dynamic.hpp +++ b/src/App/Extensions/GarmentOverride/Dynamic.hpp @@ -1,6 +1,7 @@ #pragma once #include "Red/EntityTemplate.hpp" +#include "Red/ResourcePath.hpp" namespace App { @@ -57,10 +58,13 @@ class DynamicAppearanceController [[nodiscard]] bool IsDynamicValue(const char* aValue) const; [[nodiscard]] bool IsDynamicValue(const std::string& aValue) const; [[nodiscard]] bool IsDynamicValue(const std::string_view& aValue) const; + [[nodiscard]] bool IsDynamicValue(const Red::StringView& aValue) const; + [[nodiscard]] bool IsDynamicValue(Red::CName aValue) const; void RegisterPath(Red::ResourcePath aPath, const char* aPathStr); void RegisterPath(Red::ResourcePath aPath, const std::string& aPathStr); void RegisterPath(Red::ResourcePath aPath, const std::string_view& aPathStr); + void RegisterPath(Red::ResourcePath aPath, const Red::StringView& aPathStr); bool SupportsDynamicAppearance(const Red::EntityTemplate* aTemplate); void MarkDynamicAppearanceName(Red::CName& aAppearanceName, Red::Entity* aEntity); diff --git a/src/App/Extensions/GarmentOverride/Module.cpp b/src/App/Extensions/GarmentOverride/Module.cpp index 4d479be..4369a3e 100644 --- a/src/App/Extensions/GarmentOverride/Module.cpp +++ b/src/App/Extensions/GarmentOverride/Module.cpp @@ -18,6 +18,8 @@ constexpr auto EmptyAppearanceTags = { std::pair(Red::CName("EmptyAppearance:Male"), "&Male"), std::pair(Red::CName("EmptyAppearance:Female"), "&Female"), }; + +const Red::ClassLocator s_meshResourceType; } std::string_view App::GarmentOverrideModule::GetName() @@ -550,7 +552,7 @@ void App::GarmentOverrideModule::OnReassembleAppearance(Red::Entity* aEntity, ui } } -void App::GarmentOverrideModule::OnCreateResourcePath(Red::ResourcePath* aPath, const std::string_view* aPathStr) +void App::GarmentOverrideModule::OnCreateResourcePath(Red::ResourcePath* aPath, const Red::StringView* aPathStr) { if (aPathStr && s_dynamicAppearance->IsDynamicValue(*aPathStr)) { @@ -663,7 +665,7 @@ void App::GarmentOverrideModule::SelectDynamicAppearance(Core::SharedPtrSelectTemplateAppearance(aSelector, aResource, aAppearance); + aEntityState->SelectDynamicAppearance(aSelector, aResource, aAppearance); } void App::GarmentOverrideModule::SelectDynamicAppearance(Core::SharedPtr& aEntityState, @@ -671,13 +673,13 @@ void App::GarmentOverrideModule::SelectDynamicAppearance(Core::SharedPtr& aDefinition) { - aEntityState->SelectConditionalAppearance(aSelector, aResource, aDefinition); + aEntityState->SelectDynamicAppearance(aSelector, aResource, aDefinition); } void App::GarmentOverrideModule::ApplyDynamicAppearance(Core::SharedPtr& aEntityState, Red::DynArray>& aComponents) { - aEntityState->ProcessConditionalComponents(aComponents); + aEntityState->ToggleConditionalComponents(aComponents); for (auto& component : aComponents) { diff --git a/src/App/Extensions/GarmentOverride/Module.hpp b/src/App/Extensions/GarmentOverride/Module.hpp index c49effd..5979d96 100644 --- a/src/App/Extensions/GarmentOverride/Module.hpp +++ b/src/App/Extensions/GarmentOverride/Module.hpp @@ -43,7 +43,7 @@ class GarmentOverrideModule : public ConfigurableUnitModule static void OnChangeItem(uintptr_t, Red::WeakHandle&, Red::GarmentItemChangeRequest&); static void OnChangeCustomItem(uintptr_t, Red::WeakHandle&, Red::GarmentItemChangeCustomRequest&); static void OnRemoveItem(uintptr_t, Red::WeakHandle&, Red::GarmentItemRemoveRequest&); - static void OnCreateResourcePath(Red::ResourcePath* aPath, const std::string_view* aPathStr); + static void OnCreateResourcePath(Red::ResourcePath* aPath, const Red::StringView* aPathStr); static void OnRegisterPart(uintptr_t, Red::Handle& aPart, Red::Handle& aComponents, Red::Handle& aAppearance); diff --git a/src/App/Extensions/GarmentOverride/States.cpp b/src/App/Extensions/GarmentOverride/States.cpp index 0f2ae73..56439bb 100644 --- a/src/App/Extensions/GarmentOverride/States.cpp +++ b/src/App/Extensions/GarmentOverride/States.cpp @@ -1,5 +1,6 @@ #include "States.hpp" #include "Red/Entity.hpp" +#include "Red/Mesh.hpp" namespace { @@ -392,8 +393,8 @@ void App::EntityState::UpdateDynamicAttributes() m_dynamicAppearance->UpdateState(m_entity); } -bool App::EntityState::SelectTemplateAppearance(App::DynamicAppearanceName& aSelector, Red::EntityTemplate* aResource, - Red::TemplateAppearance*& aAppearance) +bool App::EntityState::SelectDynamicAppearance(App::DynamicAppearanceName& aSelector, Red::EntityTemplate* aResource, + Red::TemplateAppearance*& aAppearance) { if (!aAppearance) { @@ -426,8 +427,8 @@ bool App::EntityState::SelectTemplateAppearance(App::DynamicAppearanceName& aSel return true; } -bool App::EntityState::SelectConditionalAppearance(DynamicAppearanceName& aSelector, Red::AppearanceResource* aResource, - Red::Handle& aDefinition) +bool App::EntityState::SelectDynamicAppearance(DynamicAppearanceName& aSelector, Red::AppearanceResource* aResource, + Red::Handle& aDefinition) { int8_t maxWeight = -1; int8_t numCandidates = 0; @@ -497,7 +498,7 @@ bool App::EntityState::SelectConditionalAppearance(DynamicAppearanceName& aSelec return false; } -void App::EntityState::ProcessConditionalComponents(Red::DynArray>& aComponents) +void App::EntityState::ToggleConditionalComponents(Red::DynArray>& aComponents) { Core::Map groups; @@ -602,7 +603,6 @@ bool App::EntityState::ApplyDynamicAppearance(Red::Handle& aCom if (finalResource != originalResource && finalResource != componentWrapper.GetResource()) { componentWrapper.SetResource(finalResource); - // componentWrapper.LoadResource(); } if (aSetAppearance) @@ -610,10 +610,9 @@ bool App::EntityState::ApplyDynamicAppearance(Red::Handle& aCom const auto originalAppearance = GetOriginalAppearance(componentWrapper); const auto finalAppearance = m_dynamicAppearance->ResolveName(m_entity, aVariant, originalAppearance); - if (finalAppearance != originalAppearance && finalAppearance != componentWrapper.GetAppearance()) + if (finalAppearance != originalAppearance && finalAppearance != componentWrapper.GetAppearanceName()) { - componentWrapper.SetAppearance(finalAppearance); - // componentWrapper.LoadAppearance(); + componentWrapper.SetAppearanceName(finalAppearance); } } } @@ -658,7 +657,7 @@ bool App::EntityState::ApplyAppearanceOverride(Red::Handle& aCo const auto& activeVariant = resourceState->GetActiveVariantParts(); finalAppearance = m_dynamicAppearance->ResolveName(m_entity, activeVariant, finalAppearance); - return finalAppearance && component.SetAppearance(finalAppearance) && component.LoadAppearance(); + return finalAppearance && component.SetAppearanceName(finalAppearance) && component.LoadAppearance(); } bool App::EntityState::ApplyChunkMaskOverride(Red::Handle& aComponent) @@ -736,7 +735,7 @@ Red::CName App::EntityState::GetOriginalAppearance(ComponentWrapper& aComponent) auto it = m_originalAppearances.find(componentId); if (it == m_originalAppearances.end()) - it = m_originalAppearances.emplace(componentId, aComponent.GetAppearance()).first; + it = m_originalAppearances.emplace(componentId, aComponent.GetAppearanceName()).first; return it.value(); } diff --git a/src/App/Extensions/GarmentOverride/States.hpp b/src/App/Extensions/GarmentOverride/States.hpp index 50cf2d2..2b2a197 100644 --- a/src/App/Extensions/GarmentOverride/States.hpp +++ b/src/App/Extensions/GarmentOverride/States.hpp @@ -106,11 +106,11 @@ class EntityState void AddOffsetOverride(uint64_t aHash, Red::ResourcePath aResourcePath, int32_t aOffset); void RemoveOffsetOverrides(uint64_t aHash); - bool SelectTemplateAppearance(DynamicAppearanceName& aSelector, Red::EntityTemplate* aResource, + bool SelectDynamicAppearance(DynamicAppearanceName& aSelector, Red::EntityTemplate* aResource, Red::TemplateAppearance*& aAppearance); - bool SelectConditionalAppearance(DynamicAppearanceName& aSelector, Red::AppearanceResource* aResource, - Red::Handle& aDefinition); - void ProcessConditionalComponents(Red::DynArray>& aComponents); + bool SelectDynamicAppearance(DynamicAppearanceName& aSelector, Red::AppearanceResource* aResource, + Red::Handle& aDefinition); + void ToggleConditionalComponents(Red::DynArray>& aComponents); bool ApplyDynamicAppearance(Red::Handle& aComponent); bool ApplyDynamicAppearance(Red::Handle& aComponent, Red::ResourcePath aResource); void LinkComponentToPart(Red::Handle& aComponent, Red::ResourcePath aResource); diff --git a/src/App/Extensions/GarmentOverride/Wrapper.cpp b/src/App/Extensions/GarmentOverride/Wrapper.cpp index e1cb908..684bf81 100644 --- a/src/App/Extensions/GarmentOverride/Wrapper.cpp +++ b/src/App/Extensions/GarmentOverride/Wrapper.cpp @@ -35,7 +35,7 @@ inline bool SetMorphComponentMeshPath(T* aComponent, Red::ResourcePath aPath) } template -inline Red::CName GetComponentAppearance(T* aComponent) +inline Red::CName GetComponentAppearanceName(T* aComponent) { return aComponent->meshAppearance; } @@ -206,18 +206,18 @@ Red::SharedPtr> App::ComponentWrapper::LoadResour return meshRef.token; } -Red::CName App::ComponentWrapper::GetAppearance() const +Red::CName App::ComponentWrapper::GetAppearanceName() const { switch (m_type) { case ComponentType::MeshComponent: - return GetComponentAppearance(m_component.GetPtr()); + return GetComponentAppearanceName(m_component.GetPtr()); case ComponentType::SkinnedMeshComponent: - return GetComponentAppearance(m_component.GetPtr()); + return GetComponentAppearanceName(m_component.GetPtr()); case ComponentType::SkinnedClothComponent: - return GetComponentAppearance(m_component.GetPtr()); + return GetComponentAppearanceName(m_component.GetPtr()); case ComponentType::MorphTargetSkinnedMeshComponent: - return GetComponentAppearance(m_component.GetPtr()); + return GetComponentAppearanceName(m_component.GetPtr()); case ComponentType::Unknown: break; } @@ -225,7 +225,7 @@ Red::CName App::ComponentWrapper::GetAppearance() const return {}; } -bool App::ComponentWrapper::SetAppearance(Red::CName aAppearance) const +bool App::ComponentWrapper::SetAppearanceName(Red::CName aAppearance) const { switch (m_type) { @@ -253,11 +253,11 @@ bool App::ComponentWrapper::LoadAppearance() const { case ComponentType::MeshComponent: meshRef = GetComponentMesh(m_component.GetPtr()); - meshAppName = GetComponentAppearance(m_component.GetPtr()); + meshAppName = GetComponentAppearanceName(m_component.GetPtr()); break; case ComponentType::SkinnedMeshComponent: meshRef = GetComponentMesh(m_component.GetPtr()); - meshAppName = GetComponentAppearance(m_component.GetPtr()); + meshAppName = GetComponentAppearanceName(m_component.GetPtr()); break; case ComponentType::SkinnedClothComponent: // meshRef = GetClothComponentMesh(m_component.GetPtr()); @@ -265,7 +265,7 @@ bool App::ComponentWrapper::LoadAppearance() const break; case ComponentType::MorphTargetSkinnedMeshComponent: meshRef = GetMorphComponentMesh(m_component.GetPtr()); - meshAppName = GetComponentAppearance(m_component.GetPtr()); + meshAppName = GetComponentAppearanceName(m_component.GetPtr()); break; case ComponentType::Unknown: break; diff --git a/src/App/Extensions/GarmentOverride/Wrapper.hpp b/src/App/Extensions/GarmentOverride/Wrapper.hpp index dcaf0bc..df59670 100644 --- a/src/App/Extensions/GarmentOverride/Wrapper.hpp +++ b/src/App/Extensions/GarmentOverride/Wrapper.hpp @@ -26,8 +26,8 @@ class ComponentWrapper bool SetResource(Red::ResourcePath aPath) const; [[nodiscard]] Red::SharedPtr> LoadResource(bool aWait = false) const; - [[nodiscard]] Red::CName GetAppearance() const; - bool SetAppearance(Red::CName aAppearance) const; + [[nodiscard]] Red::CName GetAppearanceName() const; + bool SetAppearanceName(Red::CName aAppearance) const; bool LoadAppearance() const; [[nodiscard]] uint64_t GetChunkMask() const; diff --git a/src/App/Project.hpp b/src/App/Project.hpp index 1400cb4..2280ff2 100644 --- a/src/App/Project.hpp +++ b/src/App/Project.hpp @@ -12,5 +12,5 @@ constexpr auto Author = "psiberx"; constexpr auto NameW = L"ArchiveXL"; constexpr auto AuthorW = L"psiberx"; -constexpr auto Version = semver::from_string_noexcept("1.5.10").value(); +constexpr auto Version = semver::from_string_noexcept("1.5.11").value(); } diff --git a/src/App/Version.rc b/src/App/Version.rc index 2c8878c..bc3718e 100644 --- a/src/App/Version.rc +++ b/src/App/Version.rc @@ -1,9 +1,9 @@ -#define VER_PRODUCTVERSION 1,5,10,0 -#define VER_FILEVERSION 1,5,10,2308171907 +#define VER_PRODUCTVERSION 1,5,11,0 +#define VER_FILEVERSION 1,5,11,2309031253 #define VER_PRODUCTNAME_STR "ArchiveXL\0" -#define VER_PRODUCTVERSION_STR "1.5.10\0" -#define VER_FILEVERSION_STR "1.5.10.2308171907\0" +#define VER_PRODUCTVERSION_STR "1.5.11\0" +#define VER_FILEVERSION_STR "1.5.11.2309031253\0" 1 VERSIONINFO FILEVERSION VER_FILEVERSION diff --git a/src/Red/ResourcePath.hpp b/src/Red/ResourcePath.hpp index 71c2bef..6b2bd5f 100644 --- a/src/Red/ResourcePath.hpp +++ b/src/Red/ResourcePath.hpp @@ -3,9 +3,18 @@ #include "Core/Raw.hpp" #include "Red/Addresses.hpp" +namespace Red +{ +struct StringView +{ + const char* data; + uint32_t size; +}; +} + namespace Raw::ResourcePath { constexpr auto Create = Core::RawFunc< /* addr = */ Red::Addresses::ResourcePath_Create, - /* type = */ Red::ResourcePath* (*)(Red::ResourcePath* aOut, const std::string_view* aPath)>(); + /* type = */ Red::ResourcePath* (*)(Red::ResourcePath* aOut, const Red::StringView* aPath)>(); } diff --git a/src/pch.hpp b/src/pch.hpp index 7ae920e..929d378 100644 --- a/src/pch.hpp +++ b/src/pch.hpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include diff --git a/xmake.lua b/xmake.lua index 7a81192..656e4c0 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,7 +1,7 @@ set_xmakever("2.5.9") set_project("ArchiveXL") -set_version("1.5.10", {build = "%y%m%d%H%M"}) +set_version("1.5.11", {build = "%y%m%d%H%M"}) set_arch("x64") set_languages("cxx20", "cxx2a")