Skip to content

Commit

Permalink
Merge remote-tracking branch 'vsts/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
LAGonauta committed Jul 11, 2019
2 parents a8061c3 + a3e0fb8 commit f90424f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 95 deletions.
63 changes: 11 additions & 52 deletions HLSDK/common/interface.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//========= Copyright © 1996-2002, Valve LLC, All rights reserved. ============
//
// Purpose:
// Purpose:
//
// $NoKeywords: $
//=============================================================================
Expand All @@ -11,17 +11,14 @@

#ifndef _WIN32 // LINUX
#include <dlfcn.h>
#include <unistd.h> // getcwd
#include <stdio.h> // sprintf
#include <filesystem>
#endif


// ------------------------------------------------------------------------------------ //
// InterfaceReg.
// ------------------------------------------------------------------------------------ //
InterfaceReg *InterfaceReg::s_pInterfaceRegs = NULL;


InterfaceReg::InterfaceReg(InstantiateInterfaceFn fn, const char *pName) :
m_pName(pName)
{
Expand All @@ -30,8 +27,6 @@ InterfaceReg::InterfaceReg(InstantiateInterfaceFn fn, const char *pName) :
s_pInterfaceRegs = this;
}



// ------------------------------------------------------------------------------------ //
// CreateInterface.
// ------------------------------------------------------------------------------------ //
Expand All @@ -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
Expand All @@ -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
}
7 changes: 2 additions & 5 deletions Plugins/Audio/snd_dma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::Vector3, alure::Vector3> alure_orientation(
alure::Vector3(orientation[0], orientation[1], orientation[2]),
Expand Down
10 changes: 9 additions & 1 deletion Plugins/Audio/snd_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
84 changes: 47 additions & 37 deletions metahook.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <fstream>
#include <string>
#include <algorithm>

#include "metahook.h"
#include "LoadBlob.h"
#include "Detours\detours.h"
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -110,56 +114,62 @@ 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;

gInterface.CommandLine = CommandLine();
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<LPCTSTR>(lpMsgBuf)).c_str(), "Problem loading module", MB_ICONWARNING);
LocalFree(lpMsgBuf);
continue;
}

if (!HM_LoadPlugins(line, hModule))
if (!HM_LoadPlugins(const_cast<char*>(filename.c_str()), hModule))
{
MessageBox(NULL, ("Plugin name: " + filename).c_str(), "Problem loading plugin", MB_ICONWARNING);
continue;
}
}

fclose(fp);
}
}

Expand Down Expand Up @@ -405,21 +415,21 @@ 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)
{
next = h->pNext;
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;
Expand All @@ -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)
Expand Down

0 comments on commit f90424f

Please sign in to comment.