Пример #1
0
        private void PART_BtAddSteamGridDb_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SteamGridDbType steamGridDbType = SteamGridDbType.heroes;
                if (_IsCover)
                {
                    steamGridDbType = SteamGridDbType.grids;
                }

                var    ViewExtension   = new SteamGridDbView(_gameBackgroundImages.Name, steamGridDbType);
                Window windowExtension = PlayniteUiHelper.CreateExtensionWindow(PluginDatabase.PlayniteApi, "SteamGridDB", ViewExtension);
                windowExtension.ShowDialog();

                if (ViewExtension.steamGridDbResult != null)
                {
                    GlobalProgressOptions globalProgressOptions = new GlobalProgressOptions(
                        resources.GetString("LOCCommonGettingData"),
                        false
                        );
                    globalProgressOptions.IsIndeterminate = true;

                    var ProgressDownload = PluginDatabase.PlayniteApi.Dialogs.ActivateGlobalProgress((activateGlobalProgress) =>
                    {
                        try
                        {
                            var cachedFile = HttpFileCache.GetWebFile(ViewExtension.steamGridDbResult.url);
                            _backgroundImagesEdited.Add(new ItemImage
                            {
                                Name = cachedFile
                            });
                        }
                        catch (Exception ex)
                        {
                            Common.LogError(ex, false, true, "BackgroundChanger");
                        }
                    }, globalProgressOptions);


                    Task.Run(() =>
                    {
                        while (!(bool)ProgressDownload.Result)
                        {
                        }
                    }).ContinueWith(antecedant =>
                    {
                        this.Dispatcher.BeginInvoke((Action) delegate
                        {
                            PART_LbBackgroundImages.ItemsSource = null;
                            PART_LbBackgroundImages.ItemsSource = _backgroundImagesEdited;
                        });
                    });
                }
            }
            catch (Exception ex)
            {
                Common.LogError(ex, false, true, "BackgroundChanger");
            }
        }
        public static object GetImageFromSource(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(null);
            }

            var imageId = source;

            if (string.IsNullOrEmpty(imageId))
            {
                return(null);
            }

            if (imageId.StartsWith("resources:"))
            {
                if (cache.TryGet(imageId, out var image))
                {
                    return(image);
                }
                else
                {
                    try
                    {
                        var imagePath = imageId.Replace("resources:", "pack://application:,,,");
                        var imageData = BitmapExtensions.BitmapFromFile(imagePath);
                        cache.TryAdd(imageId, imageData, imageData.GetSizeInMemory());
                        return(imageData);
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, "Failed to create bitmap from resources " + imageId);
                        return(null);
                    }
                }
            }

            if (imageId.IsHttpUrl())
            {
                try
                {
                    var cachedFile = HttpFileCache.GetWebFile(imageId);
                    if (string.IsNullOrEmpty(cachedFile))
                    {
                        logger.Warn("Web file not found: " + imageId);
                        return(null);
                    }

                    return(BitmapExtensions.BitmapFromFile(cachedFile));
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to create bitmap from {imageId} file.");
                    return(null);
                }
            }

            if (File.Exists(imageId))
            {
                try
                {
                    return(BitmapExtensions.BitmapFromFile(imageId));
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from " + imageId);
                    return(null);
                }
            }

            try
            {
                if (database == null)
                {
                    logger.Error("Cannot load database image, database not found.");
                    return(null);
                }

                try
                {
                    if (cache.TryGet(imageId, out var image))
                    {
                        return(image);
                    }

                    var imageData = database.GetFileAsImage(imageId);
                    if (imageData == null)
                    {
                        logger.Warn("Image not found in database: " + imageId);
                        return(null);
                    }
                    else
                    {
                        cache.TryAdd(imageId, imageData, imageData.GetSizeInMemory());
                        return(imageData);
                    }
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to get bitmap from {imageId} database file.");
                    return(null);
                }
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to load image from database.");
                return(null);
            }
        }