mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Work on resource and settings managers, added some documentation.
This commit is contained in:
130
RainmeterStudio/Business/ResourceProvider.cs
Normal file
130
RainmeterStudio/Business/ResourceProvider.cs
Normal file
@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
using System.Windows.Media;
|
||||
using RainmeterStudio.Core.Utils;
|
||||
|
||||
namespace RainmeterStudio.Business
|
||||
{
|
||||
/// <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;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("{{{0}; {1}}}", Manager, Assembly);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the cache
|
||||
/// </summary>
|
||||
public static void ClearCache()
|
||||
{
|
||||
_cacheImages.Clear();
|
||||
_cacheStrings.Clear();
|
||||
}
|
||||
}
|
||||
}
|
63
RainmeterStudio/Business/SettingsProvider.cs
Normal file
63
RainmeterStudio/Business/SettingsProvider.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
|
||||
namespace RainmeterStudio.Business
|
||||
{
|
||||
public static class SettingsProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Attempts to retrieve the setting of type T, where T is class
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Any class type</typeparam>
|
||||
/// <param name="name">Name of setting</param>
|
||||
/// <returns>Retrieved setting, or null if not found</returns>
|
||||
public static T GetSetting<T> (string name) where T : class
|
||||
{
|
||||
var property = Properties.Settings.Default.Properties
|
||||
.OfType<SettingsProperty>()
|
||||
.FirstOrDefault(x => String.Equals(x.Name, name));
|
||||
|
||||
return (property == null) ? null : (property.DefaultValue as T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to retrieve the setting of type T
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Any type</typeparam>
|
||||
/// <param name="name">Name of setting</param>
|
||||
/// <param name="value">Output value</param>
|
||||
/// <returns>True if attempt was successful</returns>
|
||||
public static bool TryGetSetting<T>(string name, out T value)
|
||||
{
|
||||
var property = Properties.Settings.Default.Properties.OfType<SettingsProperty>().FirstOrDefault(x => x.Name.Equals(name));
|
||||
|
||||
if (property != null)
|
||||
{
|
||||
value = (T)property.DefaultValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = default(T);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the settings
|
||||
/// </summary>
|
||||
public static void SaveSettings()
|
||||
{
|
||||
Properties.Settings.Default.Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets settings to default
|
||||
/// </summary>
|
||||
public static void ResetSettings()
|
||||
{
|
||||
Properties.Settings.Default.Reset();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user