Reorganized hooks related code

This commit is contained in:
NovaRain
2020-08-21 12:55:18 +08:00
parent aa132d9951
commit d46bb42efd
13 changed files with 83 additions and 92 deletions
+2
View File
@@ -27,6 +27,8 @@
#include "SubModules\CombatBlock.h" #include "SubModules\CombatBlock.h"
#include "HookScripts\CombatHS.h"
#include "Combat.h" #include "Combat.h"
namespace sfall namespace sfall
+22 -6
View File
@@ -37,8 +37,11 @@
namespace sfall namespace sfall
{ {
// Number of types of hooks
static constexpr int numHooks = HOOK_COUNT;
bool HookScripts::injectAllHooks; bool HookScripts::injectAllHooks;
DWORD initingHookScripts; DWORD HookScripts::initingHookScripts;
std::vector<HookFile> HookScripts::hookScriptFilesList; std::vector<HookFile> HookScripts::hookScriptFilesList;
@@ -53,7 +56,7 @@ static struct HooksPositionInfo {
long hsPosition = 0; // index of the hs_* script, or the beginning of the position for registering scripts using register_hook long hsPosition = 0; // index of the hs_* script, or the beginning of the position for registering scripts using register_hook
// long positionShift = 0; // offset to the last script registered by register_hook // long positionShift = 0; // offset to the last script registered by register_hook
bool hasHsScript = false; bool hasHsScript = false;
} hooksInfo[HOOK_COUNT]; } hooksInfo[numHooks];
static HooksInjectInfo injectHooks[] = { static HooksInjectInfo injectHooks[] = {
{HOOK_TOHIT, Inject_ToHitHook, false}, {HOOK_TOHIT, Inject_ToHitHook, false},
@@ -163,6 +166,19 @@ void HookScripts::RunHookScriptsAtProc(DWORD procId) {
} }
} }
void HookScripts::LoadHookScript(const char* name, int id) {
//if (id >= numHooks || IsGameScript(name)) return;
bool hookIsLoaded = HookScripts::LoadHookScriptFile(name, id);
if (hookIsLoaded || (HookScripts::injectAllHooks && id != HOOK_SUBCOMBATDAMAGE)) {
HookScripts::InjectingHook(id); // inject hook to engine code
if (!hookIsLoaded) return;
HookFile hookFile = { id, name };
HookScripts::hookScriptFilesList.push_back(hookFile);
}
}
bool HookScripts::LoadHookScriptFile(const char* name, int id) { bool HookScripts::LoadHookScriptFile(const char* name, int id) {
char filename[MAX_PATH]; char filename[MAX_PATH];
sprintf(filename, "scripts\\%s.int", name); sprintf(filename, "scripts\\%s.int", name);
@@ -201,9 +217,9 @@ static void HookScriptInit() {
InitObjectHookScripts(); InitObjectHookScripts();
InitMiscHookScripts(); InitMiscHookScripts();
LoadHookScript("hs_keypress", HOOK_KEYPRESS); HookScripts::LoadHookScript("hs_keypress", HOOK_KEYPRESS);
LoadHookScript("hs_mouseclick", HOOK_MOUSECLICK); HookScripts::LoadHookScript("hs_mouseclick", HOOK_MOUSECLICK);
LoadHookScript("hs_gamemodechange", HOOK_GAMEMODECHANGE); HookScripts::LoadHookScript("hs_gamemodechange", HOOK_GAMEMODECHANGE);
hooksFilesLoaded = !alwaysFindScripts; hooksFilesLoaded = !alwaysFindScripts;
} else { } else {
@@ -232,7 +248,7 @@ void HookScripts::HookScriptClear() {
for(int i = 0; i < numHooks; i++) { for(int i = 0; i < numHooks; i++) {
hooks[i].clear(); hooks[i].clear();
} }
std::memset(hooksInfo, 0, HOOK_COUNT * sizeof(HooksPositionInfo)); std::memset(hooksInfo, 0, numHooks * sizeof(HooksPositionInfo));
HookCommon::Reset(); HookCommon::Reset();
} }
+3 -4
View File
@@ -85,8 +85,11 @@ public:
const char* name() { return "HookScripts"; } const char* name() { return "HookScripts"; }
void init(); void init();
static DWORD initingHookScripts;
static std::vector<HookFile> hookScriptFilesList; static std::vector<HookFile> hookScriptFilesList;
static void LoadHookScript(const char* name, int id);
static bool LoadHookScriptFile(const char* name, int id); static bool LoadHookScriptFile(const char* name, int id);
static void LoadHookScripts(); static void LoadHookScripts();
static void HookScriptClear(); static void HookScriptClear();
@@ -103,8 +106,4 @@ public:
static void RunHookScriptsAtProc(DWORD procId); static void RunHookScriptsAtProc(DWORD procId);
}; };
extern DWORD initingHookScripts;
extern int __fastcall AmmoCostHook_Script(DWORD hookType, fo::GameObject* weapon, DWORD &rounds);
} }
+11 -11
View File
@@ -652,17 +652,17 @@ void Inject_TargetObjectHook() {
} }
void InitCombatHookScripts() { void InitCombatHookScripts() {
LoadHookScript("hs_tohit", HOOK_TOHIT); HookScripts::LoadHookScript("hs_tohit", HOOK_TOHIT);
LoadHookScript("hs_afterhitroll", HOOK_AFTERHITROLL); HookScripts::LoadHookScript("hs_afterhitroll", HOOK_AFTERHITROLL);
LoadHookScript("hs_calcapcost", HOOK_CALCAPCOST); HookScripts::LoadHookScript("hs_calcapcost", HOOK_CALCAPCOST);
LoadHookScript("hs_combatdamage", HOOK_COMBATDAMAGE); HookScripts::LoadHookScript("hs_combatdamage", HOOK_COMBATDAMAGE);
LoadHookScript("hs_findtarget", HOOK_FINDTARGET); HookScripts::LoadHookScript("hs_findtarget", HOOK_FINDTARGET);
LoadHookScript("hs_itemdamage", HOOK_ITEMDAMAGE); HookScripts::LoadHookScript("hs_itemdamage", HOOK_ITEMDAMAGE);
LoadHookScript("hs_ammocost", HOOK_AMMOCOST); HookScripts::LoadHookScript("hs_ammocost", HOOK_AMMOCOST);
LoadHookScript("hs_combatturn", HOOK_COMBATTURN); HookScripts::LoadHookScript("hs_combatturn", HOOK_COMBATTURN);
LoadHookScript("hs_onexplosion", HOOK_ONEXPLOSION); HookScripts::LoadHookScript("hs_onexplosion", HOOK_ONEXPLOSION);
LoadHookScript("hs_subcombatdmg", HOOK_SUBCOMBATDAMAGE); HookScripts::LoadHookScript("hs_subcombatdmg", HOOK_SUBCOMBATDAMAGE);
LoadHookScript("hs_targetobject", HOOK_TARGETOBJECT); HookScripts::LoadHookScript("hs_targetobject", HOOK_TARGETOBJECT);
} }
} }
+2
View File
@@ -19,6 +19,8 @@ void Inject_OnExplosionHook();
void Inject_SubCombatDamageHook(); void Inject_SubCombatDamageHook();
void Inject_TargetObjectHook(); void Inject_TargetObjectHook();
int __fastcall AmmoCostHook_Script(DWORD hookType, fo::GameObject* weapon, DWORD &rounds);
// Implementation of item_w_mp_cost_ engine function with the hook // Implementation of item_w_mp_cost_ engine function with the hook
long __fastcall sf_item_w_mp_cost(fo::GameObject* source, long hitMode, long isCalled); long __fastcall sf_item_w_mp_cost(fo::GameObject* source, long hitMode, long isCalled);
+5 -17
View File
@@ -6,8 +6,9 @@
namespace sfall namespace sfall
{ {
constexpr int maxArgs = 16; constexpr int maxArgs = 16; // Maximum number of hook arguments
constexpr int maxDepth = 8; constexpr int maxRets = 8; // Maximum number of return values
constexpr int maxDepth = 8; // Maximum recursion depth for hook calls
struct { struct {
DWORD hookID; DWORD hookID;
@@ -30,20 +31,7 @@ DWORD cArg; // how many arguments were taken by current hook script
DWORD cRet; // how many return values were set by current hook script DWORD cRet; // how many return values were set by current hook script
DWORD cRetTmp; // how many return values were set by specific hook script (when using register_hook) DWORD cRetTmp; // how many return values were set by specific hook script (when using register_hook)
std::vector<HookScript> hooks[numHooks]; std::vector<HookScript> hooks[HOOK_COUNT];
void LoadHookScript(const char* name, int id) {
//if (id >= numHooks || IsGameScript(name)) return;
bool hookIsLoaded = HookScripts::LoadHookScriptFile(name, id);
if (hookIsLoaded || (HookScripts::injectAllHooks && id != HOOK_SUBCOMBATDAMAGE)) {
HookScripts::InjectingHook(id); // inject hook to engine code
if (!hookIsLoaded) return;
HookFile hookFile = { id, name };
HookScripts::hookScriptFilesList.push_back(hookFile);
}
}
DWORD HookCommon::GetHSArgCount() { DWORD HookCommon::GetHSArgCount() {
return argCount; return argCount;
@@ -124,7 +112,7 @@ void __stdcall RunHookScript(DWORD hook) {
if (callDepth > 1) { if (callDepth > 1) {
if (CheckRecursiveHooks(hook) || callDepth > 8) { if (CheckRecursiveHooks(hook) || callDepth > 8) {
fo::func::debug_printf("\n[SFALL] The hook ID: %d cannot be executed.", hook); fo::func::debug_printf("\n[SFALL] The hook ID: %d cannot be executed.", hook);
dlog_f("The hook %d cannot be executed due to exceeded depth limit or recursive calls\n", DL_MAIN, hook); dlog_f("The hook %d cannot be executed due to exceeding depth limit or disallowed recursive calls\n", DL_MAIN, hook);
return; return;
} }
} }
+7 -17
View File
@@ -1,7 +1,5 @@
#pragma once #pragma once
#include <Windows.h>
#include "..\HookScripts.h" #include "..\HookScripts.h"
#include "..\ScriptExtender.h" #include "..\ScriptExtender.h"
@@ -10,19 +8,6 @@
namespace sfall namespace sfall
{ {
// Number of types of hooks
constexpr int numHooks = HOOK_COUNT;
// Maximum number of return values
const int maxRets = 8;
// Struct for registered hook script
struct HookScript {
ScriptProgram prog;
int callback; // proc number in script's proc table
bool isGlobalScript; // false for hs_* scripts, true for gl* scripts
};
class HookCommon { class HookCommon {
public: public:
static DWORD GetHSArgCount(); static DWORD GetHSArgCount();
@@ -39,6 +24,13 @@ public:
static void Reset(); static void Reset();
}; };
// Struct for registered hook script
struct HookScript {
ScriptProgram prog;
int callback; // proc number in script's proc table
bool isGlobalScript; // false for hs_* scripts, true for gl* scripts
};
// All currently registered hook scripts // All currently registered hook scripts
extern std::vector<HookScript> hooks[]; extern std::vector<HookScript> hooks[];
@@ -50,8 +42,6 @@ extern DWORD cArg; // how many arguments were taken by current hook script
extern DWORD cRet; // how many return values were set by current hook script extern DWORD cRet; // how many return values were set by current hook script
extern DWORD cRetTmp; // how many return values were set by specific hook script (when using register_hook) extern DWORD cRetTmp; // how many return values were set by specific hook script (when using register_hook)
void LoadHookScript(const char* name, int id);
void __stdcall BeginHook(); void __stdcall BeginHook();
void __stdcall RunHookScript(DWORD hook); void __stdcall RunHookScript(DWORD hook);
void __stdcall EndHook(); void __stdcall EndHook();
+3 -5
View File
@@ -157,11 +157,9 @@ void Inject_OnDeathHook() {
} }
void InitDeathHookScripts() { void InitDeathHookScripts() {
HookScripts::LoadHookScript("hs_deathanim1", HOOK_DEATHANIM1);
LoadHookScript("hs_deathanim1", HOOK_DEATHANIM1); HookScripts::LoadHookScript("hs_deathanim2", HOOK_DEATHANIM2);
LoadHookScript("hs_deathanim2", HOOK_DEATHANIM2); HookScripts::LoadHookScript("hs_ondeath", HOOK_ONDEATH);
LoadHookScript("hs_ondeath", HOOK_ONDEATH);
} }
} }
+4 -6
View File
@@ -127,12 +127,10 @@ void Inject_HexMoveBlockHook() {
} }
void InitHexBlockingHookScripts() { void InitHexBlockingHookScripts() {
HookScripts::LoadHookScript("hs_hexmoveblocking", HOOK_HEXMOVEBLOCKING);
LoadHookScript("hs_hexmoveblocking", HOOK_HEXMOVEBLOCKING); HookScripts::LoadHookScript("hs_hexaiblocking", HOOK_HEXAIBLOCKING);
LoadHookScript("hs_hexaiblocking", HOOK_HEXAIBLOCKING); HookScripts::LoadHookScript("hs_hexshootblocking", HOOK_HEXSHOOTBLOCKING);
LoadHookScript("hs_hexshootblocking", HOOK_HEXSHOOTBLOCKING); HookScripts::LoadHookScript("hs_hexsightblocking", HOOK_HEXSIGHTBLOCKING);
LoadHookScript("hs_hexsightblocking", HOOK_HEXSIGHTBLOCKING);
} }
} }
+5 -6
View File
@@ -658,12 +658,11 @@ long CorrectFidForRemovedItem_wHook(fo::GameObject* critter, fo::GameObject* ite
} }
void InitInventoryHookScripts() { void InitInventoryHookScripts() {
HookScripts::LoadHookScript("hs_removeinvenobj", HOOK_REMOVEINVENOBJ);
LoadHookScript("hs_removeinvenobj", HOOK_REMOVEINVENOBJ); HookScripts::LoadHookScript("hs_movecost", HOOK_MOVECOST);
LoadHookScript("hs_movecost", HOOK_MOVECOST); HookScripts::LoadHookScript("hs_inventorymove", HOOK_INVENTORYMOVE);
LoadHookScript("hs_inventorymove", HOOK_INVENTORYMOVE); HookScripts::LoadHookScript("hs_invenwield", HOOK_INVENWIELD);
LoadHookScript("hs_invenwield", HOOK_INVENWIELD); HookScripts::LoadHookScript("hs_adjustfid", HOOK_ADJUSTFID);
LoadHookScript("hs_adjustfid", HOOK_ADJUSTFID);
Inventory::OnAdjustFid() += AdjustFidHook; // should be registered last Inventory::OnAdjustFid() += AdjustFidHook; // should be registered last
} }
+11 -11
View File
@@ -707,17 +707,17 @@ void Inject_EncounterHook() {
} }
void InitMiscHookScripts() { void InitMiscHookScripts() {
LoadHookScript("hs_barterprice", HOOK_BARTERPRICE); HookScripts::LoadHookScript("hs_barterprice", HOOK_BARTERPRICE);
LoadHookScript("hs_useskillon", HOOK_USESKILLON); HookScripts::LoadHookScript("hs_useskillon", HOOK_USESKILLON);
LoadHookScript("hs_useskill", HOOK_USESKILL); HookScripts::LoadHookScript("hs_useskill", HOOK_USESKILL);
LoadHookScript("hs_steal", HOOK_STEAL); HookScripts::LoadHookScript("hs_steal", HOOK_STEAL);
LoadHookScript("hs_sneak", HOOK_SNEAK); HookScripts::LoadHookScript("hs_sneak", HOOK_SNEAK);
LoadHookScript("hs_withinperception", HOOK_WITHINPERCEPTION); HookScripts::LoadHookScript("hs_withinperception", HOOK_WITHINPERCEPTION);
LoadHookScript("hs_cartravel", HOOK_CARTRAVEL); HookScripts::LoadHookScript("hs_cartravel", HOOK_CARTRAVEL);
LoadHookScript("hs_setglobalvar", HOOK_SETGLOBALVAR); HookScripts::LoadHookScript("hs_setglobalvar", HOOK_SETGLOBALVAR);
LoadHookScript("hs_resttimer", HOOK_RESTTIMER); HookScripts::LoadHookScript("hs_resttimer", HOOK_RESTTIMER);
LoadHookScript("hs_explosivetimer", HOOK_EXPLOSIVETIMER); HookScripts::LoadHookScript("hs_explosivetimer", HOOK_EXPLOSIVETIMER);
LoadHookScript("hs_encounter", HOOK_ENCOUNTER); HookScripts::LoadHookScript("hs_encounter", HOOK_ENCOUNTER);
} }
} }
+7 -8
View File
@@ -343,14 +343,13 @@ void Inject_ScriptProcedureHook2() {
} }
void InitObjectHookScripts() { void InitObjectHookScripts() {
HookScripts::LoadHookScript("hs_useobjon", HOOK_USEOBJON);
LoadHookScript("hs_useobjon", HOOK_USEOBJON); HookScripts::LoadHookScript("hs_useobj", HOOK_USEOBJ);
LoadHookScript("hs_useobj", HOOK_USEOBJ); HookScripts::LoadHookScript("hs_useanimobj", HOOK_USEANIMOBJ);
LoadHookScript("hs_useanimobj", HOOK_USEANIMOBJ); HookScripts::LoadHookScript("hs_descriptionobj", HOOK_DESCRIPTIONOBJ);
LoadHookScript("hs_descriptionobj", HOOK_DESCRIPTIONOBJ); HookScripts::LoadHookScript("hs_setlighting", HOOK_SETLIGHTING);
LoadHookScript("hs_setlighting", HOOK_SETLIGHTING); HookScripts::LoadHookScript("hs_stdprocedure", HOOK_STDPROCEDURE); // combo hook
LoadHookScript("hs_stdprocedure", HOOK_STDPROCEDURE); // combo hook HookScripts::LoadHookScript("hs_stdprocedure", HOOK_STDPROCEDURE_END);
LoadHookScript("hs_stdprocedure", HOOK_STDPROCEDURE_END);
} }
} }
+1 -1
View File
@@ -157,7 +157,7 @@ end:
void __declspec(naked) op_init_hook() { void __declspec(naked) op_init_hook() {
__asm { __asm {
mov edx, initingHookScripts; mov edx, HookScripts::initingHookScripts;
_J_RET_VAL_TYPE(VAR_TYPE_INT); _J_RET_VAL_TYPE(VAR_TYPE_INT);
} }
} }