2013-12-20 12:38:10 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2014-01-03 11:16:05 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2014-01-12 14:02:39 +01:00
|
|
|
|
using System.Windows.Forms;
|
2014-01-29 11:33:24 +01:00
|
|
|
|
using Wox.Helper;
|
2014-02-06 15:22:02 +01:00
|
|
|
|
using Wox.Infrastructure;
|
2014-01-29 11:33:24 +01:00
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
using Wox.Plugin.System;
|
2013-12-20 12:38:10 +01:00
|
|
|
|
|
2014-01-29 11:33:24 +01:00
|
|
|
|
namespace Wox.PluginLoader
|
2013-12-20 12:38:10 +01:00
|
|
|
|
{
|
|
|
|
|
public abstract class BasePluginLoader
|
|
|
|
|
{
|
2014-01-12 14:02:39 +01:00
|
|
|
|
private static string PluginPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
|
2013-12-20 12:38:10 +01:00
|
|
|
|
private static string PluginConfigName = "plugin.ini";
|
|
|
|
|
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
2013-12-20 18:20:17 +01:00
|
|
|
|
public abstract List<PluginPair> LoadPlugin();
|
2013-12-20 12:38:10 +01:00
|
|
|
|
|
2014-01-12 11:15:30 +01:00
|
|
|
|
public static void ParsePluginsConfig()
|
2013-12-20 12:38:10 +01:00
|
|
|
|
{
|
2014-01-12 11:15:30 +01:00
|
|
|
|
pluginMetadatas.Clear();
|
2014-01-03 11:16:05 +01:00
|
|
|
|
ParseSystemPlugins();
|
|
|
|
|
ParseThirdPartyPlugins();
|
2013-12-20 12:38:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 11:16:05 +01:00
|
|
|
|
private static void ParseSystemPlugins()
|
|
|
|
|
{
|
2014-01-03 16:52:36 +01:00
|
|
|
|
PluginMetadata metadata = new PluginMetadata();
|
|
|
|
|
metadata.Name = "System Plugins";
|
|
|
|
|
metadata.Author = "System";
|
|
|
|
|
metadata.Description = "system plugins collection";
|
|
|
|
|
metadata.Language = AllowedLanguage.CSharp;
|
|
|
|
|
metadata.Version = "1.0";
|
|
|
|
|
metadata.PluginType = PluginType.System;
|
|
|
|
|
metadata.ActionKeyword = "*";
|
2014-01-29 11:33:24 +01:00
|
|
|
|
metadata.ExecuteFileName = "Wox.Plugin.System.dll";
|
2014-01-03 16:52:36 +01:00
|
|
|
|
metadata.ExecuteFilePath = AppDomain.CurrentDomain.BaseDirectory + metadata.ExecuteFileName;
|
|
|
|
|
metadata.PluginDirecotry = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
pluginMetadatas.Add(metadata);
|
2014-01-03 11:16:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ParseThirdPartyPlugins()
|
2014-01-29 11:33:24 +01:00
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(PluginPath))
|
2014-01-25 18:24:24 +01:00
|
|
|
|
Directory.CreateDirectory(PluginPath);
|
|
|
|
|
|
2013-12-20 12:38:10 +01:00
|
|
|
|
string[] directories = Directory.GetDirectories(PluginPath);
|
|
|
|
|
foreach (string directory in directories)
|
|
|
|
|
{
|
2013-12-20 18:20:17 +01:00
|
|
|
|
PluginMetadata metadata = GetMetadataFromIni(directory);
|
2013-12-20 12:38:10 +01:00
|
|
|
|
if (metadata != null) pluginMetadatas.Add(metadata);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-20 18:20:17 +01:00
|
|
|
|
private static PluginMetadata GetMetadataFromIni(string directory)
|
2013-12-20 12:38:10 +01:00
|
|
|
|
{
|
2013-12-20 18:20:17 +01:00
|
|
|
|
string iniPath = directory + "\\" + PluginConfigName;
|
|
|
|
|
|
2013-12-20 12:38:10 +01:00
|
|
|
|
if (!File.Exists(iniPath))
|
|
|
|
|
{
|
2013-12-20 18:20:17 +01:00
|
|
|
|
Log.Error(string.Format("parse plugin {0} failed: didn't find config file.", iniPath));
|
2013-12-20 12:38:10 +01: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");
|
2014-01-03 11:16:05 +01:00
|
|
|
|
metadata.PluginType = PluginType.ThirdParty;
|
2013-12-20 18:20:17 +01:00
|
|
|
|
metadata.ActionKeyword = ini.GetSetting("plugin", "ActionKeyword");
|
2014-01-12 14:02:39 +01:00
|
|
|
|
metadata.ExecuteFilePath = directory + "\\" + ini.GetSetting("plugin", "ExecuteFile");
|
|
|
|
|
metadata.PluginDirecotry = directory + "\\";
|
2013-12-23 16:53:38 +01:00
|
|
|
|
metadata.ExecuteFileName = ini.GetSetting("plugin", "ExecuteFile");
|
2013-12-20 12:38:10 +01:00
|
|
|
|
|
|
|
|
|
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
|
|
|
|
{
|
2013-12-23 12:21:51 +01:00
|
|
|
|
string error = string.Format("Parse ini {0} failed: invalid language {1}", iniPath,
|
|
|
|
|
metadata.Language);
|
|
|
|
|
Log.Error(error);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
2014-01-29 11:33:24 +01:00
|
|
|
|
throw new WoxException(error);
|
2013-12-23 12:21:51 +01:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-12-20 18:20:17 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-12-23 16:53:38 +01:00
|
|
|
|
if (!File.Exists(metadata.ExecuteFilePath))
|
2013-12-20 18:20:17 +01:00
|
|
|
|
{
|
2013-12-23 16:53:38 +01:00
|
|
|
|
string error = string.Format("Parse ini {0} failed: ExecuteFilePath didn't exist {1}", iniPath,
|
|
|
|
|
metadata.ExecuteFilePath);
|
2013-12-23 12:21:51 +01:00
|
|
|
|
Log.Error(error);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
2014-01-29 11:33:24 +01:00
|
|
|
|
throw new WoxException(error);
|
2013-12-23 12:21:51 +01:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-12-20 12:38:10 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-12-20 18:20:17 +01:00
|
|
|
|
Log.Error(string.Format("Parse ini {0} failed: {1}", iniPath, e.Message));
|
2013-12-23 12:21:51 +01:00
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-12-20 12:38:10 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-11 08:02:17 +01:00
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
///// Change python execute file name to unique file name using GUID
|
|
|
|
|
///// this is because if two pythong plugin use the same
|
|
|
|
|
///// </summary>
|
|
|
|
|
///// <param name="metadata"></param>
|
|
|
|
|
///// <returns></returns>
|
|
|
|
|
//private static PluginMetadata filterPythonMetadata(PluginMetadata metadata)
|
|
|
|
|
//{
|
2014-01-12 11:15:30 +01:00
|
|
|
|
|
2014-01-11 08:02:17 +01:00
|
|
|
|
//}
|
2013-12-20 12:38:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|