From 46e4d1c910c6462620d69cfd7d84c85588a75c8c Mon Sep 17 00:00:00 2001 From: QuestionableM <77170113+QuestionableM@users.noreply.github.com> Date: Sun, 16 Jul 2023 11:03:12 -0700 Subject: [PATCH] Custom sounds now depend on Master * Effect volume --- Code/{ => Hooks}/fmod_hooks.cpp | 38 ++++++++++++++-- Code/{ => Hooks}/fmod_hooks.hpp | 3 ++ Code/{ => Hooks}/hooks.cpp | 8 ++-- Code/{ => Hooks}/hooks.hpp | 0 Code/{ => SM}/AudioManager.hpp | 0 Code/{ => SM}/DirectoryManager.hpp | 0 Code/SM/GameSettings.hpp | 54 +++++++++++++++++++++++ Code/Utils/Console.cpp | 42 +----------------- Code/Utils/Console.hpp | 69 ++++-------------------------- Code/main.cpp | 37 +++++++++------- DLM.sln | 1 + DLM.vcxproj | 15 ++++--- DLM.vcxproj.filters | 15 ++++--- 13 files changed, 145 insertions(+), 137 deletions(-) rename Code/{ => Hooks}/fmod_hooks.cpp (91%) rename Code/{ => Hooks}/fmod_hooks.hpp (95%) rename Code/{ => Hooks}/hooks.cpp (96%) rename Code/{ => Hooks}/hooks.hpp (100%) rename Code/{ => SM}/AudioManager.hpp (100%) rename Code/{ => SM}/DirectoryManager.hpp (100%) create mode 100644 Code/SM/GameSettings.hpp diff --git a/Code/fmod_hooks.cpp b/Code/Hooks/fmod_hooks.cpp similarity index 91% rename from Code/fmod_hooks.cpp rename to Code/Hooks/fmod_hooks.cpp index d683791..178c3aa 100644 --- a/Code/fmod_hooks.cpp +++ b/Code/Hooks/fmod_hooks.cpp @@ -1,7 +1,8 @@ #include "fmod_hooks.hpp" -#include "DirectoryManager.hpp" -#include "AudioManager.hpp" +#include "SM/DirectoryManager.hpp" +#include "SM/AudioManager.hpp" +#include "SM/GameSettings.hpp" #include "win_include.hpp" @@ -166,7 +167,7 @@ FMOD_RESULT FMODHooks::h_FMOD_Studio_EventInstance_setVolume(FMOD::Studio::Event FakeEventDescription* v_fake_event = FAKE_EVENT_CAST(event_instance); if (v_fake_event->isValidHook()) { - v_fake_event->channel->setVolume(volume); + v_fake_event->channel->setVolume(SM::GameSettings::GetEffectsVolume() * volume); return FMOD_OK; } @@ -246,6 +247,31 @@ FMOD_RESULT FMODHooks::h_FMOD_Studio_EventInstance_setPitch(FMOD::Studio::EventI return FMODHooks::o_FMOD_Studio_EventInstance_setPitch(event_instance, pitch); } +using v_fmod_set_parameter_function = FMOD_RESULT(*)(FakeEventDescription* fake_event, float value); + +static FMOD_RESULT fake_event_desc_setPitch(FakeEventDescription* fake_event, float value) +{ + return fake_event->channel->setPitch(value); +} + +inline static std::unordered_map g_fake_event_parameter_table = +{ + { "DLM_Pitch", fake_event_desc_setPitch } +}; + +FMOD_RESULT FMODHooks::h_FMOD_Studio_EventInstance_setParameterByName(FMOD::Studio::EventInstance* event_instance, const char* name, float value, bool ignoreseekspeed) +{ + FakeEventDescription* v_fake_event = FAKE_EVENT_CAST(event_instance); + if (v_fake_event->isValidHook()) + { + auto v_iter = g_fake_event_parameter_table.find(std::string(name)); + if (v_iter != g_fake_event_parameter_table.end()) + return v_iter->second(v_fake_event, value); + } + + return FMODHooks::o_FMOD_Studio_EventInstance_setParameterByName(event_instance, name, value, ignoreseekspeed); +} + FMOD_RESULT FMODHooks::h_FMOD_Studio_EventDescription_getLength(FMOD::Studio::EventDescription* event_desc, int* length) { FakeEventDescription* v_fake_event = FAKE_EVENT_CAST(event_desc); @@ -270,6 +296,7 @@ FMOD_RESULT FMODHooks::h_FMOD_Studio_EventDescription_createInstance(FMOD::Studi if (v_sound_data->is_3d) { v_channel->setMode(FMOD_3D); + v_channel->setVolume(SM::GameSettings::GetEffectsVolume()); } FakeEventDescription* v_new_fake_event = new FakeEventDescription(v_sound_data->sound, v_channel); @@ -436,6 +463,11 @@ static FMODHookData g_fmodHookData[] = "?hasSustainPoint@EventDescription@Studio@FMOD@@QEBA?AW4FMOD_RESULT@@PEA_N@Z", (LPVOID)FMODHooks::h_FMOD_Studio_EventDescription_hasSustainPoint, (LPVOID*)&FMODHooks::o_FMOD_Studio_EventDescription_hasSustainPoint + }, + { + "?setParameterByName@EventInstance@Studio@FMOD@@QEAA?AW4FMOD_RESULT@@PEBDM_N@Z", + (LPVOID)FMODHooks::h_FMOD_Studio_EventInstance_setParameterByName, + (LPVOID*)&FMODHooks::o_FMOD_Studio_EventInstance_setParameterByName } }; diff --git a/Code/fmod_hooks.hpp b/Code/Hooks/fmod_hooks.hpp similarity index 95% rename from Code/fmod_hooks.hpp rename to Code/Hooks/fmod_hooks.hpp index 62e9350..3eb87c0 100644 --- a/Code/fmod_hooks.hpp +++ b/Code/Hooks/fmod_hooks.hpp @@ -23,6 +23,7 @@ namespace FEventInstance using SetTimelinePosition = FMOD_RESULT(__fastcall*)(FMOD::Studio::EventInstance*, int); using GetPitch = FMOD_RESULT(__fastcall*)(FMOD::Studio::EventInstance*, float*, float*); using SetPitch = FMOD_RESULT(__fastcall*)(FMOD::Studio::EventInstance*, float); + using SetParameterByName = FMOD_RESULT(__fastcall*)(FMOD::Studio::EventInstance*, const char*, float, bool); } namespace FEventDescription @@ -149,6 +150,7 @@ class FMODHooks inline static FEventInstance::SetTimelinePosition o_FMOD_Studio_EventInstance_setTimelinePosition = nullptr; inline static FEventInstance::GetPitch o_FMOD_Studio_EventInstance_getPitch = nullptr; inline static FEventInstance::SetPitch o_FMOD_Studio_EventInstance_setPitch = nullptr; + inline static FEventInstance::SetParameterByName o_FMOD_Studio_EventInstance_setParameterByName = nullptr; static FMOD_RESULT h_FMOD_Studio_EventInstance_release(FMOD::Studio::EventInstance* event_instance); static FMOD_RESULT h_FMOD_Studio_EventInstance_start(FMOD::Studio::EventInstance* event_instance); @@ -163,6 +165,7 @@ class FMODHooks static FMOD_RESULT h_FMOD_Studio_EventInstance_setTimelinePosition(FMOD::Studio::EventInstance* event_instance, int position); static FMOD_RESULT h_FMOD_Studio_EventInstance_getPitch(FMOD::Studio::EventInstance* event_instance, float* pitch, float* finalpitch); static FMOD_RESULT h_FMOD_Studio_EventInstance_setPitch(FMOD::Studio::EventInstance* event_instance, float pitch); + static FMOD_RESULT h_FMOD_Studio_EventInstance_setParameterByName(FMOD::Studio::EventInstance* event_instance, const char* name, float value, bool ignoreseekspeed); //FMOD EVENT DESCRIPTION HOOKS diff --git a/Code/hooks.cpp b/Code/Hooks/hooks.cpp similarity index 96% rename from Code/hooks.cpp rename to Code/Hooks/hooks.cpp index 6fefa7a..16e573b 100644 --- a/Code/hooks.cpp +++ b/Code/Hooks/hooks.cpp @@ -2,8 +2,8 @@ #include "fmod_hooks.hpp" -#include "DirectoryManager.hpp" -#include "AudioManager.hpp" +#include "SM/DirectoryManager.hpp" +#include "SM/AudioManager.hpp" #include "Utils/Console.hpp" #include "Utils/String.hpp" @@ -38,7 +38,7 @@ void load_sound_config(const std::string& key_repl) simdjson::dom::document v_document; if (!JsonReader::LoadParseSimdjsonCommentsC(v_wide_path, v_document, simdjson::dom::element_type::OBJECT)) { - DebugErrorL("[DLM] Couldn't load the DLM sound config file: ", config_path); + DebugErrorL("Couldn't load the DLM sound config file: ", config_path); return; } @@ -46,7 +46,7 @@ void load_sound_config(const std::string& key_repl) const auto v_sound_list = v_root["soundList"]; if (!v_sound_list.is_object()) { - DebugErrorL("[DLM] No sound list: ", config_path); + DebugErrorL("No sound list: ", config_path); return; } diff --git a/Code/hooks.hpp b/Code/Hooks/hooks.hpp similarity index 100% rename from Code/hooks.hpp rename to Code/Hooks/hooks.hpp diff --git a/Code/AudioManager.hpp b/Code/SM/AudioManager.hpp similarity index 100% rename from Code/AudioManager.hpp rename to Code/SM/AudioManager.hpp diff --git a/Code/DirectoryManager.hpp b/Code/SM/DirectoryManager.hpp similarity index 100% rename from Code/DirectoryManager.hpp rename to Code/SM/DirectoryManager.hpp diff --git a/Code/SM/GameSettings.hpp b/Code/SM/GameSettings.hpp new file mode 100644 index 0000000..748a348 --- /dev/null +++ b/Code/SM/GameSettings.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include "win_include.hpp" + +#include +#include + +namespace SM +{ + struct GameSettings + { + std::unordered_map int_settings; + std::unordered_map float_settings; + std::unordered_map string_settings; + + inline static GameSettings* GetInstance() + { + return *reinterpret_cast(std::uintptr_t(GetModuleHandle(NULL)) + 0x12A7840); + } + + inline float get_effect_volume() + { + auto v_iter = this->float_settings.find("EffectVolume"); + if (v_iter != this->float_settings.end()) + return v_iter->second; + + return 1.0f; + } + + inline float get_master_volume() + { + auto v_iter = this->float_settings.find("MasterVolume"); + if (v_iter != this->float_settings.end()) + return v_iter->second; + + return 1.0f; + } + + inline static float GetEffectsVolume() + { + SM::GameSettings* v_game_settings = SM::GameSettings::GetInstance(); + if (v_game_settings) + return v_game_settings->get_effect_volume() * v_game_settings->get_master_volume(); + + return 1.0f; //Default volume + } + + private: + GameSettings() = delete; + GameSettings(const GameSettings&) = delete; + GameSettings(GameSettings&&) = delete; + ~GameSettings() = delete; + }; +} \ No newline at end of file diff --git a/Code/Utils/Console.cpp b/Code/Utils/Console.cpp index 02d28e0..468dde0 100644 --- a/Code/Utils/Console.cpp +++ b/Code/Utils/Console.cpp @@ -1,46 +1,6 @@ #include "Console.hpp" -#if 1 namespace Engine { - HANDLE Console::Handle = NULL; __ConsoleOutputHandler Console::Out = {}; - - bool Console::CreateEngineConsole(const wchar_t* title) - { - if (Console::Handle == NULL) - { - if (AllocConsole()) - { - SetConsoleOutputCP(CP_UTF8); - SetConsoleTitleW(title); - - Console::Handle = GetStdHandle(STD_OUTPUT_HANDLE); - - return true; - } - } - - return false; - } - - bool Console::AttachToConsole() - { - if (Console::Handle == NULL) - { - Console::Handle = GetStdHandle(STD_OUTPUT_HANDLE); - return (Console::Handle != NULL); - } - - return false; - } - - void Console::DestroyConsole() - { - if (Console::Handle == NULL) return; - - FreeConsole(); - Console::Handle = NULL; - } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/Code/Utils/Console.hpp b/Code/Utils/Console.hpp index 4dfedf0..671c3c8 100644 --- a/Code/Utils/Console.hpp +++ b/Code/Utils/Console.hpp @@ -1,7 +1,5 @@ #pragma once -#if defined(_DEBUG) || !defined(_DEBUG) - #include "win_include.hpp" #include "ConColors.hpp" @@ -48,15 +46,9 @@ namespace Engine Console::Output("\n"); } - static bool CreateEngineConsole(const wchar_t* title); - static bool AttachToConsole(); - void DestroyConsole(); - static __ConsoleOutputHandler Out; private: - static HANDLE Handle; - template inline static void Output(const T arg) { @@ -88,13 +80,7 @@ namespace Engine { inline static void Output(const wchar_t* arg) { - #if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - WriteConsoleW(Console::Handle, arg, static_cast(wcslen(arg)), NULL, NULL); - #endif - - #if defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - OutputDebugStringW(arg); - #endif + WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), arg, static_cast(wcslen(arg)), NULL, NULL); } }; @@ -103,13 +89,7 @@ namespace Engine { inline static void Output(const char* arg) { - #if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - WriteConsoleA(Console::Handle, arg, static_cast(strlen(arg)), NULL, NULL); - #endif - - #if defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - OutputDebugStringA(arg); - #endif + WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), arg, static_cast(strlen(arg)), NULL, NULL); } }; @@ -118,13 +98,7 @@ namespace Engine { inline static void Output(const std::wstring& msg) { - #if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - WriteConsoleW(Console::Handle, msg.data(), static_cast(msg.size()), NULL, NULL); - #endif - - #if defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - OutputDebugStringW(msg.data()); - #endif + WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg.data(), static_cast(msg.size()), NULL, NULL); } }; @@ -133,13 +107,7 @@ namespace Engine { inline static void Output(const std::string& msg) { - #if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - WriteConsoleA(Console::Handle, msg.data(), static_cast(msg.size()), NULL, NULL); - #endif - - #if defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - OutputDebugStringA(msg.data()); - #endif + WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), msg.data(), static_cast(msg.size()), NULL, NULL); } }; @@ -158,7 +126,7 @@ namespace Engine QE_CREATE_CON_NUMBER_TYPE(double); QE_CREATE_CON_OUTPUT_TYPE(void (*)(), void (*func_ptr)(), { func_ptr(); }); - QE_CREATE_CON_OUTPUT_TYPE(EngineConColor, const EngineConColor& color, { SetConsoleTextAttribute(Console::Handle, static_cast(color)); }); + QE_CREATE_CON_OUTPUT_TYPE(EngineConColor, const EngineConColor& color, { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), static_cast(color));}); QE_CREATE_CON_OUTPUT_TYPE(bool, const bool& bool_val, { ConsoleOutputType::Output(bool_val ? "true" : "false"); }); //-------------CONSOLE OUTPUT HANDLER------------------ @@ -197,29 +165,8 @@ namespace Engine }; } - #if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT) - #define CreateDebugConsole(title) Engine::Console::CreateEngineConsole(title) - #define AttachDebugConsole() Engine::Console::AttachToConsole() - #else - #define CreateDebugConsole(...) ((void*)0) - #define AttachDebugConsole(...) ((void*)0) - #endif - #define DebugOut(...) Engine::Console::Out(__VA_ARGS__) -#define DebugOutL(...) Engine::Console::Out(__VA_ARGS__, Engine::Console::Endl) - -#define DebugWarningL(...) Engine::Console::Out(0b1101_fg, "WARNING: ", __FUNCTION__, "(", __LINE__, ") -> ", __VA_ARGS__, Engine::Console::Endl) -#define DebugErrorL(...) Engine::Console::Out(0b1001_fg, "ERROR: " , __FUNCTION__, "(", __LINE__, ") -> ", __VA_ARGS__, Engine::Console::Endl) -#else -#define CreateDebugConsole(title) ((void*)0) -#define AttachDebugConsole(...) ((void*)0) - -#define DebugOut(...) ((void*)0) -#define DebugOutL(...) ((void*)0) - -#define DebugWarningL(...) ((void*)0) -#define DebugErrorL(...) ((void*)0) +#define DebugOutL(...) Engine::Console::Out("[DLM] ", __VA_ARGS__, Engine::Console::Endl) -#define QE_CREATE_CON_OUTPUT_TYPE(...) ((void*)0) -#define QE_CREATE_CON_NUMBER_TYPE(...) ((void*)0) -#endif //EngineNamespace \ No newline at end of file +#define DebugWarningL(...) Engine::Console::Out(0b1101_fg, "[DLM] WARNING: ", __FUNCTION__, "(", __LINE__, ") -> ", __VA_ARGS__, Engine::Console::Endl) +#define DebugErrorL(...) Engine::Console::Out(0b1001_fg, "[DLM] ERROR: " , __FUNCTION__, "(", __LINE__, ") -> ", __VA_ARGS__, Engine::Console::Endl) \ No newline at end of file diff --git a/Code/main.cpp b/Code/main.cpp index 2b0fefa..3756b2c 100644 --- a/Code/main.cpp +++ b/Code/main.cpp @@ -1,15 +1,13 @@ #include "win_include.hpp" #include -#include "fmod_hooks.hpp" -#include "hooks.hpp" +#include "Hooks/fmod_hooks.hpp" +#include "Hooks/hooks.hpp" #include "Utils/Console.hpp" void dll_initialize() { - AttachDebugConsole(); - if (MH_Initialize() == MH_OK) { FMODHooks::Hook(); @@ -33,25 +31,32 @@ void dll_entry_func(HMODULE module) FreeLibraryAndExitThread(module, 0); } +static bool g_mhInitialized = false; +static bool g_mhAttached = false; + void dll_attach() { - AttachDebugConsole(); - if (MH_Initialize() != MH_OK) + if (MH_Initialize() == MH_OK) { - DebugErrorL("[DLM] Couldn't initialize MinHook!"); - return; - } + g_mhInitialized = true; - FMODHooks::Hook(); - Hooks::RunHooks(); + FMODHooks::Hook(); + Hooks::RunHooks(); - MH_EnableHook(MH_ALL_HOOKS); + if (MH_EnableHook(MH_ALL_HOOKS) == MH_OK) + g_mhAttached = true; + } } void dll_detach() { - MH_DisableHook(MH_ALL_HOOKS); - MH_Uninitialize(); + if (g_mhInitialized) + { + if (g_mhAttached) + MH_DisableHook(MH_ALL_HOOKS); + + MH_Uninitialize(); + } } BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) @@ -59,9 +64,11 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserve switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: - CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)dll_entry_func, hModule, NULL, NULL); + //CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)dll_entry_func, hModule, NULL, NULL); + dll_attach(); break; case DLL_PROCESS_DETACH: + dll_detach(); break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: diff --git a/DLM.sln b/DLM.sln index bfe9ffd..0034d27 100644 --- a/DLM.sln +++ b/DLM.sln @@ -6,6 +6,7 @@ MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DLM", "DLM.vcxproj", "{E1679359-E31B-4FA5-A4B4-43C2080D4A1D}" ProjectSection(ProjectDependencies) = postProject {6CCD69CD-98ED-45AA-9DC7-8E6D5251D06E} = {6CCD69CD-98ED-45AA-9DC7-8E6D5251D06E} + {FEC5F3BB-4472-43E6-A4BE-B894A64B45A2} = {FEC5F3BB-4472-43E6-A4BE-B894A64B45A2} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libMinHook", "libMinHook\libMinHook.vcxproj", "{6CCD69CD-98ED-45AA-9DC7-8E6D5251D06E}" diff --git a/DLM.vcxproj b/DLM.vcxproj index 97b9417..a5603af 100644 --- a/DLM.vcxproj +++ b/DLM.vcxproj @@ -88,23 +88,24 @@ Console true true - true + false $(SolutionDir)\Build\libs-$(PlatformShortName)-$(Configuration);$(SolutionDir)Dependencies\FMOD\lib\$(PlatformShortName);%(AdditionalLibraryDirectories) libMinHook.lib;fmod_vc.lib;fmodL_vc.lib;fmodstudio_vc.lib;fmodstudioL_vc.lib;simdjson.lib - - + + - - - - + + + + + diff --git a/DLM.vcxproj.filters b/DLM.vcxproj.filters index 9a82254..d91d81b 100644 --- a/DLM.vcxproj.filters +++ b/DLM.vcxproj.filters @@ -21,10 +21,10 @@ Source Files - + Source Files - + Source Files @@ -41,16 +41,16 @@ Header Files - + Header Files - + Header Files - + Header Files - + Header Files @@ -62,5 +62,8 @@ Header Files + + Header Files + \ No newline at end of file