Пример #1
0
        /// <summary>
        /// Asynchronously runs the patching process on the specified file.
        /// </summary>
        /// <param name="halo">The Halo installation.</param>
        private async Task PatchAsync(HaloInstallation halo)
        {
            FileInfo pakFile = halo.GetPakFilePath();

            if (!(pakFile?.Exists ?? false))
            {
                MsgBoxHelpers.Error(Resources.PakFileNotFound,
                                    callback: () => Environment.Exit(0));
                return;
            }

            Stream stream = File.Open(pakFile.FullName, FileMode.Open);

            if (halo.GetGameVersion() > new Version(1, 1270, 0, 0))
            {
                bool abort = !MsgBoxHelpers.ConfirmYesNo(
                    String.Format(Resources.UntestedVersion, halo.GetGameVersion()),
                    icon: MessageBoxIcon.Warning,
                    yes: () => true,
                    no: () => { } // internally returns default(T)
                    );

                if (abort)
                {
                    Environment.Exit(0);
                }
            }
            else
            {
                if (!this.ConfirmPatchNeeded(stream))
                {
                    MsgBoxHelpers.Info(Resources.GameAlreadyPatched, Resources.PatchSkipped, () => Environment.Exit(0));
                    return;
                }
            }

            if (this.ConfirmBackup())
            {
                // must close stream for backup to take place
                stream.Close();
                stream.Dispose();

                BackupProgressForm progressForm = new BackupProgressForm();
                progressForm.Show();
                progressForm.BringToFront();

                await FileHelpers.CreateBackupAsync(pakFile.FullName,
                                                    percentage =>
                {
                    progressForm.PercentageProgressBar.Value = (int)Math.Floor(percentage);
                    progressForm.Text =
                        String.Format(Resources.CreatingBackupPercentage, percentage);

                    Application.DoEvents();
                })
                .ConfigureAwait(false);

                // reopen stream for patching
                stream = File.Open(pakFile.FullName, FileMode.Open);
            }

            this.ApplyPatch(stream);
            stream.Close();
            stream.Dispose();

            MsgBoxHelpers.Info(Resources.PatchSuccessful, Resources.Done, () => Environment.Exit(0));
        }