mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Improved resource manager, plugin manager
This commit is contained in:
@ -47,6 +47,11 @@ namespace RainmeterStudio.Business
|
||||
/// </summary>
|
||||
public IEnumerable<IDocumentStorage> Storages { get { return _storages; } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of document templates
|
||||
/// </summary>
|
||||
public IEnumerable<DocumentTemplate> DocumentTemplates { get { return _templates; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private fields
|
||||
|
@ -4,10 +4,12 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
using System.Text;
|
||||
using RainmeterStudio.Core;
|
||||
using RainmeterStudio.Core.Documents;
|
||||
using RainmeterStudio.Core.Utils;
|
||||
using RainmeterStudio.Resources;
|
||||
|
||||
namespace RainmeterStudio.Business
|
||||
{
|
||||
@ -16,31 +18,84 @@ namespace RainmeterStudio.Business
|
||||
/// </summary>
|
||||
public class PluginManager
|
||||
{
|
||||
public delegate void RegisterMethod(object objectToRegister);
|
||||
#region Private fields
|
||||
|
||||
List<Assembly> _loadedPlugins = new List<Assembly>();
|
||||
Dictionary<Type, RegisterMethod> _registerTypes = new Dictionary<Type,RegisterMethod>();
|
||||
|
||||
Dictionary<Type, RegisterExportHandler> _registerExportTypes = new Dictionary<Type, RegisterExportHandler>();
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Gets an enumerable of the loaded plugins
|
||||
/// </summary>
|
||||
public IEnumerable<Assembly> LoadedPlugins
|
||||
{
|
||||
get
|
||||
{
|
||||
return _loadedPlugins;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A method which registers an object that was exported by a plugin.
|
||||
/// </summary>
|
||||
/// <param name="objectToRegister">Object to register</param>
|
||||
public delegate void RegisterExportHandler(object objectToRegister);
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the plugin manager
|
||||
/// </summary>
|
||||
public PluginManager()
|
||||
{
|
||||
}
|
||||
|
||||
public void AddRegisterType(Type interfaceType, RegisterMethod method)
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Adds a handler that registers exported objects of a specific type.
|
||||
/// </summary>
|
||||
/// <param name="interfaceType">The data type</param>
|
||||
/// <param name="method">Handler that does the registring</param>
|
||||
public void AddRegisterExportTypeHandler(Type interfaceType, RegisterExportHandler method)
|
||||
{
|
||||
_registerTypes.Add(interfaceType, method);
|
||||
_registerExportTypes.Add(interfaceType, method);
|
||||
}
|
||||
|
||||
public void LoadPlugins()
|
||||
/// <summary>
|
||||
/// Initializes the plugin manager
|
||||
/// </summary>
|
||||
/// <remarks>This will load all the plugins from the "StudioPlugins" folder</remarks>
|
||||
public void Initialize()
|
||||
{
|
||||
// Get "Plugins" folder path
|
||||
var location = Assembly.GetExecutingAssembly().Location;
|
||||
var pluginsPath = Path.Combine(Path.GetDirectoryName(location), "Plugins");
|
||||
// Initialize the executing assembly
|
||||
InitializePlugin(Assembly.GetExecutingAssembly());
|
||||
|
||||
// Load plugins from StudioPlugins folder
|
||||
var location = Assembly.GetExecutingAssembly().Location;
|
||||
var pluginsPath = Path.Combine(Path.GetDirectoryName(location), "StudioPlugins");
|
||||
|
||||
LoadPlugins(pluginsPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads all the plugins from the specified directory.
|
||||
/// </summary>
|
||||
/// <param name="pluginsPath">Directory path</param>
|
||||
public void LoadPlugins(string pluginsPath)
|
||||
{
|
||||
// Load all DLLs from "Plugins" folder
|
||||
foreach (var file in Directory.EnumerateFiles(pluginsPath, "*.dll"))
|
||||
LoadPlugin(file);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to load the plugin.
|
||||
/// </summary>
|
||||
/// <param name="file">File name</param>
|
||||
/// <remarks>If plugin is not loaded, the function fails silently.</remarks>
|
||||
public void LoadPlugin(string file)
|
||||
{
|
||||
Assembly assembly = null;
|
||||
@ -58,17 +113,19 @@ namespace RainmeterStudio.Business
|
||||
// Loaded, do initialization stuff
|
||||
if (assembly != null)
|
||||
{
|
||||
_loadedPlugins.Add(assembly);
|
||||
|
||||
Initialize(assembly);
|
||||
|
||||
Debug.WriteLine("Loaded plugin: {0}", assembly.FullName);
|
||||
// Check for the RainmeterStudioPlugin attribute
|
||||
if (assembly.GetCustomAttributes(typeof(RainmeterStudioPluginAttribute), false).Count() > 0)
|
||||
{
|
||||
_loadedPlugins.Add(assembly);
|
||||
InitializePlugin(assembly);
|
||||
Debug.WriteLine("Loaded plugin: {0}", (object)assembly.Location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Initialize(Assembly assembly)
|
||||
private void InitializePlugin(Assembly assembly)
|
||||
{
|
||||
// Register factories and stuff
|
||||
// Register exports
|
||||
assembly.GetTypes()
|
||||
|
||||
// Select only the classes
|
||||
@ -80,7 +137,7 @@ namespace RainmeterStudio.Business
|
||||
// Perform register
|
||||
.ForEach((type) =>
|
||||
{
|
||||
foreach (var pair in _registerTypes)
|
||||
foreach (var pair in _registerExportTypes)
|
||||
{
|
||||
if (pair.Key.IsAssignableFrom(type))
|
||||
{
|
||||
@ -91,13 +148,13 @@ namespace RainmeterStudio.Business
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public IEnumerable<Assembly> LoadedPlugins
|
||||
{
|
||||
get
|
||||
// Register .resource files
|
||||
foreach (var resourceName in assembly.GetManifestResourceNames())
|
||||
{
|
||||
return _loadedPlugins;
|
||||
var name = Path.GetFileNameWithoutExtension(resourceName);
|
||||
ResourceManager manager = new ResourceManager(name, assembly);
|
||||
ResourceProvider.RegisterManager(manager, assembly);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user