public void Finish()
            {
                if (!Directory.Exists(Path.GetDirectoryName(_downloadFilename)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(_downloadFilename));
                }

                FileStream = new FileStream(_downloadFilename, FileMode.Create, FileAccess.ReadWrite);
                FileStream.SetLength(Patch.FileDownloadInfo.FileSize);

                double parts     = _fileTable.Count;
                int    completed = 0;

                var progressReporter = _patcherContext.CreateProgressIndicator();

                progressReporter.SetLeftText($"{Patch.FileDownloadInfo.FileName}");

                foreach (var file in _fileTable.Values)
                {
                    byte[] buffer;

                    using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
                        using (var ms = new MemoryStream())
                        {
                            fs.CopyTo(ms);
                            buffer = ms.ToArray();
                        }

                    FileStream.Write(buffer, 0, buffer.Length);
                    FileStream.Flush();

                    completed++;

                    progressReporter.SetRightText($"{completed}/{parts}");
                    progressReporter.SetProgressBar(completed / parts * 100);
                }

                FileStream.Flush(true);
                FileStream.Close();
                FileStream.Dispose();
                CopyFile(progressReporter);
            }
Example #2
0
        private void Download(List <PatchFileInfo> files)
        {
            // Log the beginning of the download
            Log.Info(Properties.Resources.BeginningDownload);

            _patcherContext.UpdateMainProgress(Properties.Resources.BeginningDownload, isIndeterminate: true,
                                               isProgressbarVisible: true);

            // create the actions that will run in parrallel
            var list = new List <Action>();

            var completed   = 0;
            int failedCount = 0;

            foreach (var patchFileInfo in files)
            {
                var ftpAddress = OfficialPatchInfo.MainFtp;
                if (!ftpAddress.EndsWith("/"))
                {
                    ftpAddress += "/";
                }

                // Im kinda stuck on how I wanna do this haha

                //var downloader = new FileDownloader(new ProgressReporterViewModel(), patchFileInfo.RemoteName,
                //    patchFileInfo.Filename, patchFileInfo.Size, CancellationToken.None);

                list.Add(() =>
                {
                    try
                    {
                        var progressIndicator = _patcherContext.CreateProgressIndicator();

                        var fileDirectory = Path.GetDirectoryName(Path.Combine(PatchInfo.PatchName, patchFileInfo.Filename));

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

                        Task.Run(async() =>
                        {
                            await AsyncDownloader.DownloadFileWithCallbackAsync(string.Concat(ftpAddress, PatchInfo.EndVersion, "/",
                                                                                              patchFileInfo.RemoteName),
                                                                                Path.Combine(PatchInfo.PatchName, patchFileInfo.Filename), (d, s) =>
                            {
                                progressIndicator.SetLeftText(Path.GetFileName(patchFileInfo.Filename));
                                progressIndicator.SetRightText(s);
                                progressIndicator.SetProgressBar(d);
                            });
                        }).Wait();

                        _patcherContext.DestroyProgressIndicator(progressIndicator);
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(ex);
                        Interlocked.Increment(ref failedCount);
                    }
                    finally
                    {
                        Interlocked.Increment(ref completed);

                        _patcherContext.UpdateMainProgress(Properties.Resources.DownloadingFiles,
                                                           $"{completed}/{files.Count}", completed / (double)files.Count * 100.0,
                                                           isProgressbarVisible: true);
                    }
                });
            }

            Parallel.Invoke(new ParallelOptions {
                MaxDegreeOfParallelism = 10
            }, list.ToArray());

            Log.Info(Properties.Resources.DownloadComplete);

            _patcherContext.UpdateMainProgress(Properties.Resources.DownloadComplete);
        }