From 9fdbacf4ca03bea7fa60dec2badf213c48974603 Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 19 Jul 2024 13:36:04 +0200 Subject: [PATCH] Added new HOOK_BUILDSFXWEAPON (#560) * Hook for gsnd_build_weapon_sfx_name * Fixed long name ACM crash in AutoSearchSFX * Updated documents --- artifacts/scripting/headers/sfall.h | 2 + artifacts/scripting/hooks.yml | 14 +++++ artifacts/scripting/hookscripts.md | 15 ++++++ sfall/Modules/HookScripts.cpp | 15 ++++++ sfall/Modules/HookScripts.h | 2 + sfall/Modules/HookScripts/SoundHs.cpp | 75 +++++++++++++++++++++++++++ sfall/Modules/HookScripts/SoundHs.h | 10 ++++ sfall/Modules/Sound.cpp | 1 + sfall/ddraw.vcxproj | 2 + sfall/ddraw.vcxproj.filters | 2 + 10 files changed, 138 insertions(+) create mode 100644 sfall/Modules/HookScripts/SoundHs.cpp create mode 100644 sfall/Modules/HookScripts/SoundHs.h diff --git a/artifacts/scripting/headers/sfall.h b/artifacts/scripting/headers/sfall.h index 42076785d..eacee9970 100644 --- a/artifacts/scripting/headers/sfall.h +++ b/artifacts/scripting/headers/sfall.h @@ -76,6 +76,8 @@ #define HOOK_ROLLCHECK (46) #define HOOK_BESTWEAPON (47) #define HOOK_CANUSEWEAPON (48) +// RESERVED 49..60 +#define HOOK_BUILDSFXWEAPON (61) // Valid arguments to list_begin #define LIST_CRITTERS (0) diff --git a/artifacts/scripting/hooks.yml b/artifacts/scripting/hooks.yml index 787309ce6..2efdfb862 100644 --- a/artifacts/scripting/hooks.yml +++ b/artifacts/scripting/hooks.yml @@ -818,4 +818,18 @@ int arg3 - original result of engine function: 1 - can use, 0 - cannot use int ret0 - overrides the result of engine function. Any non-zero value allows using the weapon + + +- name: BuildSfxWeapon + id: HOOK_BUILDSFXWEAPON + doc: | + Runs before each weapon sound effect is played or put in the animation queue to determine the name of sound effect file based on the weapon, target and action being performed. + + ``` + int arg0 - weapon sound effect type: 0 - ready/reload, 1 - attack, 2 - out of ammo, 3 - flying (for projectile weapons), 4 - hit + Item arg1 - the weapon being used + int arg2 - attack type (see ATKTYPE_* constants) + Obj arg3 - target of the attack (can be 0) + + String ret0 - the new sound file name to use, without extension (relative to "sound\sfx" path) ``` diff --git a/artifacts/scripting/hookscripts.md b/artifacts/scripting/hookscripts.md index d0656d27c..bfd0355cd 100644 --- a/artifacts/scripting/hookscripts.md +++ b/artifacts/scripting/hookscripts.md @@ -936,3 +936,18 @@ int arg3 - original result of engine function: 1 - can use, 0 - cannot use int ret0 - overrides the result of engine function. Any non-zero value allows using the weapon ``` + +------------------------------------------- + +#### `HOOK_BUILDSFXWEAPON (hs_buildsfxweapon.int)` + +Runs before each weapon sound effect is played or put in the animation queue to determine the name of sound effect file based on the weapon, target and action being performed. + +``` +int arg0 - weapon sound effect type: 0 - ready/reload, 1 - attack, 2 - out of ammo, 3 - flying (for projectile weapons), 4 - hit +Item arg1 - the weapon being used +int arg2 - attack type (see ATKTYPE_* constants) +Obj arg3 - target of the attack (can be 0) + +String ret0 - the new sound file name to use, without extension (relative to "sound\sfx" path) +``` diff --git a/sfall/Modules/HookScripts.cpp b/sfall/Modules/HookScripts.cpp index 9ed79a47e..2dd422c04 100644 --- a/sfall/Modules/HookScripts.cpp +++ b/sfall/Modules/HookScripts.cpp @@ -31,6 +31,7 @@ #include "HookScripts\InventoryHs.h" #include "HookScripts\ObjectHs.h" #include "HookScripts\MiscHs.h" +#include "HookScripts\SoundHs.h" #include "HookScripts.h" @@ -110,6 +111,19 @@ static HooksInjectInfo injectHooks[] = { {HOOK_ROLLCHECK, Inject_RollCheckHook, 0}, {HOOK_BESTWEAPON, Inject_BestWeaponHook, 0}, {HOOK_CANUSEWEAPON, Inject_CanUseWeaponHook, 0}, + {-1}, // RESERVED + {-1}, // RESERVED + {-1}, // HOOK_MOUSEWHEEL + {-1}, // RESERVED + {-1}, // RESERVED + {-1}, // RESERVED + {-1}, // HOOK_COMBATATTACK + {-1}, // RESERVED + {-1}, // RESERVED + {-1}, // RESERVED + {-1}, // RESERVED + {-1}, // RESERVED + {HOOK_BUILDSFXWEAPON, Inject_BuildSfxWeaponHook, 0}, }; void HookScripts::InjectingHook(int hookId) { @@ -216,6 +230,7 @@ void HookScripts::LoadHookScripts() { InitInventoryHookScripts(); InitObjectHookScripts(); InitMiscHookScripts(); + InitSoundHookScripts(); HookScripts::LoadHookScript("hs_keypress", HOOK_KEYPRESS); HookScripts::LoadHookScript("hs_mouseclick", HOOK_MOUSECLICK); diff --git a/sfall/Modules/HookScripts.h b/sfall/Modules/HookScripts.h index a8c5b755e..d184fe042 100644 --- a/sfall/Modules/HookScripts.h +++ b/sfall/Modules/HookScripts.h @@ -75,6 +75,8 @@ enum HookType HOOK_ROLLCHECK = 46, HOOK_BESTWEAPON = 47, HOOK_CANUSEWEAPON = 48, + // RESERVED 49 to 60 + HOOK_BUILDSFXWEAPON = 61, HOOK_COUNT }; diff --git a/sfall/Modules/HookScripts/SoundHs.cpp b/sfall/Modules/HookScripts/SoundHs.cpp new file mode 100644 index 000000000..094d70aa6 --- /dev/null +++ b/sfall/Modules/HookScripts/SoundHs.cpp @@ -0,0 +1,75 @@ +#include "..\..\FalloutEngine\Fallout2.h" +#include "..\..\SafeWrite.h" +#include "..\HookScripts.h" +#include "Common.h" + +#include "SoundHs.h" + +using namespace sfall::script; + +// Object hook scripts +namespace sfall +{ + +static DWORD __fastcall BuildSfxNameHook_Script(long effectType, fo::GameObject* weapon, long hitMode, fo::GameObject* target) { + BeginHook(); + allowNonIntReturn = true; + argCount = 4; + + args[0] = effectType; + args[1] = (DWORD)weapon; + args[2] = (DWORD)hitMode; + args[3] = (DWORD)target; + + RunHookScript(HOOK_BUILDSFXWEAPON); + + DWORD textPtr = cRet > 0 && retTypes[0] == DataType::STR + ? rets[0] + : 0; + EndHook(); + + return textPtr; // -1 - default handler +} + +static __declspec(naked) void gsnd_build_weapon_sfx_name_hook() { + __asm { + pushadc; // save state + push ebx; + push ecx; // target + push ebx; // hitMode + // edx - weapon + mov ecx, eax; // effectType + call BuildSfxNameHook_Script; + test eax, eax; // pointer to text + pop ebx; // restore state + pop ecx; + pop edx; + jnz skip; + pop eax; + jmp fo::funcoffs::gsnd_build_weapon_sfx_name_; +skip: + add esp, 4; + retn; + } +} + + +void Inject_BuildSfxWeaponHook() { + HookCalls(gsnd_build_weapon_sfx_name_hook, { + 0x410DB3, // show_damage_to_object + 0x411397, 0x411538, // action_melee + 0x411787, 0x41196C, 0x411A96, 0x411B82, // action_ranged + 0x4268F5, // combat_attack_this + 0x42A9B4, 0x42AA92, 0x42AAF0, // ai_try_attack + 0x42AF4C, // cai_attempt_w_reload + 0x45BD31, // op_sfx_build_weapon_name + 0x460B87, // intface_item_reload + 0x476629, // drop_ammo_into_weapon + }); +} + +void InitSoundHookScripts() { + HookScripts::LoadHookScript("hs_buildsfxweapon", HOOK_BUILDSFXWEAPON); +} + +} diff --git a/sfall/Modules/HookScripts/SoundHs.h b/sfall/Modules/HookScripts/SoundHs.h new file mode 100644 index 000000000..27aa0b7ff --- /dev/null +++ b/sfall/Modules/HookScripts/SoundHs.h @@ -0,0 +1,10 @@ +#pragma once + +namespace sfall +{ + +void InitSoundHookScripts(); + +void Inject_BuildSfxWeaponHook(); + +} diff --git a/sfall/Modules/Sound.cpp b/sfall/Modules/Sound.cpp index 040364c1d..16f549e08 100644 --- a/sfall/Modules/Sound.cpp +++ b/sfall/Modules/Sound.cpp @@ -1061,6 +1061,7 @@ void Sound::init() { if (IniReader::GetConfigInt("Sound", "AutoSearchSFX", 1)) { HookCalls(sfxl_init_hook, {0x4A9999, 0x4A9B34}); SafeWrite8(0x4A9B3F, 0xA9); // jz 0x4A9BEC (skip error message) + SafeWrite8(0x4AA060, 127); // fix crash when ACM file has name longer than 12 symbols } if (IniReader::GetConfigString("Sound", "MainMenuMusic", "", mainMenuMusic, 9)) { diff --git a/sfall/ddraw.vcxproj b/sfall/ddraw.vcxproj index 8b4805927..f8bcdba09 100644 --- a/sfall/ddraw.vcxproj +++ b/sfall/ddraw.vcxproj @@ -402,6 +402,7 @@ + @@ -544,6 +545,7 @@ + diff --git a/sfall/ddraw.vcxproj.filters b/sfall/ddraw.vcxproj.filters index 527ec8b61..82dc79d00 100644 --- a/sfall/ddraw.vcxproj.filters +++ b/sfall/ddraw.vcxproj.filters @@ -460,6 +460,7 @@ Modules + @@ -840,6 +841,7 @@ Modules +