<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Playnite.Database;
using NLog;
using System.IO;
using Playnite;

namespace PlayniteUI
{
    public class CustomImageStringToImageConverter : IValueConverter
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
                
        public static GameDatabase Database
        {
            get; set;
        }

        public static Dictionary<string, BitmapImage> Cache
        {
            get; set;
        } = new Dictionary<string, BitmapImage>();

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return DependencyProperty.UnsetValue;
            }            

            var imageId = (string)value;
            if (string.IsNullOrEmpty(imageId))
            {
                return DependencyProperty.UnsetValue;
            }

            if (imageId.StartsWith("resources:"))
            {
                return imageId.Replace("resources:", "");
            }

            if (imageId.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    var cachedFile = Web.GetCachedWebFile(imageId);
                    if (string.IsNullOrEmpty(cachedFile))
                    {
                        logger.Warn("Web file not found: " + imageId);
                        return DependencyProperty.UnsetValue;
                    }

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

            if (File.Exists(imageId))
            {
                return BitmapExtensions.BitmapFromFile(imageId);
            }

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

                var imageData = Database.GetFileImage(imageId);
                if (imageData == null)
                {
                    logger.Warn("Image not found in database: " + imageId);
                    return DependencyProperty.UnsetValue;
                }
                else
                {
                    return imageData;
                }
            }
            catch (Exception exc) when (!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to load image from database.");
                return DependencyProperty.UnsetValue;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {<commit_msg>Fix: Crash While Downloading Metadata #324<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Playnite.Database;
using NLog;
using System.IO;
using Playnite;

namespace PlayniteUI
{
    public class CustomImageStringToImageConverter : IValueConverter
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
                
        public static GameDatabase Database
        {
            get; set;
        }

        public static Dictionary<string, BitmapImage> Cache
        {
            get; set;
        } = new Dictionary<string, BitmapImage>();

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return DependencyProperty.UnsetValue;
            }            

            var imageId = (string)value;
            if (string.IsNullOrEmpty(imageId))
            {
                return DependencyProperty.UnsetValue;
            }

            if (imageId.StartsWith("resources:"))
            {
                return imageId.Replace("resources:", "");
            }

            if (imageId.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    var cachedFile = Web.GetCachedWebFile(imageId);
                    if (string.IsNullOrEmpty(cachedFile))
                    {
                        logger.Warn("Web file not found: " + imageId);
                        return DependencyProperty.UnsetValue;
                    }

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

            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 DependencyProperty.UnsetValue;
                }
            }

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

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

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
{'fraction_non_alphanumeric': 0.0744606819763396, 'fraction_numerical': 0.0004175365344467641, 'mean_word_length': 1.363038474186123, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2276256', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1583, 'n_words': 373}