Exemplo n.º 1
0
        public static string SetupNXMHandling(Action <long, long, string> notifyProgress, Action <string> notifyFinished)
        {
            bool installNewCopy = true;
            var  value          = Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Classes\nxm\shell\open\command", "", null); // Get's 'Default'

            if (value is string path)
            {
                path = path.Replace(" \"%1\"", "").Trim('\"');
                if (!string.IsNullOrWhiteSpace(path))
                {
                    string nxmIniPath = Path.Combine(Directory.GetParent(path).FullName, "nxmhandler.ini");
                    // are we already using nxmhandler?
                    if (Path.GetFileName(path).Equals("nxmhandler.exe", StringComparison.InvariantCultureIgnoreCase) && File.Exists(nxmIniPath))
                    {
                        // Setup for nxmhandler already, we just need to adjust it to add M3
                        SetupM3InNXMHandler(nxmIniPath);
                        installNewCopy = false;
                    }
                }
            }

            if (installNewCopy)
            {
                // It's not setup. We will set up a copy of it
                var outpath = Utilities.GetCachedExecutablePath("nxmhandler");

                void onDownloadProgress(long done, long total)
                {
                    notifyProgress?.Invoke(done, total, "Downloading nxmhandler");
                }

                var archiveResult = OnlineContent.DownloadStaticAsset("nxmhandler.7z", onDownloadProgress);
                if (archiveResult.errorMessage == null)
                {
                    notifyProgress?.Invoke(0, -1, "Extracting nxmhandler");
                    var nxma = new SevenZipExtractor(archiveResult.download);
                    nxma.ExtractArchive(outpath);

                    // Register it
                    using var subkey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Classes\nxm\shell\open\command");
                    subkey.SetValue("", $"\"{ Path.Combine(outpath, "nxmhandler.exe")}\" \"%1\"");

                    var protocolKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes\nxm", true);
                    protocolKey.SetValue("URL Protocol", "");
                    protocolKey.SetValue("", "URL:NXM Protocol");

                    SetupM3InNXMHandler(Path.Combine(outpath, "nxmhandler.ini"));
                }
            }

            notifyFinished?.Invoke(null);
            return(null);
        }