Skip to content

Commit

Permalink
Rewrite ImGuiScene
Browse files Browse the repository at this point in the history
  • Loading branch information
Soreepeong committed Jul 12, 2024
1 parent d9d65da commit 3cbbc85
Show file tree
Hide file tree
Showing 42 changed files with 3,897 additions and 3,970 deletions.
12 changes: 6 additions & 6 deletions Dalamud/Dalamud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
<PackageReference Include="SharpDX.Direct3D11" Version="4.2.0" />
<PackageReference Include="SharpDX.Mathematics" Version="4.2.0" />
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
<PackageReference Include="StbiSharp" Version="1.0.13" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -103,11 +102,12 @@
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.22621.2" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Interface\ImGuiScene\resources\shaders\imgui-frag.hlsl.bytes" LogicalName="imgui-frag.hlsl.bytes" />
<EmbeddedResource Include="Interface\ImGuiScene\resources\shaders\imgui-vertex.hlsl.bytes" LogicalName="imgui-vertex.hlsl.bytes" />
<EmbeddedResource Include="Interface\ImGuiScene\resources\shaders\imgui-frag.glsl" LogicalName="imgui-frag.glsl" />
<EmbeddedResource Include="Interface\ImGuiScene\resources\shaders\imgui-vertex.glsl" LogicalName="imgui-vertex.glsl" />
<EmbeddedResource Include="Interface\ImGuiScene\costura64\stbi.dll" />
<EmbeddedResource Include="Interface\ImGuiBackend\Renderers\imgui-frag.hlsl.bytes">
<LogicalName>imgui-frag.hlsl.bytes</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Interface\ImGuiBackend\Renderers\imgui-vertex.hlsl.bytes">
<LogicalName>imgui-vertex.hlsl.bytes</LogicalName>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Dalamud.Common\Dalamud.Common.csproj" />
Expand Down
142 changes: 13 additions & 129 deletions Dalamud/Game/Internal/DXGI/SwapChainVtableResolver.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;

using Dalamud.Game.Internal.DXGI.Definitions;
using Dalamud.Interface.ImGuiBackend.Helpers;

using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel;
using Serilog;

using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;

namespace Dalamud.Game.Internal.DXGI;

