Skip to content

Commit

Permalink
client: Remove uses of Autohook from audio.cpp (R2Northstar#777)
Browse files Browse the repository at this point in the history
Removes AUTOHOOK macros from `audio.cpp` and replaces them with `HookAttach`.
  • Loading branch information
ASpoonPlaysGames authored and wolf109909 committed Sep 8, 2024
1 parent 9bba550 commit 918ed3b
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions primedev/client/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <sstream>
#include <random>

AUTOHOOK_INIT()

static const char* pszAudioEventName;

ConVar* Cvar_mileslog_enable;
Expand Down Expand Up @@ -364,14 +362,12 @@ bool ShouldPlayAudioEvent(const char* eventName, const std::shared_ptr<EventOver
return true; // good to go
}

// clang-format off
AUTOHOOK(LoadSampleMetadata, mileswin64.dll + 0xF110,
bool, __fastcall, (void* sample, void* audioBuffer, unsigned int audioBufferLength, int audioType))
// clang-format on
static bool(__fastcall* o_pLoadSampleMetadata)(void* sample, void* audioBuffer, unsigned int audioBufferLength, int audioType) = nullptr;
static bool __fastcall h_LoadSampleMetadata(void* sample, void* audioBuffer, unsigned int audioBufferLength, int audioType)
{
// Raw source, used for voice data only
if (audioType == 0)
return LoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);
return o_pLoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);

const char* eventName = pszAudioEventName;

Expand All @@ -398,7 +394,7 @@ bool, __fastcall, (void* sample, void* audioBuffer, unsigned int audioBufferLeng

if (!overrideData)
// not found either
return LoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);
return o_pLoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);
else
{
// cache found pattern to improve performance
Expand All @@ -412,7 +408,7 @@ bool, __fastcall, (void* sample, void* audioBuffer, unsigned int audioBufferLeng
overrideData = iter->second;

if (!ShouldPlayAudioEvent(eventName, overrideData))
return LoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);
return o_pLoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);

void* data = 0;
unsigned int dataLength = 0;
Expand Down Expand Up @@ -454,7 +450,7 @@ bool, __fastcall, (void* sample, void* audioBuffer, unsigned int audioBufferLeng
if (!data)
{
spdlog::warn("Could not fetch override sample data for event {}! Using original data instead.", eventName);
return LoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);
return o_pLoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);
}

audioBuffer = data;
Expand All @@ -465,41 +461,47 @@ bool, __fastcall, (void* sample, void* audioBuffer, unsigned int audioBufferLeng
*(unsigned int*)((uintptr_t)sample + 0xF0) = audioBufferLength;

// 64 - Auto-detect sample type
bool res = LoadSampleMetadata(sample, audioBuffer, audioBufferLength, 64);
bool res = o_pLoadSampleMetadata(sample, audioBuffer, audioBufferLength, 64);
if (!res)
spdlog::error("LoadSampleMetadata failed! The game will crash :(");

return res;
}

// clang-format off
AUTOHOOK(sub_1800294C0, mileswin64.dll + 0x294C0,
void*, __fastcall, (void* a1, void* a2))
// clang-format on
static void*(__fastcall* o_pSub_1800294C0)(void* a1, void* a2) = nullptr;
static void* __fastcall h_Sub_1800294C0(void* a1, void* a2)
{
pszAudioEventName = reinterpret_cast<const char*>((*((__int64*)a2 + 6)));
return sub_1800294C0(a1, a2);
return o_pSub_1800294C0(a1, a2);
}

// clang-format off
AUTOHOOK(MilesLog, client.dll + 0x57DAD0,
void, __fastcall, (int level, const char* string))
// clang-format on
static void(__fastcall* o_pMilesLog)(int level, const char* string) = nullptr;
static void __fastcall h_MilesLog(int level, const char* string)
{
if (!Cvar_mileslog_enable->GetBool())
return;

spdlog::info("[MSS] {} - {}", level, string);
}

ON_DLL_LOAD("mileswin64.dll", MilesWin64_Audio, (CModule module))
{
o_pLoadSampleMetadata = module.Offset(0xF110).RCast<decltype(o_pLoadSampleMetadata)>();
HookAttach(&(PVOID&)o_pLoadSampleMetadata, (PVOID)h_LoadSampleMetadata);

o_pSub_1800294C0 = module.Offset(0x294C0).RCast<decltype(o_pSub_1800294C0)>();
HookAttach(&(PVOID&)o_pSub_1800294C0, (PVOID)h_Sub_1800294C0);
}

ON_DLL_LOAD_RELIESON("engine.dll", MilesLogFuncHooks, ConVar, (CModule module))
{
Cvar_mileslog_enable = new ConVar("mileslog_enable", "0", FCVAR_NONE, "Enables/disables whether the mileslog func should be logged");
}

ON_DLL_LOAD_CLIENT_RELIESON("client.dll", AudioHooks, ConVar, (CModule module))
{
AUTOHOOK_DISPATCH()
o_pMilesLog = module.Offset(0x57DAD0).RCast<decltype(o_pMilesLog)>();
HookAttach(&(PVOID&)o_pMilesLog, (PVOID)h_MilesLog);

Cvar_ns_print_played_sounds = new ConVar("ns_print_played_sounds", "0", FCVAR_NONE, "");
MilesStopAll = module.Offset(0x580850).RCast<MilesStopAll_Type>();
Expand Down

0 comments on commit 918ed3b

Please sign in to comment.