PowerToys/Wox/Helper/PluginInstaller.cs

217 lines
8.4 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-12-26 19:36:43 +08:00
using Wox.Core.Plugin;
2014-01-29 18:33:24 +08:00
using Wox.Plugin;
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;
}
string pluginFolerPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "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-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);
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-12-26 19:36:43 +08:00
PluginPair existingPlugin = PluginManager.GetPlugin(plugin.ID);
2014-03-13 22:31:44 +08:00
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)
{
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.\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 && \"" +
System.Windows.Forms.Application.ExecutablePath + "\"";
2014-03-13 22:31:44 +08:00
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;
metadata.PluginDirectory = pluginDirectory;
2014-03-02 11:04:30 +08:00
}
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();
}
}
}
}