Esempio n. 1
0
        /// <summary>
        /// Creates child process.
        /// </summary>
        /// <param name="panelHandle">
        /// The panel handle.
        /// </param>
        private void CreateGameProcess(IntPtr panelHandle)
        {
            IsLoading = true;

            Task.Factory.StartNew(
                () =>
            {
                // Get all instances of SSFIV.exe running on the local
                // computer.
                Process[] sfivInstances = Process.GetProcessesByName("SSFIV");

                if (sfivInstances.Length > 0)
                {
                    sfivInstances[0].Kill();
                    sfivInstances[0].WaitForExit(10000);
                }

                Process.Start("steam://rungameid/45760");
                int timeout   = 10000;
                bool gameOpen = false;
                while (!gameOpen && timeout > 0)
                {
                    // Get all instances of SSFIV.exe running on the local
                    // computer.
                    if (Process.GetProcessesByName("SSFIV").Length > 0)
                    {
                        _gameProcess = Process.GetProcessesByName("SSFIV")[0];
                        gameOpen     = true;
                    }

                    timeout--;
                }

                _gameProcess.EnableRaisingEvents = true;
                _gameProcess.Exited += GameProcessExited;

                lock (this)
                {
                    uint _oldErrorMode =
                        NativeModel.SetErrorMode(
                            NativeModel.SEM_FAILCRITICALERRORS | NativeModel.SEM_NOGPFAULTERRORBOX);

                    try
                    {
                        _gameProcess.WaitForInputIdle();
                    }
                    catch (InvalidOperationException invEx)
                    {
                        // Expected, handled
                        Debug.WriteLine(invEx.Message);
                    }
                    catch (Exception ex)
                    {
                        Execute.OnUIThread(
                            () =>
                            MessageBox.Show(
                                Application.Current.MainWindow,
                                string.Format("The game coulnd't start: {0} !", ex.Message),
                                "Error",
                                MessageBoxButton.OK,
                                MessageBoxImage.Error));
                    }

                    // For correct response, it's important to let sleep our thread for a while.
                    Thread.Sleep(50);

                    while (_gameProcessMainWindowHandle == IntPtr.Zero)
                    {
                        Thread.Sleep(100);
                        _gameProcessMainWindowHandle = _gameProcess.MainWindowHandle;
                        _gameProcess.Refresh();
                    }

                    _sf4Memory.runScan();

                    int dwStyle = NativeModel.GetWindowLong(_gameProcessMainWindowHandle, NativeModel.GWL_STYLE);
                    NativeModel.SetWindowLong(
                        _gameProcessMainWindowHandle,
                        NativeModel.GWL_STYLE,
                        new IntPtr(dwStyle & ~NativeModel.WS_CAPTION & ~NativeModel.WS_THICKFRAME));

                    NativeModel.SetWindowPos(
                        _gameProcessMainWindowHandle,
                        IntPtr.Zero,
                        0,
                        0,
                        Convert.ToInt32(Math.Floor((double)_panel.Width)),
                        Convert.ToInt32(Math.Floor((double)_panel.Height)),
                        NativeModel.SWP_ASYNCWINDOWPOS);

                    _panel.Invoke(
                        new MethodInvoker(
                            delegate { NativeModel.SetParent(_gameProcessMainWindowHandle, _panel.Handle); }));
                }

                Execute.OnUIThread(
                    () =>
                {
                    NotifyOfPropertyChange(() => IsStopped);
                    EnableWindow();
                });

                PanelResize(this, null);
            }).ContinueWith((t) => Execute.OnUIThread(() => { IsLoading = false; }));
        }