2014-12-26 19:36:43 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Newtonsoft.Json;
|
2014-12-28 15:17:58 +08:00
|
|
|
|
using Wox.Core.Exception;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
|
using Wox.Infrastructure.Storage.UserSettings;
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Core.Plugin
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
internal abstract class PluginConfig
|
|
|
|
|
{
|
|
|
|
|
private const string pluginConfigName = "plugin.json";
|
|
|
|
|
private static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parse plugin metadata in giving directories
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pluginDirectories"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static List<PluginMetadata> Parse(List<string> pluginDirectories)
|
|
|
|
|
{
|
|
|
|
|
pluginMetadatas.Clear();
|
|
|
|
|
foreach (string pluginDirectory in pluginDirectories)
|
|
|
|
|
{
|
2015-01-03 15:20:34 +08:00
|
|
|
|
ParsePluginConfigs(pluginDirectory);
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PluginManager.DebuggerMode != null)
|
|
|
|
|
{
|
|
|
|
|
PluginMetadata metadata = GetPluginMetadata(PluginManager.DebuggerMode);
|
|
|
|
|
if (metadata != null) pluginMetadatas.Add(metadata);
|
|
|
|
|
}
|
|
|
|
|
return pluginMetadatas;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
private static void ParsePluginConfigs(string pluginDirectory)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2014-12-29 22:09:54 +08:00
|
|
|
|
if (!Directory.Exists(pluginDirectory)) return;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
|
|
|
|
|
string[] directories = Directory.GetDirectories(pluginDirectory);
|
|
|
|
|
foreach (string directory in directories)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists((Path.Combine(directory, "NeedDelete.txt"))))
|
|
|
|
|
{
|
2014-12-29 23:02:50 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(directory, true);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(ExceptionFormatter.FormatExcpetion(e));
|
|
|
|
|
}
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
PluginMetadata metadata = GetPluginMetadata(directory);
|
|
|
|
|
if (metadata != null)
|
|
|
|
|
{
|
|
|
|
|
pluginMetadatas.Add(metadata);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static PluginMetadata GetPluginMetadata(string pluginDirectory)
|
|
|
|
|
{
|
|
|
|
|
string configPath = Path.Combine(pluginDirectory, pluginConfigName);
|
|
|
|
|
if (!File.Exists(configPath))
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PluginMetadata metadata;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
2014-12-26 22:51:04 +08:00
|
|
|
|
metadata.PluginType = PluginType.User;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
metadata.PluginDirectory = pluginDirectory;
|
|
|
|
|
}
|
2014-12-28 15:17:58 +08:00
|
|
|
|
catch (System.Exception)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
|
|
|
|
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
|
|
|
|
Log.Warn(error);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw new WoxException(error);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
|
|
|
|
{
|
|
|
|
|
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath, metadata.Language);
|
|
|
|
|
Log.Warn(error);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw new WoxException(error);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(metadata.ExecuteFilePath))
|
|
|
|
|
{
|
|
|
|
|
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath, metadata.ExecuteFilePath);
|
|
|
|
|
Log.Warn(error);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw new WoxException(error);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//replace action keyword if user customized it.
|
|
|
|
|
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID);
|
|
|
|
|
if (customizedPluginConfig != null && !string.IsNullOrEmpty(customizedPluginConfig.Actionword))
|
|
|
|
|
{
|
|
|
|
|
metadata.ActionKeyword = customizedPluginConfig.Actionword;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-29 22:09:54 +08:00
|
|
|
|
}
|