Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix standalone SourceHook compilation #188

Merged
merged 5 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ on:
push:
branches:
- master
- '[0-9]+.[0-9]+-dev'
pull_request:
branches:
- master
- '[0-9]+.[0-9]+-dev'
jobs:
test:
strategy:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ Release.*/

# AMBuild build directories
build/
build-*/
obj-*/
.gdb_history
1 change: 0 additions & 1 deletion AMBuildScript
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ class MMSConfig(object):
os.path.join(context.sourcePath, 'loader'),
]

defines = []
for other_sdk in self.sdk_manifests:
cxx.defines += ['SE_{}={}'.format(other_sdk['define'], other_sdk['code'])]

Expand Down
14 changes: 9 additions & 5 deletions core/metamod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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 @@ -430,12 +430,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 @@ -446,19 +447,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 @@ -102,7 +102,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
14 changes: 12 additions & 2 deletions core/sourcehook/test/AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import os
for cxx in MMS.all_targets:
name = 'test_sourcehook'
binary = MMS.Program(cxx, name)
if binary.compiler.target.arch == 'x86_64' and binary.compiler.target.platform == 'linux':
continue

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

binary.compiler.cxxincludes += [
os.path.join(builder.sourcePath, 'core', 'sourcehook'),
]
Expand All @@ -19,6 +26,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 +44,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)
Loading