PowerToys/Wox/Helper/PluginInstaller.cs

206 lines
7.9 KiB
C#
Raw Normal View History

2014-01-12 16:28:11 +08:00
using System;
using System.Diagnostics;
using System.IO;
2014-03-11 22:17:10 +08:00
using System.Linq;
2014-01-12 16:28:11 +08:00
using System.Windows;
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-03-11 23:54:37 +08:00
using Wox.PluginLoader;
2014-01-12 16:28:11 +08:00
2014-03-11 22:17:10 +08:00
namespace Wox.Helper
2014-01-12 16:28:11 +08:00
{
public class PluginInstaller
2014-01-12 16:28:11 +08:00
{
2014-03-11 22:17:10 +08:00
public 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;
}
2014-03-11 23:54:37 +08:00
string pluginFolerPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
2014-01-12 16:28:11 +08:00
if (!Directory.Exists(pluginFolerPath))
{
2014-03-11 23:54:37 +08:00
Directory.CreateDirectory(pluginFolerPath);
2014-01-12 16:28:11 +08:00
}
2014-03-13 22:31:44 +08:00
string newPluginPath = Path.Combine(pluginFolerPath, Guid.NewGuid().ToString());
2014-01-12 16:28:11 +08:00
string content = string.Format(
2014-03-11 23:54:37 +08:00
"Do you want to install following plugin?\r\n\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}",
2014-01-12 16:28:11 +08:00
plugin.Name, plugin.Version, plugin.Author);
2014-03-13 22:31:44 +08:00
PluginPair existingPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ID == plugin.ID);
if (existingPlugin != null)
2014-01-12 16:28:11 +08:00
{
content = string.Format(
2014-03-11 23:54:37 +08:00
"Do you want to update following plugin?\r\n\r\nName: {0}\r\nOld Version: {1}\r\nNew Version: {2}\r\nAuthor: {3}",
2014-03-13 22:31:44 +08:00
plugin.Name, existingPlugin.Metadata.Version, plugin.Version, plugin.Author);
2014-01-12 16:28:11 +08:00
}
2014-03-02 11:04:30 +08:00
MessageBoxResult result = MessageBox.Show(content, "Install plugin",
2014-01-12 16:28:11 +08:00
MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
2014-03-13 22:31:44 +08:00
if (existingPlugin != null && Directory.Exists(existingPlugin.Metadata.PluginDirecotry))
2014-01-12 16:28:11 +08:00
{
2014-03-13 22:31:44 +08:00
File.Create(Path.Combine(existingPlugin.Metadata.PluginDirecotry, "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.\r\n Restart Wox to take effect?", "Install plugin",
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
2014-01-12 16:28:11 +08:00
{
2014-03-13 22:31:44 +08:00
ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 1 && \"" +
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wox.exe") + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
App.Window.CloseApp();
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));
2014-01-12 16:28:11 +08:00
metadata.PluginType = PluginType.ThirdParty;
2014-03-02 11:04:30 +08:00
metadata.PluginDirecotry = pluginDirectory;
}
catch (Exception)
{
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
#if (DEBUG)
2014-01-12 16:28:11 +08:00
{
2014-03-02 11:04:30 +08:00
throw new 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 = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
metadata.Language);
#if (DEBUG)
2014-01-12 16:28:11 +08:00
{
2014-03-02 11:04:30 +08:00
throw new 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
{
2014-03-02 11:04:30 +08:00
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath,
metadata.ExecuteFilePath);
#if (DEBUG)
{
throw new Exception(error);
}
#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();
}
}
}
}