From 4b98b4b6905430a561bc4d4c2f4980324cbbe0cb Mon Sep 17 00:00:00 2001 From: pongo1231 Date: Thu, 19 Oct 2023 21:31:21 +0000 Subject: [PATCH] ChaosMod: Refactor OnPresentCallback into an ChaosEvent --- ChaosMod/ChaosMod.vcxproj | 1 + ChaosMod/Memory/Hooks/PresentHook.cpp | 4 +++- ChaosMod/Memory/Hooks/PresentHook.h | 8 +++++++ ChaosMod/Memory/Hooks/ScriptThreadRunHook.h | 2 +- ChaosMod/Memory/Hooks/ShaderHook.cpp | 25 ++++++++++++--------- 5 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 ChaosMod/Memory/Hooks/PresentHook.h diff --git a/ChaosMod/ChaosMod.vcxproj b/ChaosMod/ChaosMod.vcxproj index 97de26c1b..ff98c027d 100644 --- a/ChaosMod/ChaosMod.vcxproj +++ b/ChaosMod/ChaosMod.vcxproj @@ -495,6 +495,7 @@ + diff --git a/ChaosMod/Memory/Hooks/PresentHook.cpp b/ChaosMod/Memory/Hooks/PresentHook.cpp index c86443ec3..852218113 100644 --- a/ChaosMod/Memory/Hooks/PresentHook.cpp +++ b/ChaosMod/Memory/Hooks/PresentHook.cpp @@ -1,5 +1,7 @@ #include +#include "PresentHook.h" + #include "Memory/Hooks/Hook.h" #include "Memory/Hooks/ShaderHook.h" @@ -10,7 +12,7 @@ HRESULT HK_IDXGISwapChain_Present(IDXGISwapChain *swapChain, UINT syncInterval, { if (!(flags & DXGI_PRESENT_TEST)) { - Hooks::OnPresentCallback(); + Hooks::OnPresent.Fire(); } return OG_IDXGISwapChain_Present(swapChain, syncInterval, flags); diff --git a/ChaosMod/Memory/Hooks/PresentHook.h b/ChaosMod/Memory/Hooks/PresentHook.h new file mode 100644 index 000000000..592960417 --- /dev/null +++ b/ChaosMod/Memory/Hooks/PresentHook.h @@ -0,0 +1,8 @@ +#pragma once + +#include "Util/Events.h" + +namespace Hooks +{ + inline ChaosEvent OnPresent; +} \ No newline at end of file diff --git a/ChaosMod/Memory/Hooks/ScriptThreadRunHook.h b/ChaosMod/Memory/Hooks/ScriptThreadRunHook.h index 3f59a19fd..b8903e124 100644 --- a/ChaosMod/Memory/Hooks/ScriptThreadRunHook.h +++ b/ChaosMod/Memory/Hooks/ScriptThreadRunHook.h @@ -1,6 +1,6 @@ #pragma once -#include +#include "Util/Events.h" namespace rage { diff --git a/ChaosMod/Memory/Hooks/ShaderHook.cpp b/ChaosMod/Memory/Hooks/ShaderHook.cpp index df37c5163..f15d76d27 100644 --- a/ChaosMod/Memory/Hooks/ShaderHook.cpp +++ b/ChaosMod/Memory/Hooks/ShaderHook.cpp @@ -2,9 +2,11 @@ #include "ShaderHook.h" -#include "Memory/Hooks/Hook.h" #include "Memory/Shader.h" +#include "Memory/Hooks/Hook.h" +#include "Memory/Hooks/PresentHook.h" + #define SHADER_CACHE_MAX_ENTRIES 20 using DWORD64 = unsigned long long; @@ -41,6 +43,18 @@ void *HK_rage__CreateShader(const char *name, BYTE *data, DWORD size, DWORD type static bool OnHook() { + static CHAOS_EVENT_LISTENER(Hooks::OnPresent) onPresentListener; + + onPresentListener.Register(Hooks::OnPresent, + []() + { + if (ms_RefreshShaders) + { + ms_RefreshShaders = false; + Memory::InvalidateShaderCache(); + } + }); + Handle handle; // @@ -128,13 +142,4 @@ namespace Hooks ms_ShaderBytecode.clear(); ms_RefreshShaders = true; } - - void OnPresentCallback() - { - if (ms_RefreshShaders) - { - ms_RefreshShaders = false; - Memory::InvalidateShaderCache(); - } - } } \ No newline at end of file