2013-12-20 19:38:10 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-12-21 01:20:17 +08:00
|
|
|
|
using System.Reflection;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
using Wox.Helper;
|
|
|
|
|
using Wox.Plugin;
|
2014-03-29 15:52:57 +08:00
|
|
|
|
using Wox.Plugin.SystemPlugins;
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
namespace Wox.PluginLoader {
|
2013-12-21 01:20:17 +08:00
|
|
|
|
|
2014-07-09 18:15:23 +08:00
|
|
|
|
public class CSharpPluginLoader : IPluginLoader
|
2014-07-06 22:57:11 +08:00
|
|
|
|
{
|
|
|
|
|
public List<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
|
|
|
|
|
{
|
2014-05-21 07:45:11 +08:00
|
|
|
|
var plugins = new List<PluginPair>();
|
2014-03-28 22:42:28 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
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));
|
|
|
|
|
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-05-21 07:45:11 +08:00
|
|
|
|
Log.Warn(string.Format("Couldn't load plugin {0}: didn't find the class who implement IPlugin", metadata.Name));
|
2014-05-10 09:24:16 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-03-28 22:42:28 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
foreach (Type type in types) {
|
|
|
|
|
PluginPair pair = new PluginPair() {
|
|
|
|
|
Plugin = Activator.CreateInstance(type) as IPlugin,
|
|
|
|
|
Metadata = metadata
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var sys = pair.Plugin as BaseSystemPlugin;
|
|
|
|
|
if (sys != null) {
|
2014-07-16 20:17:51 +08:00
|
|
|
|
sys.PluginDirectory = metadata.PluginDirectory;
|
2014-05-10 09:24:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugins.Add(pair);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2014-05-21 07:45:11 +08:00
|
|
|
|
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
|
2013-12-21 01:20:17 +08:00
|
|
|
|
#if (DEBUG)
|
2014-05-10 09:24:16 +08:00
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-12-21 01:20:17 +08:00
|
|
|
|
#endif
|
2014-05-10 09:24:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-21 01:20:17 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
return plugins;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|