From 6df5e13e23347875f8cc2cfea683ae0746405529 Mon Sep 17 00:00:00 2001 From: Det Date: Mon, 15 Aug 2022 02:57:17 -0300 Subject: [PATCH] AutoHook 2.2.2.5 [PUSH] Added #19 --- AutoHook/AutoHook.csproj | 2 +- AutoHook/AutoHook.json | 2 +- AutoHook/HookManager.cs | 11 ++++++++++ AutoHook/Utils/InputUtil.cs | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 AutoHook/Utils/InputUtil.cs diff --git a/AutoHook/AutoHook.csproj b/AutoHook/AutoHook.csproj index 0c90841..8443b16 100644 --- a/AutoHook/AutoHook.csproj +++ b/AutoHook/AutoHook.csproj @@ -2,7 +2,7 @@ Det - 2.2.2.4 + 2.2.2.5 Auto hooks for you https://github.com/InitialDet/AutoHook Release;Debug diff --git a/AutoHook/AutoHook.json b/AutoHook/AutoHook.json index f235626..e859eee 100644 --- a/AutoHook/AutoHook.json +++ b/AutoHook/AutoHook.json @@ -9,5 +9,5 @@ "Tags": ["Gathering","Fishing"], "DalamudApiLevel": 6, "IconUrl": "https://raw.githubusercontent.com/InitialDet/AutoHook/main/AutoHook/images/icon.png", - "Changelog": "- Trying to add support for CN server \n- Fixing problems with collectable fish\n- Added a new option to use Double/Triple Hooks only when Identical Cast is Enabled" + "Changelog": "- Added an experimental anti-AFK prevention\n- Trying to add support for CN server \n- Fixing problems with collectable fish\n- Added a new option to use Double/Triple Hooks only when Identical Cast is Enabled" } \ No newline at end of file diff --git a/AutoHook/HookManager.cs b/AutoHook/HookManager.cs index ea30019..fbde0eb 100644 --- a/AutoHook/HookManager.cs +++ b/AutoHook/HookManager.cs @@ -77,6 +77,8 @@ private void UnSubscribeToParser() // The current config is updates two times: When we began fishing (to get the config based on the mooch/bait) and when we hooked the fish (in case the user updated their configs). private void UpdateCurrentSetting() { + ResetAFKTimer(); + if (CurrentSetting == null) CurrentSetting = HookSettings.FirstOrDefault(mooch => mooch.BaitName.Equals(CurrentBait)); @@ -302,6 +304,15 @@ private void CheckMaxTimeLimit() } } + public static void ResetAFKTimer() + { + if (!InputUtil.TryFindGameWindow(out var windowHandle)) return; + + // Virtual key for Right Winkey. Can't be used by FFXIV normally, and in tests did not seem to cause any + // unusual interference. + InputUtil.SendKeycode(windowHandle, 0x5C); + } + private static double debugValueLast = 3000; private Stopwatch Timerrr = new(); private void CheckState() diff --git a/AutoHook/Utils/InputUtil.cs b/AutoHook/Utils/InputUtil.cs new file mode 100644 index 0000000..b5d4835 --- /dev/null +++ b/AutoHook/Utils/InputUtil.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace AutoHook.Utils; + +internal static class InputUtil +{ + private const uint WM_KEYUP = 0x101; + private const uint WM_KEYDOWN = 0x100; + + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + private static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string? lpszWindow); + + [DllImport("user32.dll")] + private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); + + [DllImport("user32.dll")] + private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); + + public static bool TryFindGameWindow(out IntPtr hwnd) + { + hwnd = IntPtr.Zero; + while (true) + { + hwnd = FindWindowEx(IntPtr.Zero, hwnd, "FFXIVGAME", null); + if (hwnd == IntPtr.Zero) break; + GetWindowThreadProcessId(hwnd, out var pid); + if (pid == Process.GetCurrentProcess().Id) break; + } + return hwnd != IntPtr.Zero; + } + + public static void SendKeycode(IntPtr hwnd, int keycode) + { + SendMessage(hwnd, WM_KEYDOWN, (IntPtr)keycode, (IntPtr)0); + SendMessage(hwnd, WM_KEYUP, (IntPtr)keycode, (IntPtr)0); + } +}