PowerToys/Wox.Core/Plugin/PluginInstaller.cs

203 lines
7.8 KiB
C#
Raw Normal View History

2014-01-12 16:28:11 +08:00
using System;
using System.IO;
2016-01-06 14:45:08 +08:00
using System.Windows;
2014-01-12 16:28:11 +08:00
using ICSharpCode.SharpZipLib.Zip;
2014-03-02 11:04:30 +08:00
using Newtonsoft.Json;
2014-01-29 18:33:24 +08:00
using Wox.Plugin;
2014-01-12 16:28:11 +08:00
2014-12-27 12:34:51 +08:00
namespace Wox.Core.Plugin
2014-01-12 16:28:11 +08:00
{
2014-12-27 12:34:51 +08:00
internal class PluginInstaller
2014-01-12 16:28:11 +08:00
{
2014-12-27 12:34:51 +08:00
internal static void Install(string path)
2014-01-12 16:28:11 +08:00
{
if (File.Exists(path))
{
2014-03-11 23:54:37 +08:00
string tempFoler = Path.Combine(Path.GetTempPath(), "wox\\plugins");
2014-01-12 16:28:11 +08:00
if (Directory.Exists(tempFoler))
{
Directory.Delete(tempFoler, true);
}
UnZip(path, tempFoler, true);
2014-03-11 23:54:37 +08:00
string iniPath = Path.Combine(tempFoler, "plugin.json");
2014-01-12 16:28:11 +08:00
if (!File.Exists(iniPath))
{
2014-03-11 23:54:37 +08:00
MessageBox.Show("Install failed: plugin config is missing");
2014-01-12 16:28:11 +08:00
return;
}
2014-03-02 11:04:30 +08:00
PluginMetadata plugin = GetMetadataFromJson(tempFoler);
2014-01-12 16:28:11 +08:00
if (plugin == null || plugin.Name == null)
{
2014-03-11 23:54:37 +08:00
MessageBox.Show("Install failed: plugin config is invalid");
2014-01-12 16:28:11 +08:00
return;
}
2015-01-20 22:33:45 +08:00
string pluginFolerPath = PluginManager.PluginDirectory;
2014-01-12 16:28:11 +08:00
2014-12-15 20:38:42 +08:00
string newPluginName = plugin.Name
.Replace("/", "_")
.Replace("\\", "_")
.Replace(":", "_")
.Replace("<", "_")
.Replace(">", "_")
.Replace("?", "_")
.Replace("*", "_")
.Replace("|", "_")
+ "-" + Guid.NewGuid();
string newPluginPath = Path.Combine(pluginFolerPath, newPluginName);
string content = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" +
$"Name: {plugin.Name}{Environment.NewLine}" +
$"Version: {plugin.Version}{Environment.NewLine}" +
$"Author: {plugin.Author}";
PluginPair existingPlugin = PluginManager.GetPluginForId(plugin.ID);
2014-03-13 22:31:44 +08:00
if (existingPlugin != null)
2014-01-12 16:28:11 +08:00
{
content = $"Do you want to update following plugin?{Environment.NewLine}{Environment.NewLine}" +
$"Name: {plugin.Name}{Environment.NewLine}" +
$"Old Version: {existingPlugin.Metadata.Version}" +
$"{Environment.NewLine}New Version: {plugin.Version}" +
$"{Environment.NewLine}Author: {plugin.Author}";
2014-01-12 16:28:11 +08:00
}
var result = MessageBox.Show(content, "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
2014-01-12 16:28:11 +08:00
{
if (existingPlugin != null && Directory.Exists(existingPlugin.Metadata.PluginDirectory))
2014-01-12 16:28:11 +08:00
{
2014-12-15 20:38:42 +08:00
//when plugin is in use, we can't delete them. That's why we need to make plugin folder a random name
File.Create(Path.Combine(existingPlugin.Metadata.PluginDirectory, "NeedDelete.txt")).Close();
2014-01-12 16:28:11 +08:00
}
2014-03-13 22:31:44 +08:00
2014-01-12 16:28:11 +08:00
UnZip(path, newPluginPath, true);
Directory.Delete(tempFoler, true);
2014-03-13 22:31:44 +08:00
//exsiting plugins may be has loaded by application,
//if we try to delelte those kind of plugins, we will get a error that indicate the
//file is been used now.
//current solution is to restart wox. Ugly.
//if (MainWindow.Initialized)
//{
// Plugins.Init();
//}
if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" +
" Restart Wox to take effect?",
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
2014-01-12 16:28:11 +08:00
{
PluginManager.API.RestarApp();
2014-01-12 16:28:11 +08:00
}
}
}
}
2014-03-02 11:04:30 +08:00
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
2014-03-11 22:17:10 +08:00
{
2014-03-02 11:04:30 +08:00
string configPath = Path.Combine(pluginDirectory, "plugin.json");
PluginMetadata metadata;
2014-01-12 16:28:11 +08:00
2014-03-02 11:04:30 +08:00
if (!File.Exists(configPath))
2014-01-12 16:28:11 +08:00
{
return null;
}
try
{
2014-03-02 11:04:30 +08:00
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginDirectory = pluginDirectory;
2014-03-02 11:04:30 +08:00
}
catch (System.Exception)
2014-03-02 11:04:30 +08:00
{
string error = $"Parse plugin config {configPath} failed: json format is not valid";
2014-03-02 11:04:30 +08:00
#if (DEBUG)
2014-01-12 16:28:11 +08:00
{
throw new System.Exception(error);
2014-01-12 16:28:11 +08:00
}
2014-03-02 11:04:30 +08:00
#endif
return null;
}
if (!AllowedLanguage.IsAllowed(metadata.Language))
{
string error = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}";
2014-03-02 11:04:30 +08:00
#if (DEBUG)
2014-01-12 16:28:11 +08:00
{
throw new System.Exception(error);
2014-01-12 16:28:11 +08:00
}
2014-03-02 11:04:30 +08:00
#endif
return null;
2014-01-12 16:28:11 +08:00
}
2014-03-02 11:04:30 +08:00
if (!File.Exists(metadata.ExecuteFilePath))
2014-01-12 16:28:11 +08:00
{
string error = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist";
2014-03-02 11:04:30 +08:00
#if (DEBUG)
{
throw new System.Exception(error);
2014-03-02 11:04:30 +08:00
}
#endif
2014-01-12 16:28:11 +08:00
return null;
}
2014-03-02 11:04:30 +08:00
return metadata;
2014-01-12 16:28:11 +08:00
}
/// <summary>
/// unzip
/// </summary>
/// <param name="zipedFile">The ziped file.</param>
/// <param name="strDirectory">The STR directory.</param>
/// <param name="overWrite">overwirte</param>
2014-03-11 22:17:10 +08:00
private static void UnZip(string zipedFile, string strDirectory, bool overWrite)
2014-01-12 16:28:11 +08:00
{
if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith("\\"))
strDirectory = strDirectory + "\\";
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;
if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\";
string fileName = Path.GetFileName(pathToZip);
Directory.CreateDirectory(strDirectory + directoryName);
if (fileName != "")
{
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
{
byte[] data = new byte[2048];
while (true)
{
int size = s.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
}
s.Close();
}
}
}
}