From ef13c679687f814745633a494cc44d0ef9d8186c Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 26 Dec 2022 17:28:38 +0100 Subject: [PATCH] add download button to notification --- EnvyUpdate/App.xaml.cs | 13 ++++-- EnvyUpdate/Debug.cs | 2 + EnvyUpdate/MainWindow.xaml.cs | 82 ++++++----------------------------- EnvyUpdate/Notify.cs | 3 ++ EnvyUpdate/Util.cs | 63 ++++++++++++++++++++++++++- 5 files changed, 90 insertions(+), 73 deletions(-) diff --git a/EnvyUpdate/App.xaml.cs b/EnvyUpdate/App.xaml.cs index 0f2c184..3bd4437 100644 --- a/EnvyUpdate/App.xaml.cs +++ b/EnvyUpdate/App.xaml.cs @@ -24,13 +24,18 @@ namespace EnvyUpdate // Obtain the arguments from the notification ToastArguments args = ToastArguments.Parse(toastArgs.Argument); - // Obtain any user input (text boxes, menu selections) from the notification - ValueSet userInput = toastArgs.UserInput; - // Need to dispatch to UI thread if performing UI operations Application.Current.Dispatcher.Invoke(delegate { - Util.ShowMain(); + switch (args.Get("action")) + { + case "download": + Process.Start(Util.GetGpuUrl()); + break; + default: + Util.ShowMain(); + break; + } }); }; } diff --git a/EnvyUpdate/Debug.cs b/EnvyUpdate/Debug.cs index 30a49cd..5d7e5fd 100644 --- a/EnvyUpdate/Debug.cs +++ b/EnvyUpdate/Debug.cs @@ -6,6 +6,8 @@ namespace EnvyUpdate { class Debug { + public static bool isDebug = false; + public static int LoadFakeIDs(string idType) { /* diff --git a/EnvyUpdate/MainWindow.xaml.cs b/EnvyUpdate/MainWindow.xaml.cs index b4346cc..2361e1d 100644 --- a/EnvyUpdate/MainWindow.xaml.cs +++ b/EnvyUpdate/MainWindow.xaml.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Net; +using System.Security.Cryptography; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Media; @@ -20,7 +21,6 @@ namespace EnvyUpdate private string onlineDriv = null; private string gpuURL = null; private string[] arguments = null; - private bool isDebug = false; private string skippedVer = null; public MainWindow() @@ -64,8 +64,7 @@ namespace EnvyUpdate { if (arguments.Contains("/debug")) { - MessageBox.Show("Debug mode!"); - isDebug = true; + Debug.isDebug = true; } else { @@ -76,7 +75,7 @@ namespace EnvyUpdate if (Util.IsDCH()) textblockLocalType.Text = "DCH"; - else if (isDebug) + else if (Debug.isDebug) textblockLocalType.Text = "DCH (Debug)"; else textblockLocalType.Text = "Standard"; @@ -116,15 +115,7 @@ namespace EnvyUpdate private async void Load() { - int psid = 0; - int pfid = 0; - int osid = 0; - int dtcid = 0; - int dtid = 0; - //TODO: Make a list of languages and match OS language to driver - //int langid; - - if (File.Exists(GlobalVars.exepath + "sd.envy")) + if (Util.GetDTID() == 18) radioSD.IsChecked = true; else radioGRD.IsChecked = true; @@ -133,69 +124,24 @@ namespace EnvyUpdate skippedVer = File.ReadLines(GlobalVars.exepath + "skip.envy").First(); // This little bool check is necessary for debug mode on systems without an Nvidia GPU. - if (!isDebug) + if (Debug.isDebug) { - psid = Util.GetIDs("psid"); - pfid = Util.GetIDs("pfid"); - osid = Util.GetIDs("osid"); - dtcid = Util.GetDTCID(); - //dtid = Util.GetDTID(); - } - else - { - psid = Debug.LoadFakeIDs("psid"); - pfid = Debug.LoadFakeIDs("pfid"); - osid = Debug.LoadFakeIDs("osid"); - dtcid = Debug.LoadFakeIDs("dtcid"); - dtid = Debug.LoadFakeIDs("dtid"); localDriv = Debug.LocalDriv(); textblockGPU.Text = localDriv; textblockGPUName.Text = Debug.GPUname(); } - //Temporary Studio Driver override logic until I have figured out how to detect it automatically - //TODO - try - { - if (radioSD.IsChecked == true) - dtid = 18; - else - dtid = 1; - } - catch (NullReferenceException) - { } + gpuURL = Util.GetGpuUrl(); - gpuURL = "http://www.nvidia.com/Download/processDriver.aspx?psid=" + psid.ToString() + "&pfid=" + pfid.ToString() + "&osid=" + osid.ToString() + "&dtcid=" + dtcid.ToString() + "&dtid=" + dtid.ToString(); // + "&lid=" + langid.ToString(); - WebClient c = new WebClient(); - gpuURL = c.DownloadString(gpuURL); - if (gpuURL.Contains("https://") || gpuURL.Contains("http://")) + using (var c = new WebClient()) { - //absolute url + string pContent = c.DownloadString(gpuURL); + var pattern = @"Windows\/\d{3}\.\d{2}"; + Regex rgx = new Regex(pattern); + var matches = rgx.Matches(pContent); + onlineDriv = Regex.Replace(Convert.ToString(matches[0]), "Windows/", ""); + textblockOnline.Text = onlineDriv; } - else if (gpuURL.Contains("//")) - { - //protocol agnostic url - gpuURL = "https:" + gpuURL; - } - else if (gpuURL.StartsWith("driverResults.aspx")) - { - //relative url - gpuURL = "https://www.nvidia.com/Download/" + gpuURL; - } - else - { - //panic. - MessageBox.Show("ERROR: Invalid API response from Nvidia. Please file an issue on GitHub."); - Environment.Exit(10); - } - - string pContent = c.DownloadString(gpuURL); - var pattern = @"Windows\/\d{3}\.\d{2}"; - Regex rgx = new Regex(pattern); - var matches = rgx.Matches(pContent); - onlineDriv = Regex.Replace(Convert.ToString(matches[0]), "Windows/", ""); - textblockOnline.Text = onlineDriv; - c.Dispose(); try { @@ -297,7 +243,7 @@ namespace EnvyUpdate { if (MessageBox.Show(Properties.Resources.exit_confirm, "", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { - ToastNotificationManagerCompat.Uninstall(); + ToastNotificationManagerCompat.Uninstall(); // Uninstall notifications to prevent issues with the app being portable. Application.Current.Shutdown(); } else diff --git a/EnvyUpdate/Notify.cs b/EnvyUpdate/Notify.cs index ac317c1..6230c6a 100644 --- a/EnvyUpdate/Notify.cs +++ b/EnvyUpdate/Notify.cs @@ -8,6 +8,9 @@ namespace EnvyUpdate { var toast = new ToastContentBuilder(); toast.AddText(Properties.Resources.update_popup_message); + toast.AddButton(new ToastButton() + .SetContent("Download") + .AddArgument("action", "download")); toast.Show(); } } diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index 76d5871..c8842f6 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -6,10 +6,12 @@ using System.IO; using System.Linq; using System.Management; using System.Net; +using System.Security.Cryptography; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Xml.Linq; +using Windows.Devices.Radios; namespace EnvyUpdate { @@ -383,7 +385,66 @@ namespace EnvyUpdate */ //TODO: find way to differentiate between driver types - return 1; + if (System.IO.File.Exists(GlobalVars.exepath + "sd.envy")) + return 18; + else + return 1; + } + + public static string GetGpuUrl() + { + //TODO: Make a list of languages and match OS language to driver + //int langid; + int psid; + int pfid; + int osid; + int dtcid; + int dtid; + + if (Debug.isDebug) + { + psid = Debug.LoadFakeIDs("psid"); + pfid = Debug.LoadFakeIDs("pfid"); + osid = Debug.LoadFakeIDs("osid"); + dtcid = Debug.LoadFakeIDs("dtcid"); + dtid = Debug.LoadFakeIDs("dtid"); + } + else + { + psid = GetIDs("psid"); + pfid = GetIDs("pfid"); + osid = GetIDs("osid"); + dtcid = GetDTCID(); + dtid = GetDTID(); + } + string gpuURL = "http://www.nvidia.com/Download/processDriver.aspx?psid=" + psid.ToString() + "&pfid=" + pfid.ToString() + "&osid=" + osid.ToString() + "&dtcid=" + dtcid.ToString() + "&dtid=" + dtid.ToString(); // + "&lid=" + langid.ToString(); + + using (var c = new WebClient()) + { + gpuURL = c.DownloadString(gpuURL); + if (gpuURL.Contains("https://") || gpuURL.Contains("http://")) + { + //absolute url + } + else if (gpuURL.Contains("//")) + { + //protocol agnostic url + gpuURL = "https:" + gpuURL; + } + else if (gpuURL.StartsWith("driverResults.aspx")) + { + //relative url + gpuURL = "https://www.nvidia.com/Download/" + gpuURL; + } + else + { + //panic. + MessageBox.Show("ERROR: Invalid API response from Nvidia. Please file an issue on GitHub."); + Environment.Exit(10); + } + } + + return gpuURL; } public static void DownloadFile(string fileURL, string savePath)