Skip to content

Commit 2c71826

Browse files
authored
Merge pull request #1 from unknownv2/feature/direct3d9-overlay-engine
Add Direct3D9 and Direct3D11 Overlay Engines
2 parents 89dddef + f8789ec commit 2c71826

36 files changed

+2202
-125
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "deps/SharpDX.Desktop"]
2+
path = deps/SharpDX.Desktop
3+
url = git://github.com/unknownv2/SharpDX.Desktop.git

GraphicsHook.sln

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2FDEAA0B-9
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Direct3DCapture.Tests", "test\Direct3DCapture.Tests\Direct3DCapture.Tests.csproj", "{39FCDDD4-78AF-4F6F-BA40-724A5D1AE620}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectX.Direct3D9.Overlay", "src\DirectX.Direct3D9.Overlay\DirectX.Direct3D9.Overlay.csproj", "{A86B01F8-1FEA-485B-85D7-3941694EC0A4}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX.Direct3D9.Overlay", "src\DirectX.Direct3D9.Overlay\DirectX.Direct3D9.Overlay.csproj", "{A86B01F8-1FEA-485B-85D7-3941694EC0A4}"
1515
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectX.Direct3D11.Overlay", "src\DirectX.Direct3D11.Overlay\DirectX.Direct3D11.Overlay.csproj", "{96BC579E-FAFD-4152-8AFC-81905D7BF210}"
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX.Direct3D11.Overlay", "src\DirectX.Direct3D11.Overlay\DirectX.Direct3D11.Overlay.csproj", "{96BC579E-FAFD-4152-8AFC-81905D7BF210}"
17+
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX.Direct3D.Core", "src\DirectX.Direct3D.Core\DirectX.Direct3D.Core.csproj", "{224F9343-46F6-4654-9381-E35239D179F1}"
19+
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectX.Direct3D10.Overlay", "src\DirectX.Direct3D10.Overlay\DirectX.Direct3D10.Overlay.csproj", "{409B3409-61FE-4614-B755-843689E50EF6}"
21+
EndProject
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpDX.Desktop", "deps\SharpDX.Desktop\SharpDX.Desktop\SharpDX.Desktop.csproj", "{4062A245-60C4-415C-84A2-FA3E5D6B9D3F}"
1723
EndProject
1824
Global
1925
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -37,6 +43,18 @@ Global
3743
{96BC579E-FAFD-4152-8AFC-81905D7BF210}.Debug|Any CPU.Build.0 = Debug|Any CPU
3844
{96BC579E-FAFD-4152-8AFC-81905D7BF210}.Release|Any CPU.ActiveCfg = Release|Any CPU
3945
{96BC579E-FAFD-4152-8AFC-81905D7BF210}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{224F9343-46F6-4654-9381-E35239D179F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{224F9343-46F6-4654-9381-E35239D179F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{224F9343-46F6-4654-9381-E35239D179F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{224F9343-46F6-4654-9381-E35239D179F1}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{409B3409-61FE-4614-B755-843689E50EF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{409B3409-61FE-4614-B755-843689E50EF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{409B3409-61FE-4614-B755-843689E50EF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{409B3409-61FE-4614-B755-843689E50EF6}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{4062A245-60C4-415C-84A2-FA3E5D6B9D3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55+
{4062A245-60C4-415C-84A2-FA3E5D6B9D3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
56+
{4062A245-60C4-415C-84A2-FA3E5D6B9D3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
57+
{4062A245-60C4-415C-84A2-FA3E5D6B9D3F}.Release|Any CPU.Build.0 = Release|Any CPU
4058
EndGlobalSection
4159
GlobalSection(SolutionProperties) = preSolution
4260
HideSolutionNode = FALSE
@@ -46,6 +64,9 @@ Global
4664
{39FCDDD4-78AF-4F6F-BA40-724A5D1AE620} = {2FDEAA0B-9F04-4CB1-89CF-EF668584453A}
4765
{A86B01F8-1FEA-485B-85D7-3941694EC0A4} = {E9D61240-9E2C-4BD5-BEF1-607D3422EAAF}
4866
{96BC579E-FAFD-4152-8AFC-81905D7BF210} = {E9D61240-9E2C-4BD5-BEF1-607D3422EAAF}
67+
{224F9343-46F6-4654-9381-E35239D179F1} = {E9D61240-9E2C-4BD5-BEF1-607D3422EAAF}
68+
{409B3409-61FE-4614-B755-843689E50EF6} = {E9D61240-9E2C-4BD5-BEF1-607D3422EAAF}
69+
{4062A245-60C4-415C-84A2-FA3E5D6B9D3F} = {E9D61240-9E2C-4BD5-BEF1-607D3422EAAF}
4970
EndGlobalSection
5071
GlobalSection(ExtensibilityGlobals) = postSolution
5172
SolutionGuid = {15C4F934-433D-4655-9D2C-5E30E7A936C3}

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ Example tool for hooking graphics APIs using [CoreHook](https://github.com/unkno
55

66
Based on [Justin Stenning's Direct3DHook](https://github.com/spazzarama/Direct3DHook).
77

8+
## Requirements
9+
10+
**[Building the sample requires the .NET Core 3.0 SDK, which can be downloaded here.](https://dotnet.microsoft.com/download/dotnet-core/3.0)**
11+
12+
The Direct3D10 and Direct3D11 modules require the .NET Windows Form libraries, which are only available with .NET Core 3.0 and above.
13+
14+
The `deps` folder contains the [`SharpDX.Desktop`](https://github.com/unknownv2/SharpDX.Desktop) library targeting .NET Core 3.0, which is used by the Direct3D10 and Direct3D11 sample modules.
15+
16+
## Building
17+
18+
Clone and build the sample with:
19+
20+
```
21+
git clone --recursive git://github.com/unknownv2/graphics-hook.git
22+
cd graphics-hook
23+
dotnet build
24+
```
825

926
## References
1027

deps/SharpDX.Desktop

Submodule SharpDX.Desktop added at 18b895c

src/Direct3DCapture/Direct3DCapture.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,20 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
<OutputPath>$(OutputDir)</OutputPath>
10+
</PropertyGroup>
11+
12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
13+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
14+
<OutputPath>$(OutputDir)</OutputPath>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="CoreHook" Version="1.0.3" />
19+
<PackageReference Include="SharpDX.Direct3D9" Version="4.2.0" />
20+
<PackageReference Include="SharpDX.Mathematics" Version="4.2.0" />
21+
</ItemGroup>
22+
723
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using DirectX.Direct3D.Core.Drawing;
3+
using DirectX.Direct3D.Core.Memory;
4+
5+
namespace DirectX.Direct3D.Core
6+
{
7+
public abstract class Direct3DHook : DisposableComponent, IDirect3DHook
8+
{
9+
protected List<IOverlay> Overlays { get; set; }
10+
11+
protected bool PendingUpdate;
12+
13+
public abstract void CreateHooks();
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace DirectX.Direct3D.Core
4+
{
5+
public enum Direct3DVersion
6+
{
7+
Auto,
8+
Direct3D9,
9+
Direct3D10,
10+
Direct3D10_1,
11+
Direct3D11,
12+
Direct3D11_1,
13+
Direct3D12
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
8+
<OutputPath>$(OutputDir)</OutputPath>
9+
</PropertyGroup>
10+
11+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
12+
<OutputPath>$(OutputDir)</OutputPath>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="SharpDX" Version="4.2.0" />
17+
<PackageReference Include="System.Drawing.Common" Version="4.6.0-preview.18571.3" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Drawing;
3+
4+
namespace DirectX.Direct3D.Core.Drawing
5+
{
6+
public class FramesPerSecondOverlay : TextOverlay
7+
{
8+
private string _fpsText = "{0:N0} FPS";
9+
10+
private int _frameCount;
11+
private int _lastTickCount;
12+
private float _lastFrameRate;
13+
public override string Text
14+
{
15+
get => string.Format(_fpsText, GetFramesPerSecond());
16+
set => _fpsText = value;
17+
}
18+
public FramesPerSecondOverlay(Font font) : base(font)
19+
{
20+
21+
}
22+
23+
public override void OnFrame()
24+
{
25+
_frameCount++;
26+
var tickCount = Environment.TickCount;
27+
if (Math.Abs(tickCount - _lastTickCount) > 1000)
28+
{
29+
_lastFrameRate = (float)_frameCount * 1000 / Math.Abs(tickCount - _lastTickCount);
30+
_frameCount = 0;
31+
_lastTickCount = tickCount;
32+
}
33+
}
34+
35+
public float GetFramesPerSecond()
36+
{
37+
return _lastFrameRate;
38+
}
39+
}
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
3+
namespace DirectX.Direct3D.Core.Drawing
4+
{
5+
public interface IOverlay : IOverlayElement
6+
{
7+
List<IOverlayElement> Elements { get; }
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace DirectX.Direct3D.Core.Drawing
4+
{
5+
public interface IOverlayElement : ICloneable
6+
{
7+
bool Hidden { get; }
8+
9+
void OnFrame();
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DirectX.Direct3D.Core.Drawing
6+
{
7+
public class ImageOverlay : OverlayElement
8+
{
9+
}
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace DirectX.Direct3D.Core.Drawing
5+
{
6+
[Serializable]
7+
public class Overlay : IOverlay
8+
{
9+
public List<IOverlayElement> Elements { get; } = new List<IOverlayElement>();
10+
11+
public bool Hidden { get; set; }
12+
13+
public object Clone() => MemberwiseClone();
14+
15+
public void OnFrame()
16+
{
17+
foreach (var overlayElement in Elements)
18+
{
19+
overlayElement.OnFrame();
20+
}
21+
}
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace DirectX.Direct3D.Core.Drawing
4+
{
5+
public class OverlayElement : IOverlayElement, IDisposable
6+
{
7+
public virtual bool Hidden { get; set; }
8+
9+
public object Clone() => MemberwiseClone();
10+
11+
public virtual void OnFrame()
12+
{
13+
}
14+
15+
protected virtual void Dispose(bool disposing)
16+
{
17+
}
18+
19+
public void Dispose()
20+
{
21+
Dispose(true);
22+
GC.SuppressFinalize(this);
23+
}
24+
25+
protected void SafeDispose(IDisposable disposableObject) => disposableObject?.Dispose();
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DirectX.Direct3D.Core.Drawing
6+
{
7+
public class TextOverlay : OverlayElement
8+
{
9+
public virtual string Text { get; set; }
10+
11+
public virtual System.Drawing.Font Font { get; set; } = System.Drawing.SystemFonts.DefaultFont;
12+
13+
public virtual System.Drawing.Color Color { get; set; } = System.Drawing.Color.Black;
14+
15+
public virtual System.Drawing.Point Location { get; set; }
16+
17+
public virtual bool AntiAliased { get; set; } = false;
18+
19+
public TextOverlay()
20+
{
21+
}
22+
23+
public TextOverlay(System.Drawing.Font font)
24+
{
25+
Font = font;
26+
}
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DirectX.Direct3D.Core
6+
{
7+
public interface IDirect3DHook : IDisposable
8+
{
9+
void CreateHooks();
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DirectX.Direct3D.Core.Memory
6+
{
7+
public abstract class DisposableBase : IDisposable
8+
{
9+
public bool IsDisposed { get; private set; }
10+
11+
public void Dispose()
12+
{
13+
CheckAndDispose(true);
14+
}
15+
16+
private void CheckAndDispose(bool disposing)
17+
{
18+
if (!IsDisposed)
19+
{
20+
IsDisposed = true;
21+
}
22+
}
23+
24+
protected abstract void Dispose(bool disposing);
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using SharpDX;
5+
6+
namespace DirectX.Direct3D.Core.Memory
7+
{
8+
public class DisposableComponent : DisposableBase
9+
{
10+
protected DisposeCollector DisposeCollector { get; set; }
11+
12+
protected override void Dispose(bool disposeManagedResources)
13+
{
14+
15+
}
16+
17+
protected internal T ToDispose<T>(T disposable)
18+
{
19+
if (!ReferenceEquals(disposable, null))
20+
{
21+
if (DisposeCollector == null)
22+
{
23+
DisposeCollector = new DisposeCollector();
24+
}
25+
26+
return DisposeCollector.Collect(disposable);
27+
}
28+
29+
return default(T);
30+
}
31+
32+
protected internal void RemoveAndDispose<T>(ref T disposable)
33+
{
34+
if (!ReferenceEquals(disposable, null))
35+
{
36+
DisposeCollector?.RemoveAndDispose(ref disposable);
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)