예제 #1
0
        public void CreateShortcut(Game game)
        {
            try
            {
                var    path = Environment.ExpandEnvironmentVariables(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Paths.GetSafeFilename(game.Name) + ".url"));
                string icon = string.Empty;

                if (!game.Icon.IsNullOrEmpty())
                {
                    icon = Database.GetFullFilePath(game.Icon);
                }
                else
                {
                    icon = game.GetDefaultIcon(AppSettings, Database, Extensions.GetLibraryPlugin(game.PluginId));
                    if (!File.Exists(icon))
                    {
                        icon = string.Empty;
                    }
                }

                if (File.Exists(icon))
                {
                    if (Path.GetExtension(icon) != ".ico")
                    {
                        var targetIconPath = Path.Combine(PlaynitePaths.TempPath, Guid.NewGuid() + ".ico");
                        BitmapExtensions.ConvertToIcon(icon, targetIconPath);
                        var md5          = FileSystem.GetMD5(targetIconPath);
                        var existingFile = Path.Combine(PlaynitePaths.TempPath, md5 + ".ico");
                        if (File.Exists(existingFile))
                        {
                            icon = existingFile;
                            File.Delete(targetIconPath);
                        }
                        else
                        {
                            File.Move(targetIconPath, existingFile);
                            icon = existingFile;
                        }
                    }
                }
                else
                {
                    icon = PlaynitePaths.DesktopExecutablePath;
                }

                var args = new CmdLineOptions()
                {
                    Start = game.Id.ToString()
                }.ToString();
                Programs.CreateUrlShortcut($"playnite://playnite/start/{game.Id}", icon, path);
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to create shortcut: ");
                Dialogs.ShowMessage(
                    string.Format(resources.GetString("LOCGameShortcutError"), exc.Message),
                    resources.GetString("LOCGameError"),
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
예제 #2
0
        public static BitmapImage GetImage(string source, bool cached)
        {
            if (DesignerTools.IsInDesignMode)
            {
                cached = false;
            }

            if (source.IsNullOrEmpty())
            {
                return(null);
            }

            if (source.StartsWith("resources:") || source.StartsWith("pack://"))
            {
                if (cached && Cache.TryGet(source, out var image))
                {
                    return(image as BitmapImage);
                }
                else
                {
                    try
                    {
                        var imagePath = source;
                        if (source.StartsWith("resources:"))
                        {
                            imagePath = source.Replace("resources:", "pack://application:,,,");
                        }

                        var imageData = BitmapExtensions.BitmapFromFile(imagePath);
                        if (imageData != null)
                        {
                            if (cached)
                            {
                                Cache.TryAdd(source, imageData, imageData.GetSizeInMemory());
                            }

                            return(imageData);
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, "Failed to create bitmap from resources " + source);
                        return(null);
                    }
                }
            }

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

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

            if (File.Exists(source))
            {
                try
                {
                    var imageData = BitmapExtensions.BitmapFromFile(source);
                    if (imageData != null)
                    {
                        if (cached)
                        {
                            Cache.TryAdd(source, imageData, imageData.GetSizeInMemory());
                        }

                        return(imageData);
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from " + source);
                    return(null);
                }
            }

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

                try
                {
                    if (cached && Cache.TryGet(source, out var image))
                    {
                        return(image as BitmapImage);
                    }

                    var imageData = database.GetFileAsImage(source);
                    if (imageData == null)
                    {
                        logger.Warn("Image not found in database: " + source);
                        return(null);
                    }
                    else
                    {
                        if (cached)
                        {
                            Cache.TryAdd(source, imageData, imageData.GetSizeInMemory());
                        }

                        return(imageData);
                    }
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to get bitmap from {source} database file.");
                    return(null);
                }
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to load image from database.");
                return(null);
            }
        }
예제 #3
0
        public static object ResolveUiItemIcon(object icon)
        {
            if (icon == null)
            {
                return(null);
            }

            if (icon is string stringIcon)
            {
                var resource = ResourceProvider.GetResource(stringIcon);
                if (resource != null)
                {
                    if (resource is BitmapImage bitmap)
                    {
                        var image = new System.Windows.Controls.Image()
                        {
                            Source = bitmap
                        };
                        RenderOptions.SetBitmapScalingMode(image, RenderOptions.GetBitmapScalingMode(bitmap));
                        return(image);
                    }
                    else if (resource is TextBlock textIcon)
                    {
                        var text = new TextBlock
                        {
                            Text       = textIcon.Text,
                            FontFamily = textIcon.FontFamily,
                            FontStyle  = textIcon.FontStyle
                        };

                        if (textIcon.ReadLocalValue(TextBlock.ForegroundProperty) != DependencyProperty.UnsetValue)
                        {
                            text.Foreground = textIcon.Foreground;
                        }

                        return(text);
                    }
                }
                else if (System.IO.File.Exists(stringIcon))
                {
                    return(BitmapExtensions.BitmapFromFile(stringIcon)?.ToImage());
                }
                else
                {
                    var themeFile = ThemeFile.GetFilePath(stringIcon);
                    if (themeFile != null)
                    {
                        return(Images.GetImageFromFile(themeFile, BitmapScalingMode.Fant, double.NaN, double.NaN));
                    }

                    var dbFile = GameDatabase.Instance.GetFileAsImage(stringIcon);
                    if (dbFile != null)
                    {
                        return(dbFile.ToImage());
                    }
                }
            }
            else
            {
                return(icon);
            }

            return(null);
        }
예제 #4
0
        public static BitmapImage GetImage(string source, bool cached, BitmapLoadProperties loadProperties = null)
        {
            if (DesignerTools.IsInDesignMode)
            {
                cached = false;
            }

            if (source.IsNullOrEmpty())
            {
                return(null);
            }

            if (cached && Cache.TryGet(source, out var image))
            {
                BitmapLoadProperties existingMetadata = null;
                if (image.Metadata.TryGetValue(btmpPropsFld, out object metaValue))
                {
                    existingMetadata = (BitmapLoadProperties)metaValue;
                }

                if (existingMetadata == loadProperties)
                {
                    return(image.CacheObject as BitmapImage);
                }
                else
                {
                    Cache.TryRemove(source);
                }
            }

            if (source.StartsWith("resources:") || source.StartsWith("pack://"))
            {
                try
                {
                    var imagePath = source;
                    if (source.StartsWith("resources:"))
                    {
                        imagePath = source.Replace("resources:", "pack://application:,,,");
                    }

                    var streamInfo = Application.GetResourceStream(new Uri(imagePath));
                    using (var stream = streamInfo.Stream)
                    {
                        var imageData = BitmapExtensions.BitmapFromStream(stream, loadProperties);
                        if (imageData != null)
                        {
                            if (cached)
                            {
                                Cache.TryAdd(source, imageData, imageData.GetSizeInMemory(),
                                             new Dictionary <string, object>
                                {
                                    { btmpPropsFld, loadProperties }
                                });
                            }

                            return(imageData);
                        }
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from resources " + source);
                    return(null);
                }
            }

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

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

            if (File.Exists(source))
            {
                try
                {
                    var imageData = BitmapExtensions.BitmapFromFile(source, loadProperties);
                    if (imageData != null)
                    {
                        if (cached)
                        {
                            Cache.TryAdd(source, imageData, imageData.GetSizeInMemory(),
                                         new Dictionary <string, object>
                            {
                                { btmpPropsFld, loadProperties }
                            });
                        }

                        return(imageData);
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from " + source);
                    return(null);
                }
            }

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

                try
                {
                    var imageData = database.GetFileAsImage(source, loadProperties);
                    if (imageData == null)
                    {
                        logger.Warn("Image not found in database: " + source);
                        return(null);
                    }
                    else
                    {
                        if (cached)
                        {
                            Cache.TryAdd(source, imageData, imageData.GetSizeInMemory(),
                                         new Dictionary <string, object>
                            {
                                { btmpPropsFld, loadProperties }
                            });
                        }

                        return(imageData);
                    }
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to get bitmap from {source} database file.");
                    return(null);
                }
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to load image from database.");
                return(null);
            }
        }