diff --git a/MarathonRecomp/patches/aspect_ratio_patches.cpp b/MarathonRecomp/patches/aspect_ratio_patches.cpp index 37e30ace6..49df2df31 100644 --- a/MarathonRecomp/patches/aspect_ratio_patches.cpp +++ b/MarathonRecomp/patches/aspect_ratio_patches.cpp @@ -205,7 +205,7 @@ PPC_FUNC(sub_828C8F60) { const auto pScene = reinterpret_cast(base + ctx.r3.u32); - g_pSceneModifier = CsdModifierManager::FindModifier(ctx.r3.u32); + g_pSceneModifier = CsdModifierManager::FindNodeModifier(ctx.r3.u32); if (g_pSceneModifier) { @@ -241,12 +241,12 @@ PPC_FUNC(sub_828C8F60) void RenderCsdCastNodeMidAsmHook(PPCRegister& r10, PPCRegister& r27) { - g_pCastNodeModifier = CsdModifierManager::FindModifier(r10.u32 + r27.u32); + g_pCastNodeModifier = CsdModifierManager::FindNodeModifier(r10.u32 + r27.u32); } void RenderCsdCastMidAsmHook(PPCRegister& r4) { - g_pCastModifier = CsdModifierManager::FindModifier(r4.u32); + g_pCastModifier = CsdModifierManager::FindNodeModifier(r4.u32); } struct CsdTransform diff --git a/MarathonRecomp/patches/csd_modifier_manager.cpp b/MarathonRecomp/patches/csd_modifier_manager.cpp index b1c90aeab..51a2fddfb 100644 --- a/MarathonRecomp/patches/csd_modifier_manager.cpp +++ b/MarathonRecomp/patches/csd_modifier_manager.cpp @@ -11,25 +11,26 @@ #include #include +struct CsdProjectInfo +{ + ptrdiff_t Length{}; + std::map Paths{}; +}; + static Mutex g_Mutex{}; +static std::map g_Projects{}; static xxHashMap g_Templates{}; static xxHashMap g_Modifiers{}; -static std::map g_Projects{}; -static std::map g_Paths{}; static void EmplacePath(Chao::CSD::Project* in_pProject, const void* in_pKey, const std::string_view in_value) { std::lock_guard lock(g_Mutex); const auto hash = HashStr(in_value); - g_Paths[in_pKey] = hash; - - auto length = reinterpret_cast(in_pKey) - reinterpret_cast(in_pProject); - if (const auto it = g_Projects.find(in_pProject); it != g_Projects.end()) - length = std::max(it->second, length); - - g_Projects[in_pProject] = length; + auto& rProject = g_Projects[in_pProject]; + rProject.Length = std::max(rProject.Length, reinterpret_cast(in_pKey) - reinterpret_cast(in_pProject)); + rProject.Paths[in_pKey] = hash; #ifdef _DEBUG printf("[*] CSD hierarchy: %s (0x%llx) (0x%llx)\n", in_value.data(), hash, reinterpret_cast(in_pKey)); @@ -126,7 +127,7 @@ PPC_FUNC(sub_82617570) std::string_view filePath = pFilePath->c_str(); - LOGFN_UTILITY("CSD loaded: {} (0x{:08X})", filePath, reinterpret_cast(pCsdProject)); + LOGFN_UTILITY("CSD loaded: {} (0x{:08X})", filePath, reinterpret_cast(pCsdProject->m_pResource.get())); // Maps to ELanguages enum. static constexpr std::array suffixes = @@ -171,28 +172,17 @@ PPC_FUNC(sub_82617570) PPC_FUNC_IMPL(__imp__sub_825CE5F0); PPC_FUNC(sub_825CE5F0) { - const auto pProject = reinterpret_cast(base + ctx.r3.u32); + const auto pProject = reinterpret_cast(base + ctx.r3.u32)->m_pResource.get(); std::lock_guard lock(g_Mutex); - const auto key = pProject->m_pResource.get(); - - if (const auto it = g_Projects.find(key); it != g_Projects.end()) + if (g_Projects.erase(pProject)) { - const auto length = g_Projects[key]; - const auto end = reinterpret_cast(key) + length; - - const auto lower = g_Paths.lower_bound(key); - const auto upper = g_Paths.upper_bound(end); - - g_Paths.erase(lower, upper); - g_Projects.erase(key); - - LOGFN_UTILITY("CSD freed: 0x{:08X} - 0x{:08X}", uintptr_t(key), uintptr_t(end)); + LOGFN_UTILITY("CSD freed: 0x{:08X}", reinterpret_cast(pProject)); } else { - LOGFN_ERROR("CSD leaked: 0x{:08X}", uintptr_t(key)); + LOGFN_ERROR("CSD leaked: 0x{:08X}", reinterpret_cast(pProject)); } __imp__sub_825CE5F0(ctx, base); @@ -323,13 +313,11 @@ const char* CsdModifierManager::GetStatusMessage(const Status in_status) void CsdModifierManager::Add(const CsdModifier& in_rModifier) { - std::lock_guard lock(g_Mutex); g_Modifiers[HashStr(in_rModifier.Path)] = in_rModifier; } bool CsdModifierManager::Remove(const std::string_view in_key) { - std::lock_guard lock(g_Mutex); return g_Modifiers.erase(HashStr(in_key)); } @@ -353,24 +341,118 @@ CsdModifier* CsdModifierManager::FindModifier(const std::string_view in_path) return &it->second; } -CsdModifier* CsdModifierManager::FindModifier(const uint32_t in_data) +const void* CsdModifierManager::FindNode(const XXH64_hash_t in_hash) { std::lock_guard lock(g_Mutex); - const auto path = g_Paths.find(g_memory.Translate(in_data)); + for (const auto& [pProject, rProjectInfo] : g_Projects) + { + for (const auto& [pNode, rHash] : rProjectInfo.Paths) + { + if (rHash != in_hash) + continue; + + return pNode; + } + } - if (path != g_Paths.end()) + return nullptr; +} + +CsdModifier* CsdModifierManager::FindNodeModifier(const uint32_t in_data) +{ + std::lock_guard lock(g_Mutex); + + for (const auto& [pProject, rProjectInfo] : g_Projects) { + const auto path = rProjectInfo.Paths.find(g_memory.Translate(in_data)); + + if (path == rProjectInfo.Paths.end()) + continue; + const auto hash = path->second; - const auto it = g_Modifiers.find(hash); + const auto modifier = g_Modifiers.find(hash); - if (it != g_Modifiers.end()) - return &it->second; + if (modifier != g_Modifiers.end()) + return &modifier->second; } return nullptr; } +static size_t _GetTotalModifierCount(const xxHashMap& in_rModifierMap, size_t* out_pSize) +{ + const auto result = in_rModifierMap.size(); + + if (out_pSize) + { + for (const auto& [rModifierHash, rModifier] : in_rModifierMap) + *out_pSize += rModifier.GetSize(); + } + + return result; +} + +size_t CsdModifierManager::GetTotalTemplateCount(size_t* out_pSize) +{ + return _GetTotalModifierCount(g_Templates, out_pSize); +} + +size_t CsdModifierManager::GetTotalModifierCount(size_t* out_pSize) +{ + return _GetTotalModifierCount(g_Modifiers, out_pSize); +} + +size_t CsdModifierManager::GetActiveModifierCount(size_t* out_pSize) +{ + size_t result{}; + + std::lock_guard lock(g_Mutex); + + for (const auto& [rModifierHash, rModifier] : g_Modifiers) + { + bool isActive{}; + + for (const auto& [pProject, rProjectInfo] : g_Projects) + { + for (const auto& [pNode, rPathHash] : rProjectInfo.Paths) + { + if (rModifierHash == rPathHash) + { + isActive = true; + break; + } + } + + if (isActive) + break; + } + + if (isActive) + { + if (out_pSize) + *out_pSize += rModifier.GetSize(); + + result++; + } + } + + return result; +} + +size_t CsdModifierManager::GetTotalProjectCount(size_t* out_pSize) +{ + const auto result = g_Projects.size(); + + if (out_pSize) + { + for (const auto& [pProject, rProjectInfo] : g_Projects) + *out_pSize += rProjectInfo.Length + sizeof(Chao::CSD::Project); + } + + return result; +} + static bool CreateFlagsTable(const std::string_view in_id, const std::map& in_rNameMap, const ImVec2& in_rSize = { 0, 0 }) { const auto tableFlags = ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY; @@ -709,11 +791,6 @@ void CsdModifierManager::DrawDebug() if (ImGui::CollapsingHeader("Modifiers", ImGuiTreeNodeFlags_DefaultOpen)) { - static size_t totalModifierSize{}; - static size_t activeModifierSize{}; - static size_t activeModifierCount{}; - size_t totalTemplateSize{}; - std::vector templateNames{}; std::vector templatePtrs{}; @@ -729,25 +806,48 @@ void CsdModifierManager::DrawDebug() { templateNames.push_back(rTemplate.Path.c_str()); templatePtrs.push_back(&rTemplate); - - totalTemplateSize += rTemplate.GetSize(); } } if (ImGui::TreeNodeEx("Diagnostics", ImGuiTreeNodeFlags_DefaultOpen)) { - if (ImGui::BeginTable("##Diagnostics", 2)) + if (ImGui::BeginTable("##Diagnostics", 3)) { - size_t projectsLength{}; + ImGui::TableSetupColumn("Name"); + ImGui::TableSetupColumn("Count"); + ImGui::TableSetupColumn("Size"); + + const auto drawDiagRow = [](const char* in_pName, const size_t in_count, const double in_size) + { + ImGui::TableNextColumn(); + ImGui::Text("%s", in_pName); + ImGui::TableNextColumn(); + ImGui::Text("%zu", in_count); + ImGui::TableNextColumn(); + ImGui::Text("%.2f KiB", in_size); + }; + + size_t totalProjectSize{}; + const auto totalProjectCount = GetTotalProjectCount(&totalProjectSize); + + size_t totalTemplateSize{}; + const auto totalTemplateCount = GetTotalTemplateCount(&totalTemplateSize); - for (const auto& [rProject, rSize] : g_Projects) - projectsLength += rSize + sizeof(Chao::CSD::Project); + size_t activeModifierSize{}; + const auto activeModifierCount = GetActiveModifierCount(&activeModifierSize); - IMGUI_GENERIC_ROW("Total Modifiers", "%zu (%.2f KiB)", g_Modifiers.size(), static_cast(totalModifierSize / 1024.0)); - IMGUI_GENERIC_ROW("Active Modifiers", "%zu (%.2f KiB)", activeModifierCount, static_cast(activeModifierSize / 1024.0)); - IMGUI_GENERIC_ROW("Templates", "%zu (%.2f KiB)", g_Templates.size(), static_cast(totalTemplateSize / 1024.0)); - IMGUI_GENERIC_ROW("Projects", "%zu (%.2f KiB)", g_Projects.size(), static_cast(projectsLength / 1024.0)); - IMGUI_GENERIC_ROW("Nodes", "%zu", g_Paths.size()); + size_t totalModifierSize{}; + const auto totalModifierCount = GetTotalModifierCount(&totalModifierSize); + + drawDiagRow("Projects", totalProjectCount, static_cast(totalProjectSize / 1024.0)); + drawDiagRow("Templates", totalTemplateCount, static_cast(totalTemplateSize / 1024.0)); + + ImGui::TableNextColumn(); + ImGui::Text("Modifiers"); + ImGui::TableNextColumn(); + ImGui::Text("%zu / %zu", activeModifierCount, totalModifierCount); + ImGui::TableNextColumn(); + ImGui::Text("%.2f KiB / %.2f KiB", static_cast(activeModifierSize / 1024.0), static_cast(totalModifierSize / 1024.0)); ImGui::EndTable(); } @@ -755,8 +855,6 @@ void CsdModifierManager::DrawDebug() ImGui::TreePop(); } - activeModifierCount = 0; - enum { Filter_All, @@ -784,28 +882,11 @@ void CsdModifierManager::DrawDebug() if (ImGui::BeginChild("##Modifiers", { 0, -footerHeight })) { - totalModifierSize = 0; - activeModifierSize = 0; - for (auto& [rModifierHash, rModifier] : g_Modifiers) { - uintptr_t addr{}; - - totalModifierSize += rModifier.GetSize(); + const auto pNode = FindNode(rModifierHash); - // Find first reference to this modifier. - for (const auto& [rPath, rPathHash] : g_Paths) - { - if (rModifierHash == rPathHash) - { - addr = uintptr_t(rPath); - activeModifierSize += rModifier.GetSize(); - activeModifierCount++; - break; - } - } - - if (filter != Filter_All && filter != (addr ? Filter_Active : Filter_Inactive)) + if (filter != Filter_All && filter != (pNode ? Filter_Active : Filter_Inactive)) continue; if (hasSearchQuery && rModifier.Path.find(reinterpret_cast(&searchBuffer)) == std::string::npos) @@ -818,7 +899,7 @@ void CsdModifierManager::DrawDebug() else if (collapseAll) ImGui::SetNextItemOpen(false); - if (!addr) + if (!pNode) { ImGui::PushStyleColor(ImGuiCol_Header, ImVec4(0.25f, 0.25f, 0.25f, 1.0f)); ImGui::PushStyleColor(ImGuiCol_HeaderHovered, ImVec4(0.5f, 0.5f, 0.5f, 1.0f)); @@ -827,7 +908,7 @@ void CsdModifierManager::DrawDebug() const auto isHeaderOpen = ImGui::CollapsingHeader(rModifier.Path.c_str()); - if (!addr) + if (!pNode) ImGui::PopStyleColor(3); if (isHeaderOpen) @@ -836,7 +917,7 @@ void CsdModifierManager::DrawDebug() { int currentIndex{ 1 }, originalIndex{}, selectedIndex{}; - for (auto& [rHash, rTemplate] : g_Templates) + for (auto& [rTemplateHash, rTemplate] : g_Templates) { if (rTemplate.Path == rModifier.TemplatePath) { @@ -853,7 +934,7 @@ void CsdModifierManager::DrawDebug() { if (const auto pTemplate = templatePtrs[selectedIndex]) { - const auto modifierPath = rModifier.Path; + const std::string modifierPath = rModifier.Path; rModifier = *pTemplate; rModifier.Path = modifierPath; @@ -926,25 +1007,28 @@ void CsdModifierManager::DrawDebug() { ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed); - for (const auto& [rPath, rPathHash] : g_Paths) + for (const auto& [pProject, rProjectInfo] : g_Projects) { - if (rModifierHash != rPathHash) - continue; + for (const auto& [pCurrentPathNode, rCurrentPathHash] : rProjectInfo.Paths) + { + if (rModifierHash != rCurrentPathHash) + continue; - ImGui::PushID(rPath); + ImGui::PushID(pCurrentPathNode); - ImGui::TableNextColumn(); - ImGui::AlignTextToFramePadding(); + ImGui::TableNextColumn(); + ImGui::AlignTextToFramePadding(); - const auto text = std::format("0x{:X}", uintptr_t(rPath)); + const auto text = std::format("0x{:X}", reinterpret_cast(pCurrentPathNode)); - ImGui::Text("- %s", text.c_str()); - ImGui::TableNextColumn(); + ImGui::Text("- %s", text.c_str()); + ImGui::TableNextColumn(); - if (ImGui::Button("Copy")) - ImGui::SetClipboardText(text.c_str()); + if (ImGui::Button("Copy")) + ImGui::SetClipboardText(text.c_str()); - ImGui::PopID(); + ImGui::PopID(); + } } ImGui::EndTable(); @@ -974,12 +1058,15 @@ void CsdModifierManager::DrawDebug() if (ImGui::Button("Yes")) { // Delete modifier from all references. - for (const auto& [rPath, rPathHash] : g_Paths) + for (const auto& [pProject, rProjectInfo] : g_Projects) { - if (rModifierHash != rPathHash) - continue; + for (const auto& [pCurrentPathNode, rCurrentPathHash] : rProjectInfo.Paths) + { + if (rModifierHash != rCurrentPathHash) + continue; - deletions.push_back(rPath); + deletions.push_back(pCurrentPathNode); + } } ImGui::CloseCurrentPopup(); @@ -1036,7 +1123,10 @@ void CsdModifierManager::DrawDebug() pFont->Scale = defaultFontScale; for (const auto& rDeleted : deletions) - g_Paths.erase(rDeleted); + { + for (auto& [pProject, rProjectInfo] : g_Projects) + rProjectInfo.Paths.erase(rDeleted); + } deletions.clear(); } diff --git a/MarathonRecomp/patches/csd_modifier_manager.h b/MarathonRecomp/patches/csd_modifier_manager.h index b05da5227..ce8ce3741 100644 --- a/MarathonRecomp/patches/csd_modifier_manager.h +++ b/MarathonRecomp/patches/csd_modifier_manager.h @@ -45,7 +45,14 @@ class CsdModifierManager static CsdModifier* FindTemplate(std::string_view in_path); static CsdModifier* FindModifier(std::string_view in_path); - static CsdModifier* FindModifier(uint32_t in_data); + + static const void* FindNode(XXH64_hash_t in_hash); + static CsdModifier* FindNodeModifier(uint32_t in_data); + + static size_t GetTotalTemplateCount(size_t* out_pSize = nullptr); + static size_t GetTotalModifierCount(size_t* out_pSize = nullptr); + static size_t GetActiveModifierCount(size_t* out_pSize = nullptr); + static size_t GetTotalProjectCount(size_t* out_pSize = nullptr); static void DrawDebug(); };