private async Task DownloadPatchAsync(PatchDownload download, int index)
        {
            var outFile = GetPatchFile(download.Patch);

            Log.Information("Downloading patch {0} at {1} to {2}", download.Patch.VersionId, download.Patch.Url, outFile.FullName);

            Actives[index] = download;

            if (outFile.Exists && IsHashCheckPass(download.Patch, outFile))
            {
                download.State    = PatchState.Downloaded;
                Slots[index]      = true;
                Progresses[index] = download.Patch.Length;
                return;
            }

            var dlService = new DownloadService(_downloadOpt);

            dlService.DownloadProgressChanged += (sender, args) =>
            {
                Progresses[index] = args.BytesReceived;
                Speeds[index]     = dlService.DownloadSpeed;
            };

            dlService.DownloadFileCompleted += (sender, args) =>
            {
                if (args.Error != null)
                {
                    Log.Error(args.Error, "Download failed for {0}", download.Patch.VersionId);
                    MessageBox.Show($"Download FAILED for {download.Patch.VersionId}.\nPlease try again.\n\n" + args.Error, "XIVLauncher Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    Environment.Exit(0);
                    return;
                }

                if (args.Cancelled)
                {
                    Log.Error("Download cancelled for {0}", download.Patch.VersionId);
                    MessageBox.Show($"Download CANCELLED for {download.Patch.VersionId}.\nPlease try again.\n\n" + args.Error, "XIVLauncher Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    Environment.Exit(0);
                }

                // Let's just bail for now, need better handling of this later
                if (!IsHashCheckPass(download.Patch, outFile))
                {
                    Log.Error("HashCHeck failed for {0} after DL", download.Patch.VersionId);
                    MessageBox.Show($"IsHashCheckPass FAILED for {download.Patch.VersionId}.\nPlease try again.", "XIVLauncher Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    outFile.Delete();
                    Environment.Exit(0);
                    return;
                }

                download.State = PatchState.Downloaded;
                Slots[index]   = true;

                Log.Verbose("Patch at {0} downloaded completely", download.Patch.Url);
            };

            await dlService.DownloadFileAsync(download.Patch.Url, outFile.FullName);
        }
Esempio n. 2
0
        private async Task DownloadPatchAsync(PatchDownload download, int index)
        {
            var outFile = GetPatchFile(download.Patch);

            Log.Information("Downloading patch {0} at {1} to {2}", download.Patch.VersionId, download.Patch.Url, outFile.FullName);

            Actives[index] = download;

            if (outFile.Exists && CheckPatchValidity(download.Patch, outFile) == HashCheckResult.Pass)
            {
                download.State    = PatchState.Downloaded;
                Slots[index]      = SlotState.Done;
                Progresses[index] = download.Patch.Length;
                return;
            }

            var dlService = new DownloadService(_downloadOpt);

            dlService.DownloadProgressChanged += (sender, args) =>
            {
                Progresses[index] = args.ReceivedBytesSize;
                Speeds[index]     = args.BytesPerSecondSpeed;
            };

            dlService.DownloadFileCompleted += (sender, args) =>
            {
                if (args.Error != null)
                {
                    Log.Error(args.Error, "Download failed for {0} with reason {1}", download.Patch.VersionId, args.Error);

                    // If we cancel downloads, we don't want to see an error message
                    if (args.Error is OperationCanceledException)
                    {
                        return;
                    }

                    CancelAllDownloads();
                    CustomMessageBox.Show(string.Format(Loc.Localize("PatchManDlFailure", "XIVLauncher could not verify the downloaded game files.\n\nThis usually indicates a problem with your internet connection.\nIf this error occurs again, try using a VPN set to Japan.\n\nContext: {0}\n{1}"), "Problem", download.Patch.VersionId), "XIVLauncher Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    Environment.Exit(0);
                    return;
                }

                if (args.Cancelled)
                {
                    Log.Error("Download cancelled for {0} with reason {1}", download.Patch.VersionId, args.Error);

                    /*
                     * Cancellation should not produce an error message, since it is always triggered by another error or the user.
                     *
                     * CancelAllDownloads();
                     * CustomMessageBox.Show(string.Format(Loc.Localize("PatchManDlFailure", "XIVLauncher could not verify the downloaded game files.\n\nThis usually indicates a problem with your internet connection.\nIf this error occurs again, try using a VPN set to Japan.\n\nContext: {0}\n{1}"), "Cancelled", download.Patch.VersionId), "XIVLauncher Error", MessageBoxButton.OK, MessageBoxImage.Error);
                     * Environment.Exit(0);
                     */
                    return;
                }

                var checkResult = CheckPatchValidity(download.Patch, outFile);

                // Indicate "Checking..."
                Slots[index] = SlotState.Checking;

                // Let's just bail for now, need better handling of this later
                if (checkResult != HashCheckResult.Pass)
                {
                    CancelAllDownloads();
                    Log.Error("IsHashCheckPass failed for {0} after DL", download.Patch.VersionId);
                    CustomMessageBox.Show(string.Format(Loc.Localize("PatchManDlFailure", "XIVLauncher could not verify the downloaded game files.\n\nThis usually indicates a problem with your internet connection.\nIf this error occurs again, try using a VPN set to Japan.\n\nContext: {0}\n{1}"), $"IsHashCheckPass({checkResult})", download.Patch.VersionId), "XIVLauncher Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    outFile.Delete();
                    Environment.Exit(0);
                    return;
                }

                download.State    = PatchState.Downloaded;
                Slots[index]      = SlotState.Done;
                Progresses[index] = 0;
                Speeds[index]     = 0;

                Log.Information("Patch at {0} downloaded completely", download.Patch.Url);

                this.CheckIsDone();
            };

            DownloadServices[index] = dlService;

            await dlService.DownloadFileTaskAsync(download.Patch.Url, outFile.FullName);
        }