2014-08-14 07:06:20 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Resources;
|
|
|
|
|
using System.Windows.Media;
|
2014-08-15 12:31:33 +00:00
|
|
|
|
using RainmeterStudio.Core.Utils;
|
2014-08-14 07:06:20 +00:00
|
|
|
|
|
2014-08-15 12:31:33 +00:00
|
|
|
|
namespace RainmeterStudio.Business
|
2014-08-14 07:06:20 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manages and provides resources
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ResourceProvider
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Holds information about a resource manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private struct ResourceManagerInfo
|
|
|
|
|
{
|
|
|
|
|
public ResourceManager Manager;
|
|
|
|
|
public Assembly Assembly;
|
2014-08-15 12:31:33 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return String.Format("{{{0}; {1}}}", Manager, Assembly);
|
|
|
|
|
}
|
2014-08-14 07:06:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<ResourceManagerInfo> _resourceManagers = new List<ResourceManagerInfo>();
|
|
|
|
|
private static Dictionary<string, string> _cacheStrings = new Dictionary<string, string>();
|
|
|
|
|
private static Dictionary<string, ImageSource> _cacheImages = new Dictionary<string, ImageSource>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a resource manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="manager">The resource manager</param>
|
|
|
|
|
/// <param name="ownerAssembly">The assembly which will contain the non-string resources</param>
|
|
|
|
|
public static void RegisterManager(ResourceManager manager, Assembly ownerAssembly)
|
|
|
|
|
{
|
|
|
|
|
_resourceManagers.Add(new ResourceManagerInfo()
|
|
|
|
|
{
|
|
|
|
|
Manager = manager,
|
|
|
|
|
Assembly = ownerAssembly
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a string from the resource managers
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">Identifier of the resource</param>
|
|
|
|
|
/// <param name="bypassCache">By default, strings are cached. Setting this to true will bypass the cache.</param>
|
|
|
|
|
/// <param name="keepInCache">If this parameter is set to true, the obtained string will be cached.</param>
|
|
|
|
|
/// <returns>The string, or null if not found</returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an image from the resource manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">Identifier of the resource</param>
|
|
|
|
|
/// <param name="bypassCache">By default, images are cached. Setting this to true will bypass the cache.</param>
|
|
|
|
|
/// <param name="keepInCache">If this parameter is set to true, the obtained image will be cached.</param>
|
|
|
|
|
/// <returns>The image source, or null if not found</returns>
|
|
|
|
|
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
|
2014-08-15 12:31:33 +00:00
|
|
|
|
var bitmap = info.Manager.GetObject(key) as System.Drawing.Bitmap;
|
2014-08-14 07:06:20 +00:00
|
|
|
|
|
|
|
|
|
// Found
|
2014-08-15 12:31:33 +00:00
|
|
|
|
if (bitmap != null)
|
2014-08-14 07:06:20 +00:00
|
|
|
|
{
|
2014-08-15 12:31:33 +00:00
|
|
|
|
image = bitmap.GetImageSource();
|
2014-08-14 07:06:20 +00:00
|
|
|
|
|
|
|
|
|
if (keepInCache)
|
|
|
|
|
_cacheImages[key] = image;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resource not found
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clears the cache
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void ClearCache()
|
|
|
|
|
{
|
|
|
|
|
_cacheImages.Clear();
|
|
|
|
|
_cacheStrings.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|