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;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
using System.Threading;
|
2013-12-20 19:38:10 +08:00
|
|
|
|
using WinAlfred.Helper;
|
|
|
|
|
using WinAlfred.Plugin;
|
2014-01-03 23:52:36 +08:00
|
|
|
|
using WinAlfred.Plugin.System;
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
|
|
|
|
namespace WinAlfred.PluginLoader
|
|
|
|
|
{
|
2013-12-21 01:20:17 +08:00
|
|
|
|
public class CSharpPluginLoader : BasePluginLoader
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2013-12-21 01:20:17 +08:00
|
|
|
|
public override List<PluginPair> LoadPlugin()
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2013-12-21 01:20:17 +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
|
|
|
|
|
{
|
2013-12-23 23:53:38 +08:00
|
|
|
|
Assembly asm = Assembly.LoadFile(metadata.ExecuteFilePath);
|
2014-01-03 23:52:36 +08:00
|
|
|
|
List<Type> types = asm.GetTypes().Where(o => o.IsClass && o.GetInterfaces().Contains(typeof(IPlugin)) || o.GetInterfaces().Contains(typeof(ISystemPlugin))).ToList();
|
2013-12-21 01:20:17 +08:00
|
|
|
|
if (types.Count == 0)
|
|
|
|
|
{
|
2013-12-22 00:44:56 +08:00
|
|
|
|
Log.Error(string.Format("Cound't load plugin {0}: didn't find the class who implement IPlugin",
|
|
|
|
|
metadata.Name));
|
2013-12-21 01:20:17 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 23:52:36 +08:00
|
|
|
|
foreach (Type type in types)
|
2013-12-21 01:20:17 +08:00
|
|
|
|
{
|
2014-01-03 23:52:36 +08:00
|
|
|
|
PluginPair pair = new PluginPair()
|
|
|
|
|
{
|
|
|
|
|
Plugin = Activator.CreateInstance(type) as IPlugin,
|
|
|
|
|
Metadata = metadata
|
|
|
|
|
};
|
|
|
|
|
plugins.Add(pair);
|
|
|
|
|
}
|
2013-12-21 01:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
2013-12-21 01:20:17 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-22 19:35:21 +08:00
|
|
|
|
InitPlugin(plugins);
|
2013-12-20 19:38:10 +08:00
|
|
|
|
return plugins;
|
|
|
|
|
}
|
2013-12-22 19:35:21 +08:00
|
|
|
|
|
|
|
|
|
private void InitPlugin(List<PluginPair> plugins)
|
|
|
|
|
{
|
2014-01-03 23:52:36 +08:00
|
|
|
|
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2013-12-20 19:38:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|