From 2be0026f32b58967dda8bdbeb35331a8147de6a8 Mon Sep 17 00:00:00 2001 From: LAGonauta Date: Wed, 10 Jul 2019 21:14:41 -0300 Subject: [PATCH 1/3] Clamp gain and doppler --- Plugins/Audio/snd_dma.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Plugins/Audio/snd_dma.cpp b/Plugins/Audio/snd_dma.cpp index 914ce11..4ffb217 100644 --- a/Plugins/Audio/snd_dma.cpp +++ b/Plugins/Audio/snd_dma.cpp @@ -366,15 +366,12 @@ void S_Update(float *origin, float *forward, float *right, float *up) else { if (volume) - al_listener.setGain(max(min(volume->value, 1), 0)); + al_listener.setGain(std::clamp(volume->value, 0.0f, 1.0f)); else al_listener.setGain(1.0f); } - if (al_doppler->value >= 0.0f && al_doppler->value <= 10.0f) - { - al_context.setDopplerFactor(al_doppler->value); - } + al_context.setDopplerFactor(std::clamp(al_doppler->value, 0.0f, 10.0f)); std::pair alure_orientation( alure::Vector3(orientation[0], orientation[1], orientation[2]), From c2e10686da27b3fea07cc1d708a399223e8cf7e0 Mon Sep 17 00:00:00 2001 From: LAGonauta Date: Wed, 10 Jul 2019 21:19:59 -0300 Subject: [PATCH 2/3] Better plugin loading code with C++ constructs --- HLSDK/common/interface.cpp | 63 +++++----------------------- metahook.cpp | 84 +++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 89 deletions(-) diff --git a/HLSDK/common/interface.cpp b/HLSDK/common/interface.cpp index 240de51..4b2f391 100644 --- a/HLSDK/common/interface.cpp +++ b/HLSDK/common/interface.cpp @@ -1,6 +1,6 @@ //========= Copyright © 1996-2002, Valve LLC, All rights reserved. ============ // -// Purpose: +// Purpose: // // $NoKeywords: $ //============================================================================= @@ -11,17 +11,14 @@ #ifndef _WIN32 // LINUX #include -#include // getcwd -#include // sprintf +#include #endif - // ------------------------------------------------------------------------------------ // // InterfaceReg. // ------------------------------------------------------------------------------------ // InterfaceReg *InterfaceReg::s_pInterfaceRegs = NULL; - InterfaceReg::InterfaceReg(InstantiateInterfaceFn fn, const char *pName) : m_pName(pName) { @@ -30,8 +27,6 @@ InterfaceReg::InterfaceReg(InstantiateInterfaceFn fn, const char *pName) : s_pInterfaceRegs = this; } - - // ------------------------------------------------------------------------------------ // // CreateInterface. // ------------------------------------------------------------------------------------ // @@ -58,60 +53,34 @@ EXPORT_FUNCTION IBaseInterface *CreateInterface(const char *pName, int *pReturnC return NULL; } - #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include "windows.h" #endif - -#ifdef _WIN32 HINTERFACEMODULE Sys_LoadModule(const char *pModuleName) { +#ifdef _WIN32 return (HINTERFACEMODULE)LoadLibrary(pModuleName); -} - #else // LINUX -HINTERFACEMODULE Sys_LoadModule(const char *pModuleName) -{ // Linux dlopen() doesn't look in the current directory for libraries. // We tell it to, so people don't have to 'install' libraries as root. + std::string library_path = std::filesystem::current_path().string() + "/" + pModuleName; - char szCwd[1024]; - char szAbsoluteLibFilename[1024]; - - getcwd(szCwd, sizeof(szCwd)); - if (szCwd[strlen(szCwd) - 1] == '/') - szCwd[strlen(szCwd) - 1] = 0; - - sprintf(szAbsoluteLibFilename, "%s/%s", szCwd, pModuleName); - - return (HINTERFACEMODULE)dlopen(szAbsoluteLibFilename, RTLD_NOW); -} - + return (HINTERFACEMODULE)dlopen(library_path.c_str(), RTLD_NOW); #endif +} - -#ifdef _WIN32 void Sys_FreeModule(HINTERFACEMODULE hModule) { if (!hModule) return; - +#ifdef _WIN32 FreeLibrary((HMODULE)hModule); -} - #else // LINUX -void Sys_FreeModule(HINTERFACEMODULE hModule) -{ - if (!hModule) - return; - dlclose((void *)hModule); -} - #endif - +} //----------------------------------------------------------------------------- // Purpose: returns the instance of this module @@ -122,29 +91,19 @@ CreateInterfaceFn Sys_GetFactoryThis(void) return CreateInterface; } - //----------------------------------------------------------------------------- // Purpose: returns the instance of the named module // Input : *pModuleName - name of the module // Output : interface_instance_t - instance of that module //----------------------------------------------------------------------------- -#ifdef _WIN32 CreateInterfaceFn Sys_GetFactory(HINTERFACEMODULE hModule) { if (!hModule) - return NULL; - + return nullptr; +#ifdef _WIN32 return (CreateInterfaceFn)GetProcAddress((HMODULE)hModule, CREATEINTERFACE_PROCNAME); -} - #else // LINUX -CreateInterfaceFn Sys_GetFactory(HINTERFACEMODULE hModule) -{ - if (!hModule) - return NULL; - return (CreateInterfaceFn)dlsym((void *)hModule, CREATEINTERFACE_PROCNAME); -} - #endif +} \ No newline at end of file diff --git a/metahook.cpp b/metahook.cpp index c5fefe0..061217e 100644 --- a/metahook.cpp +++ b/metahook.cpp @@ -1,3 +1,7 @@ +#include +#include +#include + #include "metahook.h" #include "LoadBlob.h" #include "Detours\detours.h" @@ -85,7 +89,7 @@ bool HM_LoadPlugins(char *filename, HINTERFACEMODULE hModule) plug->module = hModule; CreateInterfaceFn fnCreateInterface = Sys_GetFactory(plug->module); - plug->pPluginAPI = fnCreateInterface(METAHOOK_PLUGIN_API_VERSION, NULL); + plug->pPluginAPI = fnCreateInterface(METAHOOK_PLUGIN_API_VERSION, nullptr); if (plug->pPluginAPI) { @@ -94,7 +98,7 @@ bool HM_LoadPlugins(char *filename, HINTERFACEMODULE hModule) } else { - plug->pPluginAPI = fnCreateInterface(METAHOOK_PLUGIN_API_VERSION_V1, NULL); + plug->pPluginAPI = fnCreateInterface(METAHOOK_PLUGIN_API_VERSION_V1, nullptr); if (plug->pPluginAPI) plug->iInterfaceVersion = 1; @@ -110,14 +114,14 @@ bool HM_LoadPlugins(char *filename, HINTERFACEMODULE hModule) void MH_Init(const char *pszGameName) { - g_pfnbuild_number = NULL; - g_pfnClientDLL_Init = NULL; - g_phClientDLL_Init = NULL; + g_pfnbuild_number = nullptr; + g_pfnClientDLL_Init = nullptr; + g_phClientDLL_Init = nullptr; g_dwEngineBase = 0; g_dwEngineSize = 0; - g_pHookBase = NULL; - g_pExportFuncs = NULL; + g_pHookBase = nullptr; + g_pExportFuncs = nullptr; g_bSaveVideo = false; g_szTempFile[0] = 0; @@ -125,41 +129,47 @@ void MH_Init(const char *pszGameName) gInterface.FileSystem = g_pFileSystem; gInterface.Registry = registry; - char metapath[MAX_PATH], filename[MAX_PATH]; - sprintf(metapath, "%s/metahook", pszGameName); - sprintf(filename, "%s/configs/plugins.lst", metapath); + std::string metapath(pszGameName); + metapath += "/metahook"; - FILE *fp = fopen(filename, "rt"); + std::string filename(metapath); + filename += "/configs/plugins.lst"; - if (fp) - { - static char line[1024]; + std::ifstream plugin_list(filename); - while (!feof(fp)) + std::string plugin; + while (std::getline(plugin_list, plugin)) + { + if (!plugin.empty() && !std::all_of(plugin.begin(), plugin.end(), ::isspace)) { - static char plugins[64]; - fgets(line, sizeof(line), fp); - - if (line[0] == '\0' || line[0] == ';') - continue; - - sscanf(line, "%s ", plugins); - - if (!isalnum(plugins[0])) - continue; - - sprintf(filename, "%s/plugins/%s", metapath, plugins); - - HINTERFACEMODULE hModule = Sys_LoadModule(filename); + filename = metapath + "/plugins/" + plugin; + HINTERFACEMODULE hModule = Sys_LoadModule(filename.c_str()); if (!hModule) + { + DWORD dw = GetLastError(); + LPVOID lpMsgBuf; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&lpMsgBuf, + 0, NULL); + + MessageBox(NULL, ("Module name: " + filename + ". Error code: " + std::to_string(dw) + ". Message: " + static_cast(lpMsgBuf)).c_str(), "Problem loading module", MB_ICONWARNING); + LocalFree(lpMsgBuf); continue; + } - if (!HM_LoadPlugins(line, hModule)) + if (!HM_LoadPlugins(const_cast(filename.c_str()), hModule)) + { + MessageBox(NULL, ("Plugin name: " + filename).c_str(), "Problem loading plugin", MB_ICONWARNING); continue; + } } - - fclose(fp); } } @@ -405,7 +415,7 @@ void MH_FreeHook(hook_t *pHook) void MH_FreeAllHook(void) { - hook_t *next = NULL; + hook_t *next = nullptr; for (hook_t *h = g_pHookBase; h; h = next) { @@ -413,13 +423,13 @@ void MH_FreeAllHook(void) MH_FreeHook(h); } - g_pHookBase = NULL; + g_pHookBase = nullptr; } BOOL MH_UnHook(hook_t *pHook) { if (!g_pHookBase) - return FALSE; + return false; hook_t *h, **back; back = &g_pHookBase; @@ -435,13 +445,13 @@ BOOL MH_UnHook(hook_t *pHook) { *back = h->pNext; MH_FreeHook(h); - return TRUE; + return true; } back = &h->pNext; } - return FALSE; + return false; } hook_t *MH_InlineHook(void *pOldFuncAddr, void *pNewFuncAddr, void *&pCallBackFuncAddr) From a3e0fb828c92ed51ea7f78f9628d85c679a40dec Mon Sep 17 00:00:00 2001 From: LAGonauta Date: Wed, 10 Jul 2019 21:20:56 -0300 Subject: [PATCH 3/3] Fixed loading plugin on engine build 8279 and hopefully newer --- Plugins/Audio/snd_hook.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Plugins/Audio/snd_hook.cpp b/Plugins/Audio/snd_hook.cpp index 3649c6d..7b13ec9 100644 --- a/Plugins/Audio/snd_hook.cpp +++ b/Plugins/Audio/snd_hook.cpp @@ -27,6 +27,7 @@ aud_engine_t gAudEngine; #define S_STOPALLSOUNDS_SIG_NEW "\x55\x8B\xEC\xA1\x2A\x2A\x2A\x2A\x85\xC0\x74\x4F\x56\xC7\x05" #define S_UPDATE_SIG_NEW "\x55\x8B\xEC\xA1\x2A\x2A\x2A\x2A\x85\xC0\x0F\x84\x2A\x2A\x00\x00\xA1\x2A\x2A\x2A\x2A\x85\xC0\x0F\x8F\x2A\x2A\x00\x00" #define S_LOADSOUND_SIG_NEW "\x55\x8B\xEC\x81\xEC\x44\x05\x00\x00\x53\x56\x8B\x75\x08" +#define S_LOADSOUND_SIG_NEWEST "\x55\x8B\xEC\x81\xEC\x28\x05\x00\x00\x53\x8B\x5D\x08\x56\x57\x8A" #define SEQUENCE_GETSENTENCEBYINDEX_SIG_NEW "\x55\x8B\xEC\x56\x8B\x35\x2A\x2A\x2A\x2A\x85\xF6\x57\x74\x2A\x8B\x7D\x08\x8B\x06\x57\x50\xE8" #define VOICESE_IDLE_SIG_NEW "\x55\x8B\xEC\xA0\x2A\x2A\x2A\x2A\xD9\x05\x2A\x2A\x2A\x2A\x84\xC0\x74\x2A\xD8\x1D" #ifdef _DEBUG @@ -87,7 +88,14 @@ void S_FillAddress(void) gAudEngine.S_Update = (void(*)(float *, float *, float *, float *))Search_Pattern_From(S_StopAllSounds, S_UPDATE_SIG_NEW); Sig_FuncNotFound(S_Update); - gAudEngine.S_LoadSound = (aud_sfxcache_t *(*)(sfx_t *, aud_channel_t *))Search_Pattern_From(S_Update, S_LOADSOUND_SIG_NEW); + if (g_dwEngineBuildnum >= 8279) + { + gAudEngine.S_LoadSound = (aud_sfxcache_t *(*)(sfx_t *, aud_channel_t *))Search_Pattern_From(S_Update, S_LOADSOUND_SIG_NEWEST); + } + else + { + gAudEngine.S_LoadSound = (aud_sfxcache_t *(*)(sfx_t *, aud_channel_t *))Search_Pattern_From(S_Update, S_LOADSOUND_SIG_NEW); + } Sig_FuncNotFound(S_LoadSound); gAudEngine.SequenceGetSentenceByIndex = (sentenceEntry_s*(*)(unsigned int))Search_Pattern(SEQUENCE_GETSENTENCEBYINDEX_SIG_NEW);