private IntPtr ResizeBuffersImpl(IntPtr swapchainPtr, uint bufferCount, uint width, uint height, Format newFormat, uint swapchainFlags)
        {
            if (_resizeRecursionLock)
            {
                Debug.WriteLine($"[DX11 ResizeBuffers] Discarding via Recursion Lock");
                return(_resizeBuffersHook.OriginalFunction.Value.Invoke(swapchainPtr, bufferCount, width, height, newFormat, swapchainFlags));
            }

            _resizeRecursionLock = true;
            try
            {
                var swapChain    = new SwapChain(swapchainPtr);
                var windowHandle = swapChain.Description.OutputHandle;
                Debug.DebugWriteLine($"[DX11 ResizeBuffers] Window Handle {windowHandle}");

                // Ignore windows which don't belong to us.
                if (!ImguiHook.CheckWindowHandle(windowHandle))
                {
                    Debug.WriteLine($"[DX11 ResizeBuffers] Discarding Window Handle {windowHandle} due to Mismatch");
                    return(_resizeBuffersHook.OriginalFunction.Value.Invoke(swapchainPtr, bufferCount, width, height, newFormat, swapchainFlags));
                }

                PreResizeBuffers();
                var result = _resizeBuffersHook.OriginalFunction.Value.Invoke(swapchainPtr, bufferCount, width, height, newFormat, swapchainFlags);
                PostResizeBuffers(swapchainPtr);

                return(result);
            }
            finally
            {
                _resizeRecursionLock = false;
            }
        }
示例#2
0
        private IntPtr ResetImpl(IntPtr device, PresentParameters *presentParameters)
        {
            // With multi-viewports, ImGui might call EndScene again; so we need to prevent stack overflow here.
            if (_resetRecursionLock)
            {
                Debug.WriteLine($"[DX9 Reset] Discarding via Recursion Lock");
                return(_endSceneHook.OriginalFunction.Value.Invoke(device));
            }

            _resetRecursionLock = true;
            try
            {
                // Ignore windows which don't belong to us.
                if (!ImguiHook.CheckWindowHandle(presentParameters->DeviceWindowHandle))
                {
                    Debug.WriteLine($"[DX9 Reset] Discarding Window Handle {(long)presentParameters->DeviceWindowHandle:X}");
                    return(_resetHook.OriginalFunction.Value.Invoke(device, presentParameters));
                }

                Debug.WriteLine($"[DX9 Reset] Reset with Handle {(long)presentParameters->DeviceWindowHandle:X}");
                ImGui.ImGuiImplDX9InvalidateDeviceObjects();
                var result = _resetHook.OriginalFunction.Value.Invoke(device, presentParameters);
                ImGui.ImGuiImplDX9CreateDeviceObjects();
                return(result);
            }
            finally
            {
                _resetRecursionLock = false;
            }
        }
        private unsafe IntPtr PresentHook(IntPtr swapChainPtr, int syncInterval, PresentFlags flags)
        {
            var swapChain = new SwapChain(swapChainPtr);
            var device    = swapChain.GetDevice <Device>();

            if (!_initialized)
            {
                _windowHandle = swapChain.Description.OutputHandle;
                ImGui.ImGuiImplDX11Init((void *)device.NativePointer, (void *)device.ImmediateContext.NativePointer);

                var backBuffer = swapChain.GetBackBuffer <Texture2D>(0);
                _renderTargetView = new RenderTargetView(device, backBuffer);
                _initialized      = true;
                backBuffer.Dispose();
            }


            ImGui.ImGuiImplDX11NewFrame();
            ImguiHook.NewFrame();
            device.ImmediateContext.OutputMerger.SetRenderTargets(_renderTargetView);
            ImGui.ImGuiImplDX11RenderDrawData(ImGui.GetDrawData());

            swapChain.Dispose();
            device.Dispose();
            return(_presentHook.OriginalFunction(swapChainPtr, syncInterval, flags));
        }
 public ImguiHookDX11(ImguiHook hook)
 {
     ImguiHook          = hook;
     Hook               = new DX11Hook(SDK.Hooks);
     _presentHook       = Hook.DXGIVTable.CreateFunctionHook <DX11Hook.Present>((int)IDXGISwapChain.Present, PresentHook).Activate();
     _resizeBuffersHook = Hook.DXGIVTable.CreateFunctionHook <DX11Hook.ResizeBuffers>((int)IDXGISwapChain.ResizeBuffers, ResizeBuffers).Activate();
 }
        private unsafe IntPtr EndScene(IntPtr device)
        {
            if (!_initialized)
            {
                // Try our best to initialize if not hooked at boot.
                // This can fail though if window handle is only passed in presentation parameters.
                if (_windowHandle == IntPtr.Zero)
                {
                    var dev = new Device(device);
                    _windowHandle = dev.CreationParameters.HFocusWindow;
                    dev.Dispose();
                }

                Misc.Debug.WriteLine($"EndScene Window Handle {(long)_windowHandle:X}");
                if (_windowHandle == IntPtr.Zero)
                {
                    return(_endSceneHook.OriginalFunction(device));
                }

                ImGui.ImGuiImplDX9Init((void *)device);
                _initialized = true;
            }

            ImGui.ImGuiImplDX9NewFrame();
            ImguiHook.NewFrame();
            ImGui.ImGuiImplDX9RenderDrawData(ImGui.GetDrawData());

            return(_endSceneHook.OriginalFunction(device));
        }
 public ImguiHookDX9(ImguiHook hook)
 {
     ImguiHook         = hook;
     Hook              = new DX9Hook(SDK.Hooks);
     _endSceneHook     = Hook.DeviceVTable.CreateFunctionHook <DX9Hook.EndScene>((int)DirectX.Definitions.IDirect3DDevice9.EndScene, EndScene).Activate();
     _resetHook        = Hook.DeviceVTable.CreateFunctionHook <DX9Hook.Reset>((int)DirectX.Definitions.IDirect3DDevice9.Reset, Reset).Activate();
     _createDeviceHook = Hook.Direct3D9VTable.CreateFunctionHook <DX9Hook.CreateDevice>((int)IDirect3D9.CreateDevice, CreateDevice).Activate();
 }
示例#7
0
 private void Shutdown()
 {
     Debug.WriteLine($"[DX9 Shutdown] Shutdown");
     ImGui.ImGuiImplDX9Shutdown();
     _windowHandle = IntPtr.Zero;
     _device       = IntPtr.Zero;
     _initialized  = false;
     ImguiHook.Shutdown();
 }
        /// <summary>
        /// Entry point for your mod.
        /// </summary>
        public async void Start(IModLoaderV1 loader)
        {
            _modLoader = (IModLoader)loader;
            _logger    = (ILogger)_modLoader.GetLogger();
            _modLoader.GetController <IReloadedHooks>().TryGetTarget(out _hooks);

            /* Your mod code starts here. */
            SDK.Init(_hooks);
            _imguiHook = await ImguiHook.Create(RenderTestWindow);
        }
示例#9
0
        private unsafe IntPtr EndSceneImpl(IntPtr device)
        {
            // With multi-viewports, ImGui might call EndScene again; so we need to prevent stack overflow here.
            if (_endSceneRecursionLock)
            {
                Debug.WriteLine($"[DX9 EndScene] Discarding via Recursion Lock");
                return(_endSceneHook.OriginalFunction.Value.Invoke(device));
            }

            _endSceneRecursionLock = true;
            try
            {
                var dev = new Device(device);
                using var swapChain = dev.GetSwapChain(0);
                var windowHandle    = dev.CreationParameters.HFocusWindow;
                var swapChainHandle = swapChain.PresentParameters.DeviceWindowHandle;
                windowHandle = windowHandle == IntPtr.Zero ? swapChainHandle : windowHandle;

                // Ignore windows which don't belong to us.
                if (!ImguiHook.CheckWindowHandle(windowHandle))
                {
                    Debug.WriteLine($"[DX9 EndScene] Discarding Window Handle {(long)windowHandle:X}");
                    return(_endSceneHook.OriginalFunction.Value.Invoke(device));
                }

                if (!_initialized)
                {
                    _device       = device;
                    _windowHandle = windowHandle;
                    if (_windowHandle == IntPtr.Zero)
                    {
                        return(_endSceneHook.OriginalFunction.Value.Invoke(device));
                    }

                    Debug.WriteLine($"[DX9 EndScene] Init, Window Handle {(long)windowHandle:X}");
                    ImguiHook.InitializeWithHandle(windowHandle);
                    ImGui.ImGuiImplDX9Init((void *)device);
                    _initialized = true;
                }

                ImGui.ImGuiImplDX9NewFrame();
                ImguiHook.NewFrame();
                using var drawData = ImGui.GetDrawData();
                ImGui.ImGuiImplDX9RenderDrawData(drawData);
                return(_endSceneHook.OriginalFunction.Value.Invoke(device));
            }
            finally
            {
                _endSceneRecursionLock = false;
            }
        }
        private unsafe IntPtr PresentImpl(IntPtr swapChainPtr, int syncInterval, PresentFlags flags)
        {
            if (_presentRecursionLock)
            {
                Debug.WriteLine($"[DX11 Present] Discarding via Recursion Lock");
                return(_presentHook.OriginalFunction.Value.Invoke(swapChainPtr, syncInterval, flags));
            }

            _presentRecursionLock = true;
            try
            {
                var swapChain    = new SwapChain(swapChainPtr);
                var windowHandle = swapChain.Description.OutputHandle;

                // Ignore windows which don't belong to us.
                if (!ImguiHook.CheckWindowHandle(windowHandle))
                {
                    Debug.WriteLine($"[DX11 Present] Discarding Window Handle {windowHandle} due to Mismatch");
                    return(_presentHook.OriginalFunction.Value.Invoke(swapChainPtr, syncInterval, flags));
                }

                // Initialise
                using var device = swapChain.GetDevice <Device>();
                if (!_initialized)
                {
                    Debug.WriteLine($"[DX11 Present] Init DX11, Window Handle: {windowHandle:X}");
                    ImguiHook.InitializeWithHandle(windowHandle);
                    ImGui.ImGuiImplDX11Init((void *)device.NativePointer, (void *)device.ImmediateContext.NativePointer);

                    using var backBuffer = swapChain.GetBackBuffer <Texture2D>(0);
                    _renderTargetView    = new RenderTargetView(device, backBuffer);
                    _initialized         = true;
                }

                ImGui.ImGuiImplDX11NewFrame();
                ImguiHook.NewFrame();
                device.ImmediateContext.OutputMerger.SetRenderTargets(_renderTargetView);
                using var drawData = ImGui.GetDrawData();
                ImGui.ImGuiImplDX11RenderDrawData(drawData);

                return(_presentHook.OriginalFunction.Value.Invoke(swapChainPtr, syncInterval, flags));
            }
            finally
            {
                _presentRecursionLock = false;
            }
        }
示例#11
0
        /// <summary>
        /// Entry point for your mod.
        /// </summary>
        public async void Start(IModLoaderV1 loader)
        {
            _modLoader = (IModLoader)loader;
            _logger    = (ILogger)_modLoader.GetLogger();
            _modLoader.GetController <IReloadedHooks>().TryGetTarget(out _hooks);

            /* Your mod code starts here. */
            SDK.Init(_hooks, s => { _logger.WriteLine(s); });
            await ImguiHook.Create(RenderTestWindow, new ImguiHookOptions()
            {
                EnableViewports        = true,
                IgnoreWindowUnactivate = true,
                Implementations        = new List <IImguiHook>()
                {
                    new ImguiHookDx9(),
                    new ImguiHookDx11()
                }
            }).ConfigureAwait(false);
        }
示例#12
0
 public void Unload() => ImguiHook.Destroy();
示例#13
0
 public void Resume() => ImguiHook.Enable();
示例#14
0
 /* Mod loader actions. */
 public void Suspend() => ImguiHook.Disable();