From 2c5e044296bf6f3d1534ab374d6f4a80975473a7 Mon Sep 17 00:00:00 2001 From: NovaRain Date: Fri, 19 Jul 2024 19:46:12 +0800 Subject: [PATCH] Prevent RegisterHook() from registering placeholder hooks Minor edits to code and documents. --- artifacts/scripting/hooks.yml | 15 +++++++++----- artifacts/scripting/hookscripts.md | 13 +++++++++---- sfall/Modules/HookScripts.cpp | 6 +++--- sfall/Modules/HookScripts/SoundHs.cpp | 28 +++++++++++++-------------- sfall/Modules/Sound.cpp | 2 +- sfall/ddraw.vcxproj.filters | 8 ++++++-- 6 files changed, 42 insertions(+), 30 deletions(-) diff --git a/artifacts/scripting/hooks.yml b/artifacts/scripting/hooks.yml index 2efdfb86..f5e86876 100644 --- a/artifacts/scripting/hooks.yml +++ b/artifacts/scripting/hooks.yml @@ -818,18 +818,23 @@ 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. + Runs before each weapon sound effect is played or added to the animation queue to determine the name of the 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 + int arg0 - weapon sound effect type: + 0 - ready/reload + 1 - attack + 2 - out of ammo + 3 - flying (for projectiles from 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) + Obj arg3 - the target of the attack (can be 0) - String ret0 - the new sound file name to use, without extension (relative to "sound\sfx" path) + String ret0 - the filename of the new sound effect to use, without extension (relative to the sound\sfx\ directory) ``` diff --git a/artifacts/scripting/hookscripts.md b/artifacts/scripting/hookscripts.md index bfd0355c..0006562a 100644 --- a/artifacts/scripting/hookscripts.md +++ b/artifacts/scripting/hookscripts.md @@ -941,13 +941,18 @@ int ret0 - overrides the result of engine function. Any non-zero value allow #### `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. +Runs before each weapon sound effect is played or added to the animation queue to determine the name of the 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 +int arg0 - weapon sound effect type: + 0 - ready/reload + 1 - attack + 2 - out of ammo + 3 - flying (for projectiles from 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) +Obj arg3 - the target of the attack (can be 0) -String ret0 - the new sound file name to use, without extension (relative to "sound\sfx" path) +String ret0 - the filename of the new sound effect to use, without extension (relative to the sound\sfx\ directory) ``` diff --git a/sfall/Modules/HookScripts.cpp b/sfall/Modules/HookScripts.cpp index 2dd422c0..32da3df8 100644 --- a/sfall/Modules/HookScripts.cpp +++ b/sfall/Modules/HookScripts.cpp @@ -107,7 +107,7 @@ static HooksInjectInfo injectHooks[] = { {HOOK_TARGETOBJECT, Inject_TargetObjectHook, 0}, {HOOK_ENCOUNTER, Inject_EncounterHook, 0}, {HOOK_ADJUSTPOISON, Inject_AdjustPoisonHook, 0}, - {HOOK_ADJUSTRADS, Inject_AdjustRadsHook, 1}, // always embedded for party control fix + {HOOK_ADJUSTRADS, Inject_AdjustRadsHook, 1}, // always embedded for party control fix {HOOK_ROLLCHECK, Inject_RollCheckHook, 0}, {HOOK_BESTWEAPON, Inject_BestWeaponHook, 0}, {HOOK_CANUSEWEAPON, Inject_CanUseWeaponHook, 0}, @@ -123,7 +123,7 @@ static HooksInjectInfo injectHooks[] = { {-1}, // RESERVED {-1}, // RESERVED {-1}, // RESERVED - {HOOK_BUILDSFXWEAPON, Inject_BuildSfxWeaponHook, 0}, + {HOOK_BUILDSFXWEAPON, Inject_BuildSfxWeaponHook, 0}, }; void HookScripts::InjectingHook(int hookId) { @@ -143,7 +143,7 @@ bool HookScripts::HookHasScript(int hookId) { } void HookScripts::RegisterHook(fo::Program* script, int id, int procNum, bool specReg) { - if (id >= numHooks) return; + if (id >= numHooks || injectHooks[id].id < 0) return; for (std::vector::iterator it = hooks[id].begin(); it != hooks[id].end(); ++it) { if (it->prog.ptr == script) { if (procNum == 0) hooks[id].erase(it); // unregister diff --git a/sfall/Modules/HookScripts/SoundHs.cpp b/sfall/Modules/HookScripts/SoundHs.cpp index 094d70aa..0c8d1873 100644 --- a/sfall/Modules/HookScripts/SoundHs.cpp +++ b/sfall/Modules/HookScripts/SoundHs.cpp @@ -7,11 +7,10 @@ using namespace sfall::script; -// Object hook scripts namespace sfall { -static DWORD __fastcall BuildSfxNameHook_Script(long effectType, fo::GameObject* weapon, long hitMode, fo::GameObject* target) { +static DWORD __fastcall BuildSfxWeaponHook_Script(long effectType, fo::GameObject* weapon, long hitMode, fo::GameObject* target) { BeginHook(); allowNonIntReturn = true; argCount = 4; @@ -24,8 +23,8 @@ static DWORD __fastcall BuildSfxNameHook_Script(long effectType, fo::GameObject* RunHookScript(HOOK_BUILDSFXWEAPON); DWORD textPtr = cRet > 0 && retTypes[0] == DataType::STR - ? rets[0] - : 0; + ? rets[0] + : 0; EndHook(); return textPtr; // -1 - default handler @@ -37,9 +36,8 @@ static __declspec(naked) void gsnd_build_weapon_sfx_name_hook() { push ebx; push ecx; // target push ebx; // hitMode - // edx - weapon mov ecx, eax; // effectType - call BuildSfxNameHook_Script; + call BuildSfxWeaponHook_Script; // edx - weapon test eax, eax; // pointer to text pop ebx; // restore state pop ecx; @@ -56,15 +54,15 @@ skip: 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 + 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_ }); } diff --git a/sfall/Modules/Sound.cpp b/sfall/Modules/Sound.cpp index 16f549e0..a36bb5a7 100644 --- a/sfall/Modules/Sound.cpp +++ b/sfall/Modules/Sound.cpp @@ -1061,7 +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 + SafeWrite8(0x4AA060, 127); // fix crash when ACM file has name longer than 12 characters } if (IniReader::GetConfigString("Sound", "MainMenuMusic", "", mainMenuMusic, 9)) { diff --git a/sfall/ddraw.vcxproj.filters b/sfall/ddraw.vcxproj.filters index 82dc79d0..0c64fa47 100644 --- a/sfall/ddraw.vcxproj.filters +++ b/sfall/ddraw.vcxproj.filters @@ -460,7 +460,9 @@ Modules - + + Modules\HookScripts + @@ -841,7 +843,9 @@ Modules - + + Modules\HookScripts +