using AppUpdater.UI.Helpers;
using MahApps.Metro.Controls;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace AppUpdater.UI.Views
public partial class DownloadWindow : MetroWindow
async Task RunDownloading(string fileStandard, string fileStudio, bool downloadStudioToo)
Random random = new Random(Guid.NewGuid().GetHashCode());
var downloadOpt = new DownloadConfiguration()
MaximumBytesPerSecond = 0,
MaxTryAgainOnFailover = 5,
OnTheFlyDownload = false,
ParallelDownload = false,
var downloader = new DownloadService(downloadOpt);
downloader.DownloadProgressChanged += OnDownloadProgressChanged;
await downloader.DownloadFileTaskAsync(DARTUpdate.urlStandard + "?random=" + random.Next().ToString(), fileStandard);
await downloader.DownloadFileTaskAsync(DARTUpdate.urlStudio + "?random=" + random.Next().ToString(), fileStudio);
private async void StartDownloading()
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
bool downloadStudioToo = Directory.EnumerateDirectories(desktopPath + "\\", "DART *")
.SelectMany(parent => Directory.EnumerateDirectories(parent, "DART Studio")).ToArray().Length > 0;
Random random = new Random(Guid.NewGuid().GetHashCode());
string fileStandard = System.IO.Path.Combine(desktopPath, System.IO.Path.GetFileName(DARTUpdate.urlStandard));
string fileStudio = System.IO.Path.Combine(desktopPath, System.IO.Path.GetFileName(DARTUpdate.urlStudio));
File.Delete(fileStandard);
_downloadTask = Task.Run(() => RunDownloading(fileStandard, fileStudio, downloadStudioToo));
DownloaderShowError("Download Error!", "DART update failed!");
if (ProcessHelper.ProcessExists("DART "))
UpdateStatusTitle.Text = "DART is running!";
UpdateStatusText.Text = "Please close it!";
this.WindowState = System.Windows.WindowState.Normal;
await Task.Factory.StartNew(() =>
} while (ProcessHelper.ProcessExists("DART "));
UpdateStatusTitle.Text = "DART is getting better!";
UpdateStatusText.Text = "New DART update is installing.";
string desktopDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\";
string ZipFileStandard = System.IO.Path.Combine(desktopDir, System.IO.Path.GetFileName(DARTUpdate.urlStandard));
string ZipFileStudio = System.IO.Path.Combine(desktopDir, System.IO.Path.GetFileName(DARTUpdate.urlStudio));
if (File.Exists(ZipFileStandard))
Unzip_Files(ZipFileStandard, desktopDir);
if (File.Exists(ZipFileStudio))
Unzip_Files(ZipFileStudio, desktopDir);
DownloaderShowError("Install Error!", "DART installation failed!");
Properties.Settings.Default.LastVersion = DARTUpdate.newVersion;
Properties.Settings.Default.Save();
File.Copy(@"DART Creator 4.lnk", desktopPath + @"\DART Creator 4.lnk",true);
File.Copy(@"DART Experience 4.lnk", desktopPath + @"\DART Experience 4.lnk", true);
File.Copy(@"DART Range 4.lnk", desktopPath + @"\DART Range 4.lnk", true);
if (File.Exists(ZipFileStudio))
File.Copy(@"DART Studio 4.lnk", desktopPath + @"\DART Studio 4.lnk", true);
new ReleaseNotesWindow().Show();
void DownloaderShowError(string errorTitle, string errorText)
UpdateStatusTitle.Text = errorTitle;
UpdateStatusText.Text = errorText;
downloadProgressBar.Value = 100;
downloadProgressBar.Foreground = Brushes.Tomato;
downloadCancelButton.Content = "Exit";
private void Unzip_Files(string zipFilePath, string targetDir)
using (ZipFile zip = ZipFile.Read(zipFilePath))
new EventHandler<ExtractProgressEventArgs>(Zip_ExtractProgress);
zip.ExtractAll(targetDir, ExtractExistingFileAction.OverwriteSilently);
void Zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
if (e.TotalBytesToTransfer > 0)
downloadProgressBar.Value = Convert.ToInt32(100 * e.BytesTransferred / e.TotalBytesToTransfer);
private void OnDownloadProgressChanged(object sender, Downloader.DownloadProgressChangedEventArgs e)
this.Dispatcher.BeginInvoke(new Action(() =>
downloadProgressBar.Value = e.ProgressPercentage;
private void Downloader_ProgressChanged(long? totalFileSize, long totalBytesDownloaded, double? progressPercentage)
this.Dispatcher.Invoke(new Action(() =>
downloadProgressBar.Value = progressPercentage ?? 0;
private void Button_Click(object sender, RoutedEventArgs e)
Application.Current.Shutdown();