示例#1
0
        private async void SteamCMDAuthenticate_Click(object sender, RoutedEventArgs e)
        {
            var cursor = this.Cursor;

            try
            {
                if (string.IsNullOrWhiteSpace(Config.Default.SteamCmd_Username))
                {
                    MessageBox.Show("A steam username has not be entered.", "SteamCMD Authentication Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                var steamCmdFile = SteamCmdUpdater.GetSteamCmdFile(Config.Default.DataPath);
                if (string.IsNullOrWhiteSpace(steamCmdFile) || !File.Exists(steamCmdFile))
                {
                    MessageBox.Show("Could not locate the SteamCMD executable. Try reinstalling SteamCMD.", "SteamCMD Authentication Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                Application.Current.Dispatcher.Invoke(() => this.Cursor = System.Windows.Input.Cursors.Wait);
                await Task.Delay(500);

                var steamCmdArgs     = SteamUtils.BuildSteamCmdArguments(CommonConfig.Default.SteamCmdRemoveQuit, CommonConfig.Default.SteamCmdAuthenticateArgs, Config.Default.SteamCmd_Username, Config.Default.SteamCmd_Password);
                var workingDirectory = Config.Default.DataPath;

                var result = await ProcessUtils.RunProcessAsync(steamCmdFile, steamCmdArgs, string.Empty, workingDirectory, null, null, null, CancellationToken.None);

                if (result)
                {
                    MessageBox.Show("The authentication was completed.", "SteamCMD Authentication", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("An error occurred while trying to authenticate with steam. Please try again.", "SteamCMD Authentication Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SteamCMD Authentication Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                Application.Current.Dispatcher.Invoke(() => this.Cursor = cursor);
            }
        }
        private async Task InstallSteamCmdAsync(string dataPath, IProgress <Update> reporter, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(dataPath))
            {
                return;
            }

            string steamCmdDirectory = Path.Combine(dataPath, CommonConfig.Default.SteamCmdRelativePath);

            if (!Directory.Exists(steamCmdDirectory))
            {
                Directory.CreateDirectory(steamCmdDirectory);
            }

            reporter?.Report(statuses[Status.DownloadingSteamCmd]);

            // Get SteamCmd.exe if necessary
            string steamCmdPath = Path.Combine(steamCmdDirectory, CommonConfig.Default.SteamCmdExeFile);

            if (!File.Exists(steamCmdPath))
            {
                // download the SteamCMD zip file
                var steamZipPath = Path.Combine(steamCmdDirectory, CommonConfig.Default.SteamCmdZipFile);
                using (var webClient = new WebClient())
                {
                    using (var cancelRegistration = cancellationToken.Register(webClient.CancelAsync))
                    {
                        await webClient.DownloadFileTaskAsync(CommonConfig.Default.SteamCmdUrl, steamZipPath);
                    }
                }

                // Unzip the downloaded file
                reporter?.Report(statuses[Status.UnzippingSteamCmd]);

                ZipFile.ExtractToDirectory(steamZipPath, steamCmdDirectory);
                File.Delete(steamZipPath);

                // Run the SteamCmd updater
                reporter?.Report(statuses[Status.RunningSteamCmd]);

                var arguments = SteamUtils.BuildSteamCmdArguments(false, CommonConfig.Default.SteamCmdInstallArgs);
                ProcessStartInfo startInfo = new ProcessStartInfo()
                {
                    FileName        = steamCmdPath,
                    Arguments       = arguments,
                    UseShellExecute = false,
                };

                var process = Process.Start(startInfo);
                process.EnableRaisingEvents = true;

                var ts = new TaskCompletionSource <bool>();
                using (var cancelRegistration = cancellationToken.Register(() =>
                {
                    try
                    {
                        process.Kill();
                    }
                    finally
                    {
                        ts.TrySetCanceled();
                    }
                }))
                {
                    process.Exited += (s, e) =>
                    {
                        ts.TrySetResult(process.ExitCode == 0);
                    };
                    await ts.Task;
                }
            }

            return;
        }