Exemplo n.º 1
0
        public async Task AddDefaultCatalogsAsync()
        {
            IsAdding = true;

            Task addTask = Task.Factory.StartNew(() =>
            {
                // get current list of catalogs from the list
                CatalogSettings updatedSettings = new CatalogSettings()
                {
                    CatalogUrls = CatalogList.Select(c => new CatalogSubscription()
                    {
                        Name     = c.Name,
                        Url      = c.Url,
                        IsActive = c.IsActive
                    }).ToList()
                };

                CatalogSettings.AddDefaults(updatedSettings);

                // update the UI list with the new catalogs
                CatalogList = updatedSettings.CatalogUrls.Select(c => new CatalogSubscriptionViewModel(c)).ToList();
            });

            // ensure error is logged when task is done and IsAdding is set back to false
            await addTask.ContinueWith((result) =>
            {
                if (result.IsFaulted)
                {
                    Logger.Error(result.Exception.GetBaseException());
                }

                IsAdding = false;
            });
        }
Exemplo n.º 2
0
        public Task CheckForCatalogUpdatesAsync(bool clearCache = true)
        {
            object countLock = new object();

            Task t = Task.Factory.StartNew(() =>
            {
                string catFile = AbsolutePathToCatalogSettingsJson;

                Directory.CreateDirectory(AbsolutePathToTempDownloads);

                CatalogSettings currentSettings = new CatalogSettings();

                if (File.Exists(catFile))
                {
                    currentSettings = JsonConvert.DeserializeObject <CatalogSettings>(File.ReadAllText(catFile));
                    currentSettings.CatalogUrls.RemoveAll(s => string.IsNullOrWhiteSpace(s.Url));
                }
                else
                {
                    CatalogSettings.AddDefaults(currentSettings);
                }


                if (currentSettings.CatalogUrls.Count == 0)
                {
                    if (File.Exists(catFile))
                    {
                        File.Delete(catFile);
                    }

                    _catalogCache = new AssetCatalog();
                    ReloadAllAssets();
                    RefreshFilteredAssetList();
                    return;
                }

                if (clearCache)
                {
                    lock (catalogCacheLock)
                    {
                        _catalogCache = new AssetCatalog();
                    }
                }

                foreach (CatalogSubscription sub in currentSettings.CatalogUrls.Where(c => c.IsActive).ToArray())
                {
                    Logger.Info($"Checking catalog {sub.Url}");

                    string uniqueFileName = $"cattemp{Path.GetRandomFileName()}.xml"; // save temp catalog update to unique filename so multiple catalog updates can download async
                    string path           = Path.Combine(AbsolutePathToTempDownloads, uniqueFileName);

                    Guid downloadId = Guid.NewGuid();

                    Action onCancel = () =>
                    {
                        RemoveFromDownloads(downloadId);
                    };

                    Action onError = () =>
                    {
                        RemoveFromDownloads(downloadId);
                    };

                    Action onComplete = () =>
                    {
                        RemoveFromDownloads(downloadId);

                        try
                        {
                            AssetCatalog c = JsonConvert.DeserializeObject <AssetCatalog>(File.ReadAllText(path));

                            lock (catalogCacheLock) // put a lock on the Catalog so multiple threads can only merge one at a time
                            {
                                _catalogCache = AssetCatalog.Merge(_catalogCache, c);
                                File.WriteAllText(AbsolutePathToCatalogJson, JsonConvert.SerializeObject(_catalogCache, Formatting.Indented));
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex);
                        }
                        finally
                        {
                            // delete temp catalog
                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }

                            ReloadAllAssets();
                            RefreshFilteredAssetList();
                        }
                    };

                    DownloadItemViewModel catalogDownload = new DownloadItemViewModel()
                    {
                        UniqueId     = downloadId,
                        DownloadType = DownloadType.Catalog,
                        ItemName     = $"Checking catalog {sub.Url}",
                        DownloadUrl  = AssetCatalog.FormatUrl(sub.Url),
                        SaveFilePath = path,
                        OnComplete   = onComplete,
                        OnError      = onError,
                        OnCancel     = onCancel
                    };

                    AddToDownloads(catalogDownload);
                }
            });

            return(t);
        }