From 2f88a4e0e64e4c88473362501adca0418fefd94e Mon Sep 17 00:00:00 2001 From: goat Date: Mon, 13 Apr 2020 20:49:27 +0200 Subject: [PATCH] fix: dodgy RTSS workaround --- Dalamud/Interface/InterfaceManager.cs | 37 +++++++++++++++++++++++++++ Dalamud/NativeFunctions.cs | 25 ++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/Dalamud/Interface/InterfaceManager.cs b/Dalamud/Interface/InterfaceManager.cs index 83d496efa..1a7959989 100644 --- a/Dalamud/Interface/InterfaceManager.cs +++ b/Dalamud/Interface/InterfaceManager.cs @@ -1,7 +1,12 @@ using System; +using System.ComponentModel; +using System.Diagnostics; using System.IO; +using System.Linq; using System.Numerics; using System.Runtime.InteropServices; +using System.Text; +using CheapLoc; using Dalamud.Game; using Dalamud.Game.Internal.DXGI; using Dalamud.Hooking; @@ -44,6 +49,9 @@ namespace Dalamud.Interface private Dalamud dalamud; private RawDX11Scene scene; + private delegate void InstallRTSSHook(); + private string rtssPath; + /// /// This event gets called by a plugin UiBuilder when read /// @@ -72,6 +80,23 @@ namespace Dalamud.Interface Address = vtableResolver; } + try { + var rtss = NativeFunctions.GetModuleHandle("RTSSHooks64.dll"); + + if (rtss != IntPtr.Zero) { + var fileName = new StringBuilder(255); + NativeFunctions.GetModuleFileName(rtss, fileName, fileName.Capacity); + this.rtssPath = fileName.ToString(); + Log.Verbose("RTSS at {0}", this.rtssPath); + + if (!NativeFunctions.FreeLibrary(rtss)) + throw new Win32Exception(); + } + } catch (Exception e) { + Log.Error(e, "RTSS Free failed"); + } + + var setCursorAddr = LocalHook.GetProcAddress("user32.dll", "SetCursor"); Log.Verbose("===== S W A P C H A I N ====="); @@ -97,6 +122,18 @@ namespace Dalamud.Interface this.setCursorHook.Enable(); this.presentHook.Enable(); this.resizeBuffersHook.Enable(); + + try { + if (!string.IsNullOrEmpty(this.rtssPath)) { + NativeFunctions.LoadLibrary(this.rtssPath); + + var installAddr = LocalHook.GetProcAddress("RTSSHooks64.dll", "InstallRTSSHook"); + var installDele = Marshal.GetDelegateForFunctionPointer(installAddr); + installDele.Invoke(); + } + } catch (Exception ex) { + Log.Error(ex, "Could not reload RTSS"); + } } private void Disable() diff --git a/Dalamud/NativeFunctions.cs b/Dalamud/NativeFunctions.cs index b880940f4..1b5f8a5be 100644 --- a/Dalamud/NativeFunctions.cs +++ b/Dalamud/NativeFunctions.cs @@ -61,5 +61,30 @@ namespace Dalamud [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool FlashWindowEx(ref FLASHWINFO pwfi); + + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool FreeLibrary(IntPtr hModule); + + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] + public static extern IntPtr GetModuleHandle(string lpModuleName); + + [DllImport("kernel32.dll", SetLastError = true)] + [PreserveSig] + public static extern uint GetModuleFileName + ( + [In] + IntPtr hModule, + + [Out] + StringBuilder lpFilename, + + [In] + [MarshalAs(UnmanagedType.U4)] + int nSize + ); + + [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)] + public static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName); } }