Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
66c172d
Introduce KHook
Kenzzer Apr 17, 2025
fe9b9ee
Fix incorrect submodule
Kenzzer Apr 17, 2025
d548c9b
Add khooks to source 2 provider
Kenzzer Apr 19, 2025
6cb137e
update khook
Kenzzer Apr 19, 2025
df87bde
Fix provider_source_console & dota2 compilation
Kenzzer Apr 19, 2025
52eb132
Disable CI sourcehook testing (temporarily)
Kenzzer Apr 19, 2025
eee0ae7
expose khook to plugins
Kenzzer Apr 23, 2025
fbc8d42
Fix errors from rebase
Kenzzer Apr 23, 2025
04c0c99
update submodule
Kenzzer Apr 23, 2025
dc1bfdd
move submodule to third_party directory
Kenzzer Apr 29, 2025
5e58900
update submodule to latest commit
Kenzzer Apr 29, 2025
37e0273
Fix submodule name + fix invalid function call
Kenzzer Apr 29, 2025
c31229b
Allow workflow to be manually dispatched + Fix windows compilation
Kenzzer Apr 29, 2025
5957edb
Fix const-ness issue, and stabilise virtual function api
Kenzzer Apr 29, 2025
c5fd003
update submodule
Kenzzer Apr 29, 2025
a88a34d
Fix erroneous definition for khook global var
Kenzzer Apr 29, 2025
dd6d96d
Slow down metamod load + fix memory flags
Kenzzer Aug 14, 2025
393bcf2
Fix deadlock + Fix crash on x86
Kenzzer Aug 14, 2025
3b71561
Re-allow metamod to load fast
Kenzzer Aug 14, 2025
20a9cc5
Update sample extension to use khook
Kenzzer Aug 15, 2025
b1bc1b5
Fix another deadlock
Kenzzer Aug 15, 2025
3564257
Update s2 sample extension to khook
Kenzzer Aug 15, 2025
80e9709
Supercede -> Supersede + Call original helper
Kenzzer Aug 15, 2025
b0d5161
Make gamedll bridge more legible
Kenzzer Aug 15, 2025
87913ea
Fix hook callback mistake + fix s2 sample compilation
Kenzzer Aug 15, 2025
db50642
Add Hook recall feature
Kenzzer Aug 16, 2025
b802856
Add Recall feature to x86
Kenzzer Aug 17, 2025
b460ed8
Add thread safety to return value
Kenzzer Aug 27, 2025
e54ec20
Remove GetHookAction
Kenzzer Aug 27, 2025
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
63 changes: 32 additions & 31 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Continuous Integration
on:
workflow_dispatch:
push:
branches:
- master
Expand Down Expand Up @@ -98,36 +99,36 @@ jobs:
python ../configure.py --enable-optimize --sdks=${{ join(fromJSON(env.SDKS)) }} --hl2sdk-root=${{ env.DEPENDENCIES_ROOT }}
ambuild

- name: Test SourceHook (Optimized)
working-directory: metamod-source
shell: bash
# SourceHook tests are busted on Windows
continue-on-error: ${{ startsWith(runner.os, 'Windows') }}
run: |
mkdir build-sh-opt && cd build-sh-opt
python ../configure.py --enable-optimize --enable-tests --sdks=
ambuild
cd ./core/sourcehook/test/test_sourcehook
if [ -d "${{ matrix.os_short }}-x86" ]; then
./${{ matrix.os_short }}-x86/test_sourcehook -v
fi
if [ -d "${{ matrix.os_short }}-x86_64" ]; then
./${{ matrix.os_short }}-x86_64/test_sourcehook -v
fi
#- name: Test SourceHook (Optimized)
# working-directory: metamod-source
# shell: bash
# # SourceHook tests are busted on Windows
# continue-on-error: ${{ startsWith(runner.os, 'Windows') }}
# run: |
# mkdir build-sh-opt && cd build-sh-opt
# python ../configure.py --enable-optimize --enable-tests --sdks=
# ambuild
# cd ./core/sourcehook/test/test_sourcehook
# if [ -d "${{ matrix.os_short }}-x86" ]; then
# ./${{ matrix.os_short }}-x86/test_sourcehook -v
# fi
# if [ -d "${{ matrix.os_short }}-x86_64" ]; then
# ./${{ matrix.os_short }}-x86_64/test_sourcehook -v
# fi

