From 71e549d59d7991477fe290926211e300e7c78fcc Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 9 Aug 2021 11:29:00 -0400 Subject: [PATCH] Delete ResourceManager --- .../Game/Internal/Resource/ResourceManager.cs | 159 ------------------ .../ResourceManagerAddressResolver.cs | 28 --- 2 files changed, 187 deletions(-) delete mode 100644 Dalamud/Game/Internal/Resource/ResourceManager.cs delete mode 100644 Dalamud/Game/Internal/Resource/ResourceManagerAddressResolver.cs diff --git a/Dalamud/Game/Internal/Resource/ResourceManager.cs b/Dalamud/Game/Internal/Resource/ResourceManager.cs deleted file mode 100644 index 7e3c2b045..000000000 --- a/Dalamud/Game/Internal/Resource/ResourceManager.cs +++ /dev/null @@ -1,159 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Runtime.InteropServices; - -using Dalamud.Hooking; -using Serilog; - -namespace Dalamud.Game.Internal.File -{ - /// - /// This class facilitates modifying how the game loads resources from disk. - /// - public class ResourceManager - { - private readonly Dalamud dalamud; - private readonly ResourceManagerAddressResolver address; - private readonly Hook getResourceAsyncHook; - private readonly Hook getResourceSyncHook; - - private Dictionary resourceHookMap = new(); - - /// - /// Initializes a new instance of the class. - /// - /// The Dalamud instance. - /// The SigScanner instance. - internal ResourceManager(Dalamud dalamud, SigScanner scanner) - { - this.dalamud = dalamud; - this.address = new ResourceManagerAddressResolver(); - this.address.Setup(scanner); - - Log.Verbose("===== R E S O U R C E M A N A G E R ====="); - Log.Verbose("GetResourceAsync address {GetResourceAsync}", this.address.GetResourceAsync); - Log.Verbose("GetResourceSync address {GetResourceSync}", this.address.GetResourceSync); - - this.getResourceAsyncHook = new Hook(this.address.GetResourceAsync, this.GetResourceAsyncDetour); - this.getResourceSyncHook = new Hook(this.address.GetResourceSync, this.GetResourceSyncDetour); - } - - [UnmanagedFunctionPointer(CallingConvention.ThisCall)] - private delegate IntPtr GetResourceAsyncDelegate(IntPtr manager, IntPtr a2, IntPtr a3, IntPtr a4, IntPtr pathPtr, IntPtr a6, byte a7); - - [UnmanagedFunctionPointer(CallingConvention.ThisCall)] - private delegate IntPtr GetResourceSyncDelegate(IntPtr manager, IntPtr a2, IntPtr a3, IntPtr a4, IntPtr pathPtr, IntPtr a6); - - /// - /// Check if a filepath has any invalid characters. - /// - /// The filepath to check. - /// A value indicating whether the filepath is safe to use. - public static bool FilePathHasInvalidChars(string path) - { - return !string.IsNullOrEmpty(path) && path.IndexOfAny(Path.GetInvalidPathChars()) >= 0; - } - - /// - /// Enable this module. - /// - public void Enable() - { - this.getResourceAsyncHook.Enable(); - this.getResourceSyncHook.Enable(); - } - - /// - /// Dispose of managed and unmanaged resources. - /// - public void Dispose() - { - this.getResourceAsyncHook.Dispose(); - this.getResourceSyncHook.Dispose(); - } - - private IntPtr GetResourceAsyncDetour(IntPtr manager, IntPtr a2, IntPtr a3, IntPtr a4, IntPtr pathPtr, IntPtr a6, byte a7) - { - try - { - var path = Marshal.PtrToStringAnsi(pathPtr); - - var resourceHandle = this.getResourceAsyncHook.Original(manager, a2, a3, a4, IntPtr.Zero, a6, a7); - // var resourceHandle = IntPtr.Zero; - - Log.Verbose("GetResourceAsync CALL - this:{0} a2:{1} a3:{2} a4:{3} a5:{4} a6:{5} a7:{6} => RET:{7}", manager, a2, a3, a4, pathPtr, a6, a7, resourceHandle); - - Log.Verbose($"->{path}"); - - this.HandleGetResourceHookAcquire(resourceHandle, path); - - return resourceHandle; - } - catch (Exception ex) - { - Log.Error(ex, "Exception on ReadResourceAsync hook."); - - return this.getResourceAsyncHook.Original(manager, a2, a3, a4, pathPtr, a6, a7); - } - } - - private IntPtr GetResourceSyncDetour(IntPtr manager, IntPtr a2, IntPtr a3, IntPtr a4, IntPtr pathPtr, IntPtr a6) - { - try - { - var resourceHandle = this.getResourceSyncHook.Original(manager, a2, a3, a4, pathPtr, a6); - - Log.Verbose("GetResourceSync CALL - this:{0} a2:{1} a3:{2} a4:{3} a5:{4} a6:{5} => RET:{6}", manager, a2, a3, a4, pathPtr, a6, resourceHandle); - - var path = Marshal.PtrToStringAnsi(pathPtr); - - Log.Verbose($"->{path}"); - - this.HandleGetResourceHookAcquire(resourceHandle, path); - - return resourceHandle; - } - catch (Exception ex) - { - Log.Error(ex, "Exception on ReadResourceSync hook."); - - return this.getResourceSyncHook.Original(manager, a2, a3, a4, pathPtr, a6); - } - } - - private void HandleGetResourceHookAcquire(IntPtr handlePtr, string path) - { - if (FilePathHasInvalidChars(path)) - return; - - if (this.resourceHookMap.ContainsKey(handlePtr)) - { - Log.Verbose($"-> Handle {handlePtr.ToInt64():X}({path}) was cached!"); - return; - } - - var hookInfo = new ResourceHandleHookInfo - { - Path = path, - }; - - var hookPath = Path.Combine(this.dalamud.StartInfo.WorkingDirectory, "ResourceHook", path); - - if (System.IO.File.Exists(hookPath)) - { - hookInfo.DetourFile = new FileStream(hookPath, FileMode.Open); - Log.Verbose("-> Added resource hook detour at {0}", hookPath); - } - - this.resourceHookMap.Add(handlePtr, hookInfo); - } - - private class ResourceHandleHookInfo - { - public string Path { get; set; } - - public Stream DetourFile { get; set; } - } - } -} diff --git a/Dalamud/Game/Internal/Resource/ResourceManagerAddressResolver.cs b/Dalamud/Game/Internal/Resource/ResourceManagerAddressResolver.cs deleted file mode 100644 index b92ea8209..000000000 --- a/Dalamud/Game/Internal/Resource/ResourceManagerAddressResolver.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; - -namespace Dalamud.Game.Internal.File -{ - /// - /// The address resolver for the class. - /// - internal class ResourceManagerAddressResolver : BaseAddressResolver - { - /// - /// Gets the address of the GetResourceAsync method. - /// - public IntPtr GetResourceAsync { get; private set; } - - /// - /// Gets the address of the GetResourceSync method. - /// - public IntPtr GetResourceSync { get; private set; } - - /// - protected override void Setup64Bit(SigScanner sig) - { - this.GetResourceAsync = sig.ScanText("48 89 5C 24 08 48 89 54 24 10 57 48 83 EC 20 B8 03 00 00 00 48 8B F9 86 82 A1 00 00 00 48 8B 5C 24 38 B8 01 00 00 00 87 83 90 00 00 00 85 C0 74"); - this.GetResourceSync = sig.ScanText("48 89 5C 24 08 48 89 6C 24 10 48 89 74 24 18 57 41 54 41 55 41 56 41 57 48 83 EC 30 48 8B F9 49 8B E9 48 83 C1 30 4D 8B F0 4C 8B EA FF 15 CE F6"); - // ReadResourceSync = sig.ScanText("48 89 74 24 18 57 48 83 EC 50 8B F2 49 8B F8 41 0F B7 50 02 8B CE E8 ?? ?? 7A FF 0F B7 57 02 8D 42 89 3D 5F 02 00 00 0F 87 60 01 00 00 4C 8D 05"); - } - } -}