2014-01-12 09:28:11 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using ICSharpCode.SharpZipLib.Zip;
|
|
|
|
|
using Microsoft.Win32;
|
2014-03-02 04:04:30 +01:00
|
|
|
|
using Newtonsoft.Json;
|
2014-01-29 11:33:24 +01:00
|
|
|
|
using Wox.Plugin;
|
2014-01-12 09:28:11 +01:00
|
|
|
|
|
2014-03-02 04:04:30 +01:00
|
|
|
|
namespace Wox.UAC
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
2014-02-06 15:22:02 +01:00
|
|
|
|
public class PluginInstaller
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
2014-02-06 15:22:02 +01:00
|
|
|
|
[DllImport("shell32.dll")]
|
|
|
|
|
private static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// associate filetype with specified program
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filePath"></param>
|
|
|
|
|
/// <param name="fileType"></param>
|
|
|
|
|
/// <param name="iconPath"></param>
|
|
|
|
|
/// <param name="overrides"></param>
|
|
|
|
|
private static void SaveReg(string filePath, string fileType, string iconPath, bool overrides)
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
2014-02-06 15:22:02 +01:00
|
|
|
|
RegistryKey classRootKey = Registry.ClassesRoot.OpenSubKey("", true);
|
|
|
|
|
RegistryKey woxKey = classRootKey.OpenSubKey(fileType, true);
|
|
|
|
|
if (woxKey != null)
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
2014-02-06 15:22:02 +01:00
|
|
|
|
if (!overrides)
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
2014-02-06 15:22:02 +01:00
|
|
|
|
return;
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
2014-02-06 15:22:02 +01:00
|
|
|
|
classRootKey.DeleteSubKeyTree(fileType);
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
2014-02-06 15:22:02 +01:00
|
|
|
|
classRootKey.CreateSubKey(fileType);
|
|
|
|
|
woxKey = classRootKey.OpenSubKey(fileType, true);
|
|
|
|
|
woxKey.SetValue("", "wox.wox");
|
|
|
|
|
woxKey.SetValue("Content Type", "application/wox");
|
|
|
|
|
|
|
|
|
|
RegistryKey iconKey = woxKey.CreateSubKey("DefaultIcon");
|
|
|
|
|
iconKey.SetValue("", iconPath);
|
|
|
|
|
|
|
|
|
|
woxKey.CreateSubKey("shell");
|
|
|
|
|
RegistryKey shellKey = woxKey.OpenSubKey("shell", true);
|
|
|
|
|
shellKey.SetValue("", "Open");
|
|
|
|
|
RegistryKey openKey = shellKey.CreateSubKey("open");
|
|
|
|
|
openKey.SetValue("", "Open with wox");
|
|
|
|
|
|
|
|
|
|
openKey = shellKey.OpenSubKey("open", true);
|
|
|
|
|
openKey.CreateSubKey("command");
|
|
|
|
|
RegistryKey commandKey = openKey.OpenSubKey("command", true);
|
2014-03-02 04:04:30 +01:00
|
|
|
|
string pathString = "\"" + filePath + "\" \"installPlugin\" \"%1\"";
|
2014-02-06 15:22:02 +01:00
|
|
|
|
commandKey.SetValue("", pathString);
|
|
|
|
|
|
|
|
|
|
//refresh cache
|
|
|
|
|
SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 15:22:02 +01:00
|
|
|
|
public void RegisterInstaller()
|
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
string filePath = Directory.GetCurrentDirectory() + "\\Wox.UAC.exe";
|
2014-02-06 15:22:02 +01:00
|
|
|
|
string iconPath = Directory.GetCurrentDirectory() + "\\app.ico";
|
|
|
|
|
|
2014-03-02 04:04:30 +01:00
|
|
|
|
SaveReg(filePath, ".wox", iconPath, true);
|
2014-02-06 15:22:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Install(string path)
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
string tempFoler = System.IO.Path.GetTempPath() + "\\wox\\plugins";
|
2014-01-12 09:28:11 +01:00
|
|
|
|
if (Directory.Exists(tempFoler))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(tempFoler, true);
|
|
|
|
|
}
|
|
|
|
|
UnZip(path, tempFoler, true);
|
|
|
|
|
|
2014-03-02 04:04:30 +01:00
|
|
|
|
string iniPath = tempFoler + "\\plugin.json";
|
2014-01-12 09:28:11 +01:00
|
|
|
|
if (!File.Exists(iniPath))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Install failed: config is missing");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-02 04:04:30 +01:00
|
|
|
|
PluginMetadata plugin = GetMetadataFromJson(tempFoler);
|
2014-01-12 09:28:11 +01:00
|
|
|
|
if (plugin == null || plugin.Name == null)
|
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
MessageBox.Show("Install failed: config of this plugin is invalid");
|
2014-01-12 09:28:11 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string pluginFolerPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
|
|
|
|
|
if (!Directory.Exists(pluginFolerPath))
|
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
MessageBox.Show("Install failed: cound't find plugin directory");
|
2014-01-12 09:28:11 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string newPluginPath = pluginFolerPath + "\\" + plugin.Name;
|
|
|
|
|
string content = string.Format(
|
2014-03-02 04:04:30 +01:00
|
|
|
|
"Do you want to install following plugin?\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}",
|
2014-01-12 09:28:11 +01:00
|
|
|
|
plugin.Name, plugin.Version, plugin.Author);
|
|
|
|
|
if (Directory.Exists(newPluginPath))
|
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
PluginMetadata existingPlugin = GetMetadataFromJson(newPluginPath);
|
2014-01-12 09:28:11 +01:00
|
|
|
|
if (existingPlugin == null || existingPlugin.Name == null)
|
|
|
|
|
{
|
|
|
|
|
//maybe broken plugin, just delete it
|
|
|
|
|
Directory.Delete(newPluginPath, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
content = string.Format(
|
2014-03-02 04:04:30 +01:00
|
|
|
|
"Do you want to update following plugin?\r\nName: {0}\r\nOld Version: {1}\r\nNew Version: {2}\r\nAuthor: {3}",
|
2014-01-12 09:28:11 +01:00
|
|
|
|
plugin.Name, existingPlugin.Version, plugin.Version, plugin.Author);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-02 04:04:30 +01:00
|
|
|
|
MessageBoxResult result = MessageBox.Show(content, "Install plugin",
|
2014-01-12 09:28:11 +01:00
|
|
|
|
MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(newPluginPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(newPluginPath, true);
|
|
|
|
|
}
|
|
|
|
|
UnZip(path, newPluginPath, true);
|
|
|
|
|
Directory.Delete(tempFoler, true);
|
|
|
|
|
|
|
|
|
|
|
2014-01-29 11:33:24 +01:00
|
|
|
|
string wox = AppDomain.CurrentDomain.BaseDirectory + "Wox.exe";
|
|
|
|
|
if (File.Exists(wox))
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
ProcessStartInfo info = new ProcessStartInfo(wox, "reloadplugin")
|
2014-01-12 14:02:39 +01:00
|
|
|
|
{
|
|
|
|
|
UseShellExecute = true
|
|
|
|
|
};
|
|
|
|
|
Process.Start(info);
|
2014-03-02 04:04:30 +01:00
|
|
|
|
MessageBox.Show("You have installed plugin " + plugin.Name + " successfully.");
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
MessageBox.Show("You have installed plugin " + plugin.Name + " successfully. Please restart your wox to use new plugin.");
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-02 04:04:30 +01:00
|
|
|
|
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
|
|
|
|
|
{
|
|
|
|
|
string configPath = Path.Combine(pluginDirectory, "plugin.json");
|
|
|
|
|
PluginMetadata metadata;
|
2014-01-12 09:28:11 +01:00
|
|
|
|
|
2014-03-02 04:04:30 +01:00
|
|
|
|
if (!File.Exists(configPath))
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
2014-01-12 09:28:11 +01:00
|
|
|
|
metadata.PluginType = PluginType.ThirdParty;
|
2014-03-02 04:04:30 +01: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 09:28:11 +01:00
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
throw new Exception(error);
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
2014-03-02 04:04:30 +01: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 09:28:11 +01:00
|
|
|
|
{
|
2014-03-02 04:04:30 +01:00
|
|
|
|
throw new Exception(error);
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
2014-03-02 04:04:30 +01:00
|
|
|
|
#endif
|
|
|
|
|
return null;
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
2014-03-02 04:04:30 +01:00
|
|
|
|
if (!File.Exists(metadata.ExecuteFilePath))
|
2014-01-12 09:28:11 +01:00
|
|
|
|
{
|
2014-03-02 04:04:30 +01: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 09:28:11 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-03-02 04:04:30 +01:00
|
|
|
|
|
|
|
|
|
return metadata;
|
2014-01-12 09:28:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// unzip
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="zipedFile">The ziped file.</param>
|
|
|
|
|
/// <param name="strDirectory">The STR directory.</param>
|
|
|
|
|
/// <param name="overWrite">overwirte</param>
|
2014-02-06 15:22:02 +01:00
|
|
|
|
private void UnZip(string zipedFile, string strDirectory, bool overWrite)
|
2014-01-12 09:28:11 +01: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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|