84 lines
2.0 KiB
C#
84 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace Crysome.Server.Converters;
|
|
|
|
public class FlagConverter : IValueConverter
|
|
{
|
|
private static readonly Dictionary<string, BitmapImage> Cache = new Dictionary<string, BitmapImage>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
private static bool _loaded;
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value == null)
|
|
{
|
|
return null;
|
|
}
|
|
string text = value.ToString().Trim().ToUpperInvariant();
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return null;
|
|
}
|
|
if (!_loaded)
|
|
{
|
|
LoadAll();
|
|
}
|
|
if (Cache.TryGetValue(text, out var value2))
|
|
{
|
|
return value2;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
return DependencyProperty.UnsetValue;
|
|
}
|
|
|
|
private static void LoadAll()
|
|
{
|
|
_loaded = true;
|
|
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
string[] array = new string[2]
|
|
{
|
|
Path.Combine(baseDirectory, "flags"),
|
|
baseDirectory
|
|
};
|
|
foreach (string path in array)
|
|
{
|
|
if (!Directory.Exists(path))
|
|
{
|
|
continue;
|
|
}
|
|
string[] files = Directory.GetFiles(path, "*.png");
|
|
foreach (string text in files)
|
|
{
|
|
try
|
|
{
|
|
string text2 = Path.GetFileNameWithoutExtension(text).ToUpperInvariant();
|
|
if (!text2.Contains("@") && text2.Length == 2 && !Cache.ContainsKey(text2))
|
|
{
|
|
BitmapImage bitmapImage = new BitmapImage();
|
|
bitmapImage.BeginInit();
|
|
bitmapImage.UriSource = new Uri(text);
|
|
bitmapImage.DecodePixelHeight = 16;
|
|
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
|
bitmapImage.EndInit();
|
|
((Freezable)bitmapImage).Freeze();
|
|
Cache[text2] = bitmapImage;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|