- name: Test SourceHook (Debug)
working-directory: metamod-source
shell: bash
#- name: Test SourceHook (Debug)
# working-directory: metamod-source
# shell: bash
# SourceHook tests are busted on Windows
continue-on-error: ${{ startsWith(runner.os, 'Windows') }}
run: |
mkdir build-sh-debug && cd build-sh-debug
python ../configure.py --enable-debug --enable-tests --sdks=
ambuild
cd ./core/sourcehook/test/test_sourcehook
if [ -d "${{ matrix.os_short }}-x86" ]; then
./${{ matrix.os_short }}-x86/test_sourcehook -v
fi
if [ -d "${{ matrix.os_short }}-x86_64" ]; then
./${{ matrix.os_short }}-x86_64/test_sourcehook -v
fi
# continue-on-error: ${{ startsWith(runner.os, 'Windows') }}
# run: |
# mkdir build-sh-debug && cd build-sh-debug
# python ../configure.py --enable-debug --enable-tests --sdks=
# ambuild
# cd ./core/sourcehook/test/test_sourcehook
# if [ -d "${{ matrix.os_short }}-x86" ]; then
# ./${{ matrix.os_short }}-x86/test_sourcehook -v
# fi
# if [ -d "${{ matrix.os_short }}-x86_64" ]; then
# ./${{ matrix.os_short }}-x86_64/test_sourcehook -v
# fi
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "hl2sdk-manifests"]
path = hl2sdk-manifests
url = https://github.com/alliedmodders/hl2sdk-manifests
[submodule "third_party/khook"]
path = third_party/khook
url = https://github.com/Kenzzer/KHook
5 changes: 3 additions & 2 deletions AMBuildScript
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class MMSConfig(object):
self.versionlib = None
self.all_targets = []
self.target_archs = set()
self.libsafetyhook = {}
self.libkhook = {}

if builder.options.targets:
target_archs = builder.options.targets.split(',')
Expand Down Expand Up @@ -325,7 +327,6 @@ class MMSConfig(object):

cxx.cxxincludes += [
os.path.join(context.currentSourcePath),
os.path.join(context.currentSourcePath, 'sourcehook'),
os.path.join(context.sourcePath, 'loader'),
]

Expand Down Expand Up @@ -370,7 +371,6 @@ BuildScripts = [
]
if getattr(builder.options, 'enable_tests', False):
BuildScripts += [
'core/sourcehook/test/AMBuilder',
'loader/test/AMBuilder',
]

Expand All @@ -379,6 +379,7 @@ if builder.backend == 'amb2':
'support/buildbot/PackageScript',
]

builder.Build(['third_party/khook/AMBuilder', 'third_party/khook/third_party/safetyhook/AMBuilder'], { 'KHook': MMS, 'SafetyHook': MMS })
builder.Build(BuildScripts, { 'MMS': MMS })

if builder.options.breakpad_dump:
Expand Down
20 changes: 9 additions & 11 deletions core/AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ for sdk_target in MMS.sdk_targets:

name = 'metamod.' + sdk['extension']
binary = MMS.HL2Library(builder, cxx, name, sdk)
binary.compiler.cxxincludes += [os.path.join(builder.sourcePath, 'third_party', 'khook', 'include')]

binary.compiler.defines += ['KHOOK_STANDALONE', 'KHOOK_EXPORT']
for task in MMS.libkhook:
if task.target.arch == binary.compiler.target.arch:
binary.compiler.linkflags += [task.binary]
for task in MMS.libsafetyhook:
if task.target.arch == binary.compiler.target.arch:
binary.compiler.linkflags += [task.binary]

binary.sources += [
'metamod.cpp',
Expand All @@ -15,12 +24,6 @@ for sdk_target in MMS.sdk_targets:
'metamod_plugins.cpp',
'metamod_util.cpp',
'provider/provider_base.cpp',
'sourcehook/sourcehook.cpp',
'sourcehook/sourcehook_impl_chookidman.cpp',
'sourcehook/sourcehook_impl_chookmaninfo.cpp',
'sourcehook/sourcehook_impl_cproto.cpp',
'sourcehook/sourcehook_impl_cvfnptr.cpp',
'sourcehook/sourcehook_hookmangen.cpp',
'gamedll_bridge.cpp'
]

Expand All @@ -38,10 +41,5 @@ for sdk_target in MMS.sdk_targets:
'vsp_bridge.cpp'
]
binary.compiler.defines += ['_ALLOW_KEYWORD_MACROS']

if cxx.target.arch == 'x86':
binary.sources += ['sourcehook/sourcehook_hookmangen_x86.cpp']
elif binary.compiler.target.arch == 'x86_64' and binary.compiler.target.platform != 'linux':
binary.sources += ['sourcehook/sourcehook_hookmangen_x86_64.cpp']
nodes = builder.Add(binary)
MMS.binaries += [nodes]
13 changes: 2 additions & 11 deletions core/ISmmAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

#include <cstdarg>

#include "sourcehook.h"
#include "IPluginManager.h"

#if defined META_NO_HL2SDK
Expand All @@ -57,6 +56,7 @@ typedef ConCommandBase ProviderConCommand;
#endif
#endif

