PowerToys/Wox/PluginLoader/CSharpPluginLoader.cs

66 lines
2.2 KiB
C#
Raw Normal View History

2013-12-20 19:38:10 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2013-12-20 19:38:10 +08:00
using System.Linq;
using System.Reflection;
2013-12-22 19:35:21 +08:00
using System.Threading;
2014-01-29 18:33:24 +08:00
using Wox.Helper;
using Wox.Plugin;
using Wox.Plugin.System;
2013-12-20 19:38:10 +08:00
2014-01-29 18:33:24 +08:00
namespace Wox.PluginLoader
2013-12-20 19:38:10 +08:00
{
public class CSharpPluginLoader : BasePluginLoader
2013-12-20 19:38:10 +08:00
{
public override List<PluginPair> LoadPlugin()
2013-12-20 19:38:10 +08:00
{
List<PluginPair> plugins = new List<PluginPair>();
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
foreach (PluginMetadata metadata in metadatas)
{
try
{
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
2014-01-15 22:45:02 +08:00
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && (o.BaseType == typeof(BaseSystemPlugin) || o.GetInterfaces().Contains(typeof(IPlugin)))).ToList();
if (types.Count == 0)
{
2014-03-23 17:43:46 +08:00
Log.Warn(string.Format("Cound't load plugin {0}: didn't find the class who implement IPlugin",
2013-12-22 00:44:56 +08:00
metadata.Name));
continue;
}
2014-01-03 23:52:36 +08:00
foreach (Type type in types)
{
2014-01-03 23:52:36 +08:00
PluginPair pair = new PluginPair()
{
Plugin = Activator.CreateInstance(type) as IPlugin,
Metadata = metadata
};
var sys = pair.Plugin as BaseSystemPlugin;
if (sys != null)
{
sys.PluginDirectory = metadata.PluginDirecotry;
}
2014-01-03 23:52:36 +08:00
plugins.Add(pair);
}
}
catch (Exception e)
{
Log.Error(string.Format("Cound't load plugin {0}: {1}", metadata.Name, e.Message));
#if (DEBUG)
2013-12-22 00:44:56 +08:00
{
throw;
}
#endif
}
}
2013-12-20 19:38:10 +08:00
return plugins;
}
}
}