Don't execute hook code for HOOK_ADJUSTFID/KEYPRESS/MOUSECLICK/GAMEMODECHANGE/SETGLOBALVAR when their scripts are not loaded (from Mr.Stalin)

This commit is contained in:
NovaRain
2018-11-20 11:03:09 +08:00
parent d8bdf785d5
commit c220e133a2
6 changed files with 67 additions and 24 deletions
+31 -8
View File
@@ -86,10 +86,16 @@ static HooksInjectInfo injectHooks[] = {
bool HookScripts::injectAllHooks;
DWORD initingHookScripts;
// BEGIN HOOKS
// flags for checking if these hooks have script
bool HookScripts::hookSetGlobalVar = false;
bool HookScripts::hookAdjustFid = false;
static bool hookKeyPress = false;
static bool hookMouseClick = false;
static bool hookGameModeChange = false;
// BEGIN HOOKS
void HookScripts::KeyPressHook(DWORD* dxKey, bool pressed, DWORD vKey) {
if (!IsMapLoaded()) {
if (!hookKeyPress || !IsMapLoaded()) {
return;
}
BeginHook();
@@ -103,7 +109,7 @@ void HookScripts::KeyPressHook(DWORD* dxKey, bool pressed, DWORD vKey) {
}
void _stdcall MouseClickHook(DWORD button, bool pressed) {
if (!IsMapLoaded()) {
if (!hookMouseClick || !IsMapLoaded()) {
return;
}
BeginHook();
@@ -115,13 +121,13 @@ void _stdcall MouseClickHook(DWORD button, bool pressed) {
}
void HookScripts::GameModeChangeHook(DWORD exit) {
if (!hookGameModeChange) return;
BeginHook();
argCount = 1;
args[0] = exit;
RunHookScript(HOOK_GAMEMODECHANGE);
EndHook();
}
// END HOOKS
DWORD _stdcall GetHSArgCount() {
@@ -177,7 +183,24 @@ void _stdcall RegisterHook(fo::Program* script, int id, int procNum) {
hook.callback = procNum;
hook.isGlobalScript = true;
hooks[id].push_back(hook);
HookScripts::InjectingHook(id); // inject hook to engine code
switch (id) {
case HOOK_KEYPRESS:
hookKeyPress = true;
break;
case HOOK_MOUSECLICK:
hookMouseClick = true;
break;
case HOOK_ADJUSTFID:
HookScripts::hookAdjustFid = true;
break;
case HOOK_GAMEMODECHANGE:
hookGameModeChange = true;
break;
case HOOK_SETGLOBALVAR: // this should be placed to last without the break keyword
HookScripts::hookSetGlobalVar = true;
default:
HookScripts::InjectingHook(id); // inject hook to engine code
}
}
}
@@ -191,9 +214,9 @@ static void HookScriptInit() {
InitObjectHookScripts();
InitMiscHookScripts();
LoadHookScript("hs_keypress", HOOK_KEYPRESS);
LoadHookScript("hs_mouseclick", HOOK_MOUSECLICK);
LoadHookScript("hs_gamemodechange", HOOK_GAMEMODECHANGE);
hookKeyPress = LoadHookScript("hs_keypress", HOOK_KEYPRESS);
hookMouseClick = LoadHookScript("hs_mouseclick", HOOK_MOUSECLICK);
hookGameModeChange = LoadHookScript("hs_gamemodechange", HOOK_GAMEMODECHANGE);
dlogr("Finished loading hook scripts", DL_HOOK|DL_INIT);
}
+4 -1
View File
@@ -72,9 +72,12 @@ public:
const char* name() { return "HookScripts"; }
void init();
static bool hookSetGlobalVar;
static bool hookAdjustFid;
static bool injectAllHooks;
static void InjectingHook(int hookId);
static bool IsInjectHook(int hookId);
static bool injectAllHooks;
static void GameModeChangeHook(DWORD exit);
static void KeyPressHook(DWORD* dxKey, bool pressed, DWORD vKey);
+11 -3
View File
@@ -24,8 +24,8 @@ DWORD cRetTmp; // how many return values were set by specific hook script (when
std::vector<HookScript> hooks[numHooks];
void LoadHookScript(const char* name, int id) {
if (id >= numHooks || IsGameScript(name)) return;
bool LoadHookScript(const char* name, int id) {
if (id >= numHooks || IsGameScript(name)) return false;
char filename[MAX_PATH];
sprintf(filename, "scripts\\%s.int", name);
@@ -46,7 +46,9 @@ void LoadHookScript(const char* name, int id) {
dlogr(" Error!", DL_HOOK);
}
}
if (HookScripts::injectAllHooks || prog.ptr != nullptr) HookScripts::InjectingHook(id); // inject hook to engine code
bool hookIsLoaded = (prog.ptr != nullptr);
if (hookIsLoaded || HookScripts::injectAllHooks) HookScripts::InjectingHook(id); // inject hook to engine code
return hookIsLoaded;
}
void _stdcall BeginHook() {
@@ -61,6 +63,9 @@ void _stdcall BeginHook() {
}
}
callDepth++;
#ifndef NDEBUG
dlog_f("Begin running hook, current depth: %d\n", DL_HOOK, callDepth);
#endif
}
static void _stdcall RunSpecificHookScript(HookScript *hook) {
@@ -87,6 +92,9 @@ void _stdcall RunHookScript(DWORD hook) {
}
void _stdcall EndHook() {
#ifndef NDEBUG
dlog_f("End running hook, current depth: %d\n", DL_HOOK, callDepth);
#endif
callDepth--;
if (callDepth && callDepth <= maxDepth) {
argCount = lastCount[callDepth - 1];
+1 -1
View File
@@ -37,7 +37,7 @@ 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 cRetTmp; // how many return values were set by specific hook script (when using register_hook)
void LoadHookScript(const char* name, int id);
bool LoadHookScript(const char* name, int id);
void _stdcall BeginHook();
void _stdcall RunHookScript(DWORD hook);
void _stdcall EndHook();
+3 -1
View File
@@ -361,6 +361,8 @@ static void _declspec(naked) CorrectFidForRemovedItemHook() {
}
void AdjustFidHook(DWORD vanillaFid) {
if (!HookScripts::hookAdjustFid) return;
BeginHook();
argCount = 1;
args[0] = vanillaFid;
@@ -414,7 +416,7 @@ void InitInventoryHookScripts() {
LoadHookScript("hs_movecost", HOOK_MOVECOST);
LoadHookScript("hs_inventorymove", HOOK_INVENTORYMOVE);
LoadHookScript("hs_invenwield", HOOK_INVENWIELD);
LoadHookScript("hs_adjustfid", HOOK_ADJUSTFID);
HookScripts::hookAdjustFid = LoadHookScript("hs_adjustfid", HOOK_ADJUSTFID);
Inventory::OnAdjustFid() += AdjustFidHook;
}
+17 -10
View File
@@ -2,6 +2,7 @@
#include "..\..\SafeWrite.h"
#include "..\HookScripts.h"
#include "..\Karma.h"
#include "..\LoadGameHook.h"
#include "Common.h"
#include "MiscHs.h"
@@ -255,15 +256,17 @@ static void __declspec(naked) CarTravelHack() {
static void __fastcall GlobalVarHook_Script(register DWORD number, register int value) {
int old = fo::var::game_global_vars[number];
BeginHook();
argCount = 2;
args[0] = number;
args[1] = value;
RunHookScript(HOOK_SETGLOBALVAR);
EndHook();
if (HookScripts::hookSetGlobalVar && IsMapLoaded()) { // don't execute hook until loading sfall scripts
BeginHook();
argCount = 2;
args[0] = number;
args[1] = value;
if (cRet > 0) value = rets[0];
RunHookScript(HOOK_SETGLOBALVAR);
if (cRet > 0) value = rets[0];
EndHook();
}
if (number == fo::GVAR_PLAYER_REPUTATION && displayKarmaChanges) {
int diff = value - old;
if (diff != 0) Karma::DisplayKarma(diff);
@@ -272,10 +275,14 @@ static void __fastcall GlobalVarHook_Script(register DWORD number, register int
static void __declspec(naked) SetGlobalVarHook() {
__asm {
pushad;
push eax;
push ecx;
push edx;
mov ecx, eax; // number
call GlobalVarHook_Script; // edx - value
popad;
pop edx;
pop ecx;
pop eax;
cmp cRet, 1;
cmovnb edx, dword ptr rets[0];
jmp fo::funcoffs::game_set_global_var_;
@@ -536,7 +543,7 @@ void InitMiscHookScripts() {
LoadHookScript("hs_steal", HOOK_STEAL);
LoadHookScript("hs_withinperception", HOOK_WITHINPERCEPTION);
LoadHookScript("hs_cartravel", HOOK_CARTRAVEL);
LoadHookScript("hs_setglobalvar", HOOK_SETGLOBALVAR);
HookScripts::hookSetGlobalVar = LoadHookScript("hs_setglobalvar", HOOK_SETGLOBALVAR);
LoadHookScript("hs_resttimer", HOOK_RESTTIMER);
LoadHookScript("hs_explosivetimer", HOOK_EXPLOSIVETIMER);
LoadHookScript("hs_useskillon", HOOK_USESKILLON);