Skip to content

Commit

Permalink
fix(sourcehook): Fix compilation issues with standalone SourceHook
Browse files Browse the repository at this point in the history
  • Loading branch information
psychonic committed Aug 4, 2024
1 parent aa3d9f8 commit f5fe5a7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 15 deletions.
14 changes: 9 additions & 5 deletions core/metamod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static bool g_bIsVspBridged = false;

MetamodSource g_Metamod;
PluginId g_PLID = Pl_Console;
CSourceHookImpl g_SourceHook;
CSourceHookImpl g_SourceHook(mm_LogMessage);
ISourceHook *g_SHPtr = &g_SourceHook;
SourceMM::ISmmAPI *g_pMetamod = &g_Metamod;

Expand Down Expand Up @@ -432,12 +432,13 @@ class SaveAndSet {
T old_;
};

void
int
mm_LogMessage(const char *msg, ...)
{
int ret = 0;
static bool g_logging = false;
if (g_logging) {
return;
return ret;
}
SaveAndSet<bool>(&g_logging, true);

Expand All @@ -448,19 +449,22 @@ mm_LogMessage(const char *msg, ...)
size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
len = std::min<size_t>(len, sizeof(buffer) - 2);
if (len < 0) {
return;
return ret;
}

va_end(ap);

buffer[len++] = '\n';
buffer[len] = '\0';


if (!provider->LogMessage(buffer))
{
fprintf(stdout, "%s", buffer);
ret = fprintf(stdout, "%s", buffer);
}
provider->ConsolePrint(buffer);

return ret;
}

static void
Expand Down
2 changes: 1 addition & 1 deletion core/metamod.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class MetamodSource : public ISmmAPI
bool
mm_DetectGameInformation();

void
int
mm_LogMessage(const char *msg, ...);

int
Expand Down
9 changes: 8 additions & 1 deletion core/sourcehook/generate/sourcehook.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ enum META_RES

namespace SourceHook
{
/**
* @brief SourceHook's debug log function
*/
typedef int (*DebugLogFunc)(const char*, ...);

/**
* @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
*/
Expand Down Expand Up @@ -194,7 +199,7 @@ namespace SourceHook
// SH tries to auto-detect these
// If you want to override SH's auto-detection, pass them in yourself
PassFlag_RetMem = (1<<6), /**< Object is returned in memory (through hidden first param */
PassFlag_RetReg = (1<<7) /**< Object is returned in EAX(:EDX) */
PassFlag_RetReg = (1<<7) /**< Object is returned in EAX(:EDX)/RAX(x86_64) */
};

size_t size; //!< Size of the data being passed
Expand Down Expand Up @@ -499,6 +504,8 @@ namespace SourceHook
const void *origRetPtr, void *overrideRetPtr) = 0;

virtual void EndContext(IHookContext *pCtx) = 0;

virtual void LogDebug(const char *pFormat, ...) = 0;
};


Expand Down
11 changes: 10 additions & 1 deletion core/sourcehook/sourcehook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ namespace SourceHook
//////////////////////////////////////////////////////////////////////////


CSourceHookImpl::CSourceHookImpl()
CSourceHookImpl::CSourceHookImpl(DebugLogFunc logfunc)
{
m_LogFunc = logfunc;
}
CSourceHookImpl::~CSourceHookImpl()
{
Expand Down Expand Up @@ -639,6 +640,14 @@ namespace SourceHook
ResolvePendingUnloads();
}

void CSourceHookImpl::LogDebug(const char *format, ...)
{
va_list args;
va_start(args, format);
m_LogFunc(format, args);
va_end(args);
}

