public void GetPackagesToDownload(IEnumerable <string> allMissing) { IEnumerable <string> depsToDownload = allMissing.Intersect(DownloadableDeps); while (depsToDownload.Count() > 0) { Package pkg = CachedPackages.First(x => x.PackageId == DownloadableDepsPackages[depsToDownload.First()]); PkgsToDownload.Add(pkg.PackageId); depsToDownload = depsToDownload.Except(pkg.FilesContained); } }
public void DownloadDependencies() { Task.Run(async() => { if (await CheckLogin(1) < 0 || App.IsDownloading) { App.Window.Dispatcher.Invoke(() => { MainWindow.ScanRailworks.IsEnabled = true; MainWindow.SelectRailworksLocation.IsEnabled = true; MainWindow.DownloadMissing.IsEnabled = true; }); return; } int maxThreads = Math.Min(Environment.ProcessorCount, DownloadableDeps.Count); Parallel.For(0, maxThreads, workerId => { int max = DownloadableDeps.Count * (workerId + 1) / maxThreads; for (int i = DownloadableDeps.Count * workerId / maxThreads; i < max; i++) { Task.Run(async() => { string dependency = DownloadableDeps.ElementAt(i); List <int> pkgId = await FindFile(dependency); if (pkgId.Count >= 0) { lock (PkgsToDownload) { PkgsToDownload.UnionWith(pkgId); } } }).Wait(); } }); if (PkgsToDownload.Count > 0) { App.IsDownloading = true; MainWindow.Dispatcher.Invoke(() => { MainWindow.DownloadDialog.ShowAsync(); }); // TODO: Check if works MainWindow.DownloadDialog.DownloadPackages(PkgsToDownload, CachedPackages, InstalledPackages, WebWrapper, SqLiteAdapter).Wait(); App.IsDownloading = false; MainWindow.RW_CrawlingComplete(); } else { new Task(() => { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download packages", Content = "All availaible packages were downloaded.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); MainWindow.Dispatcher.Invoke(() => { MainWindow.ScanRailworks.IsEnabled = true; MainWindow.SelectRailworksLocation.IsEnabled = true; }); }).Start(); } }); }
public async Task <HashSet <string> > GetDownloadableDependencies(HashSet <string> globalDependencies, HashSet <string> existing, MainWindow mw) { InstalledPackages = SqLiteAdapter.LoadInstalledPackages(); HashSet <string> allDownloadableDeps = await WebWrapper.QueryArray("listFiles"); HashSet <string> conflictDeps = existing.Intersect(allDownloadableDeps).Except(InstalledPackages.SelectMany(x => x.FilesContained)).ToHashSet(); HashSet <int> conflictPackages = new HashSet <int>(); int maxThreads = Math.Min(Environment.ProcessorCount, conflictDeps.Count); Parallel.For(0, maxThreads, workerId => { Task.Run(async() => { int max = conflictDeps.Count * (workerId + 1) / maxThreads; for (int i = conflictDeps.Count * workerId / maxThreads; i < max; i++) { List <int> packages = await FindFile(conflictDeps.ElementAt(i), false); Trace.Assert(packages.Count > 0, $"FindFile for {conflictDeps.ElementAt(i)} returned no packages!"); if (packages.Count > 0) { int id = packages.First(); lock (conflictPackages) { if (conflictPackages.Contains(id)) { continue; } conflictPackages.Add(id); } } } }).Wait(); }); bool rewriteAll = false; bool keepAll = false; for (int i = 0; i < conflictPackages.Count; i++) { int id = conflictPackages.ElementAt(i); if (Settings.Default.IgnoredPackages?.Contains(id) == true) { continue; } Package p = CachedPackages.FirstOrDefault(x => x.PackageId == id); bool rewrite = false; if (!rewriteAll && !keepAll) { Task <ContentDialogResult> t = null; mw.Dispatcher.Invoke(() => { MainWindow.ContentDialog = new ConflictPackageDialog(p.DisplayName); t = MainWindow.ContentDialog.ShowAsync(); }); ContentDialogResult result = await t; ConflictPackageDialog dlg = (ConflictPackageDialog)MainWindow.ContentDialog; rewrite = dlg.RewriteLocal; rewriteAll = dlg.RewriteAll; keepAll = dlg.KeepAll; } if (rewrite || rewriteAll) { PkgsToDownload.Add(id); HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { id }, depsPkgs); PkgsToDownload.UnionWith(depsPkgs); } else { if (Settings.Default.IgnoredPackages == null) { Settings.Default.IgnoredPackages = new List <int>(); } Settings.Default.IgnoredPackages.Add(id); Settings.Default.Save(); } } CheckUpdates(); DownloadableDeps = allDownloadableDeps.Intersect(globalDependencies).ToHashSet(); return(DownloadableDeps); }
public async Task ResolveConflicts() { HashSet <string> conflictDeps = MainWindow.RW.AllInstalledDeps.Intersect(DownloadableDeps).Except(InstalledPackages.SelectMany(x => x.FilesContained)).ToHashSet(); HashSet <int> conflictPackages = new HashSet <int>(); while (conflictDeps.Count > 0) { Package pkg = CachedPackages.First(x => x.FilesContained.Contains(conflictDeps.First())); conflictPackages.Add(pkg.PackageId); conflictDeps.ExceptWith(pkg.FilesContained); } //HashSet<int> conflictPackages = conflictPkgs.Select(x => x.PackageId).ToHashSet(); bool rewriteAll = false; bool keepAll = false; for (int i = 0; i < conflictPackages.Count; i++) { int id = conflictPackages.ElementAt(i); if (Settings.Default.IgnoredPackages?.Contains(id) == true) { continue; } Package p = CachedPackages.FirstOrDefault(x => x.PackageId == id); bool rewrite = false; if (!rewriteAll && !keepAll) { Task <ContentDialogResult> t = null; MainWindow.Dispatcher.Invoke(() => { MainWindow.ContentDialog = new ConflictPackageDialog(p.DisplayName); t = MainWindow.ContentDialog.ShowAsync(); }); ContentDialogResult result = await t; ConflictPackageDialog dlg = (ConflictPackageDialog)MainWindow.ContentDialog; rewrite = dlg.RewriteLocal; rewriteAll = dlg.RewriteAll; keepAll = dlg.KeepAll; } if (rewrite || rewriteAll) { PkgsToDownload.Add(id); HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { id }, depsPkgs); PkgsToDownload.UnionWith(depsPkgs); } else { if (Settings.Default.IgnoredPackages == null) { Settings.Default.IgnoredPackages = new List <int>(); } Settings.Default.IgnoredPackages.Add(id); Settings.Default.Save(); } } }