#define MMIFACE_KHOOK "IKHook" /**< IKHook Pointer */
#define MMIFACE_SOURCEHOOK "ISourceHook" /**< ISourceHook Pointer */
#define MMIFACE_PLMANAGER "IPluginManager" /**< SourceMM Plugin Functions */
#define MMIFACE_SH_HOOKMANAUTOGEN "IHookManagerAutoGen" /**< SourceHook::IHookManagerAutoGen Pointer */
Expand Down Expand Up @@ -189,14 +189,6 @@ namespace SourceMM
*/
virtual void GetApiVersions(int &major, int &minor, int &plvers, int &plmin) =0;

/**
* @brief Returns sourcehook API version and implementation version.
*
* @param shvers Filled with the SourceHook API version number.
* @param shimpl Filled with the SourceHook implementation number.
*/
virtual void GetShVersions(int &shvers, int &shimpl) =0;

/**
* @brief Adds a Metamod listener.
*
Expand Down Expand Up @@ -467,5 +459,4 @@ using namespace SourceMM;
* 1.6.0 Added API for Orange Box. Broke backwards compatibility.
*/

#endif //_INCLUDE_ISMM_API_H

#endif //_INCLUDE_ISMM_API_H
8 changes: 4 additions & 4 deletions core/ISmmPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
* The pure-virtual ISmmPlugin callbacks must be implemented for the load to load.
*/

#include "sourcehook.h"
#include "IPluginManager.h"
#include "ISmmAPI.h"
#include "ISmmPluginExt.h"
Expand Down Expand Up @@ -487,11 +486,12 @@ using namespace SourceMM;
#define PL_EXPOSURE_FUNC(name, var) EXPOSE_SINGLE_INTERFACE_GLOBALVAR(ISmmPlugin, ISmmPlugin, METAMOD_PLAPI_NAME, var);
#endif

#include "khook.hpp"
#define PLUGIN_EXPOSE(name, var) \
ISmmAPI *g_SMAPI = NULL; \
ISmmPlugin *g_PLAPI = NULL; \
PluginId g_PLID = (PluginId)0; \
SourceHook::ISourceHook *g_SHPtr = NULL; \
namespace KHook { KHook::IKHook* __exported__khook = nullptr; } \
PL_EXPOSURE_FUNC(name, var)


Expand All @@ -503,7 +503,7 @@ using namespace SourceMM;
* to use values like g_SHPtr in other files.
*/
#define PLUGIN_GLOBALVARS() \
extern SourceHook::ISourceHook *g_SHPtr; \
namespace KHooK { extern KHook::IKHook* __exported__khook; } \
extern ISmmAPI *g_SMAPI; \
extern ISmmPlugin *g_PLAPI; \
extern PluginId g_PLID;
Expand All @@ -513,7 +513,7 @@ using namespace SourceMM;
*/
#define PLUGIN_SAVEVARS() \
g_SMAPI = ismm; \
g_SHPtr = static_cast<SourceHook::ISourceHook *>(ismm->MetaFactory(MMIFACE_SOURCEHOOK, NULL, NULL)); \
KHook::__exported__khook = static_cast<KHook::IKHook*>(ismm->MetaFactory(MMIFACE_KHOOK, nullptr, nullptr)); \
g_PLAPI = static_cast<ISmmPlugin *>(this); \
g_PLID = id;

Expand Down
2 changes: 0 additions & 2 deletions core/ISmmPluginExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ struct MetamodVersionInfo
{
int api_major; /**< ISmmAPI major version */
int api_minor; /**< ISmmAPI minor version */
int sh_iface; /**< SourceHook interface version */
int sh_impl; /**< SourceHook implementation version */
int pl_min; /**< Plugin API minimum version */
int pl_max; /**< Plugin API maximum version */
int source_engine; /**< Source Engine version (SOURCE_* constants) */
Expand Down
14 changes: 5 additions & 9 deletions core/gamedll_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "metamod_util.h"
#include "loader_bridge.h"
#include "provider/provider_base.h"
#include "khook.hpp"

using namespace SourceMM;

Expand Down Expand Up @@ -62,18 +63,13 @@ class GameDllBridge : public IGameDllBridge
}
virtual void DLLInit_Post(int *isgdUnload)
{
SourceHook::MemFuncInfo mfi;

mfi.isVirtual = false;
#ifdef META_IS_SOURCE2
SourceHook::GetFuncInfo(&ISource2ServerConfig::Disconnect, mfi);
auto mfi = KHook::GetVtableIndex(&ISource2ServerConfig::Disconnect);
#else
SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
auto mfi = KHook::GetVtableIndex(&IServerGameDLL::DLLShutdown);
#endif
assert(mfi.isVirtual);
assert(mfi.vtbloffs == 0);
assert(mfi.thisptroffs == 0);
*isgdUnload = mfi.vtblindex;
assert(mfi != -1);
*isgdUnload = mfi;

g_PluginMngr.SetAllLoaded();
}
Expand Down
Loading
Loading