using System; using System.Collections.Generic; using System.Reflection; using System.Resources; using System.Windows.Data; using System.Windows.Media; using RainmeterStudio.Core.Utils; namespace RainmeterStudio.Business { /// /// Manages and provides resources /// public static class ResourceProvider { /// /// Holds information about a resource manager /// private struct ResourceManagerInfo { public ResourceManager Manager; public Assembly Assembly; public override string ToString() { return String.Format("{{{0}; {1}}}", Manager, Assembly); } } private static List _resourceManagers = new List(); private static Dictionary _cacheStrings = new Dictionary(); private static Dictionary _cacheImages = new Dictionary(); /// /// Registers a resource manager /// /// The resource manager /// The assembly which will contain the non-string resources public static void RegisterManager(ResourceManager manager, Assembly ownerAssembly) { _resourceManagers.Add(new ResourceManagerInfo() { Manager = manager, Assembly = ownerAssembly }); } /// /// Gets a string from the resource managers /// /// Identifier of the resource /// By default, strings are cached. Setting this to true will bypass the cache. /// If this parameter is set to true, the obtained string will be cached. /// The string, or null if not found public static string GetString(string key, bool bypassCache = false, bool keepInCache = true) { string value = null; // Look up in cache if (bypassCache || !_cacheStrings.TryGetValue(key, out value)) { // Not found, query resource managers foreach (var info in _resourceManagers) { // Try to get resource var str = info.Manager.GetString(key); // Found? if (str != null) { value = str; if (keepInCache) _cacheStrings[key] = str; break; } } } // Resource not found return value; } /// /// Gets an image from the resource manager. /// /// Identifier of the resource /// By default, images are cached. Setting this to true will bypass the cache. /// If this parameter is set to true, the obtained image will be cached. /// The image source, or null if not found public static ImageSource GetImage(string key, bool bypassCache = false, bool keepInCache = true) { ImageSource image = null; // Look up in cache if (bypassCache || !_cacheImages.TryGetValue(key, out image)) { // Not found, query resource managers foreach (var info in _resourceManagers) { // Try to get resource var bitmap = info.Manager.GetObject(key) as System.Drawing.Bitmap; // Found if (bitmap != null) { image = bitmap.GetImageSource(); if (keepInCache) _cacheImages[key] = image; break; } } } // Resource not found return image; } /// /// Clears the cache /// public static void ClearCache() { _cacheImages.Clear(); _cacheStrings.Clear(); } } /// /// Resource converter - converts resource identifier string into actual resource /// public class StringResourceConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string identifier = value as string; object resource = null; if (identifier != null) { resource = ResourceProvider.GetString(identifier); } return resource; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException("Operation not supported."); } } /// /// Resource converter - converts resource identifier string into actual resource /// public class ImageResourceConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string identifier = value as string; object resource = null; if (identifier != null) { resource = ResourceProvider.GetImage(identifier); } return resource; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException("Operation not supported."); } } }