void CSourceHookImpl::CompleteShutdown()
{
CVector<int> removehooks;
Expand Down
10 changes: 8 additions & 2 deletions core/sourcehook/sourcehook.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
#endif

#ifdef SH_DEBUG

# include <stdio.h>
# include <stdlib.h>

Expand Down Expand Up @@ -133,6 +132,11 @@ enum META_RES

namespace SourceHook
{
/**
* @brief SourceHook's debug log function
*/
typedef int (*DebugLogFunc)(const char*, ...);

/**
* @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
*/
Expand Down Expand Up @@ -500,6 +504,8 @@ namespace SourceHook
const void *origRetPtr, void *overrideRetPtr) = 0;

virtual void EndContext(IHookContext *pCtx) = 0;

virtual void LogDebug(const char *pFormat, ...) = 0;
};


Expand Down Expand Up @@ -4929,4 +4935,4 @@ namespace SourceHook
}

#endif
// The pope is dead. -> :(
// The pope is dead. -> :(
13 changes: 10 additions & 3 deletions core/sourcehook/sourcehook_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ New SH_CALL
#include "sourcehook_impl_ciface.h"
#include "sourcehook_impl_cvfnptr.h"
#include "sourcehook_impl_chookidman.h"
#include <cstdio>
#include <stdarg.h>

void mm_LogMessage(const char* msg, ...);
extern SourceHook::ISourceHook *g_SHPtr;

namespace SourceHook
{
Expand All @@ -203,10 +204,13 @@ namespace SourceHook
template<typename... Args>
inline void SH_DEBUG_LOG(SH_LOG log_level, const char* message, Args... args)
{
#ifndef SOURCEHOOK_TESTS
if (log_level < sh_log_level) {
return;
}
mm_LogMessage(message, args...);

SH_GLOB_SHPTR->LogDebug(message, args...);
#endif
}

struct CHookContext : IHookContext
Expand Down Expand Up @@ -322,12 +326,13 @@ namespace SourceHook
CHookIDManager m_HookIDMan;
HookContextStack m_ContextStack;
List<PendingUnload *> m_PendingUnloads;
DebugLogFunc m_LogFunc;

bool SetHookPaused(int hookid, bool paused);
CHookManList::iterator RemoveHookManager(CHookManList::iterator iter);
List<CVfnPtr>::iterator RevertAndRemoveVfnPtr(List<CVfnPtr>::iterator vfnptr_iter);
public:
CSourceHookImpl();
CSourceHookImpl(DebugLogFunc logfunc = printf);
virtual ~CSourceHookImpl();

/**
Expand Down Expand Up @@ -381,6 +386,8 @@ namespace SourceHook

void DoRecall();

void LogDebug(const char *pFormat, ...) override;

IHookContext *SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origCallAddr, META_RES *statusPtr,
META_RES *prevResPtr, META_RES *curResPtr, const void *origRetPtr, void *overrideRetPtr);

Expand Down
12 changes: 10 additions & 2 deletions core/sourcehook/test/AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import os
for cxx in MMS.all_targets:
name = 'test_sourcehook'
binary = MMS.Program(cxx, name)

binary.compiler.defines += [
'SOURCEHOOK_TESTS',
]

binary.compiler.cxxincludes += [
os.path.join(builder.sourcePath, 'core', 'sourcehook'),
]
Expand All @@ -19,6 +24,7 @@ for cxx in MMS.all_targets:
'../sourcehook_impl_chookidman.cpp',
'../sourcehook_impl_cproto.cpp',
'../sourcehook_impl_cvfnptr.cpp',
'../sourcehook_hookmangen.cpp',
'test1.cpp',
'test2.cpp',
'test3.cpp',
Expand All @@ -36,7 +42,9 @@ for cxx in MMS.all_targets:
'testrefret.cpp',
'testvphooks.cpp',
]
if binary.compiler.target.arch == 'x86':
binary.sources += ['../sourcehook_hookmangen.cpp']
if cxx.target.arch == 'x86':
binary.sources += ['../sourcehook_hookmangen_x86.cpp']
elif binary.compiler.target.arch == 'x86_64':
binary.sources += ['../sourcehook_hookmangen_x86_64.cpp']

builder.Add(binary)

0 comments on commit f5fe5a7

Please sign in to comment.