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;
|
2019-12-14 21:40:10 +00:00
|
|
|
|
var value = tempKey.GetValue("");
|
|
|
|
|
return value == null || value.ToString().Equals($"\"{appPath}\" -i \"%1\"");
|
2019-10-30 17:19:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-12-14 21:40:10 +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);
|
2019-12-14 21:40:10 +00:00
|
|
|
|
foreach (var entry in ProgIDList)
|
2019-10-30 16:48:22 +00:00
|
|
|
|
{
|
|
|
|
|
if (entry.Key.Contains("\\"))
|
|
|
|
|
{
|
2019-12-14 21:40:10 +00:00
|
|
|
|
var tempKey = progIDKey?.CreateSubKey(entry.Key);
|
|
|
|
|
tempKey?.SetValue("", entry.Value.Replace("{appPath}", appPath));
|
2019-10-30 16:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
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);
|
2019-12-14 21:40:10 +00:00
|
|
|
|
foreach (var 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
|
|
|
|
}
|
|
|
|
|
}
|