Example #1
0
        public void ManualPresentHook(IntPtr swapChainPtr)
        {
            DebugMessage("ManualPresentHook Method start");
            SwapChain swapChain = (SharpDX.DXGI.SwapChain)swapChainPtr;

            if (swapChainPtr != IntPtr.Zero)
            {
                try
                {
                    DebugMessage("ManualPresentHook:1");
                    #region Draw overlay (after screenshot so we don't capture overlay as well)

                    #region Initialise Overlay Engine
                    if (_swapChainPointer != swapChain.NativePointer || OverlayEngine == null)
                    {
                        DebugMessage("ManualPresentHook:2");
                        NewSwapchain = true;
                        List <IOverlayElement> oldOverlays = null;

                        if (OverlayEngine != null)
                        {
                            DebugMessage("ManualPresentHook:3");
                            if (OverlayEngine.Overlays.Count > 0 && OverlayEngine.Overlays[0].Elements != null)
                            {
                                DebugMessage("ManualPresentHook:4");
                                oldOverlays = new List <IOverlayElement>(OverlayEngine.Overlays[0].Elements);

                                foreach (var element in oldOverlays)
                                {
                                    if (element is ImageElement)
                                    {
                                        DebugMessage("ManualPresentHook:5");
                                        ((ImageElement)element).Image?.Dispose();
                                        ((ImageElement)element).Image = null;
                                    }
                                }
                            }
                            OverlayEngine.Dispose();
                        }

                        DebugMessage("ManualPresentHook:6");
                        OverlayEngine          = new DX11.DXOverlayEngine(this);
                        OverlayEngine.Overlays = new List <IOverlay>();
                        OverlayEngine.Overlays.Add(new Overlay());
                        OverlayEngine.Overlays.Add(new Overlay());
                        DebugMessage("ManualPresentHook:7");
                        if (oldOverlays != null)
                        {
                            DebugMessage("ManualPresentHook:8");
                            OverlayEngine.Overlays[0].Elements = oldOverlays;
                        }
                        DebugMessage("ManualPresentHook:9");
                        if (ObligatoryElement != null)
                        {
                            DebugMessage("ManualPresentHook:10");
                            OverlayEngine.Overlays[0].Elements.Add(ObligatoryElement);
                        }
                        DebugMessage("ManualPresentHook:11");
                        OverlayEngine.Initialise(swapChain);
                        DebugMessage("ManualPresentHook:12");
                        _swapChainPointer = swapChain.NativePointer;
                    }
                    #endregion

                    // ---LOOP---
                    // Draw Overlay(s)
                    if (OverlayEngine != null)
                    {
                        DebugMessage("ManualPresentHook:13");
                        foreach (var overlay in OverlayEngine.Overlays)
                        {
                            overlay.Frame();
                        }
                        OverlayEngine.Draw();
                        DebugMessage("ManualPresentHook:14");
                    }
                    // ---LOOP---

                    #endregion
                }
                catch (Exception e)
                {
                    // If there is an error we do not want to crash the hooked application, so swallow the exception
                    LogManager.DebugLog("PresentHook: Exeception: " + e.GetType().FullName + ": " + e.ToString());
                    LogManager.LogException(e, "PresentHook");
                    //return unchecked((int)0x8000FFFF); //E_UNEXPECTED
                }
            }
        }
Example #2
0
        /// <summary>
        /// Our present hook that will grab a copy of the backbuffer when requested. Note: this supports multi-sampling (anti-aliasing)
        /// </summary>
        /// <param name="swapChainPtr"></param>
        /// <param name="syncInterval"></param>
        /// <param name="flags"></param>
        /// <returns>The HRESULT of the original method</returns>
        int PresentHook(IntPtr swapChainPtr, int syncInterval, SharpDX.DXGI.PresentFlags flags)
        {
            DebugMessage("PresentHook");
            SwapChain swapChain = (SharpDX.DXGI.SwapChain)swapChainPtr;

            if (swapChainPtr != IntPtr.Zero)
            {
                try
                {
                    #region Draw overlay (after screenshot so we don't capture overlay as well)

                    // Initialise Overlay Engine
                    if (_swapChainPointer != swapChain.NativePointer || OverlayEngine == null)
                    {
                        NewSwapchain = true;

                        if (OverlayEngine != null)
                        {
                            OverlayEngine.Dispose();
                        }
                        OverlayEngine = new DX11.DXOverlayEngine(this);
                        OverlayEngine.Overlays.Add(new DirectXHook.Hook.Common.Overlay
                        {
                            Elements =
                            {
                                new Common.TextElement(new System.Drawing.Font("Times New Roman",                                            22))
                                {
                                    Text        = "*",
                                    Location    = new System.Drawing.Point(0, 0),
                                    Color       = System.Drawing.Color.Red,
                                    AntiAliased = false
                                },
                                new Common.ImageElement(new Bitmap(Width,                         Height))
                                {
                                    Location = new System.Drawing.Point(0,                                                                0)
                                },
                            }
                        });
                        OverlayEngine.Initialise(swapChain);

                        _swapChainPointer = swapChain.NativePointer;
                    }

                    // Draw Overlay(s)

                    if (OverlayEngine != null)
                    {
                        foreach (var overlay in OverlayEngine.Overlays)
                        {
                            overlay.Frame();
                        }
                        OverlayEngine.Draw();
                    }

                    #endregion
                }
                catch (Exception e)
                {
                    // If there is an error we do not want to crash the hooked application, so swallow the exception
                    LogManager.DebugLog("PresentHook: Exeception: " + e.GetType().FullName + ": " + e.ToString());
                    LogManager.LogException(e, "PresentHook");
                    //return unchecked((int)0x8000FFFF); //E_UNEXPECTED
                }
            }

            // As always we need to call the original method, note that EasyHook will automatically skip the hook and call the original method
            // i.e. calling it here will not cause a stack overflow into this function
            return(DXGISwapChain_PresentHook.Original(swapChainPtr, syncInterval, flags));
        }