2014-12-26 19:36:43 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2015-11-09 09:32:33 +08:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Core.Plugin
|
|
|
|
|
{
|
|
|
|
|
internal class CSharpPluginLoader : IPluginLoader
|
|
|
|
|
{
|
|
|
|
|
public IEnumerable<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
|
|
|
|
|
{
|
|
|
|
|
var plugins = new List<PluginPair>();
|
|
|
|
|
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
|
|
|
|
|
|
|
|
|
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
2015-11-08 01:32:58 +08:00
|
|
|
|
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin))).ToList();
|
2014-12-26 19:36:43 +08:00
|
|
|
|
if (types.Count == 0)
|
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Warn($"Couldn't load plugin {metadata.Name}: didn't find the class that implement IPlugin");
|
2014-12-26 19:36:43 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Type type in types)
|
|
|
|
|
{
|
|
|
|
|
PluginPair pair = new PluginPair()
|
|
|
|
|
{
|
|
|
|
|
Plugin = Activator.CreateInstance(type) as IPlugin,
|
|
|
|
|
Metadata = metadata
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
plugins.Add(pair);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-28 15:17:58 +08:00
|
|
|
|
catch (System.Exception e)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Error(new WoxPluginException(metadata.Name, $"Couldn't load plugin", e));
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return plugins;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|