127 lines
3.0 KiB
C#
127 lines
3.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 OSIconConverter : 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().ToLower();
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return null;
|
|
}
|
|
if (!_loaded)
|
|
{
|
|
LoadAll();
|
|
}
|
|
if (text.Contains("windows 11") || text.Contains("server 2022"))
|
|
{
|
|
return GetCached("windows11");
|
|
}
|
|
if (text.Contains("windows 10") || text.Contains("server 2019") || text.Contains("server 2016"))
|
|
{
|
|
return GetCached("windows10");
|
|
}
|
|
if (text.Contains("8.1") || text.Contains("server 2012"))
|
|
{
|
|
return GetCached("windows81");
|
|
}
|
|
if (text.Contains("windows 8"))
|
|
{
|
|
return GetCached("windows81");
|
|
}
|
|
if (text.Contains("windows 7") || text.Contains("vista") || text.Contains("server 2008"))
|
|
{
|
|
return GetCached("windows7");
|
|
}
|
|
if (text.Contains("xp") || text.Contains("server 2003"))
|
|
{
|
|
return GetCached("windowsxp");
|
|
}
|
|
if (text.Contains("linux"))
|
|
{
|
|
return GetCached("linux");
|
|
}
|
|
if (text.Contains("windows"))
|
|
{
|
|
return GetCached("windows10");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
return DependencyProperty.UnsetValue;
|
|
}
|
|
|
|
private static BitmapImage GetCached(string key)
|
|
{
|
|
if (Cache.TryGetValue(key, out var value))
|
|
{
|
|
return value;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static void LoadAll()
|
|
{
|
|
_loaded = true;
|
|
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
string[] array = new string[2]
|
|
{
|
|
baseDirectory,
|
|
Path.Combine(baseDirectory, "icons")
|
|
};
|
|
string[] array2 = new string[6] { "windows7", "windows81", "windows10", "windows11", "windowsxp", "linux" };
|
|
string[] array3 = array;
|
|
foreach (string text in array3)
|
|
{
|
|
if (!Directory.Exists(text))
|
|
{
|
|
continue;
|
|
}
|
|
string[] array4 = array2;
|
|
foreach (string text2 in array4)
|
|
{
|
|
if (Cache.ContainsKey(text2))
|
|
{
|
|
continue;
|
|
}
|
|
string text3 = Path.Combine(text, text2 + ".png");
|
|
if (File.Exists(text3))
|
|
{
|
|
try
|
|
{
|
|
BitmapImage bitmapImage = new BitmapImage();
|
|
bitmapImage.BeginInit();
|
|
bitmapImage.UriSource = new Uri(text3);
|
|
bitmapImage.DecodePixelHeight = 16;
|
|
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
|
bitmapImage.EndInit();
|
|
((Freezable)bitmapImage).Freeze();
|
|
Cache[text2] = bitmapImage;
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|