Expand All @@ -17,30 +17,23 @@ namespace Dalamud.Game.Internal.DXGI;
internal class SwapChainVtableResolver : BaseAddressResolver, ISwapChainAddressResolver
{
/// <inheritdoc/>
public IntPtr Present { get; set; }
public nint Present { get; set; }

/// <inheritdoc/>
public IntPtr ResizeBuffers { get; set; }

/// <summary>
/// Gets a value indicating whether or not ReShade is loaded/used.
/// </summary>
public bool IsReshade { get; private set; }
public nint ResizeBuffers { get; set; }

/// <inheritdoc/>
protected override unsafe void Setup64Bit(ISigScanner sig)
{
Device* kernelDev;
SwapChain* swapChain;
void* dxgiSwapChain;

while (true)
{
kernelDev = Device.Instance();
var kernelDev = Device.Instance();
if (kernelDev == null)
continue;

swapChain = kernelDev->SwapChain;
var swapChain = kernelDev->SwapChain;
if (swapChain == null)
continue;

Expand All @@ -51,119 +44,10 @@ protected override unsafe void Setup64Bit(ISigScanner sig)
break;
}

var scVtbl = GetVTblAddresses(new IntPtr(dxgiSwapChain), Enum.GetValues(typeof(IDXGISwapChainVtbl)).Length);

this.Present = scVtbl[(int)IDXGISwapChainVtbl.Present];

var modules = Process.GetCurrentProcess().Modules;
foreach (ProcessModule processModule in modules)
{
if (processModule.FileName == null || !processModule.FileName.EndsWith("game\\dxgi.dll"))
continue;

try
{
var fileInfo = FileVersionInfo.GetVersionInfo(processModule.FileName);

if (fileInfo.FileDescription == null)
break;

if (!fileInfo.FileDescription.Contains("GShade") && !fileInfo.FileDescription.Contains("ReShade"))
break;

// warning: these comments may no longer be accurate.
// reshade master@4232872 RVA
// var p = processModule.BaseAddress + 0x82C7E0; // DXGISwapChain::Present
// var p = processModule.BaseAddress + 0x82FAC0; // DXGISwapChain::runtime_present
// DXGISwapChain::handle_device_loss =>df DXGISwapChain::Present => DXGISwapChain::runtime_present
// 5.2+ - F6 C2 01 0F 85
// 6.0+ - F6 C2 01 0F 85 88

var scanner = new SigScanner(processModule);
var reShadeDxgiPresent = IntPtr.Zero;

if (fileInfo.FileVersion?.StartsWith("6.") == true)
{
// No Addon
if (scanner.TryScanText("F6 C2 01 0F 85 A8", out reShadeDxgiPresent))
{
Log.Information("Hooking present for ReShade 6 No-Addon");
}

// Addon
else if (scanner.TryScanText("F6 C2 01 0F 85 88", out reShadeDxgiPresent))
{
Log.Information("Hooking present for ReShade 6 Addon");
}

// Fallback
else
{
Log.Error("Failed to get ReShade 6 DXGISwapChain::on_present offset!");
}
}

// Looks like this sig only works for GShade 4
if (reShadeDxgiPresent == IntPtr.Zero && fileInfo.FileDescription?.Contains("GShade 4.") == true)
{
if (scanner.TryScanText("E8 ?? ?? ?? ?? 45 0F B6 5E ??", out reShadeDxgiPresent))
{
Log.Information("Hooking present for GShade 4");
}
else
{
Log.Error("Failed to find GShade 4 DXGISwapChain::on_present offset!");
}
}

if (reShadeDxgiPresent == IntPtr.Zero)
{
if (scanner.TryScanText("F6 C2 01 0F 85", out reShadeDxgiPresent))
{
Log.Information("Hooking present for ReShade with fallback 5.X sig");
}
else
{
Log.Error("Failed to find ReShade DXGISwapChain::on_present offset with fallback sig!");
}
}

Log.Information("ReShade DLL: {FileName} ({Info} - {Version}) with DXGISwapChain::on_present at {Address}",
processModule.FileName,
fileInfo.FileDescription ?? "Unknown",
fileInfo.FileVersion ?? "Unknown",
reShadeDxgiPresent.ToString("X"));

if (reShadeDxgiPresent != IntPtr.Zero)
{
this.Present = reShadeDxgiPresent;
this.IsReshade = true;
}

break;
}
catch (Exception e)
{
Log.Error(e, "Failed to get ReShade version info");
break;
}
}

this.ResizeBuffers = scVtbl[(int)IDXGISwapChainVtbl.ResizeBuffers];
}

private static List<IntPtr> GetVTblAddresses(IntPtr pointer, int numberOfMethods)
{
return GetVTblAddresses(pointer, 0, numberOfMethods);
}

private static List<IntPtr> GetVTblAddresses(IntPtr pointer, int startIndex, int numberOfMethods)
{
var vtblAddresses = new List<IntPtr>();
var vTable = Marshal.ReadIntPtr(pointer);
for (var i = startIndex; i < startIndex + numberOfMethods; i++)
vtblAddresses.Add(Marshal.ReadIntPtr(vTable, i * IntPtr.Size)); // using IntPtr.Size allows us to support both 32 and 64-bit processes
using var sc = new ComPtr<IDXGISwapChain>((IDXGISwapChain*)dxgiSwapChain);
ReShadePeeler.PeelSwapChain(&sc);

return vtblAddresses;
this.Present = (nint)sc.Get()->lpVtbl[(int)IDXGISwapChainVtbl.Present];
this.ResizeBuffers = (nint)sc.Get()->lpVtbl[(int)IDXGISwapChainVtbl.ResizeBuffers];
}
}
2 changes: 1 addition & 1 deletion Dalamud/Interface/DragDrop/DragDropManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private DragDropManager()
Service<InterfaceManager.InterfaceManagerWithScene>.GetAsync()
.ContinueWith(t =>
{
this.windowHandlePtr = t.Result.Manager.WindowHandlePtr;
this.windowHandlePtr = t.Result.Manager.GameWindowHandle;
this.Enable();
});
}
Expand Down
Loading

0 comments on commit 3cbbc85

Please sign in to comment.