mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Split into smaller projects, now uses plugins.
This commit is contained in:
@ -4,10 +4,10 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using RainmeterStudio.Documents;
|
||||
using RainmeterStudio.Model;
|
||||
using RainmeterStudio.Model.Events;
|
||||
using RainmeterStudio.Utils;
|
||||
using RainmeterStudio.Core.Documents;
|
||||
using RainmeterStudio.Core.Model;
|
||||
using RainmeterStudio.Core.Model.Events;
|
||||
using RainmeterStudio.Core.Storage;
|
||||
|
||||
namespace RainmeterStudio.Business
|
||||
{
|
||||
@ -67,61 +67,6 @@ namespace RainmeterStudio.Business
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all classes with the auto register flag
|
||||
/// </summary>
|
||||
/// <remarks>We love linq</remarks>
|
||||
public void PerformAutoRegister()
|
||||
{
|
||||
// Get all assemblies
|
||||
AppDomain.CurrentDomain.GetAssemblies()
|
||||
|
||||
// Get all types
|
||||
.SelectMany(assembly => assembly.GetTypes())
|
||||
|
||||
// Select only the classes
|
||||
.Where(type => type.IsClass)
|
||||
|
||||
// That have the AutoRegister attribute
|
||||
.Where(type => type.GetCustomAttributes(typeof(AutoRegisterAttribute), false).Length > 0)
|
||||
|
||||
// That implement any of the types that can be registered
|
||||
.Where((type) =>
|
||||
{
|
||||
bool res = false;
|
||||
res |= typeof(IDocumentEditorFactory).IsAssignableFrom(type);
|
||||
res |= typeof(IDocumentStorage).IsAssignableFrom(type);
|
||||
res |= typeof(DocumentTemplate).IsAssignableFrom(type);
|
||||
|
||||
return res;
|
||||
})
|
||||
|
||||
// Obtain their default constructor
|
||||
.Select(type => type.GetConstructor(new Type[0]))
|
||||
|
||||
// Invoke the default constructor
|
||||
.Select(constructor => constructor.Invoke(new object[0]))
|
||||
|
||||
// Register
|
||||
.ForEach(obj =>
|
||||
{
|
||||
// Try to register factory
|
||||
var factory = obj as IDocumentEditorFactory;
|
||||
if (factory != null)
|
||||
RegisterEditorFactory(factory);
|
||||
|
||||
// Try to register as storage
|
||||
var storage = obj as IDocumentStorage;
|
||||
if (storage != null)
|
||||
RegisterStorage(storage);
|
||||
|
||||
// Try to register as document template
|
||||
var doctemplate = obj as DocumentTemplate;
|
||||
if (doctemplate != null)
|
||||
RegisterTemplate(doctemplate);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a document editor factory
|
||||
/// </summary>
|
||||
|
104
RainmeterStudio/Business/PluginManager.cs
Normal file
104
RainmeterStudio/Business/PluginManager.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using RainmeterStudio.Core;
|
||||
using RainmeterStudio.Core.Documents;
|
||||
using RainmeterStudio.Core.Utils;
|
||||
|
||||
namespace RainmeterStudio.Business
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages RainmeterStudio plugins
|
||||
/// </summary>
|
||||
public class PluginManager
|
||||
{
|
||||
public delegate void RegisterMethod(object objectToRegister);
|
||||
|
||||
List<Assembly> _loadedPlugins = new List<Assembly>();
|
||||
Dictionary<Type, RegisterMethod> _registerTypes = new Dictionary<Type,RegisterMethod>();
|
||||
|
||||
public PluginManager()
|
||||
{
|
||||
}
|
||||
|
||||
public void AddRegisterType(Type interfaceType, RegisterMethod method)
|
||||
{
|
||||
_registerTypes.Add(interfaceType, method);
|
||||
}
|
||||
|
||||
public void LoadPlugins()
|
||||
{
|
||||
// Get "Plugins" folder path
|
||||
var location = Assembly.GetExecutingAssembly().Location;
|
||||
var pluginsPath = Path.Combine(Path.GetDirectoryName(location), "Plugins");
|
||||
|
||||
// Load all DLLs from "Plugins" folder
|
||||
foreach (var file in Directory.EnumerateFiles(pluginsPath, "*.dll"))
|
||||
LoadPlugin(file);
|
||||
}
|
||||
|
||||
public void LoadPlugin(string file)
|
||||
{
|
||||
Assembly assembly = null;
|
||||
|
||||
// Try to load assembly
|
||||
try
|
||||
{
|
||||
assembly = Assembly.LoadFile(file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("Failed to load assembly {0}: {1}", file, ex);
|
||||
}
|
||||
|
||||
// Loaded, do initialization stuff
|
||||
if (assembly != null)
|
||||
{
|
||||
_loadedPlugins.Add(assembly);
|
||||
|
||||
Initialize(assembly);
|
||||
|
||||
Debug.WriteLine("Loaded plugin: {0}", assembly.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
private void Initialize(Assembly assembly)
|
||||
{
|
||||
// Register factories and stuff
|
||||
assembly.GetTypes()
|
||||
|
||||
// Select only the classes
|
||||
.Where(type => type.IsClass)
|
||||
|
||||
// That have the AutoRegister attribute
|
||||
.Where(type => type.GetCustomAttributes(typeof(PluginExportAttribute), false).Length > 0)
|
||||
|
||||
// Perform register
|
||||
.ForEach((type) =>
|
||||
{
|
||||
foreach (var pair in _registerTypes)
|
||||
{
|
||||
if (pair.Key.IsAssignableFrom(type))
|
||||
{
|
||||
var constructor = type.GetConstructor(new Type[0]);
|
||||
var obj = constructor.Invoke(new object[0]);
|
||||
|
||||
pair.Value(obj);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public IEnumerable<Assembly> LoadedPlugins
|
||||
{
|
||||
get
|
||||
{
|
||||
return _loadedPlugins;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,8 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using RainmeterStudio.Model;
|
||||
using RainmeterStudio.Core.Model;
|
||||
using RainmeterStudio.Core.Storage;
|
||||
using RainmeterStudio.Storage;
|
||||
|
||||
namespace RainmeterStudio.Business
|
||||
|
Reference in New Issue
Block a user