Skip to content

Commit

Permalink
chore: removed a lot of unnecessary code in ivp hook
Browse files Browse the repository at this point in the history
  • Loading branch information
shockpast committed Nov 25, 2024
1 parent a4d5913 commit bd2c3f7
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 172 deletions.
17 changes: 9 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
development
Bootil/projects/obj/
Bootil/projects/release_x64_windows/
Bootil/projects/release_x32_windows/
Bootil/projects/release_x64_linux/
Bootil/projects/release_x32_linux/
**/.vs/
**/.vscode/

development

source/modules/template.c++
source/modules/32x/**
source/modules/64x/**
source/sourcesdk/tempshit

projects
premake5

# scripts
build.sh
build_debug.sh
build.sh
build_symbols.sh
76 changes: 26 additions & 50 deletions source/detour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,47 @@ SymbolFinder detour::symFinder;

void* detour::GetFunction(void* module, Symbol symbol)
{
return symFinder.Resolve(module, symbol.name.c_str(), symbol.length);
return symFinder.Resolve(module, symbol.name.c_str(), symbol.length);
}

std::unordered_set<std::string> pDisabledDetours;
std::map<unsigned int, std::vector<Detouring::Hook*>> g_pDetours = {};
void detour::Create(Detouring::Hook* hook, const char* name, void* module, Symbol symbol, void* hook_func, unsigned int category)
{
if (pDisabledDetours.find(name) != pDisabledDetours.end())
{
Msg("detour %s was disabled!\n", name);
return;
}
if (pDisabledDetours.find(name) != pDisabledDetours.end())
{
Msg("detour %s was disabled!\n", name);
return;
}

void* func = detour::GetFunction(module, symbol);
if (!CheckFunction(func, name))
return;
void* func = detour::GetFunction(module, symbol);
if (!CheckFunction(func, name))
return;

hook->Create(func, hook_func);
hook->Enable();
hook->Create(func, hook_func);
hook->Enable();

g_pDetours[category].push_back(hook);
g_pDetours[category].push_back(hook);

if (!DETOUR_ISVALID((*hook)))
Msg("failed to detour %s!\n", name);
if (!DETOUR_ISVALID((*hook)))
Msg("failed to detour %s!\n", name);
}

void detour::Remove(unsigned int category) // NOTE: Do we need to check if the provided category is valid?
{
for (Detouring::Hook* hook : g_pDetours[category]) {
if (hook->IsEnabled())
{
hook->Disable();
hook->Destroy();
}
}
g_pDetours[category].clear();
for (Detouring::Hook* hook : g_pDetours[category]) {
if (hook->IsEnabled())
{
hook->Disable();
hook->Destroy();
}
}
g_pDetours[category].clear();
}

void detour::ReportLeak()
{
for (auto& [id, hooks] : g_pDetours)
if (hooks.size() > 0)
Msg("ID %d failed to shutdown it's detours!\n", id);
}

static void ToggleDetour(const CCommand& args)
{
if (args.ArgC() < 1 || V_stricmp(args.Arg(1), "") == 0)
{
Msg("disabled detours:\n");
for (auto it = pDisabledDetours.begin(); it != pDisabledDetours.end(); ++it)
Msg(" - \"%s\"\n", it->c_str());

return;
}

auto it = pDisabledDetours.find(args.Arg(1));
if (it != pDisabledDetours.end())
{
pDisabledDetours.erase(it);
Msg("enabled detour %s!\n", args.Arg(1));
return;
}

pDisabledDetours.insert(args.Arg(1));
Msg("disabled detour %s!\n", args.Arg(1));
}
static ConCommand toggledetour("sho_toggledetour", ToggleDetour, "Debug command. Enables/Disables the given detour.", 0);
for (auto& [id, hooks] : g_pDetours)
if (hooks.size() > 0)
Msg("ID %d failed to shutdown it's detours!\n", id);
}
135 changes: 65 additions & 70 deletions source/detour.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// https://github.com/RaphaelIT7/gmod-holylib/blob/main/source/detours.h

#pragma once

#define TIER0_DLL_EXPORT
Expand All @@ -17,54 +15,51 @@
//---------------------------------------------------------------------------------
namespace detour
{
#define DETOUR_ISVALID(detour) detour.IsValid()

#define DETOUR_ISENABLED(detour) detour.IsEnabled()

#define DETOUR_ENABLE(detour) detour.Enable()

#define DETOUR_DISABLE(detour) detour.Disable()

inline bool CheckValue(const char* msg, const char* name, bool ret)
{
if (!ret) {
Msg("[sho] Failed to %s %s!\n", msg, name);
return false;
}

return true;
}

inline bool CheckValue(const char* name, bool ret)
{
return CheckValue("get function", name, ret);
}

inline bool CheckFunction(void* func, const char* name)
{
return CheckValue("get function", name, func != nullptr);
}

extern void* GetFunction(void* module, Symbol symbol);
extern void Create(Detouring::Hook* hook, const char* name, void* module, Symbol symbol, void* func, unsigned int category);
extern void Remove(unsigned int category); // 0 = All
extern void ReportLeak();
extern unsigned int g_pCurrentCategory;

extern SymbolFinder symFinder;
template<class T>
inline T* ResolveSymbol(SourceSDK::FactoryLoader& loader, const Symbol& symbol)
{
if (symbol.type == Symbol::Type::None)
return nullptr;
#define DETOUR_ISVALID(detour) detour.IsValid()
#define DETOUR_ISENABLED(detour) detour.IsEnabled()
#define DETOUR_ENABLE(detour) detour.Enable()
#define DETOUR_DISABLE(detour) detour.Disable()

inline bool CheckValue(const char* msg, const char* name, bool ret)
{
if (!ret) {
Msg("[sho] Failed to %s %s!\n", msg, name);
return false;
}

return true;
}

inline bool CheckValue(const char* name, bool ret)
{
return CheckValue("get function", name, ret);
}

inline bool CheckFunction(void* func, const char* name)
{
return CheckValue("get function", name, func != nullptr);
}

extern void* GetFunction(void* module, Symbol symbol);
extern void Create(Detouring::Hook* hook, const char* name, void* module, Symbol symbol, void* func, unsigned int category);
extern void Remove(unsigned int category); // 0 = All
extern void ReportLeak();
extern unsigned int g_pCurrentCategory;

extern SymbolFinder symFinder;
template<class T>
inline T* ResolveSymbol(SourceSDK::FactoryLoader& loader, const Symbol& symbol)
{
if (symbol.type == Symbol::Type::None)
return nullptr;

#if defined SYSTEM_WINDOWS
auto iface = reinterpret_cast<T**>(symFinder.Resolve(loader.GetModule(), symbol.name.c_str(), symbol.length));
return iface != nullptr ? *iface : nullptr;
auto iface = reinterpret_cast<T**>(symFinder.Resolve(loader.GetModule(), symbol.name.c_str(), symbol.length));
return iface != nullptr ? *iface : nullptr;
#elif defined SYSTEM_POSIX
return reinterpret_cast<T*>(symFinder.Resolve(loader.GetModule(), symbol.name.c_str(), symbol.length));
return reinterpret_cast<T*>(symFinder.Resolve(loader.GetModule(), symbol.name.c_str(), symbol.length));
#endif
}
}

#ifdef SYSTEM_LINUX
#if ARCHITECTURE_IS_X86
Expand All @@ -80,42 +75,42 @@ namespace detour
#endif
#endif

template<class T>
inline T* ResolveSymbol(SourceSDK::FactoryLoader& loader, const std::vector<Symbol>& symbols)
{
template<class T>
inline T* ResolveSymbol(SourceSDK::FactoryLoader& loader, const std::vector<Symbol>& symbols)
{
#if DETOUR_SYMBOL_ID != 0
if ((symbols.size() - 1) < DETOUR_SYMBOL_ID)
return NULL;
if ((symbols.size() - 1) < DETOUR_SYMBOL_ID)
return NULL;
#endif

#if defined SYSTEM_WINDOWS
auto iface = reinterpret_cast<T**>(symFinder.Resolve(loader.GetModule(), symbols[DETOUR_SYMBOL_ID].name.c_str(), symbols[DETOUR_SYMBOL_ID].length));
return iface != nullptr ? *iface : nullptr;
auto iface = reinterpret_cast<T**>(symFinder.Resolve(loader.GetModule(), symbols[DETOUR_SYMBOL_ID].name.c_str(), symbols[DETOUR_SYMBOL_ID].length));
return iface != nullptr ? *iface : nullptr;
#elif defined SYSTEM_POSIX
return reinterpret_cast<T*>(symFinder.Resolve(loader.GetModule(), symbols[DETOUR_SYMBOL_ID].name.c_str(), symbols[DETOUR_SYMBOL_ID].length));
return reinterpret_cast<T*>(symFinder.Resolve(loader.GetModule(), symbols[DETOUR_SYMBOL_ID].name.c_str(), symbols[DETOUR_SYMBOL_ID].length));
#endif
}
}

inline void* GetFunction(void* module, std::vector<Symbol> symbols)
{
inline void* GetFunction(void* module, std::vector<Symbol> symbols)
{
#if DETOUR_SYMBOL_ID != 0
if ((symbols.size() - 1) < DETOUR_SYMBOL_ID)
return NULL;
if ((symbols.size() - 1) < DETOUR_SYMBOL_ID)
return NULL;
#endif

return GetFunction(module, symbols[DETOUR_SYMBOL_ID]);
}
return GetFunction(module, symbols[DETOUR_SYMBOL_ID]);
}

inline void Create(Detouring::Hook* hook, const char* name, void* module, std::vector<Symbol> symbols, void* func, unsigned int category)
{
inline void Create(Detouring::Hook* hook, const char* name, void* module, std::vector<Symbol> symbols, void* func, unsigned int category)
{
#if DETOUR_SYMBOL_ID != 0
if ((symbols.size() - 1) < DETOUR_SYMBOL_ID)
{
CheckFunction(nullptr, name);
return;
}
if ((symbols.size() - 1) < DETOUR_SYMBOL_ID)
{
CheckFunction(nullptr, name);
return;
}
#endif

Create(hook, name, module, symbols[DETOUR_SYMBOL_ID], func, category);
}
Create(hook, name, module, symbols[DETOUR_SYMBOL_ID], func, category);
}
}
8 changes: 4 additions & 4 deletions source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <GarrysMod/Lua/Interface.h>
#include "interfaces/interfaces.h"
#include <interfaces/interfaces.h>

#include "modules/hush.h"

GMOD_MODULE_OPEN()
{
hush::initialize();
hush::initialize();

return 0;
return 0;
}

GMOD_MODULE_CLOSE()
{
return 0;
return 0;
}
30 changes: 7 additions & 23 deletions source/modules/hush.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include <iostream>
#include <cstdarg>
#include <basetypes.h>
#include <stdio.h>
#include <regex>

#include "hush.h"
#include "../symbols.h"
#include "detour.h"

#define MAX_ERROR_BUFFER_LEN 10000 // vphysics
Expand All @@ -14,32 +12,18 @@
static Detouring::Hook detour_ivp_message;
void hush::ivp_message(const char* templat, ...)
{
if (strstr(templat, "do_constraint_system: Couldn't invert rot matrix!"))
return;
if (strstr(templat, "do_constraint_system: Couldn't invert rot matrix!"))
return;

// making args.. yada yada yada
//
// not sure that all of below is needed
// most likely this can be simplified
char buffer[MAX_ERROR_BUFFER_LEN];
char* p = buffer;
va_list args;
va_start(args, templat);

memset(buffer, 0, MIN(1000, MAX_ERROR_BUFFER_LEN));
sprintf(buffer, "ERROR: ");
p += strlen(buffer);

va_list args;
va_start(args, templat);
vsnprintf(buffer, MAX_MAKE_STRING_LEN, templat, args);
va_end(args);

//
detour_ivp_message.GetTrampoline<symbols::ivp_message>()(buffer, args);
detour_ivp_message.GetTrampoline<symbols::ivp_message>()(templat, args);
}

void hush::initialize()
{
SourceSDK::ModuleLoader vphysics("vphysics");
SourceSDK::ModuleLoader vphysics("vphysics");

detour::Create(&detour_ivp_message, "ivp_message", vphysics.GetModule(), symbols::ivp_messageSym, (void*)hush::ivp_message, 0);
detour::Create(&detour_ivp_message, "ivp_message", vphysics.GetModule(), symbols::ivp_messageSym, (void*)hush::ivp_message, 0);
}
5 changes: 2 additions & 3 deletions source/modules/hush.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

#include <iostream>
#include <cstdarg>
#include <basetypes.h>

#define MAX_ERROR_BUFFER_LEN 10000 // vphysics
#define MAX_MAKE_STRING_LEN 10000 // vphysics

namespace hush
{
void ivp_message(const char* templat, ...);
void ivp_message(const char* templat, ...);

void initialize();
void initialize();
}
Loading

0 comments on commit bd2c3f7

Please sign in to comment.