2013-12-20 19:38:10 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using WinAlfred.Helper;
|
|
|
|
|
using WinAlfred.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace WinAlfred.PluginLoader
|
|
|
|
|
{
|
|
|
|
|
public abstract class BasePluginLoader
|
|
|
|
|
{
|
|
|
|
|
private static string PluginPath = "Plugins";
|
|
|
|
|
private static string PluginConfigName = "plugin.ini";
|
|
|
|
|
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
|
|
|
|
|
2013-12-21 01:20:17 +08:00
|
|
|
|
public abstract List<PluginPair> LoadPlugin();
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
|
|
|
|
static BasePluginLoader()
|
|
|
|
|
{
|
|
|
|
|
ParsePlugins();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 01:20:17 +08:00
|
|
|
|
private static void ParsePlugins()
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
|
|
|
|
ParseDirectories();
|
|
|
|
|
ParsePackagedPlugin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ParseDirectories()
|
|
|
|
|
{
|
|
|
|
|
string[] directories = Directory.GetDirectories(PluginPath);
|
|
|
|
|
foreach (string directory in directories)
|
|
|
|
|
{
|
2013-12-21 01:20:17 +08:00
|
|
|
|
PluginMetadata metadata = GetMetadataFromIni(directory);
|
2013-12-20 19:38:10 +08:00
|
|
|
|
if (metadata != null) pluginMetadatas.Add(metadata);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ParsePackagedPlugin()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 01:20:17 +08:00
|
|
|
|
private static PluginMetadata GetMetadataFromIni(string directory)
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2013-12-21 01:20:17 +08:00
|
|
|
|
string iniPath = directory + "\\" + PluginConfigName;
|
|
|
|
|
|
2013-12-20 19:38:10 +08:00
|
|
|
|
if (!File.Exists(iniPath))
|
|
|
|
|
{
|
2013-12-21 01:20:17 +08:00
|
|
|
|
Log.Error(string.Format("parse plugin {0} failed: didn't find config file.", iniPath));
|
2013-12-20 19:38:10 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
PluginMetadata metadata = new PluginMetadata();
|
|
|
|
|
IniParser ini = new IniParser(iniPath);
|
|
|
|
|
metadata.Name = ini.GetSetting("plugin", "Name");
|
|
|
|
|
metadata.Author = ini.GetSetting("plugin", "Author");
|
|
|
|
|
metadata.Description = ini.GetSetting("plugin", "Description");
|
|
|
|
|
metadata.Language = ini.GetSetting("plugin", "Language");
|
|
|
|
|
metadata.Version = ini.GetSetting("plugin", "Version");
|
2013-12-21 01:20:17 +08:00
|
|
|
|
metadata.ActionKeyword = ini.GetSetting("plugin", "ActionKeyword");
|
|
|
|
|
metadata.ExecuteFile = AppDomain.CurrentDomain.BaseDirectory + directory + "\\" + ini.GetSetting("plugin", "ExecuteFile");
|
2013-12-22 19:35:21 +08:00
|
|
|
|
metadata.PluginDirecotry = AppDomain.CurrentDomain.BaseDirectory + directory + "\\";
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
|
|
|
|
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
|
|
|
|
{
|
2013-12-21 01:20:17 +08:00
|
|
|
|
Log.Error(string.Format("Parse ini {0} failed: invalid language {1}", iniPath, metadata.Language));
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (!File.Exists(metadata.ExecuteFile))
|
|
|
|
|
{
|
|
|
|
|
Log.Error(string.Format("Parse ini {0} failed: ExecuteFile didn't exist {1}", iniPath, metadata.ExecuteFile));
|
2013-12-20 19:38:10 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-12-21 01:20:17 +08:00
|
|
|
|
Log.Error(string.Format("Parse ini {0} failed: {1}", iniPath, e.Message));
|
2013-12-20 19:38:10 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|