From b0f108c89f583a418e1410c11a905b7566923aef Mon Sep 17 00:00:00 2001 From: NovaRain Date: Sat, 15 May 2021 21:57:10 +0800 Subject: [PATCH] Fixed value bounds for the redefined key code value in HOOK_KEYPRESS Minor code & document edits. --- artifacts/ddraw.ini | 4 ++-- artifacts/scripting/hookscripts.md | 2 +- sfall/Animations.cpp | 14 +++++++------- sfall/FalloutStructs.h | 25 ++++++++++++++----------- sfall/HookScripts.cpp | 5 ++++- sfall/InputFuncs.cpp | 9 +++++++-- 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/artifacts/ddraw.ini b/artifacts/ddraw.ini index 3c6a614b..be9b52b5 100644 --- a/artifacts/ddraw.ini +++ b/artifacts/ddraw.ini @@ -132,9 +132,9 @@ ScrollMod=0 MouseSensitivity=100 ;DX scancode of a key to press when the middle mouse button is clicked -;The default of 0x30 toggles between your two weapons +;The default of 48 ('B' key) toggles between your active items ;Set to 0 to disable -MiddleMouse=0x30 +MiddleMouse=48 ;Set to 1 to reverse the left and right mouse buttons ReverseMouseButtons=0 diff --git a/artifacts/scripting/hookscripts.md b/artifacts/scripting/hookscripts.md index c9af971c..9c1985b2 100644 --- a/artifacts/scripting/hookscripts.md +++ b/artifacts/scripting/hookscripts.md @@ -406,7 +406,7 @@ int ret0 - The new amount of ammo to be consumed, or ammo cost per round for #### `HOOK_KEYPRESS (hs_keypress.int)` Runs once every time when any key was pressed or released. -- DX codes: (see **dik.h** header) +- DX codes: see **dik.h** header or https://kippykip.com/b3ddocs/commands/scancodes.htm - VK codes: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx __NOTE:__ if you want to override a key, the new key DX scancode should be the same for both pressed and released events. diff --git a/sfall/Animations.cpp b/sfall/Animations.cpp index 3ad84ac7..6d84a5ac 100644 --- a/sfall/Animations.cpp +++ b/sfall/Animations.cpp @@ -25,7 +25,7 @@ enum AnimFlags { e_InCombat = 0x002, e_Reserved = 0x004, e_InUse = 0x008, - e_Suspend = 0x010, + e_Suspend = 0x010, // animation not start running in register_end e_Clear = 0x020, e_EndAnim = 0x040, e_DontStand = 0x080, @@ -60,7 +60,7 @@ static const DWORD anim_set_0[] = { // curr_anim 0x414E48, 0x414EDA, 0x414F5E, 0x414FEE, 0x41505C, 0x4150D0, 0x415158, 0x4151B8, 0x415286, 0x41535C, 0x4153D0, 0x41544A, 0x4154EC, 0x4155EA, 0x4156C0, 0x4156D5, 0x4156F2, 0x41572F, 0x41573E, 0x415B1B, 0x415B56, - 0x415BB6, 0x415C7C, 0x415CA3, /*0x415DE4, - conflct with 0x415DE2*/ + 0x415BB6, 0x415C7C, 0x415CA3, /*0x415DE4, - conflict with 0x415DE2*/ }; static const DWORD anim_set_4[] = { // counter @@ -172,10 +172,10 @@ skip: } static bool __fastcall CheckSetSad(BYTE openFlag, DWORD slot) { - if (animSad[slot].currentAnim == -1000) { + if (animSad[slot].animStep == -1000) { return true; } else if (!InCombat() && !(openFlag & 1)) { - animSad[slot].currentAnim = -1000; + animSad[slot].animStep = -1000; return true; } return false; @@ -208,7 +208,7 @@ static void __declspec(naked) action_climb_ladder_hook() { pop edx; jne skip; reset: - and al, ~0x4; // reset RB_DONTSTAND flag + and al, ~RB_DONTSTAND; // unset flag skip: jmp register_begin_; } @@ -278,9 +278,9 @@ void ApplyAnimationsAtOncePatches(signed char aniMax) { SafeWriteBatch((DWORD)&animSad->animCode, sad_C); SafeWriteBatch((DWORD)&animSad->ticks, sad_10); SafeWriteBatch((DWORD)&animSad->tpf, sad_14); - SafeWriteBatch((DWORD)&animSad->currAnimSet, sad_18); + SafeWriteBatch((DWORD)&animSad->animSetSlot, sad_18); SafeWriteBatch((DWORD)&animSad->pathCount, sad_1C); - SafeWriteBatch((DWORD)&animSad->currentAnim, sad_20); + SafeWriteBatch((DWORD)&animSad->animStep, sad_20); SafeWriteBatch((DWORD)&animSad->dstTile, sad_24); SafeWrite32(0x416903, (DWORD)&animSad->rotation1); SafeWriteBatch((DWORD)&animSad->rotation2, sad_27); diff --git a/sfall/FalloutStructs.h b/sfall/FalloutStructs.h index 13a5e295..e099b7df 100644 --- a/sfall/FalloutStructs.h +++ b/sfall/FalloutStructs.h @@ -56,8 +56,8 @@ struct AnimationSet { struct Animation { long animType; - long source; - long target; + TGameObj* source; + TGameObj* target; long data1; long elevation; long animCode; @@ -74,24 +74,27 @@ static_assert(sizeof(AnimationSet) == 2656, "Incorrect AnimationSet definition." struct AnimationSad { long flags; - long source; + TGameObj* source; long fid; long animCode; long ticks; long tpf; // fps - long currAnimSet; + long animSetSlot; long pathCount; // len - long currentAnim; + long animStep; // current step in rotationData/pathData short dstTile; char rotation1; char rotation2; - struct BuildPathData { - long tile; - long elevation; - long sX; - long sY; - } pathData[200]; + union { + long rotationData[800]; + struct BuildPathData { + long tile; + long elevation; + long sX; + long sY; + } pathData[200]; + }; }; static_assert(sizeof(AnimationSad) == 3240, "Incorrect AnimationSad definition."); diff --git a/sfall/HookScripts.cpp b/sfall/HookScripts.cpp index 6aa9a90a..be189d86 100644 --- a/sfall/HookScripts.cpp +++ b/sfall/HookScripts.cpp @@ -938,7 +938,10 @@ void __stdcall KeyPressHook(DWORD* dxKey, bool pressed, DWORD vKey) { args[1] = *dxKey; args[2] = vKey; RunHookScript(HOOK_KEYPRESS); - if (cRet != 0) *dxKey = rets[0]; + if (cRet != 0) { + long retKey = rets[0]; + if (retKey > 0 && retKey < 264) *dxKey = retKey; + } EndHook(); } diff --git a/sfall/InputFuncs.cpp b/sfall/InputFuncs.cpp index 3913ca68..68009b6b 100644 --- a/sfall/InputFuncs.cpp +++ b/sfall/InputFuncs.cpp @@ -54,6 +54,7 @@ static double mousePartX; static double mousePartY; #define MAX_KEYS (264) + static DWORD keysDown[MAX_KEYS] = {0}; static int mouseX; @@ -62,6 +63,7 @@ static int mouseY; static DWORD forcingGraphicsRefresh = 0; void __stdcall ForceGraphicsRefresh(DWORD d) { + if (!d3d9Device) return; forcingGraphicsRefresh = (d == 0) ? 0 : 1; } @@ -287,11 +289,14 @@ public: for (DWORD i = 0; i < *count; i++) { DWORD dxKey = buf[i].dwOfs; + assert(dxKey >= 0 && dxKey < MAX_KEYS); DWORD state = buf[i].dwData & 0x80; DWORD oldState = keysDown[dxKey]; keysDown[dxKey] = state; + KeyPressHook(&dxKey, (state > 0), MapVirtualKeyEx(dxKey, MAPVK_VSC_TO_VK, keyboardLayout)); - if (dxKey > 0 && dxKey != buf[i].dwOfs) { + + if ((signed)dxKey > 0 && dxKey != buf[i].dwOfs) { keysDown[buf[i].dwOfs] = oldState; buf[i].dwOfs = dxKey; // Override key keysDown[buf[i].dwOfs] = state; @@ -446,7 +451,7 @@ HRESULT __stdcall FakeDirectInputCreate(HINSTANCE a, DWORD b, IDirectInputA** c, mousePartY = 0; } else adjustMouseSpeed = false; - middleMouseKey = GetConfigInt("Input", "MiddleMouse", 0x30); + middleMouseKey = GetConfigInt("Input", "MiddleMouse", DIK_B); middleMouseDown = false; backgroundKeyboard = GetConfigInt("Input", "BackgroundKeyboard", 0) != 0;