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 <List <int> > FindFile(string file_name, bool withDeps = true) { Package package = InstalledPackages.FirstOrDefault(x => x.FilesContained.Contains(file_name)); if (package != default) { return new List <int>() { package.PackageId } } ; lock (CachedPackages) package = CachedPackages.FirstOrDefault(x => x.FilesContained.Contains(file_name)); if (package != default) { return new List <int>() { package.PackageId } } ; Package onlinePackage = await WebWrapper.SearchForFile(file_name); if (onlinePackage != null && onlinePackage.PackageId > 0) { lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == onlinePackage.PackageId)) { CachedPackages.Add(onlinePackage); } } HashSet <int> dependencyPkgIds = new HashSet <int>(); if (withDeps) { await GetDependencies(new HashSet <int>() { onlinePackage.PackageId }, dependencyPkgIds); } return(new List <int>() { onlinePackage.PackageId }.Union(dependencyPkgIds).ToList()); } return(new List <int>()); }
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(); } }
public async Task VerifyCache() { List <Package> localCache = SqLiteAdapter.LoadPackages(true); ServerVersions = new Dictionary <int, int>(); localCache.ForEach(x => ServerVersions[x.PackageId] = x.Version); Tuple <IEnumerable <Package>, HashSet <int> > tRemoteCache = await WebWrapper.ValidateCache(ServerVersions); IEnumerable <Package> remoteCache = tRemoteCache.Item1; HashSet <int> remoteVersions = tRemoteCache.Item2; lock (CachedPackages) { CachedPackages = remoteCache.ToList(); localCache.ForEach(x => { if (!remoteVersions.Contains(x.PackageId)) { CachedPackages.Add(x); } else { ServerVersions[x.PackageId] = CachedPackages.First(y => y.PackageId == x.PackageId).Version; } }); foreach (Package pkg in remoteCache) { ServerVersions[pkg.PackageId] = pkg.Version; } } if (remoteVersions.Count > 0) { new Task(() => { foreach (Package pkg in CachedPackages) { SqLiteAdapter.SavePackage(pkg, true); } SqLiteAdapter.FlushToFile(true); }).Start(); } CachedPackages.ForEach(x => { if (x.IsPaid) { x.FilesContained.ForEach(y => { DownloadablePaidDepsPackages[y] = x.PackageId; DownloadablePaidDeps.Add(y); }); } else { x.FilesContained.ForEach(y => { DownloadableDepsPackages[y] = x.PackageId; DownloadableDeps.Add(y); }); } }); }