PowerToys/Wox.Core/Plugin/PluginInstaller.cs

213 lines
8.3 KiB
C#
Raw Normal View History

2014-01-12 16:28:11 +08:00
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
2014-12-27 12:34:51 +08:00
using System.Windows.Forms;
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;
2015-01-02 23:07:49 +08:00
using MessageBox = System.Windows.Forms.MessageBox;
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;
}
2014-12-27 12:34:51 +08:00
string pluginFolerPath = PluginManager.DefaultPluginDirectory;
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
}
2015-01-02 23:07:49 +08:00
DialogResult result = System.Windows.Forms.MessageBox.Show(content, "Install plugin", MessageBoxButtons.YesNo,
2014-12-27 12:34:51 +08:00
MessageBoxIcon.Question);
if (result == DialogResult.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.\r\n Restart Wox to take effect?", "Install plugin",
2014-12-27 12:34:51 +08:00
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.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);
2014-12-27 12:34:51 +08:00
PluginManager.API.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-12-26 22:51:04 +08:00
metadata.PluginType = PluginType.User;
metadata.PluginDirectory = pluginDirectory;
2014-03-02 11:04:30 +08:00
}
catch (System.Exception)
2014-03-02 11:04:30 +08:00
{
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
{
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 = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
metadata.Language);
#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
{
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 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();
}
}
}
}