ExtensionManager: Cleanup and refactoring

This commit is contained in:
erri120 2019-10-30 17:48:22 +01:00
parent 39fc9eba9d
commit 7c3f92ecc9
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
2 changed files with 45 additions and 62 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Win32;
@ -11,72 +12,53 @@ namespace Wabbajack.Common
public static string Extension = ".wabbajack";
private static readonly string ExtRegPath = $"Software\\Classes\\{Extension}";
private static readonly string AppRegPath = "Software\\Classes\\Applications\\Wabbajack.exe";
private static readonly string AppAssocRegPath =
$"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\{Extension}";
private static readonly string Win8RegPath = $"Software\\Classes\\{Extension.Replace(".", "")}_auto_file";
public static bool IsExtensionAssociated()
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>
{
return (Registry.CurrentUser.OpenSubKey(AppAssocRegPath, false) == null);
{"", "Wabbajack"},
{"FriendlyTypeName", "Wabbajack"},
{"shell\\open\\command", "\"{appPath}\" -i \"%1\""},
};
private static readonly Dictionary<string, string> ExtList = new Dictionary<string, string>
{
{"", "Wabbajack"},
{"PerceivedType", "Compressed"}
};
public static bool IsAssociated(string appPath)
{
var progIDKey = Registry.CurrentUser.OpenSubKey(ProgIDPath);
var extKey = Registry.CurrentUser.OpenSubKey(ExtPath);
return (progIDKey == null || extKey == null);
}
private static bool IsWin8()
public static void Associate(string appPath)
{
var winVersion = new Version(6, 2, 9200, 0);
return (Environment.OSVersion.Platform >= PlatformID.Win32NT &&
Environment.OSVersion.Version >= winVersion);
}
public static bool IsAssociationOutdated(string currentIconPath, string currentAppPath)
{
var extReg = Registry.CurrentUser.OpenSubKey(ExtRegPath, false);
var appReg = Registry.CurrentUser.OpenSubKey(AppAssocRegPath, false);
var appAssocReg = Registry.CurrentUser.OpenSubKey(AppAssocRegPath, false);
if (extReg == null || appReg == null || appAssocReg == null) return true;
if (extReg.OpenSubKey("DefaultIcon", false) == null) return true;
if (extReg.OpenSubKey("PerceivedType", false) == null) return true;
if (extReg.OpenSubKey("DefaultIcon", false)?.GetValue("").ToString() != currentIconPath) return true;
if (appReg.OpenSubKey("shell\\open\\command", false) == null) return true;
if (appReg.OpenSubKey("DefaultIcon", false) == null) return true;
if (appReg.OpenSubKey("shell\\open\\command", false)?.GetValue("").ToString() != $"\"{currentAppPath}\" -i %i") return true;
if (appReg.OpenSubKey("DefaultIcon", false)?.GetValue("").ToString() != currentIconPath) return true;
if (appAssocReg.OpenSubKey("UserChoice", false) == null) return true;
if (appAssocReg.OpenSubKey("UserChoice", false)?.GetValue("Progid").ToString() !=
"Applications\\Wabbajack.exe") return true;
if (!IsWin8()) return false;
if (extReg.GetValue("").ToString() != Extension.Replace(".", "") + "_auto_file") return true;
var win8FileReg = Registry.CurrentUser.OpenSubKey(Win8RegPath, false);
if (win8FileReg?.OpenSubKey("shell\\open\\command", false) == null) return true;
return win8FileReg.OpenSubKey("shell\\open\\command", false)?.GetValue("").ToString() !=
$"\"{currentAppPath}\" -i %i";
}
public static void AssociateExtension(string iconPath, string appPath)
{
var extReg = Registry.CurrentUser.CreateSubKey(ExtRegPath);
if (IsWin8())
extReg?.SetValue("", Extension.Replace(".", "") + "_auto_file");
var appReg = Registry.CurrentUser.CreateSubKey(AppRegPath);
var appAssocReg = Registry.CurrentUser.CreateSubKey(AppAssocRegPath);
extReg?.CreateSubKey("DefaultIcon")?.SetValue("", iconPath);
extReg?.CreateSubKey("PerceivedType")?.SetValue("", "Compressed");
appReg?.CreateSubKey("shell\\open\\command")?.SetValue("", $"\"{appPath}\" -i %i");
appReg?.CreateSubKey("DefaultIcon")?.SetValue("", iconPath);
appAssocReg?.CreateSubKey("UserChoice")?.SetValue("Progid", "Applications\\Wabbajack.exe");
if (IsWin8())
var progIDKey = Registry.CurrentUser.CreateSubKey(ProgIDPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
foreach (KeyValuePair<string, string> entry in ProgIDList)
{
var win8FileReg = Registry.CurrentUser.CreateSubKey(Win8RegPath);
win8FileReg?.CreateSubKey("shell\\open\\command")?.SetValue("", $"\"{appPath}\" -i %i");
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));
}
}
var extKey = Registry.CurrentUser.CreateSubKey(ExtPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
foreach (KeyValuePair<string, string> entry in ExtList)
{
extKey?.SetValue(entry.Key, entry.Value);
}
progIDKey?.Close();
extKey?.Close();
SHChangeNotify(0x000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}
}

View File

@ -95,9 +95,10 @@ namespace Wabbajack
s.CopyTo(fs);
}
}
if (!ExtensionManager.IsExtensionAssociated() || ExtensionManager.IsAssociationOutdated(iconPath, appPath))
if (!ExtensionManager.IsAssociated(appPath))
{
ExtensionManager.AssociateExtension(iconPath, appPath);
ExtensionManager.Associate(appPath);
}
Mode = mode;