public async Task GetDependencies(HashSet <int> dependecies, HashSet <int> returnDependencies) { foreach (int depPackageId in dependecies) { if (!returnDependencies.Contains(depPackageId) && !InstalledPackages.Any(x => x.PackageId == depPackageId)) { Package dependencyPackage = CachedPackages.FirstOrDefault(x => x.PackageId == depPackageId); if (dependencyPackage == default) { dependencyPackage = await WebWrapper.GetPackage(depPackageId); lock (CachedPackages) { CachedPackages.Add(dependencyPackage); } } if (!dependencyPackage.IsPaid) { returnDependencies.Add(depPackageId); } await GetDependencies(dependencyPackage.Dependencies.ToHashSet(), returnDependencies); } } }
public async Task ReceiveMSMQ() { string queueFile = Path.Combine(Path.GetTempPath(), "DLS.queue"); HashSet <string> queuedPkgs = File.Exists(queueFile) ? File.ReadAllText(queueFile).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet() : new HashSet <string>(); if (queuedPkgs.Count > 0) { MainWindow.Dispatcher.Invoke(() => { MainWindow.Activate(); }); if (await CheckLogin(1) < 0 || App.IsDownloading) { File.WriteAllText(queueFile, string.Empty); return; } int idToDownload = Convert.ToInt32(queuedPkgs.PopOne()); if (!InstalledPackages.Exists(x => x.PackageId == idToDownload)) { Task.Run(async() => { Package packageToDownload = await WebWrapper.GetPackage(idToDownload); lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == packageToDownload.PackageId)) { CachedPackages.Add(packageToDownload); } } if (packageToDownload.IsPaid) { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download package", Content = "This is paid package. Paid packages cannot be downloaded through this app.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); return; } HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { packageToDownload.PackageId }, depsPkgs); HashSet <int> packageIds = new HashSet <int>() { packageToDownload.PackageId }.Union(depsPkgs).ToHashSet(); if (packageIds.Count > 0) { App.IsDownloading = true; MainWindow.Dispatcher.Invoke(() => { MainWindow.DownloadDialog.ShowAsync(); }); await MainWindow.DownloadDialog.DownloadPackages(packageIds, CachedPackages, InstalledPackages, WebWrapper, SqLiteAdapter); App.IsDownloading = false; } else { new Task(() => { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download packages", Content = "An error ocured when trying to install package.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); }).Start(); } }).Wait(); } else { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download package", Content = "This package is already downloaded.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); } File.WriteAllText(queueFile, string.Join(",", queuedPkgs)); await ReceiveMSMQ(); } }
public async Task ReceiveMSMQ() { string queueFile = Path.Combine(Path.GetTempPath(), "DLS.queue"); HashSet <string> queuedPkgs = File.Exists(queueFile) ? File.ReadAllText(queueFile).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet() : new HashSet <string>(); if (queuedPkgs.Count > 0) { MainWindow.Dispatcher.Invoke(() => { MainWindow.Activate(); }); if (!await Utils.CheckLogin(async delegate { await ReceiveMSMQ(); MSMQRunning = false; }, MainWindow, ApiUrl) || App.IsDownloading) { //File.WriteAllText(queueFile, string.Empty); return; } int idToDownload = Convert.ToInt32(queuedPkgs.PopOne()); if (!InstalledPackages.Exists(x => x.PackageId == idToDownload)) { Task.Run(async() => { Package packageToDownload = await WebWrapper.GetPackage(idToDownload); lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == packageToDownload.PackageId)) { CachedPackages.Add(packageToDownload); } } if (packageToDownload.IsPaid) { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.PaidPackageFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); return; } HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { packageToDownload.PackageId }, depsPkgs); HashSet <int> packageIds = new HashSet <int>() { packageToDownload.PackageId }.Union(depsPkgs).ToHashSet(); if (packageIds.Count > 0) { App.IsDownloading = true; MainWindow.Dispatcher.Invoke(() => { MainWindow.DownloadDialog.ShowAsync(); }); await MainWindow.DownloadDialog.DownloadPackages(packageIds, CachedPackages, InstalledPackages, WebWrapper, SqLiteAdapter); App.IsDownloading = false; } else { new Task(() => { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.InstalFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); }).Start(); } }).Wait(); } else { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.AlreadyInstalFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); } File.WriteAllText(queueFile, string.Join(",", queuedPkgs)); await ReceiveMSMQ(); } }