2019-10-28 13:25:14 +00:00
|
|
|
|
using System;
|
2019-10-30 16:48:22 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-10-28 13:25:14 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
2019-10-28 13:03:58 +00:00
|
|
|
|
{
|
|
|
|
|
public class ExtensionManager
|
|
|
|
|
{
|
2019-10-28 13:25:14 +00:00
|
|
|
|
[DllImport("Shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
|
|
|
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
|
|
|
|
|
|
2019-10-28 13:03:58 +00:00
|
|
|
|
public static string Extension = ".wabbajack";
|
2019-10-28 13:25:14 +00:00
|
|
|
|
|
2019-10-30 16:48:22 +00:00
|
|
|
|
private static readonly string ProgIDPath = "Software\\Classes\\Wabbajack";
|
|
|
|
|
private static readonly string ExtPath = $"Software\\Classes\\{Extension}";
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<string, string> ProgIDList = new Dictionary<string, string>
|
2019-10-28 13:25:14 +00:00
|
|
|
|
{
|
2019-10-30 16:48:22 +00:00
|
|
|
|
{"", "Wabbajack"},
|
|
|
|
|
{"FriendlyTypeName", "Wabbajack"},
|
|
|
|
|
{"shell\\open\\command", "\"{appPath}\" -i \"%1\""},
|
|
|
|
|
};
|
2019-10-28 13:25:14 +00:00
|
|
|
|
|
2019-10-30 16:48:22 +00:00
|
|
|
|
private static readonly Dictionary<string, string> ExtList = new Dictionary<string, string>
|
2019-10-28 13:25:14 +00:00
|
|
|
|
{
|
2019-10-30 16:48:22 +00:00
|
|
|
|
{"", "Wabbajack"},
|
|
|
|
|
{"PerceivedType", "Compressed"}
|
|
|
|
|
};
|
2019-10-28 14:00:26 +00:00
|
|
|
|
|
2019-10-30 17:19:37 +00:00
|
|
|
|
public static bool NeedsUpdating(string appPath)
|
|
|
|
|
{
|
|
|
|
|
var progIDKey = Registry.CurrentUser.OpenSubKey(ProgIDPath);
|
|
|
|
|
var tempKey = progIDKey?.OpenSubKey("shell\\open\\command");
|
|
|
|
|
if (progIDKey == null || tempKey == null) return true;
|
|
|
|
|
return tempKey.GetValue("").ToString().Equals($"\"{appPath}\" -i \"%1\"");
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 11:14:14 +00:00
|
|
|
|
public static bool IsAssociated()
|
2019-10-28 14:00:26 +00:00
|
|
|
|
{
|
2019-10-30 16:48:22 +00:00
|
|
|
|
var progIDKey = Registry.CurrentUser.OpenSubKey(ProgIDPath);
|
|
|
|
|
var extKey = Registry.CurrentUser.OpenSubKey(ExtPath);
|
2019-10-30 20:27:44 +00:00
|
|
|
|
return (progIDKey != null && extKey != null);
|
2019-10-28 14:00:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 16:48:22 +00:00
|
|
|
|
public static void Associate(string appPath)
|
2019-10-28 14:00:26 +00:00
|
|
|
|
{
|
2019-10-30 16:48:22 +00:00
|
|
|
|
var progIDKey = Registry.CurrentUser.CreateSubKey(ProgIDPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
|
|
|
|
|
foreach (KeyValuePair<string, string> entry in ProgIDList)
|
|
|
|
|
{
|
|
|
|
|
if (entry.Key.Contains("\\"))
|
|
|
|
|
{
|
|
|
|
|
var tempKey = progIDKey.CreateSubKey(entry.Key);
|
|
|
|
|
tempKey.SetValue("", entry.Value.Replace("{appPath}", appPath));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
progIDKey?.SetValue(entry.Key, entry.Value.Replace("{appPath}", appPath));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-28 13:25:14 +00:00
|
|
|
|
|
2019-10-30 16:48:22 +00:00
|
|
|
|
var extKey = Registry.CurrentUser.CreateSubKey(ExtPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
|
|
|
|
|
foreach (KeyValuePair<string, string> entry in ExtList)
|
2019-10-28 14:07:30 +00:00
|
|
|
|
{
|
2019-10-30 16:48:22 +00:00
|
|
|
|
extKey?.SetValue(entry.Key, entry.Value);
|
2019-10-28 14:07:30 +00:00
|
|
|
|
}
|
2019-10-30 16:48:22 +00:00
|
|
|
|
|
|
|
|
|
progIDKey?.Close();
|
|
|
|
|
extKey?.Close();
|
2019-10-28 14:07:30 +00:00
|
|
|
|
SHChangeNotify(0x000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
|
2019-10-28 13:25:14 +00:00
|
|
|
|
}
|
2019-10-28 13:03:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|