简体   繁体   English

使用C#和EasyHook在DX11游戏(CryEngine)中绘制叠加

[英]Drawing Overlay inside DX11 Game (CryEngine) with C# and EasyHook

What I want to do: 我想做的事:

I have a Game based on CrySDK which is DirectX11 and I want to draw an ingame overlay like the steam overlay. 我有一个基于CrySDK的游戏,它是DirectX11,我想绘制像蒸汽覆盖一样的游戏覆盖层。

-> I write C# since I don't know any C++, so I'm looking for ways to do this in C#. - >我写C#,因为我不知道任何C ++,所以我正在寻找在C#中做到这一点的方法。

-> I use EasyHook and SharpDX Libaries because it seems this is the best way to go. - >我使用EasyHook和SharpDX Libaries因为看起来这是最好的方法。

-> I found a reference project which is apparently some kind of hack for SC2, I'm just interested in the overlay part however. - >我发现了一个参考项目 ,显然是SC2的某种黑客攻击,但我只对叠加部分感兴趣。

-> I use Visual Studio 2013 - >我使用的是Visual Studio 2013

-> For now I'd be happy if I could draw random stuff inside a game at all - >现在,如果我可以在游戏中随机抽取东西,我会很高兴

What I did so far: 到目前为止我做了什么:

I was looking through google and SO search results, problem is, that either the stuff is in C++, C or other, the results are from 2010 and not up to date with the current dx11, contain dead links or are unusable for any other reasons. 我正在浏览google和SO搜索结果,问题是,这些东西是C ++,C还是其他,结果是从2010年开始,并不是最新的当前dx11,包含死链接或由于任何其他原因无法使用。 This covers 95% of all I've turned up myself, some usuable hints have been included into my code. 这覆盖了我自己出现的所有95%,我的代码中包含了一些可用的提示。

I managed to create a Injector.dll and inject it into the game (at least i think it is injected, haven't had any usuable results so far) 我设法创建一个Injector.dll并将其注入游戏(至少我认为它是注入的,到目前为止还没有任何可用的结果)

What I have: 我有的:

The file which does the injection: 执行注入的文件:

...                
// Try inject thingy

            EasyHook.Config.Register("Star Citizen Noise.sc Client Overlay",
                                    //         "Direct3D9Hook.dll",
                                             "SharpDX.dll",
                                             "SharpDX.Direct3D11.dll",
                                             "Injector.dll");

            // Do the inject
            RemoteHooking.Inject(scProcess.Id, InjectionOptions.DoNotRequireStrongName, "Injector.dll", "Injector.dll", "Star Citizen Noise.sc Injector.dll");
            Console.WriteLine("DLL Injected.");
...

The injector.dll itself consists of this file, called Hooks/Graphics/Direct3D11Hook.cs (I tried to keep the structure similar to the oter sc2 reference project) injector.dll本身由这个文件组成,名为Hooks / Graphics / Direct3D11Hook.cs(我试图保持结构类似于oter sc2参考项目)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Runtime.InteropServices;
using EasyHook;
using SharpDX;
using SharpDX.Direct3D11;
using Rectangle = SharpDX.Rectangle;

namespace Injector.Hooks.Graphics
{
    public class Direct3D11Hook : HookBase
    {

        private Device device = null;

        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
        private delegate Result EndSceneDelegate(IntPtr devicePointer);

        public delegate void EndSceneEventDelegate(ref IntPtr devicePointer);
        public event EndSceneEventDelegate OnEndScene;

        private Result EndScene(IntPtr devicePointer)
        {
            try
            {
                //this.Log.LogMethodSignatureTypesAndValues(devicePointer);

                //GetOrCreateDevice
                if (this.device == null)
                    this.device = Device.FromPointer<Device>(devicePointer);

                if (this.OnEndScene != null)
                    this.OnEndScene(ref devicePointer);

                System.Drawing.Graphics g = System.Drawing.Graphics.FromHdc(devicePointer);
                //System.Drawing.Graphics g = System.Drawing.Graphics.FromHwndInternal(devicePointer);

                // lets try to draw something
                Pen myPen = new Pen(System.Drawing.Color.Pink, 4);
                System.Drawing.Point P1 = new System.Drawing.Point(50, 50);
                System.Drawing.Point P2 = new System.Drawing.Point(500, 500);
                g.DrawLine(myPen, P1, P2);
                g.Dispose();
                //this.device.EndScene();

                return Result.Ok;
            }
            catch (SharpDXException ex)
            {
                //Log.Warn(ex);
                Console.WriteLine(ex);
                return ex.ResultCode;
            }
            catch (Exception ex)
            {
                //this.Log.Fatal(ex);
                Console.WriteLine(ex);
                return Result.UnexpectedFailure;
            }
        }

        public override void Install()
        {
            try
            {
                EndSceneDelegate esd = new EndSceneDelegate(this.EndScene);

            }
            catch (Exception)
            {

                throw;
            }
        }

        public override void Uninstall()
        {

        }

    }
}

Here I tried to provide an override function for the EndScene function, because i think this is where I need to be. 在这里,我尝试为EndScene函数提供覆盖函数,因为我认为这是我需要的地方。 Needless to say, nothing appears to be drawn. 毋庸置疑,似乎没有任何内容。

The delegate definitions are somewhat of a copypaste from the reference project however I have yet to find out how to actually hook my function into the render pipeline and/or react to events which might help my cause. 委托定义在某种程度上来自参考项目副本,但我还没有找到如何将我的函数实际挂钩到渲染管道和/或对可能有助于我的事业的事件作出反应。

There is also this huge file [ https://github.com/jasonpang/Starcraft2Hook/blob/master/Game/Hooks/Graphics/Direct3D9.cs] which defines function addresses which seems to work for DX9, I don't think that it can just be copy pasted and used for DX11 as well, so probably there are new function addresses in DX11? 还有这个巨大的文件[ https://github.com/jasonpang/Starcraft2Hook/blob/master/Game/Hooks/Graphics/Direct3D9.cs] ,它定义了似乎适用于DX9的函数地址,我不认为它也可以复制粘贴并用于DX11,所以DX11中可能有新的功能地址? How can I find those? 我怎么能找到那些? Where do I need to look? 我需要在哪里看? Is it something SharpDX can help me with? SharpDX可以帮助我吗?

TL;DR TL; DR

Want to create steam-like overlay for DX11 game, need to hook into D3D stuff and magic and shit, got dll inject working, need help with pipeline injection 想要为DX11游戏创建类似蒸汽的叠加,需要挂入D3D的东西和魔法和狗屎,得到dll注入工作,需要管道注入帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM