예제 #1
0
 static CryptoFunctionsHookLib()
 {
     if (!Injection.PROFILING_ENABLED)
     {
         hooks_.Add(new CreateFile_Delegate(CreateFile_Hooked));
         hooks_.Add(new ReadFile_Delegate(ReadFile_Hooked));
     }
 }
예제 #2
0
 static FileAccessFunctionsHookLib()
 {
     if (!Injection.PROFILING_ENABLED)
     {
         hooks_.Add(new NtCreateFile_Delegate(NtCreateFile_Hooked));
     }
 }
예제 #3
0
        private void AddHook(string libName, string entryPoint, Delegate newProcedure)
        {
            try
            {
                var localHook = LocalHook.Create(LocalHook.GetProcAddress(libName, entryPoint), newProcedure, null);

                // Exclude current thread (EasyHook)
                localHook.ThreadACL.SetExclusiveACL(new[] { 0 });

                lock (Hooks)
                {
                    Hooks.Add(localHook);
                }

                DebugMessage(
                    nameof(AddHook),
                    "`{0}` @ `{1}` hooked successfully.",
                    entryPoint,
                    libName
                    );
            }
            catch (Exception e)
            {
                DebugMessage(nameof(AddHook), e.ToString());
                DebugMessage(
                    nameof(AddHook),
                    "Failed to hook `{0}` @ `{1}`.",
                    entryPoint,
                    libName
                    );
            }
        }
예제 #4
0
        public override void Hook()
        {
            this.DebugMessage("Hook: Begin");
            if (_d3d11VTblAddresses == null)
            {
                _d3d11VTblAddresses         = new List <IntPtr>();
                _dxgiSwapChainVTblAddresses = new List <IntPtr>();

                #region Get Device and SwapChain method addresses
                // Create temporary device + swapchain and determine method addresses
                _renderForm = ToDispose(new SharpDX.Windows.RenderForm());
                this.DebugMessage("Hook: Before device creation");
                SharpDX.Direct3D11.Device.CreateWithSwapChain(
                    DriverType.Hardware,
                    DeviceCreationFlags.BgraSupport,
                    DXGI.CreateSwapChainDescription(_renderForm.Handle),
                    out _device,
                    out _swapChain);

                ToDispose(_device);
                ToDispose(_swapChain);

                if (_device != null && _swapChain != null)
                {
                    this.DebugMessage("Hook: Device created");
                    _d3d11VTblAddresses.AddRange(GetVTblAddresses(_device.NativePointer, D3D11_DEVICE_METHOD_COUNT));
                    _dxgiSwapChainVTblAddresses.AddRange(GetVTblAddresses(_swapChain.NativePointer, DXGI.DXGI_SWAPCHAIN_METHOD_COUNT));
                }
                else
                {
                    this.DebugMessage("Hook: Device creation failed");
                }
                #endregion
            }

            // We will capture the backbuffer here
            DXGISwapChain_PresentHook = new Hook <DXGISwapChain_PresentDelegate>(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.Present],
                new DXGISwapChain_PresentDelegate(PresentHook),
                this);

            // We will capture target/window resizes here
            DXGISwapChain_ResizeTargetHook = new Hook <DXGISwapChain_ResizeTargetDelegate>(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.ResizeTarget],
                new DXGISwapChain_ResizeTargetDelegate(ResizeTargetHook),
                this);

            /*
             * Don't forget that all hooks will start deactivated...
             * The following ensures that all threads are intercepted:
             * Note: you must do this for each hook.
             */
            DXGISwapChain_PresentHook.Activate();

            DXGISwapChain_ResizeTargetHook.Activate();

            Hooks.Add(DXGISwapChain_PresentHook);
            Hooks.Add(DXGISwapChain_ResizeTargetHook);
        }
예제 #5
0
        public override void Hook()
        {
            DebugMessage("Hook: Begin");

            // Determine method addresses in Direct3D10.Device, and DXGI.SwapChain
            if (_d3d10_1VTblAddresses == null)
            {
                _d3d10_1VTblAddresses       = new List <IntPtr>();
                _dxgiSwapChainVTblAddresses = new List <IntPtr>();
                DebugMessage("Hook: Before device creation");
                using (var factory = new Factory1())
                {
                    using (
                        var device = new Device1(factory.GetAdapter(0), DeviceCreationFlags.None,
                                                 FeatureLevel.Level_10_1))
                    {
                        DebugMessage("Hook: Device created");
                        _d3d10_1VTblAddresses.AddRange(GetVTblAddresses(device.NativePointer,
                                                                        D3D10_1_DEVICE_METHOD_COUNT));

                        using (var renderForm = new RenderForm())
                        {
                            using (
                                var sc = new SwapChain(factory, device,
                                                       DXGI.CreateSwapChainDescription(renderForm.Handle)))
                            {
                                _dxgiSwapChainVTblAddresses.AddRange(GetVTblAddresses(sc.NativePointer,
                                                                                      DXGI.DXGI_SWAPCHAIN_METHOD_COUNT));
                            }
                        }
                    }
                }
            }

            // We will capture the backbuffer here
            DXGISwapChain_PresentHook = new Hook <DXGISwapChain_PresentDelegate>(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.Present],
                new DXGISwapChain_PresentDelegate(PresentHook),
                this);

            // We will capture target/window resizes here
            DXGISwapChain_ResizeTargetHook = new Hook <DXGISwapChain_ResizeTargetDelegate>(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.ResizeTarget],
                new DXGISwapChain_ResizeTargetDelegate(ResizeTargetHook),
                this);

            /*
             * Don't forget that all hooks will start deactivated...
             * The following ensures that all threads are intercepted:
             * Note: you must do this for each hook.
             */
            DXGISwapChain_PresentHook.Activate();

            DXGISwapChain_ResizeTargetHook.Activate();

            Hooks.Add(DXGISwapChain_PresentHook);
            Hooks.Add(DXGISwapChain_ResizeTargetHook);
        }
예제 #6
0
        public void Init()
        {
            var appData = Environment.GetFolderPath(
                Environment.SpecialFolder.LocalApplicationData
                );

            StateBasePath = Path.Combine(appData, "smuxi");
            StateBasePath = Path.Combine(StateBasePath, "hook-state");
            foreach (var path in PathElements)
            {
                StateBasePath = Path.Combine(StateBasePath, path);
            }

            var hookPath = Path.Combine(appData, "smuxi");

            hookPath = Path.Combine(hookPath, "hooks");
            foreach (var path in PathElements)
            {
                hookPath = Path.Combine(hookPath, path);
            }
            if (!Directory.Exists(hookPath))
            {
                return;
            }
            foreach (var file in Directory.GetFiles(hookPath).OrderBy(x => x))
            {
                try {
                    File.OpenRead(file).Close();
                } catch (Exception ex) {
#if LOG4NET
                    Logger.Error("Init(): error opening " + file, ex);
#endif
                    continue;
                }
                Hooks.Add(file);
            }


            if (!HasHooks)
            {
                return;
            }

            var env = EnvironmentVariables;
            if (Engine.Version != null)
            {
                env.Add("ENGINE_VERSION", Engine.Version.ToString());
            }

            foreach (var environment in Environments)
            {
                foreach (var entry in environment)
                {
                    env.Add(entry.Key, entry.Value);
                }
            }
        }
예제 #7
0
        public override void Hook()
        {
            this.DebugMessage("Hook: Begin");
            // First we need to determine the function address for IDirect3DDevice9
            Device device;

            id3dSwapChainFunctionAddresses = new List <IntPtr>();
            id3dSurfaceFunctionAddresses   = new List <IntPtr>();
            this.DebugMessage("Hook: Before device creation");
            using (Direct3D d3d = new Direct3D())
            {
                using (var renderForm = new System.Windows.Forms.Form())
                {
                    using (device = new Device(d3d, 0, SharpDX.Direct3D9.DeviceType.Hardware, renderForm.Handle, CreateFlags.HardwareVertexProcessing, new SharpDX.Direct3D9.PresentParameters(1, 1)))
                    {
                        this.DebugMessage("Hook: Device created");

                        using (Surface surface = Surface.CreateOffscreenPlain(device, 1, 1, Format.A8R8G8B8, Pool.SystemMemory))
                        {
                            this.DebugMessage("Hook: Surface created");
                            id3dSurfaceFunctionAddresses.AddRange(GetVTblAddresses(surface.NativePointer, D3D9_SURFACE_METHOD_COUNT));
                        }

                        using (SwapChain swapChain = device.GetSwapChain(0))
                        {
                            this.DebugMessage("Hook: SwapChain created");
                            id3dSwapChainFunctionAddresses.AddRange(GetVTblAddresses(swapChain.NativePointer, D3D9_SWAP_CHAIN_METHOD_COUNT));
                        }
                    }
                }
            }

            Direct3D9Surface_FreePrivateDataHook = new Hook <Direct3D9Surface_FreePrivateDataDelegate>(
                id3dSurfaceFunctionAddresses[(int)Direct3DSurface9FunctionOrdinals.FreePrivateData],
                new Direct3D9Surface_FreePrivateDataDelegate(SurfaceFreePrivateDataHook),
                this);

            Direct3D9SwapChain_PresentHook = new Hook <Direct3D9SwapChain_PresentDelegate>(
                id3dSwapChainFunctionAddresses[(int)DirectXSwapChainFunctionOrdinals.Present],
                new Direct3D9SwapChain_PresentDelegate(SwapChainPresentHook),
                this);


            /*
             * Don't forget that all hooks will start deactivated...
             * The following ensures that all threads are intercepted:
             * Note: you must do this for each hook.
             */

            Direct3D9Surface_FreePrivateDataHook.Activate();
            Hooks.Add(Direct3D9Surface_FreePrivateDataHook);

            Direct3D9SwapChain_PresentHook.Activate();
            Hooks.Add(Direct3D9SwapChain_PresentHook);

            this.DebugMessage("Hook: End");
        }
 public void BeginListening()
 {
     lock (Hooks) {
         foreach (var eachEvent in Source.GetType().GetEvents())
         {
             Hooks.Add(new EventHook(Source, eachEvent, InterceptEvent));
         }
     }
 }
예제 #9
0
        public Hooker <T> CreateHooker()
        {
            Hooker <T> hooker = new Hooker <T>(AvailablePort);

            hooker.OnProcessExited  += Hooker_OnProcessExited;
            hooker.OnProcessStarted += Hooker_OnProcessStarted;

            Hooks.Add(hooker.ProxyPort, hooker);

            return(hooker);
        }
예제 #10
0
        public override void Hook()
        {
            this.DebugMessage("Hook: Begin");

            // Determine method addresses in Direct3D10.Device, and DXGI.SwapChain
            if (_d3d10VTblAddresses == null)
            {
                _d3d10VTblAddresses         = new List <IntPtr>();
                _dxgiSwapChainVTblAddresses = new List <IntPtr>();
                this.DebugMessage("Hook: Before device creation");
                using (var factory = new Factory(IntPtr.Zero)) //???????????????????????????
                {
                    using (var device = new Device(factory.GetAdapter(0), DeviceCreationFlags.None))
                    {
                        this.DebugMessage("Hook: Device created");
                        _d3d10VTblAddresses.AddRange(GetVTblAddresses(device.NativePointer, D3D10_DEVICE_METHOD_COUNT));

                        using (var renderForm = new System.Windows.Forms.Form())
                        {
                            using (SharpDX.DXGI.SwapChain sc = new SharpDX.DXGI.SwapChain(factory, device, DXGI.CreateSwapChainDescription(renderForm.Handle)))
                            {
                                _dxgiSwapChainVTblAddresses.AddRange(GetVTblAddresses(sc.NativePointer, DXGI.DXGI_SWAPCHAIN_METHOD_COUNT));
                            }
                        }
                    }
                }
            }

            // We will capture the backbuffer here
            DXGISwapChain_PresentHook = LocalHook.Create(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.Present],
                new DXGISwapChain_PresentDelegate(PresentHook),
                this);

            // We will capture target/window resizes here
            DXGISwapChain_ResizeTargetHook = LocalHook.Create(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.ResizeTarget],
                new DXGISwapChain_ResizeTargetDelegate(ResizeTargetHook),
                this);

            /*
             * Don't forget that all hooks will start deactivated...
             * The following ensures that all threads are intercepted:
             * Note: you must do this for each hook.
             */
            DXGISwapChain_PresentHook.ThreadACL.SetExclusiveACL(new Int32[1]);

            DXGISwapChain_ResizeTargetHook.ThreadACL.SetExclusiveACL(new Int32[1]);

            Hooks.Add(DXGISwapChain_PresentHook);
            Hooks.Add(DXGISwapChain_ResizeTargetHook);
        }
예제 #11
0
        public static void CreateHook(HookPair source, bool copyHook = true, float minRopeLength = MIN_MIN_ROPE_LENGTH, float customRopeLength = 0.0f)
        {
            if (!CheckHookPermission(source))
            {
                return;
            }

            HookPair resultHook = CreateEntityHook(source, copyHook, HookPedsAtBonesCoords, minRopeLength, customRopeLength);

            if (resultHook != null)
            {
                Hooks.Add(resultHook);
            }
        }
예제 #12
0
        public void Run(RemoteHooking.IContext context)
        {
            try
            {
                var  args        = Environment.GetCommandLineArgs();
                bool enableDebug = false;
                foreach (var arg in args)
                {
                    if (arg == "--launch-debugger")
                    {
                        Debugger.Launch();
                    }
                    if (arg == "-d" || arg == "--enable-debug")
                    {
                        Debugger.Launch();
                    }
                }
                var kernel           = GetKernelModule().BaseAddress;
                var loadLibraryAFunc = Kernel32.GetProcAddress(kernel, "LoadLibraryA");
                var hook             = LocalHook.Create(loadLibraryAFunc, new LoadLibraryADelegate(LoadLibraryHook), null);
                hook.ThreadACL.SetExclusiveACL(Array.Empty <int>());
                Hooks.Add("LoadLibraryA", hook);

                if (Is64Bit)
                {
                    Kernel32.LoadLibrary("EngineWin64s.dll");
                }
                else
                {
                    Kernel32.LoadLibrary("EngineWin32s.dll");
                }
                foreach (ProcessModule module in Process.GetCurrentProcess().Modules)
                {
                    Console.WriteLine($"Module: {module.ModuleName}");
                }
                module   = GetEngineModule();
                resolver = new DiaSymbolResolver(module);
#if DEBUG
                LogEngineSymbols(resolver);
#endif
                PdbSymbolImporter.ImportSymbols(resolver);
                LuaHelper.InitHelper(resolver);

                Hook <InitLuaDelegate>("?InitLua@ScriptManager@sgg@@SAXXZ", InitLua);
                Hook <ScriptManagerUpdateDelegate>("?Update@ScriptManager@sgg@@SAXAEBM@Z", ScriptManagerUpdate);

                scriptManager = new ScriptManager(resolver, enableDebug);
                Console.WriteLine($"Created ScriptManager");
                RemoteHooking.WakeUpProcess();

                while (true)
                {
                    var code = Console.ReadLine();
                    Console.WriteLine("> {0}", code);
                    scriptManager.Eval(code);
                    Thread.Sleep(500);
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                throw;
            }
        }
예제 #13
0
 /// <summary>
 /// Registers the hook in the context.
 /// </summary>
 /// <param name="hook">The hook to register.</param>
 internal void RegisterHook(HookInstance hook) => Hooks.Add(hook);
예제 #14
0
        public void Merge(Directives other)
        {
            if (Ignore != null && other.Ignore != null)
            {
                Ignore.Merge(other.Ignore);
            }
            else if (other.Ignore != null)
            {
                Ignore = other.Ignore;
            }

            if (other.UseTortoiseMerge != null)
            {
                UseTortoiseMerge = other.UseTortoiseMerge;
            }

            if (Include != null && other.Include != null)
            {
                Include.Merge(other.Include);
            }
            else if (other.Include != null)
            {
                Include = other.Include;
            }

            if (other.DefaultCompression != null)
            {
                DefaultCompression = other.DefaultCompression;
            }
            if (other.ExternalDiff != null)
            {
                ExternalDiff = other.ExternalDiff;
            }
            if (other.ExternalMerge != null)
            {
                ExternalMerge = other.ExternalMerge;
            }
            if (other.ExternalMerge2Way != null)
            {
                ExternalMerge2Way = other.ExternalMerge2Way;
            }
            if (other.NonBlockingDiff != null)
            {
                NonBlockingDiff = other.NonBlockingDiff;
            }
            if (other.m_UserName != null)
            {
                m_UserName = other.m_UserName;
            }
            if (!string.IsNullOrEmpty(other.ObjectStorePath))
            {
                ObjectStorePath = other.ObjectStorePath;
            }

            if (other.Externals != null)
            {
                if (Externals == null)
                {
                    Externals = new Dictionary <string, Extern>();
                }
                foreach (var x in other.Externals)
                {
                    Externals[x.Key] = x.Value;
                }
            }

            foreach (var x in other.Tokens)
            {
                Newtonsoft.Json.Linq.JToken prior;
                if (Tokens.TryGetValue(x.Key, out prior))
                {
                    if (prior.Type == Newtonsoft.Json.Linq.JTokenType.Array && x.Value.Type == Newtonsoft.Json.Linq.JTokenType.Array)
                    {
                        Newtonsoft.Json.Linq.JArray array = prior as Newtonsoft.Json.Linq.JArray;
                        Newtonsoft.Json.Linq.JArray merge = x.Value as Newtonsoft.Json.Linq.JArray;
                        foreach (var y in merge)
                        {
                            array.Add(y);
                        }
                        continue;
                    }
                }
                Tokens[x.Key] = x.Value;
            }

            if (other.Hooks != null)
            {
                if (Hooks != null)
                {
                    foreach (var x in other.Hooks)
                    {
                        Hooks.Add(x);
                    }
                }
                else
                {
                    Hooks = other.Hooks;
                }
            }

            if (other.Sparse.Count > 0)
            {
                Sparse = Sparse.Concat(other.Sparse).ToList();
            }
            if (other.Excludes.Count > 0)
            {
                Excludes = Excludes.Concat(other.Excludes).ToList();
            }

            if (other.TagPresets != null)
            {
                if (TagPresets == null)
                {
                    TagPresets = new List <TagPreset>();
                }
                TagPresets.AddRange(other.TagPresets);
                TagPresets = TagPresets.Distinct().ToList();
            }
        }
예제 #15
0
 public void SetHook(Hook hook)
 {
     Hooks.RemoveAll(h => h.Name == hook.Name);
     Hooks.Add(hook);
 }
예제 #16
0
        public override unsafe void Hook()
        {
            DebugMessage("Hook: Begin");

            DebugMessage("Hook: Before device creation");
            using (var d3d = new Direct3D())
            {
                DebugMessage("Hook: Direct3D created");
                using (
                    var device = new Device(
                        d3d,
                        0,
                        DeviceType.NullReference,
                        IntPtr.Zero,
                        CreateFlags.HardwareVertexProcessing,
                        new PresentParameters {
                    BackBufferWidth = 1, BackBufferHeight = 1
                }))
                {
                    _id3DDeviceFunctionAddresses.AddRange(GetVTblAddresses(device.NativePointer, D3D9_DEVICE_METHOD_COUNT));
                }
            }

            try
            {
                using (var d3dEx = new Direct3DEx())
                {
                    DebugMessage("Hook: Try Direct3DEx...");
                    using (
                        var deviceEx = new DeviceEx(
                            d3dEx,
                            0,
                            DeviceType.NullReference,
                            IntPtr.Zero,
                            CreateFlags.HardwareVertexProcessing,
                            new PresentParameters {
                        BackBufferWidth = 1, BackBufferHeight = 1
                    },
                            new DisplayModeEx {
                        Width = 800, Height = 600
                    }))
                    {
                        _id3DDeviceFunctionAddresses.AddRange(
                            GetVTblAddresses(deviceEx.NativePointer, D3D9_DEVICE_METHOD_COUNT, D3D9Ex_DEVICE_METHOD_COUNT));
                        _supportsDirect3DEx = true;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            DebugMessage("Setting up Direct3D hooks...");
            Direct3DDevice_EndSceneHook =
                new HookData <Direct3D9Device_EndSceneDelegate>(
                    _id3DDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.EndScene],
                    new Direct3D9Device_EndSceneDelegate(EndSceneHook),
                    this);

            Direct3DDevice_EndSceneHook.ReHook();
            Hooks.Add(Direct3DDevice_EndSceneHook.Hook);

            Direct3DDevice_PresentHook =
                new HookData <Direct3D9Device_PresentDelegate>(
                    _id3DDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Present],
                    new Direct3D9Device_PresentDelegate(PresentHook),
                    this);

            Direct3DDevice_ResetHook =
                new HookData <Direct3D9Device_ResetDelegate>(
                    _id3DDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Reset],
                    new Direct3D9Device_ResetDelegate(ResetHook),
                    this);

            if (_supportsDirect3DEx)
            {
                DebugMessage("Setting up Direct3DEx hooks...");
                Direct3DDeviceEx_PresentExHook =
                    new HookData <Direct3D9DeviceEx_PresentExDelegate>(
                        _id3DDeviceFunctionAddresses[(int)Direct3DDevice9ExFunctionOrdinals.PresentEx],
                        new Direct3D9DeviceEx_PresentExDelegate(PresentExHook),
                        this);

                Direct3DDeviceEx_ResetExHook =
                    new HookData <Direct3D9DeviceEx_ResetExDelegate>(
                        _id3DDeviceFunctionAddresses[(int)Direct3DDevice9ExFunctionOrdinals.ResetEx],
                        new Direct3D9DeviceEx_ResetExDelegate(ResetExHook),
                        this);
            }

            Direct3DDevice_ResetHook.ReHook();
            Hooks.Add(Direct3DDevice_ResetHook.Hook);

            Direct3DDevice_PresentHook.ReHook();
            Hooks.Add(Direct3DDevice_PresentHook.Hook);

            if (_supportsDirect3DEx)
            {
                Direct3DDeviceEx_PresentExHook.ReHook();
                Hooks.Add(Direct3DDeviceEx_PresentExHook.Hook);

                Direct3DDeviceEx_ResetExHook.ReHook();
                Hooks.Add(Direct3DDeviceEx_ResetExHook.Hook);
            }

            DebugMessage("Hook: End");
        }
예제 #17
0
 public void RegisterHook(IHook hook)
 {
     Hooks.Add(hook);
     hook.OnHooked();
 }
예제 #18
0
        public override void Hook()
        {
            this.DebugMessage("Hook: Begin");
            // First we need to determine the function address for IDirect3DDevice9
            Device device;

            id3dDeviceFunctionAddresses = new List <IntPtr>();
            //id3dDeviceExFunctionAddresses = new List<IntPtr>();
            this.DebugMessage("Hook: Before device creation");
            using (Direct3D d3d = new Direct3D())
            {
                using (var renderForm = new System.Windows.Forms.Form())
                {
                    using (device = new Device(d3d, 0, DeviceType.NullReference, IntPtr.Zero, CreateFlags.HardwareVertexProcessing, new PresentParameters()
                    {
                        BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = renderForm.Handle
                    }))
                    {
                        this.DebugMessage("Hook: Device created");
                        id3dDeviceFunctionAddresses.AddRange(GetVTblAddresses(device.NativePointer, D3D9_DEVICE_METHOD_COUNT));
                    }
                }
            }

            try
            {
                using (Direct3DEx d3dEx = new Direct3DEx())
                {
                    this.DebugMessage("Hook: Direct3DEx...");
                    using (var renderForm = new System.Windows.Forms.Form())
                    {
                        using (var deviceEx = new DeviceEx(d3dEx, 0, DeviceType.NullReference, IntPtr.Zero, CreateFlags.HardwareVertexProcessing, new PresentParameters()
                        {
                            BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = renderForm.Handle
                        }, new DisplayModeEx()
                        {
                            Width = 800, Height = 600
                        }))
                        {
                            this.DebugMessage("Hook: DeviceEx created - PresentEx supported");
                            id3dDeviceFunctionAddresses.AddRange(GetVTblAddresses(deviceEx.NativePointer, D3D9_DEVICE_METHOD_COUNT, D3D9Ex_DEVICE_METHOD_COUNT));
                            _supportsDirect3D9Ex = true;
                        }
                    }
                }
            }
            catch (Exception)
            {
                _supportsDirect3D9Ex = false;
            }

            // We want to hook each method of the IDirect3DDevice9 interface that we are interested in

            // 42 - EndScene (we will retrieve the back buffer here)
            Direct3DDevice_EndSceneHook = new Hook <Direct3D9Device_EndSceneDelegate>(
                id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.EndScene],
                // On Windows 7 64-bit w/ 32-bit app and d3d9 dll version 6.1.7600.16385, the address is equiv to:
                // (IntPtr)(GetModuleHandle("d3d9").ToInt32() + 0x1ce09),
                // A 64-bit app would use 0xff18
                // Note: GetD3D9DeviceFunctionAddress will output these addresses to a log file
                new Direct3D9Device_EndSceneDelegate(EndSceneHook),
                this);

            unsafe
            {
                // If Direct3D9Ex is available - hook the PresentEx
                if (_supportsDirect3D9Ex)
                {
                    Direct3DDeviceEx_PresentExHook = new Hook <Direct3D9DeviceEx_PresentExDelegate>(
                        id3dDeviceFunctionAddresses[(int)Direct3DDevice9ExFunctionOrdinals.PresentEx],
                        new Direct3D9DeviceEx_PresentExDelegate(PresentExHook),
                        this);
                }

                // Always hook Present also (device will only call Present or PresentEx not both)
                Direct3DDevice_PresentHook = new Hook <Direct3D9Device_PresentDelegate>(
                    id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Present],
                    new Direct3D9Device_PresentDelegate(PresentHook),
                    this);
            }

            // 16 - Reset (called on resolution change or windowed/fullscreen change - we will reset some things as well)
            Direct3DDevice_ResetHook = new Hook <Direct3D9Device_ResetDelegate>(
                id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Reset],
                // On Windows 7 64-bit w/ 32-bit app and d3d9 dll version 6.1.7600.16385, the address is equiv to:
                //(IntPtr)(GetModuleHandle("d3d9").ToInt32() + 0x58dda),
                // A 64-bit app would use 0x3b3a0
                // Note: GetD3D9DeviceFunctionAddress will output these addresses to a log file
                new Direct3D9Device_ResetDelegate(ResetHook),
                this);

            /*
             * Don't forget that all hooks will start deactivated...
             * The following ensures that all threads are intercepted:
             * Note: you must do this for each hook.
             */

            Direct3DDevice_EndSceneHook.Activate();
            Hooks.Add(Direct3DDevice_EndSceneHook);

            Direct3DDevice_PresentHook.Activate();
            Hooks.Add(Direct3DDevice_PresentHook);

            if (_supportsDirect3D9Ex)
            {
                Direct3DDeviceEx_PresentExHook.Activate();
                Hooks.Add(Direct3DDeviceEx_PresentExHook);
            }

            Direct3DDevice_ResetHook.Activate();
            Hooks.Add(Direct3DDevice_ResetHook);

            this.DebugMessage("Hook: End");
        }
예제 #19
0
        public override void Hook()
        {
            CurrentProcess = Process.GetProcessById(ProcessId);
            TraceListener listen = new DebugListener(Interface);

            Trace.Listeners.Add(listen);
            Services.Tracker.Configure(Services.CompassSettings).Apply();
            DebugMessage("Settings loaded");


            DebugMessage("Hook: Begin");

            // First we need to determine the function address for IDirect3DDevice9
            id3dDeviceFunctionAddresses = new List <IntPtr>();
            using (var d3d = new Direct3D())
            {
                using (var renderForm = new Form())
                {
                    Device device;
                    using (device = new Device(d3d, 0, DeviceType.NullReference, IntPtr.Zero, CreateFlags.HardwareVertexProcessing,
                                               new PresentParameters {
                        BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = renderForm.Handle
                    }))
                    {
                        id3dDeviceFunctionAddresses.AddRange(GetVTblAddresses(device.NativePointer, D3D9_DEVICE_METHOD_COUNT));
                    }
                }
            }

            try
            {
                using (var d3dEx = new Direct3DEx())
                {
                    using (var renderForm = new Form())
                    {
                        using (var deviceEx = new DeviceEx(d3dEx, 0, DeviceType.NullReference, IntPtr.Zero, CreateFlags.HardwareVertexProcessing,
                                                           new PresentParameters {
                            BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = renderForm.Handle
                        },
                                                           new DisplayModeEx {
                            Width = 800, Height = 600
                        }))
                        {
                            id3dDeviceFunctionAddresses.AddRange(GetVTblAddresses(deviceEx.NativePointer, D3D9_DEVICE_METHOD_COUNT, D3D9Ex_DEVICE_METHOD_COUNT));
                            _supportsDirect3D9Ex = true;
                        }
                    }
                }
            }
            catch (Exception)
            {
                _supportsDirect3D9Ex = false;
            }

            // We want to hook each method of the IDirect3DDevice9 interface that we are interested in

            // 42 - EndScene (we will retrieve the back buffer here)
            Direct3DDevice_EndSceneHook = new Hook <Direct3D9Device_EndSceneDelegate>(
                id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.EndScene],
                new Direct3D9Device_EndSceneDelegate(EndSceneHook),
                this);

            unsafe
            {
                // If Direct3D9Ex is available - hook the PresentEx
                if (_supportsDirect3D9Ex)
                {
                    Direct3DDeviceEx_PresentExHook = new Hook <Direct3D9DeviceEx_PresentExDelegate>(
                        id3dDeviceFunctionAddresses[(int)Direct3DDevice9ExFunctionOrdinals.PresentEx],
                        new Direct3D9DeviceEx_PresentExDelegate(PresentExHook),
                        this);
                }

                // Always hook Present also (device will only call Present or PresentEx not both)
                Direct3DDevice_PresentHook = new Hook <Direct3D9Device_PresentDelegate>(
                    id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Present],
                    new Direct3D9Device_PresentDelegate(PresentHook),
                    this);
            }

            // 16 - Reset (called on resolution change or windowed/fullscreen change - we will reset some things as well)
            Direct3DDevice_ResetHook = new Hook <Direct3D9Device_ResetDelegate>(
                id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Reset],
                new Direct3D9Device_ResetDelegate(ResetHook),
                this);

            /*
             * Don't forget that all hooks will start deactivated...
             * The following ensures that all threads are intercepted:
             * Note: you must do this for each hook.
             */

            Direct3DDevice_EndSceneHook.Activate();
            Hooks.Add(Direct3DDevice_EndSceneHook);

            Direct3DDevice_PresentHook.Activate();
            Hooks.Add(Direct3DDevice_PresentHook);

            if (_supportsDirect3D9Ex)
            {
                Direct3DDeviceEx_PresentExHook.Activate();
                Hooks.Add(Direct3DDeviceEx_PresentExHook);
            }

            Direct3DDevice_ResetHook.Activate();
            Hooks.Add(Direct3DDevice_ResetHook);

            DebugMessage("Hook: End");
        }