Skip to content
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
15 changes: 2 additions & 13 deletions .github/actions/build-windows-x86/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,17 @@ outputs:
runs:
using: composite
steps:
- name: Clone MetaHook SDK headers
shell: pwsh
run: |
$metahookPath = Join-Path (Split-Path $env:GITHUB_WORKSPACE -Parent) 'MetaHook'
& git clone --depth 1 --branch main https://github.com/MetaHookSv/MetaHook $metahookPath
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
& git -C $metahookPath rev-parse HEAD
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
# The plugin consumes include/ only: it compiles interface.cpp and links no SDK library.
"METAHOOK_SOURCE_PATH=$metahookPath" >> $env:GITHUB_ENV

- name: Build and install
shell: cmd
run: call scripts\build-SvenSoundcacheControl-x86-Release.bat "-DMETAHOOK_SOURCE_PATH=%METAHOOK_SOURCE_PATH%"
run: call scripts\build-SvenSoundcacheControl-x86-Release.bat "-DMETAHOOK_SOURCE_PATH="

- name: Test behavior
shell: cmd
run: ctest --test-dir build\x86\Release -C Release --output-on-failure

- name: Validate installed gamedata
shell: cmd
run: python "%METAHOOK_SOURCE_PATH%\scripts\validate-gamedata.py" install\x86\Release\svencoop\metahook\gamedata\svensoundcachecontrol --manifest scripts\manifests\svensoundcachecontrol.json
run: python build\x86\Release\_deps\svensoundcachecontrol_metahook-src\scripts\validate-gamedata.py install\x86\Release\svencoop\metahook\gamedata\svensoundcachecontrol --manifest scripts\manifests\svensoundcachecontrol.json

- name: Package plugin
id: package
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ not included; do not load both plugins.
## Behavior and limits

The original sound engine clears its in-memory indices only when the map name
changes. This plugin calls its native `ClearSoundArrays` on every map start and
invalidates the native disk sound-list cache, then uses the original loader.
changes. This plugin marks each join at `HUD_VidInit`, then calls native
`ClearSoundArrays` and invalidates the native disk sound-list cache before the
first sound-list load or map-start callback. A later `MapStarted` callback keeps
sounds already loaded for that join. Loading still uses the original loader.
It does not delete audio assets or implement a replacement sound engine.

A remote join can require one automatic reconnect while the original `dlfile`
Expand All @@ -42,13 +44,16 @@ remove the file the same server must serve to other players.
Windows MSVC x86 only, MetaHook API 109 or newer, Sven client builds 8948/10257.
Other clients and missing gamedata produce a clear startup error. Catalog
coverage alone does not imply that every build has been tested in a live game.
Connection tracking uses the MetaHook SDK's `net_api_svengine.h` types: Sven's
network status ABI is 104 bytes, rather than the original HLSDK's 72 bytes.

## Build and test

Requires Visual Studio 2022, CMake 3.21+, Python 3.9+, and network access to the
symbol catalog. The MetaHook source tree supplies SDK headers, `interface.cpp`
and the shared gamedata synchronization/validation scripts. It is fetched from
`main` unless `METAHOOK_SOURCE_PATH` is set; no SDK library is linked.
and the shared gamedata synchronization/validation scripts. A pinned SDK commit
including `net_api_svengine.h` is fetched unless `METAHOOK_SOURCE_PATH` is set;
no SDK library is linked.

