2013-12-20 19:38:10 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2014-01-03 18:16:05 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2014-01-12 21:02:39 +08:00
|
|
|
|
using System.Windows.Forms;
|
2014-03-01 15:42:33 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
using Wox.Helper;
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
using Wox.Plugin.System;
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
2014-01-29 18:33:24 +08:00
|
|
|
|
namespace Wox.PluginLoader
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
|
|
|
|
public abstract class BasePluginLoader
|
|
|
|
|
{
|
2014-01-12 21:02:39 +08:00
|
|
|
|
private static string PluginPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
|
2014-03-01 15:42:33 +08:00
|
|
|
|
private static string PluginConfigName = "plugin.json";
|
2013-12-20 19:38:10 +08:00
|
|
|
|
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
|
|
|
|
|
2014-01-12 18:15:30 +08:00
|
|
|
|
public static void ParsePluginsConfig()
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2014-01-12 18:15:30 +08:00
|
|
|
|
pluginMetadatas.Clear();
|
2014-01-03 18:16:05 +08:00
|
|
|
|
ParseSystemPlugins();
|
|
|
|
|
ParseThirdPartyPlugins();
|
2013-12-20 19:38:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 18:16:05 +08:00
|
|
|
|
private static void ParseSystemPlugins()
|
|
|
|
|
{
|
2014-01-03 23:52:36 +08: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 18:33:24 +08:00
|
|
|
|
metadata.ExecuteFileName = "Wox.Plugin.System.dll";
|
2014-01-03 23:52:36 +08:00
|
|
|
|
metadata.PluginDirecotry = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
pluginMetadatas.Add(metadata);
|
2014-01-03 18:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ParseThirdPartyPlugins()
|
2014-01-29 18:33:24 +08:00
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(PluginPath))
|
2014-01-26 01:24:24 +08:00
|
|
|
|
Directory.CreateDirectory(PluginPath);
|
|
|
|
|
|
2013-12-20 19:38:10 +08:00
|
|
|
|
string[] directories = Directory.GetDirectories(PluginPath);
|
|
|
|
|
foreach (string directory in directories)
|
|
|
|
|
{
|
2014-03-11 23:54:37 +08:00
|
|
|
|
if (File.Exists((Path.Combine(directory, "NeedDelete.txt"))))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(directory,true);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-03-01 15:42:33 +08:00
|
|
|
|
PluginMetadata metadata = GetMetadataFromJson(directory);
|
2013-12-20 19:38:10 +08:00
|
|
|
|
if (metadata != null) pluginMetadatas.Add(metadata);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 15:42:33 +08:00
|
|
|
|
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2014-03-01 15:42:33 +08:00
|
|
|
|
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
|
|
|
|
PluginMetadata metadata;
|
2013-12-21 01:20:17 +08:00
|
|
|
|
|
2014-03-01 15:42:33 +08:00
|
|
|
|
if (!File.Exists(configPath))
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2014-03-01 15:42:33 +08:00
|
|
|
|
Log.Error(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
|
2013-12-20 19:38:10 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-03-01 15:42:33 +08:00
|
|
|
|
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
2014-01-03 18:16:05 +08:00
|
|
|
|
metadata.PluginType = PluginType.ThirdParty;
|
2014-03-01 15:42:33 +08:00
|
|
|
|
metadata.PluginDirecotry = pluginDirectory;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
|
|
|
|
Log.Error(error);
|
2013-12-23 19:21:51 +08:00
|
|
|
|
#if (DEBUG)
|
2013-12-21 01:20:17 +08:00
|
|
|
|
{
|
2014-03-01 15:42:33 +08:00
|
|
|
|
throw new WoxException(error);
|
2013-12-20 19:38:10 +08:00
|
|
|
|
}
|
2014-03-01 15:42:33 +08:00
|
|
|
|
#endif
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
2014-03-01 15:42:33 +08:00
|
|
|
|
|
|
|
|
|
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
|
|
|
|
{
|
|
|
|
|
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
|
|
|
|
|
metadata.Language);
|
|
|
|
|
Log.Error(error);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw new WoxException(error);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return null;
|
2013-12-20 19:38:10 +08:00
|
|
|
|
}
|
2014-03-01 15:42:33 +08:00
|
|
|
|
if (!File.Exists(metadata.ExecuteFilePath))
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2014-03-01 15:42:33 +08:00
|
|
|
|
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath,
|
|
|
|
|
metadata.ExecuteFilePath);
|
|
|
|
|
Log.Error(error);
|
2013-12-23 19:21:51 +08:00
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
2014-03-01 15:42:33 +08:00
|
|
|
|
throw new WoxException(error);
|
2013-12-23 19:21:51 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-12-20 19:38:10 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-01-11 15:02:17 +08:00
|
|
|
|
|
2014-03-01 15:42:33 +08:00
|
|
|
|
return metadata;
|
|
|
|
|
}
|
2013-12-20 19:38:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|