```powershell
cmake -S . -B build/x86/Release -G "Visual Studio 17 2022" -A Win32 -DMETAHOOK_SOURCE_PATH=../../MetaHook -DCMAKE_INSTALL_PREFIX=install/x86/Release
Expand All @@ -70,4 +75,6 @@ Optional Visual Studio F5 deployment uses `METAHOOKSV_ENABLE_LAUNCH_GAME=ON` and
the shared LaunchGame module; it requires an existing MetaHook installation with
this plugin already enabled. Normal builds do not deploy to a game.

Issue: https://github.com/MetaHookSv/MetaHookSv/issues/974
Issues:
- https://github.com/MetaHookSv/MetaHookSv/issues/974
- https://github.com/MetaHookSv/MetaHookSv/issues/975
9 changes: 5 additions & 4 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
set(METAHOOK_SOURCE_PATH "$ENV{METAHOOK_SOURCE_PATH}" CACHE PATH "MetaHook source tree; empty fetches main")
set(METAHOOK_SOURCE_PATH "$ENV{METAHOOK_SOURCE_PATH}" CACHE PATH "MetaHook source tree; empty fetches the pinned SDK")

# The plugin compiles part of the SDK and includes its public headers, so an
# external tree must provide both the sources and the interfaces it uses.
function(svensoundcachecontrol_validate_metahook_source source)
foreach(required include/metahook.h include/HLSDK/common/interface.cpp
include/HLSDK/common/cvardef.h include/Interface/IPlugins.h
include/HLSDK/common/cvardef.h include/HLSDK/common/net_api_svengine.h
include/Interface/IPlugins.h
scripts/sync-gamedata.py scripts/validate-gamedata.py)
if(NOT EXISTS "${source}/${required}" OR IS_DIRECTORY "${source}/${required}")
message(FATAL_ERROR "METAHOOK_SOURCE_PATH is missing ${required}: ${source}")
Expand All @@ -20,8 +21,8 @@ function(svensoundcachecontrol_prepare_dependencies)
include(FetchContent)
FetchContent_Declare(svensoundcachecontrol_metahook
GIT_REPOSITORY https://github.com/MetaHookSv/MetaHook
# MetaHook is tracked as a branch: always fetch the latest main.
GIT_TAG origin/main
# Includes the SvEngine network ABI types required by this plugin.
GIT_TAG c7c2af7d9dbfee94ae3fb4514e97efb856835ec7
GIT_SUBMODULES ""
# This SDK directory has no CMakeLists.txt: populate without building the launcher.
SOURCE_SUBDIR include
Expand Down
39 changes: 30 additions & 9 deletions src/privatehook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "soundcache_control.h"
#include <cvardef.h>
#include <demo_api.h>
#include <net_api.h>
#include <net_api_svengine.h>
#include <initializer_list>

static_assert(METAHOOK_API_VERSION >= svensoundcachecontrol::RequiredMetaHookAPIVersion,
Expand Down Expand Up @@ -38,10 +38,12 @@ ClientCmdFn originalClientCmd = nullptr;
// The engine keeps calling the wrapped exports after hooks are removed, so the
// originals stay valid until the client is loaded again.
HUD_INIT_FUNC originalHudInit = nullptr;
HUD_VIDINIT_FUNC originalHudVidInit = nullptr;
HUD_SHUTDOWN_FUNC originalHudShutdown = nullptr;

cvar_t* alwaysFlush = nullptr;
bool loadingSoundList = false;
cvar_t* alwaysFlush = nullptr;
bool loadingSoundList = false;
bool mapRefreshPending = true;
SoundcacheControl control;

void ReportFailure(const char* symbol, const char* reason)
Expand Down Expand Up @@ -85,18 +87,22 @@ bool IsEnabled()
// Identify the join so a recovery reconnect can be told apart from a new one.
void BeginMap()
{
net_status_t status = {};
const auto net = gEngfuncs.pNetAPI;
net_status_svengine_t status = {};
const auto net = gEngfuncs.pNetAPI;
if (net && net->Status)
net->Status(&status);
const char* server = net && net->AdrToString ? net->AdrToString(&status.remote_address) : nullptr;
reinterpret_cast<net_api_status_svengine_func_t>(net->Status)(&status);
const char* server = net && net->AdrToString ? reinterpret_cast<net_api_adrtostring_svengine_func_t>(net->AdrToString)(&status.remote_address) : nullptr;
const char* map = gEngfuncs.pfnGetLevelName();
control.BeginMap(map ? map : "", server ? server : "", status.remote_address.type == NA_LOOPBACK);
}

// The original engine clears its in-memory indices only when the map name changes.
void __fastcall MapStartedHook(void* self, int edx)
// StartSound can load the list before InitHUD calls MapStarted. Refresh once at
// whichever callback arrives first, so the later one preserves freshly loaded sounds.
void PrepareMap(void* self, int edx)
{
if (!mapRefreshPending)
return;
mapRefreshPending = false;
BeginMap();
if (IsEnabled())
{
Expand All @@ -106,6 +112,11 @@ void __fastcall MapStartedHook(void* self, int edx)
originalFlushCache(self, edx, true);
clearSoundArrays(self, edx);
}
}

void __fastcall MapStartedHook(void* self, int edx)
{
PrepareMap(self, edx);
originalMapStarted(self, edx);
}

Expand Down Expand Up @@ -142,6 +153,7 @@ bool __fastcall LoadSoundListHook(void* self, int edx)
{
if (loadingSoundList)
return originalLoadSoundList(self, edx);
PrepareMap(self, edx);
// Only observe commands generated by this load, keeping the client's
// existing command chain and the engine's asynchronous download behavior.
loadingSoundList = true;
Expand Down Expand Up @@ -169,6 +181,12 @@ void HudShutdownHook()
originalHudShutdown();
}

int HudVidInitHook()
{
mapRefreshPending = true;
return originalHudVidInit();
}

template <typename Fn>
bool Hook(const char* symbol, void* address, Fn replacement, Fn* original, hook_t** handle)
{
Expand Down Expand Up @@ -209,8 +227,10 @@ bool InstallHooks(cl_exportfuncs_t* exports)
!Hook(LoadSoundListSymbol, loadSoundListAddress, LoadSoundListHook, &originalLoadSoundList, &loadSoundListHook))
return false;
originalHudInit = exports->HUD_Init;
originalHudVidInit = exports->HUD_VidInit;
originalHudShutdown = exports->HUD_Shutdown;
exports->HUD_Init = HudInitHook;
exports->HUD_VidInit = HudVidInitHook;
exports->HUD_Shutdown = HudShutdownHook;
return true;
}
Expand All @@ -231,6 +251,7 @@ void UninstallHooks()
clearSoundArrays = nullptr;
clientEngfuncs = nullptr;
alwaysFlush = nullptr;
mapRefreshPending = true;
control.Reset();
}
} // namespace svensoundcachecontrol
2 changes: 1 addition & 1 deletion src/privatehook.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using ClearSoundArraysFn = void(__fastcall*)(void* self, int edx);
using FlushCacheFn = void(__fastcall*)(void* self, int edx, bool includingLocal);
using LoadSoundListFn = bool(__fastcall*)(void* self, int edx);

// Hooks the client sound engine and wraps HUD_Init/HUD_Shutdown in `exports`.
// Hooks the client sound engine and wraps HUD_Init/HUD_VidInit/HUD_Shutdown.
bool InstallHooks(cl_exportfuncs_t* exports);
void UninstallHooks();
} // namespace svensoundcachecontrol
Loading
Loading