From 7d0460d1c26ecabf0c84bcfe707557149e46760b Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 3 Apr 2023 18:57:04 +0200 Subject: [PATCH 01/76] remove langid stubs --- EnvyUpdate/Util.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index 7ad428b..f01767d 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -141,9 +141,6 @@ namespace EnvyUpdate case "osid": xmlcontent = wc.DownloadString("https://www.nvidia.com/Download/API/lookupValueSearch.aspx?TypeID=4"); break; - case "langid": - xmlcontent = wc.DownloadString("https://www.nvidia.com/Download/API/lookupValueSearch.aspx?TypeID=5"); - break; default: break; } @@ -162,10 +159,6 @@ namespace EnvyUpdate case "osid": id = GetOSID(); break; - case "langid": - // Currently unsupported, because Nvidia has a weird way of naming languages in their native OR english version. - // https://www.nvidia.com/Download/API/lookupValueSearch.aspx?TypeID=5 - break; default: break; } From 41f25d48fb00c41f22744d0441242e69e57f066c Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 3 Apr 2023 19:00:51 +0200 Subject: [PATCH 02/76] add comments for GetOSID --- EnvyUpdate/Util.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index f01767d..72df3c4 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -243,18 +243,18 @@ namespace EnvyUpdate string OS = Environment.OSVersion.Version.Major.ToString() + "." + Environment.OSVersion.Version.Minor.ToString(); // Here the 32bit values are used. Later, if the OS is 64bit, we'll add 1, since that is how Nvidia does their IDs. - switch (OS) + switch (OS) //TODO until Win10 EOL: Differentiate between Win10 and Win11 { - case "10.0": + case "10.0": //Win10 or Win11 value = 56; break; - case "6.1": + case "6.1": //Win7 value = 18; break; - case "6.2": + case "6.2": //Win8 value = 27; break; - case "6.3": + case "6.3": //Win8.1 value = 40; break; default: From adb8a5508b23b8ce0061d104d6a8b0cf3f63ea14 Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 3 Apr 2023 19:16:01 +0200 Subject: [PATCH 03/76] cleanup util --- EnvyUpdate/Util.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index 72df3c4..60eb27d 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -138,9 +138,6 @@ namespace EnvyUpdate case "pfid": xmlcontent = wc.DownloadString("https://www.nvidia.com/Download/API/lookupValueSearch.aspx?TypeID=3"); break; - case "osid": - xmlcontent = wc.DownloadString("https://www.nvidia.com/Download/API/lookupValueSearch.aspx?TypeID=4"); - break; default: break; } @@ -178,10 +175,10 @@ namespace EnvyUpdate int value = 0; int i = 0; int value1 = 0; - int value2 = 0; + int value2 = 0; //Two values are used to cover the eventuality of there being two Nvidia cards (unlikely) in a mobile device var names = xDoc.Descendants("Name"); - foreach (var name in names) + foreach (var name in names) // Looping through the XML Doc because the name is not the primary key { string sName = name.Value.ToString().ToLower(); if (sName == query) From 671824f3cd373f4b7cf19e2e7b1d8627a2eedae8 Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 3 Apr 2023 20:37:11 +0200 Subject: [PATCH 04/76] working to implement full logging --- EnvyUpdate/Debug.cs | 13 +++- EnvyUpdate/InfoWindow.xaml.cs | 2 + EnvyUpdate/MainWindow.xaml.cs | 135 +++++++++++++++++++++++++++++++++- EnvyUpdate/Util.cs | 48 +++++++++++- 4 files changed, 191 insertions(+), 7 deletions(-) diff --git a/EnvyUpdate/Debug.cs b/EnvyUpdate/Debug.cs index 676384b..c0b6a5f 100644 --- a/EnvyUpdate/Debug.cs +++ b/EnvyUpdate/Debug.cs @@ -1,12 +1,16 @@ using System; +using System.Diagnostics; using System.IO; using System.Linq; +using System.Security.Policy; namespace EnvyUpdate { class Debug { - public static bool isDebug = false; + public static bool isFake = false; + public static bool isVerbose = false; + public static string debugFile = Path.Combine(GlobalVars.exedirectory, "envyupdate.log"); public static int LoadFakeIDs(string idType) { @@ -38,5 +42,12 @@ namespace EnvyUpdate { return "Nvidia GeForce RTX 4080 (debug)"; } + + [ConditionalAttribute("DEBUG")] + public static void LogToFile(string content) + { + if (isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, content); + } } } diff --git a/EnvyUpdate/InfoWindow.xaml.cs b/EnvyUpdate/InfoWindow.xaml.cs index 6efe4a5..a7ee090 100644 --- a/EnvyUpdate/InfoWindow.xaml.cs +++ b/EnvyUpdate/InfoWindow.xaml.cs @@ -25,6 +25,8 @@ namespace EnvyUpdate private void ButtonWeb_Click(object sender, RoutedEventArgs e) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Launching website."); System.Diagnostics.Process.Start("https://github.com/fyr77/EnvyUpdate/"); } } diff --git a/EnvyUpdate/MainWindow.xaml.cs b/EnvyUpdate/MainWindow.xaml.cs index ecdb465..8b17b38 100644 --- a/EnvyUpdate/MainWindow.xaml.cs +++ b/EnvyUpdate/MainWindow.xaml.cs @@ -37,47 +37,82 @@ namespace EnvyUpdate // This is necessary, since .NET throws an exception if you check for a non-existant arg. } + if (arguments.Contains("/verbose")) + { + Debug.isVerbose = true; + if (!File.Exists(Debug.debugFile)) + File.CreateText(Debug.debugFile); + File.AppendAllText(Debug.debugFile, "INFO Starting EnvyUpdate, version " + System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion); + } + // Check if EnvyUpdate is already running if (Util.IsInstanceOpen("EnvyUpdate")) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "FATAL Found another instance, terminating."); + MessageBox.Show(Properties.Resources.instance_already_running); Environment.Exit(1); } // Delete installed legacy versions if (Directory.Exists(GlobalVars.appdata)) + { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Found old appdata installation, uninstalling."); UninstallAll(); + } GlobalVars.isMobile = Util.IsMobile(); + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Mobile: " + GlobalVars.isMobile); localDriv = Util.GetLocDriv(); + + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Local driver version: " + localDriv); + if (localDriv != null) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Local driver version already known, updating info without reloading."); UpdateLocalVer(false); } else { - if (arguments.Contains("/debug")) + if (arguments.Contains("/fake")) { - Debug.isDebug = true; + Debug.isFake = true; + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "WARN Faking GPU with debug info."); } else { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "FATAL No supported GPU found, terminating."); MessageBox.Show(Properties.Resources.no_compatible_gpu); Environment.Exit(255); } } + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Detecting driver type."); + if (Util.IsDCH()) textblockLocalType.Text = "DCH"; - else if (Debug.isDebug) + else if (Debug.isFake) textblockLocalType.Text = "DCH (Debug)"; else textblockLocalType.Text = "Standard"; + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Done detecting driver type."); + // Check for startup shortcut if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "EnvyUpdate.lnk"))) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Autostart is enabled."); chkAutostart.IsChecked = true; chkAutostart_Click(null, null); //Automatically recreate shortcut to account for moved EXE. } @@ -85,6 +120,8 @@ namespace EnvyUpdate //Check if launched as miminized with arg if (arguments.Contains("/minimize")) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Launching minimized."); WindowState = WindowState.Minimized; Hide(); } @@ -94,6 +131,8 @@ namespace EnvyUpdate // Check for new updates every 5 hours. Dt.Interval = new TimeSpan(5, 0, 0); Dt.Start(); + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Started check timer."); string watchDirPath = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramW6432%"), "NVIDIA Corporation\\Installer2\\InstallerCore"); if (Directory.Exists(watchDirPath)) @@ -112,6 +151,8 @@ namespace EnvyUpdate driverFileChangedWatcher.Filter = "*.dll"; driverFileChangedWatcher.IncludeSubdirectories = false; driverFileChangedWatcher.EnableRaisingEvents = true; + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Started update file system watcher."); } Load(); @@ -124,6 +165,8 @@ namespace EnvyUpdate private void buttonHelp_Click(object sender, RoutedEventArgs e) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Showing info window."); InfoWindow infoWin = new InfoWindow(); infoWin.ShowDialog(); } @@ -131,15 +174,27 @@ namespace EnvyUpdate private void Load() { if (Util.GetDTID() == 18) + { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Found studio driver."); radioSD.IsChecked = true; + } else + { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Found standard driver."); radioGRD.IsChecked = true; + } if (File.Exists(GlobalVars.exedirectory + "skip.envy")) + { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Found version skip config."); skippedVer = File.ReadLines(GlobalVars.exedirectory + "skip.envy").First(); + } // This little bool check is necessary for debug mode on systems without an Nvidia GPU. - if (Debug.isDebug) + if (Debug.isFake) { localDriv = Debug.LocalDriv(); textblockGPU.Text = localDriv; @@ -148,10 +203,14 @@ namespace EnvyUpdate try { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Trying to get GPU update URL."); gpuURL = Util.GetGpuUrl(); } catch (ArgumentException) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "WARN Could not get GPU update URL, trying again with standard driver."); try { // disable SD and try with GRD @@ -167,6 +226,8 @@ namespace EnvyUpdate catch (ArgumentException e) { // Now we have a problem. + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "FATAL Invalid API response from Nvidia. Attempted API call: " + e.Message); MessageBox.Show("ERROR: Invalid API response from Nvidia. Please file an issue on GitHub.\nAttempted API call:\n" + e.Message); Environment.Exit(10); } @@ -174,18 +235,24 @@ namespace EnvyUpdate using (var c = new WebClient()) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Trying to get newest driver version."); 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; + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Got online driver version: " + onlineDriv); } try { if (float.Parse(localDriv) < float.Parse(onlineDriv)) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Local version is older than online. Setting UI..."); textblockOnline.Foreground = Brushes.Red; buttonDL.IsEnabled = true; if (skippedVer == null) @@ -195,22 +262,36 @@ namespace EnvyUpdate } else buttonSkip.Content = Properties.Resources.ui_skipped; + + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO UI set."); + if (skippedVer != onlineDriv) + { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Showing update popup notification."); Notify.ShowDrivUpdatePopup(); + } } else { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Local version is up to date."); buttonSkip.IsEnabled = false; textblockOnline.Foreground = Brushes.Green; } } catch (FormatException) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Caught FormatException, assuming locale workaround is necessary."); //Thank you locales. Some languages need , instead of . for proper parsing string cLocalDriv = localDriv.Replace('.', ','); string cOnlineDriv = onlineDriv.Replace('.', ','); if (float.Parse(cLocalDriv) < float.Parse(cOnlineDriv)) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Local version is older than online. Setting UI..."); textblockOnline.Foreground = Brushes.Red; buttonDL.IsEnabled = true; if (skippedVer == null) @@ -218,10 +299,16 @@ namespace EnvyUpdate else buttonSkip.Content = Properties.Resources.ui_skipped; if (skippedVer != onlineDriv) + { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Showing update popup notification."); Notify.ShowDrivUpdatePopup(); + } } else { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Local version is up to date."); buttonSkip.IsEnabled = false; textblockOnline.Foreground = Brushes.Green; } @@ -230,6 +317,8 @@ namespace EnvyUpdate //Check for different version than skipped version if (skippedVer != onlineDriv) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Skipped version is surpassed, deleting setting."); skippedVer = null; if (File.Exists(GlobalVars.exedirectory + "skip.envy")) File.Delete(GlobalVars.exedirectory + "skip.envy"); @@ -240,11 +329,15 @@ namespace EnvyUpdate private void buttonDL_Click(object sender, RoutedEventArgs e) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Opening download page."); Process.Start(gpuURL); } private void TaskbarIcon_TrayLeftMouseDown(object sender, RoutedEventArgs e) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Tray was clicked, opening main window."); Util.ShowMain(); } @@ -252,6 +345,8 @@ namespace EnvyUpdate { if (WindowState == WindowState.Minimized) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Window was minimized, closing to tray."); Hide(); } } @@ -260,20 +355,28 @@ namespace EnvyUpdate { if (File.Exists(GlobalVars.startup + "\\EnvyUpdate.lnk")) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Deleted startup entry."); File.Delete(GlobalVars.startup + "\\EnvyUpdate.lnk"); } if (File.Exists(GlobalVars.startmenu + "\\EnvyUpdate.lnk")) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Deleted start menu entry."); File.Delete(GlobalVars.startmenu + "\\EnvyUpdate.lnk"); } if ((GlobalVars.exedirectory == GlobalVars.appdata) && File.Exists(GlobalVars.appdata + "EnvyUpdate.exe")) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Deleting EnvyUpdate appdata and self."); MessageBox.Show(Properties.Resources.uninstall_legacy_message); Util.SelfDelete(); } else if (Directory.Exists(GlobalVars.appdata)) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Deleting EnvyUpdate appdata folder"); Directory.Delete(GlobalVars.appdata, true); } } @@ -282,17 +385,25 @@ namespace EnvyUpdate { if (MessageBox.Show(Properties.Resources.exit_confirm, "", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Uninstalling notifications and shutting down."); ToastNotificationManagerCompat.Uninstall(); // Uninstall notifications to prevent issues with the app being portable. Application.Current.Shutdown(); } else + { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Application shutdown was cancelled."); e.Cancel = true; + } } private void radioGRD_Checked(object sender, RoutedEventArgs e) { if (File.Exists(GlobalVars.exedirectory + "sd.envy")) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Switching to game ready driver."); File.Delete(GlobalVars.exedirectory + "sd.envy"); Load(); } @@ -302,6 +413,8 @@ namespace EnvyUpdate { if (!File.Exists(GlobalVars.exedirectory + "sd.envy")) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Switching to studio driver."); File.Create(GlobalVars.exedirectory + "sd.envy").Close(); Load(); } @@ -311,16 +424,22 @@ namespace EnvyUpdate { if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "EnvyUpdate.lnk"))) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Removing autostart entry."); File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "EnvyUpdate.lnk")); } if (chkAutostart.IsChecked == true) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Creating autostart entry."); Util.CreateShortcut("EnvyUpdate", Environment.GetFolderPath(Environment.SpecialFolder.Startup), GlobalVars.exeloc, "NVidia Update Checker", "/minimize"); } } private void buttonSkip_Click(object sender, RoutedEventArgs e) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Skipping version."); skippedVer = onlineDriv; File.WriteAllText(GlobalVars.exedirectory + "skip.envy", onlineDriv); buttonSkip.IsEnabled = false; @@ -330,8 +449,14 @@ namespace EnvyUpdate private void UpdateLocalVer(bool reloadLocalDriv = true) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Updating local driver version in UI."); if (reloadLocalDriv) + { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Reloading local driver version."); localDriv = Util.GetLocDriv(); + } textblockGPU.Text = localDriv; if (GlobalVars.isMobile) textblockGPUName.Text = Util.GetGPUName(false) + " (mobile)"; @@ -341,6 +466,8 @@ namespace EnvyUpdate void DriverFileChanged(object sender, FileSystemEventArgs e) { + if (Debug.isVerbose) + File.AppendAllText(Debug.debugFile, "INFO Watched driver file changed! Reloading data."); System.Threading.Thread.Sleep(10000); Application.Current.Dispatcher.Invoke(delegate { diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index 60eb27d..0329f35 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -29,6 +29,9 @@ namespace EnvyUpdate // query local driver version try { + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Looking for driver version in ManagementObjects"); + foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT * FROM Win32_VideoController").Get()) { if (obj["Description"].ToString().ToLower().Contains("nvidia")) @@ -37,12 +40,18 @@ namespace EnvyUpdate OfflineGPUVersion = OfflineGPUVersion.Substring(Math.Max(0, OfflineGPUVersion.Length - 5)); OfflineGPUVersion = OfflineGPUVersion.Substring(0, 3) + "." + OfflineGPUVersion.Substring(3); // add dot foundGpu = true; + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Found driver in ManagementObjects."); break; } } if (!foundGpu) + { + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "WARN Did NOT find driver in ManagementObjects."); throw new InvalidDataException(); + } return OfflineGPUVersion; } @@ -70,6 +79,8 @@ namespace EnvyUpdate shortcut.Arguments = arguments; shortcut.Description = description; shortcut.TargetPath = targetFileLocation; + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Saving shortcut link."); shortcut.Save(); } /// @@ -126,10 +137,11 @@ namespace EnvyUpdate } public static int GetIDs(string IDtype) { - // TODO: check for 2 occurences of GPU - if yes ask if mobile!!! string xmlcontent = null; int id = -1; + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Getting Nvidia GPU list..."); using (var wc = new WebClient()) { switch (IDtype) @@ -142,6 +154,16 @@ namespace EnvyUpdate break; } } + + if (Debug.isVerbose) + { + System.IO.File.AppendAllText(Debug.debugFile, "INFO Got Nvidia GPU list."); + if (xmlcontent == null) + { + System.IO.File.AppendAllText(Debug.debugFile, "WARN GPU list is NULL! This is a possible error source."); + } + } + XDocument xDoc = XDocument.Parse(xmlcontent); string gpuName = GetGPUName(true); @@ -149,14 +171,22 @@ namespace EnvyUpdate { case "psid": id = GetValueFromName(xDoc, gpuName, true); + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Got psid: " + id); break; case "pfid": id = GetValueFromName(xDoc, gpuName, false); + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Got pfid: " + id); break; case "osid": id = GetOSID(); + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Got osid: " + id); break; default: + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "WARN GetIDs was called, but nothing was specified."); break; } @@ -183,10 +213,15 @@ namespace EnvyUpdate string sName = name.Value.ToString().ToLower(); if (sName == query) { + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "DEBUG Matched GetValueFromName query: " + sName); string cleanResult = null; if (psid) { + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "DEBUG Getting psid."); + if (i == 0) value1 = int.Parse(name.Parent.FirstAttribute.Value); else @@ -194,6 +229,9 @@ namespace EnvyUpdate } else { + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "DEBUG Getting something else than psid."); + string result = name.Parent.Value.ToLower(); int index = result.IndexOf(sName); cleanResult = (index < 0) @@ -226,6 +264,7 @@ namespace EnvyUpdate } } + return value; } /// @@ -271,6 +310,9 @@ namespace EnvyUpdate public static string GetGPUName(bool lower) { string GPUName = null; + + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Trying to get GPU name from ManagementObjects..."); foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT * FROM Win32_VideoController").Get()) { //if (obj["Description"].ToString().ToLower().Contains("radeon")) @@ -296,6 +338,8 @@ namespace EnvyUpdate break; } } + if (Debug.isVerbose) + System.IO.File.AppendAllText(Debug.debugFile, "INFO Found GPU name: " + GPUName); // This should NEVER return null outside of debugging mode, since EnvyUpdate should refuse to start without and Nvidia GPU. return GPUName; } @@ -388,7 +432,7 @@ namespace EnvyUpdate int dtcid; int dtid; - if (Debug.isDebug) + if (Debug.isFake) { psid = Debug.LoadFakeIDs("psid"); pfid = Debug.LoadFakeIDs("pfid"); From e4281943cd4010d63a31329c171dc5ca81f6a48d Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 3 Apr 2023 20:55:22 +0200 Subject: [PATCH 05/76] finish implementing logging --- EnvyUpdate/InfoWindow.xaml.cs | 3 +- EnvyUpdate/MainWindow.xaml.cs | 141 ++++++++++++---------------------- EnvyUpdate/Util.cs | 76 +++++++++--------- 3 files changed, 88 insertions(+), 132 deletions(-) diff --git a/EnvyUpdate/InfoWindow.xaml.cs b/EnvyUpdate/InfoWindow.xaml.cs index a7ee090..9bba5d5 100644 --- a/EnvyUpdate/InfoWindow.xaml.cs +++ b/EnvyUpdate/InfoWindow.xaml.cs @@ -25,8 +25,7 @@ namespace EnvyUpdate private void ButtonWeb_Click(object sender, RoutedEventArgs e) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Launching website."); + Debug.LogToFile("INFO Launching website."); System.Diagnostics.Process.Start("https://github.com/fyr77/EnvyUpdate/"); } } diff --git a/EnvyUpdate/MainWindow.xaml.cs b/EnvyUpdate/MainWindow.xaml.cs index 8b17b38..b10aa50 100644 --- a/EnvyUpdate/MainWindow.xaml.cs +++ b/EnvyUpdate/MainWindow.xaml.cs @@ -48,8 +48,7 @@ namespace EnvyUpdate // Check if EnvyUpdate is already running if (Util.IsInstanceOpen("EnvyUpdate")) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "FATAL Found another instance, terminating."); + Debug.LogToFile("FATAL Found another instance, terminating."); MessageBox.Show(Properties.Resources.instance_already_running); Environment.Exit(1); @@ -58,24 +57,20 @@ namespace EnvyUpdate // Delete installed legacy versions if (Directory.Exists(GlobalVars.appdata)) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Found old appdata installation, uninstalling."); + Debug.LogToFile("INFO Found old appdata installation, uninstalling."); UninstallAll(); } GlobalVars.isMobile = Util.IsMobile(); - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Mobile: " + GlobalVars.isMobile); + Debug.LogToFile("INFO Mobile: " + GlobalVars.isMobile); localDriv = Util.GetLocDriv(); - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Local driver version: " + localDriv); + Debug.LogToFile("INFO Local driver version: " + localDriv); if (localDriv != null) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Local driver version already known, updating info without reloading."); + Debug.LogToFile("INFO Local driver version already known, updating info without reloading."); UpdateLocalVer(false); } else @@ -83,13 +78,11 @@ namespace EnvyUpdate if (arguments.Contains("/fake")) { Debug.isFake = true; - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "WARN Faking GPU with debug info."); + Debug.LogToFile("WARN Faking GPU with debug info."); } else { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "FATAL No supported GPU found, terminating."); + Debug.LogToFile("FATAL No supported GPU found, terminating."); MessageBox.Show(Properties.Resources.no_compatible_gpu); Environment.Exit(255); } @@ -105,14 +98,12 @@ namespace EnvyUpdate else textblockLocalType.Text = "Standard"; - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Done detecting driver type."); + Debug.LogToFile("INFO Done detecting driver type."); // Check for startup shortcut if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "EnvyUpdate.lnk"))) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Autostart is enabled."); + Debug.LogToFile("INFO Autostart is enabled."); chkAutostart.IsChecked = true; chkAutostart_Click(null, null); //Automatically recreate shortcut to account for moved EXE. } @@ -120,8 +111,7 @@ namespace EnvyUpdate //Check if launched as miminized with arg if (arguments.Contains("/minimize")) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Launching minimized."); + Debug.LogToFile("INFO Launching minimized."); WindowState = WindowState.Minimized; Hide(); } @@ -131,8 +121,7 @@ namespace EnvyUpdate // Check for new updates every 5 hours. Dt.Interval = new TimeSpan(5, 0, 0); Dt.Start(); - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Started check timer."); + Debug.LogToFile("INFO Started check timer."); string watchDirPath = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramW6432%"), "NVIDIA Corporation\\Installer2\\InstallerCore"); if (Directory.Exists(watchDirPath)) @@ -151,8 +140,7 @@ namespace EnvyUpdate driverFileChangedWatcher.Filter = "*.dll"; driverFileChangedWatcher.IncludeSubdirectories = false; driverFileChangedWatcher.EnableRaisingEvents = true; - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Started update file system watcher."); + Debug.LogToFile("INFO Started update file system watcher."); } Load(); @@ -165,8 +153,7 @@ namespace EnvyUpdate private void buttonHelp_Click(object sender, RoutedEventArgs e) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Showing info window."); + Debug.LogToFile("INFO Showing info window."); InfoWindow infoWin = new InfoWindow(); infoWin.ShowDialog(); } @@ -175,21 +162,18 @@ namespace EnvyUpdate { if (Util.GetDTID() == 18) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Found studio driver."); + Debug.LogToFile("INFO Found studio driver."); radioSD.IsChecked = true; } else { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Found standard driver."); + Debug.LogToFile("INFO Found standard driver."); radioGRD.IsChecked = true; } if (File.Exists(GlobalVars.exedirectory + "skip.envy")) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Found version skip config."); + Debug.LogToFile("INFO Found version skip config."); skippedVer = File.ReadLines(GlobalVars.exedirectory + "skip.envy").First(); } @@ -203,14 +187,12 @@ namespace EnvyUpdate try { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Trying to get GPU update URL."); + Debug.LogToFile("INFO Trying to get GPU update URL."); gpuURL = Util.GetGpuUrl(); } catch (ArgumentException) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "WARN Could not get GPU update URL, trying again with standard driver."); + Debug.LogToFile("WARN Could not get GPU update URL, trying again with standard driver."); try { // disable SD and try with GRD @@ -226,8 +208,7 @@ namespace EnvyUpdate catch (ArgumentException e) { // Now we have a problem. - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "FATAL Invalid API response from Nvidia. Attempted API call: " + e.Message); + Debug.LogToFile("FATAL Invalid API response from Nvidia. Attempted API call: " + e.Message); MessageBox.Show("ERROR: Invalid API response from Nvidia. Please file an issue on GitHub.\nAttempted API call:\n" + e.Message); Environment.Exit(10); } @@ -235,24 +216,21 @@ namespace EnvyUpdate using (var c = new WebClient()) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Trying to get newest driver version."); + Debug.LogToFile("INFO Trying to get newest driver version."); 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; - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Got online driver version: " + onlineDriv); + Debug.LogToFile("INFO Got online driver version: " + onlineDriv); } try { if (float.Parse(localDriv) < float.Parse(onlineDriv)) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Local version is older than online. Setting UI..."); + Debug.LogToFile("INFO Local version is older than online. Setting UI..."); textblockOnline.Foreground = Brushes.Red; buttonDL.IsEnabled = true; if (skippedVer == null) @@ -263,35 +241,30 @@ namespace EnvyUpdate else buttonSkip.Content = Properties.Resources.ui_skipped; - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO UI set."); + Debug.LogToFile("INFO UI set."); if (skippedVer != onlineDriv) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Showing update popup notification."); + Debug.LogToFile("INFO Showing update popup notification."); Notify.ShowDrivUpdatePopup(); } } else { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Local version is up to date."); + Debug.LogToFile("INFO Local version is up to date."); buttonSkip.IsEnabled = false; textblockOnline.Foreground = Brushes.Green; } } catch (FormatException) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Caught FormatException, assuming locale workaround is necessary."); + Debug.LogToFile("INFO Caught FormatException, assuming locale workaround is necessary."); //Thank you locales. Some languages need , instead of . for proper parsing string cLocalDriv = localDriv.Replace('.', ','); string cOnlineDriv = onlineDriv.Replace('.', ','); if (float.Parse(cLocalDriv) < float.Parse(cOnlineDriv)) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Local version is older than online. Setting UI..."); + Debug.LogToFile("INFO Local version is older than online. Setting UI..."); textblockOnline.Foreground = Brushes.Red; buttonDL.IsEnabled = true; if (skippedVer == null) @@ -300,15 +273,13 @@ namespace EnvyUpdate buttonSkip.Content = Properties.Resources.ui_skipped; if (skippedVer != onlineDriv) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Showing update popup notification."); + Debug.LogToFile("INFO Showing update popup notification."); Notify.ShowDrivUpdatePopup(); } } else { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Local version is up to date."); + Debug.LogToFile("INFO Local version is up to date."); buttonSkip.IsEnabled = false; textblockOnline.Foreground = Brushes.Green; } @@ -317,8 +288,7 @@ namespace EnvyUpdate //Check for different version than skipped version if (skippedVer != onlineDriv) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Skipped version is surpassed, deleting setting."); + Debug.LogToFile("INFO Skipped version is surpassed, deleting setting."); skippedVer = null; if (File.Exists(GlobalVars.exedirectory + "skip.envy")) File.Delete(GlobalVars.exedirectory + "skip.envy"); @@ -329,15 +299,13 @@ namespace EnvyUpdate private void buttonDL_Click(object sender, RoutedEventArgs e) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Opening download page."); + Debug.LogToFile("INFO Opening download page."); Process.Start(gpuURL); } private void TaskbarIcon_TrayLeftMouseDown(object sender, RoutedEventArgs e) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Tray was clicked, opening main window."); + Debug.LogToFile("INFO Tray was clicked, opening main window."); Util.ShowMain(); } @@ -345,8 +313,7 @@ namespace EnvyUpdate { if (WindowState == WindowState.Minimized) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Window was minimized, closing to tray."); + Debug.LogToFile("INFO Window was minimized, closing to tray."); Hide(); } } @@ -355,28 +322,24 @@ namespace EnvyUpdate { if (File.Exists(GlobalVars.startup + "\\EnvyUpdate.lnk")) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Deleted startup entry."); + Debug.LogToFile("INFO Deleted startup entry."); File.Delete(GlobalVars.startup + "\\EnvyUpdate.lnk"); } if (File.Exists(GlobalVars.startmenu + "\\EnvyUpdate.lnk")) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Deleted start menu entry."); + Debug.LogToFile("INFO Deleted start menu entry."); File.Delete(GlobalVars.startmenu + "\\EnvyUpdate.lnk"); } if ((GlobalVars.exedirectory == GlobalVars.appdata) && File.Exists(GlobalVars.appdata + "EnvyUpdate.exe")) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Deleting EnvyUpdate appdata and self."); + Debug.LogToFile("INFO Deleting EnvyUpdate appdata and self."); MessageBox.Show(Properties.Resources.uninstall_legacy_message); Util.SelfDelete(); } else if (Directory.Exists(GlobalVars.appdata)) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Deleting EnvyUpdate appdata folder"); + Debug.LogToFile("INFO Deleting EnvyUpdate appdata folder"); Directory.Delete(GlobalVars.appdata, true); } } @@ -385,15 +348,13 @@ namespace EnvyUpdate { if (MessageBox.Show(Properties.Resources.exit_confirm, "", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Uninstalling notifications and shutting down."); + Debug.LogToFile("INFO Uninstalling notifications and shutting down."); ToastNotificationManagerCompat.Uninstall(); // Uninstall notifications to prevent issues with the app being portable. Application.Current.Shutdown(); } else { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Application shutdown was cancelled."); + Debug.LogToFile("INFO Application shutdown was cancelled."); e.Cancel = true; } } @@ -402,8 +363,7 @@ namespace EnvyUpdate { if (File.Exists(GlobalVars.exedirectory + "sd.envy")) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Switching to game ready driver."); + Debug.LogToFile("INFO Switching to game ready driver."); File.Delete(GlobalVars.exedirectory + "sd.envy"); Load(); } @@ -413,8 +373,7 @@ namespace EnvyUpdate { if (!File.Exists(GlobalVars.exedirectory + "sd.envy")) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Switching to studio driver."); + Debug.LogToFile("INFO Switching to studio driver."); File.Create(GlobalVars.exedirectory + "sd.envy").Close(); Load(); } @@ -424,22 +383,19 @@ namespace EnvyUpdate { if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "EnvyUpdate.lnk"))) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Removing autostart entry."); + Debug.LogToFile("INFO Removing autostart entry."); File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "EnvyUpdate.lnk")); } if (chkAutostart.IsChecked == true) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Creating autostart entry."); + Debug.LogToFile("INFO Creating autostart entry."); Util.CreateShortcut("EnvyUpdate", Environment.GetFolderPath(Environment.SpecialFolder.Startup), GlobalVars.exeloc, "NVidia Update Checker", "/minimize"); } } private void buttonSkip_Click(object sender, RoutedEventArgs e) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Skipping version."); + Debug.LogToFile("INFO Skipping version."); skippedVer = onlineDriv; File.WriteAllText(GlobalVars.exedirectory + "skip.envy", onlineDriv); buttonSkip.IsEnabled = false; @@ -449,12 +405,10 @@ namespace EnvyUpdate private void UpdateLocalVer(bool reloadLocalDriv = true) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Updating local driver version in UI."); + Debug.LogToFile("INFO Updating local driver version in UI."); if (reloadLocalDriv) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Reloading local driver version."); + Debug.LogToFile("INFO Reloading local driver version."); localDriv = Util.GetLocDriv(); } textblockGPU.Text = localDriv; @@ -466,8 +420,7 @@ namespace EnvyUpdate void DriverFileChanged(object sender, FileSystemEventArgs e) { - if (Debug.isVerbose) - File.AppendAllText(Debug.debugFile, "INFO Watched driver file changed! Reloading data."); + Debug.LogToFile("INFO Watched driver file changed! Reloading data."); System.Threading.Thread.Sleep(10000); Application.Current.Dispatcher.Invoke(delegate { diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index 0329f35..dd84e97 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using System.Windows; using System.Xml.Linq; using Windows.Devices.Radios; +using Windows.UI.Xaml.Input; namespace EnvyUpdate { @@ -29,8 +30,7 @@ namespace EnvyUpdate // query local driver version try { - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Looking for driver version in ManagementObjects"); + Debug.LogToFile("INFO Looking for driver version in ManagementObjects"); foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT * FROM Win32_VideoController").Get()) { @@ -40,16 +40,14 @@ namespace EnvyUpdate OfflineGPUVersion = OfflineGPUVersion.Substring(Math.Max(0, OfflineGPUVersion.Length - 5)); OfflineGPUVersion = OfflineGPUVersion.Substring(0, 3) + "." + OfflineGPUVersion.Substring(3); // add dot foundGpu = true; - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Found driver in ManagementObjects."); + Debug.LogToFile("INFO Found driver in ManagementObjects."); break; } } if (!foundGpu) { - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "WARN Did NOT find driver in ManagementObjects."); + Debug.LogToFile("WARN Did NOT find driver in ManagementObjects."); throw new InvalidDataException(); } @@ -79,8 +77,7 @@ namespace EnvyUpdate shortcut.Arguments = arguments; shortcut.Description = description; shortcut.TargetPath = targetFileLocation; - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Saving shortcut link."); + Debug.LogToFile("INFO Saving shortcut link."); shortcut.Save(); } /// @@ -140,8 +137,7 @@ namespace EnvyUpdate string xmlcontent = null; int id = -1; - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Getting Nvidia GPU list..."); + Debug.LogToFile("INFO Getting Nvidia GPU list..."); using (var wc = new WebClient()) { switch (IDtype) @@ -155,14 +151,9 @@ namespace EnvyUpdate } } - if (Debug.isVerbose) - { - System.IO.File.AppendAllText(Debug.debugFile, "INFO Got Nvidia GPU list."); - if (xmlcontent == null) - { - System.IO.File.AppendAllText(Debug.debugFile, "WARN GPU list is NULL! This is a possible error source."); - } - } + Debug.LogToFile("INFO Got Nvidia GPU list."); + if (xmlcontent == null) + Debug.LogToFile("WARN GPU list is NULL! This is a possible error source."); XDocument xDoc = XDocument.Parse(xmlcontent); string gpuName = GetGPUName(true); @@ -171,22 +162,18 @@ namespace EnvyUpdate { case "psid": id = GetValueFromName(xDoc, gpuName, true); - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Got psid: " + id); + Debug.LogToFile("INFO Got psid: " + id); break; case "pfid": id = GetValueFromName(xDoc, gpuName, false); - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Got pfid: " + id); + Debug.LogToFile("INFO Got pfid: " + id); break; case "osid": id = GetOSID(); - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Got osid: " + id); + Debug.LogToFile("INFO Got osid: " + id); break; default: - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "WARN GetIDs was called, but nothing was specified."); + Debug.LogToFile("WARN GetIDs was called, but nothing was specified."); break; } @@ -213,14 +200,12 @@ namespace EnvyUpdate string sName = name.Value.ToString().ToLower(); if (sName == query) { - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "DEBUG Matched GetValueFromName query: " + sName); + Debug.LogToFile("DEBUG Matched GetValueFromName query: " + sName); string cleanResult = null; if (psid) { - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "DEBUG Getting psid."); + Debug.LogToFile("DEBUG Getting psid."); if (i == 0) value1 = int.Parse(name.Parent.FirstAttribute.Value); @@ -229,8 +214,7 @@ namespace EnvyUpdate } else { - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "DEBUG Getting something else than psid."); + Debug.LogToFile("DEBUG Getting something else than psid."); string result = name.Parent.Value.ToLower(); int index = result.IndexOf(sName); @@ -311,8 +295,7 @@ namespace EnvyUpdate { string GPUName = null; - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Trying to get GPU name from ManagementObjects..."); + Debug.LogToFile("INFO Trying to get GPU name from ManagementObjects..."); foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT * FROM Win32_VideoController").Get()) { //if (obj["Description"].ToString().ToLower().Contains("radeon")) @@ -338,8 +321,7 @@ namespace EnvyUpdate break; } } - if (Debug.isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, "INFO Found GPU name: " + GPUName); + Debug.LogToFile("INFO Found GPU name: " + GPUName); // This should NEVER return null outside of debugging mode, since EnvyUpdate should refuse to start without and Nvidia GPU. return GPUName; } @@ -353,10 +335,12 @@ namespace EnvyUpdate foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT * FROM Win32_Battery").Get()) { + Debug.LogToFile("INFO Found Win32_Battery, assuming mobile device."); result = true; } foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT * FROM Win32_PortableBattery").Get()) { + Debug.LogToFile("INFO Found Win32_PortableBattery, assuming mobile device."); result = true; } @@ -371,6 +355,7 @@ namespace EnvyUpdate { try { + Debug.LogToFile("INFO Trying to find DCH key in registry..."); RegistryKey nvlddmkm = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\nvlddmkm", false); return nvlddmkm.GetValueNames().Contains("DCHUVen"); } @@ -378,6 +363,7 @@ namespace EnvyUpdate { if (ex.Message == "Object reference not set to an instance of an object." || ex.InnerException is NullReferenceException) { + Debug.LogToFile("INFO could not find key. Assuming non-DCH driver."); // Assume no DCH driver is installed if key is not found. return false; } @@ -385,14 +371,23 @@ namespace EnvyUpdate { try { + Debug.LogToFile("WARN Could not read registry, probing file system instead..."); //Registry reading error. Check for existance of file nvsvs.dll instead. if (System.IO.File.Exists(Path.Combine(Environment.SystemDirectory, "nvsvs.dll"))) + { + Debug.LogToFile("INFO Found DCH driver file."); return true; + } else + { + Debug.LogToFile("INFO Did not find DCH driver file. Assuming non-DCH driver."); return false; + } + } catch (Exception) { + Debug.LogToFile("FATAL Could not probe file system. Error: " + ex.Message); MessageBox.Show("An error has occured. Please report this on GitHub.\nError:" + ex.Message); Environment.Exit(20); return false; @@ -434,6 +429,7 @@ namespace EnvyUpdate if (Debug.isFake) { + Debug.LogToFile("DEBUG Loading fake IDs."); psid = Debug.LoadFakeIDs("psid"); pfid = Debug.LoadFakeIDs("pfid"); osid = Debug.LoadFakeIDs("osid"); @@ -447,13 +443,20 @@ namespace EnvyUpdate osid = GetIDs("osid"); dtcid = GetDTCID(); dtid = GetDTID(); + Debug.LogToFile("INFO Getting GPU URLs. IDs in order psid, pfid, osid, dtcid, dtid: " + psid + ", " + pfid + ", " + osid + ", " + dtcid + ", " + dtid); } string gpuUrlBuild = "http://www.nvidia.com/Download/processDriver.aspx?psid=" + psid.ToString() + "&pfid=" + pfid.ToString() + "&osid=" + osid.ToString() + "&dtcid=" + dtcid.ToString() + "&dtid=" + dtid.ToString(); + + Debug.LogToFile("INFO Built GPU URL: " + gpuUrlBuild); + string gpuUrl; using (var c = new WebClient()) { gpuUrl = c.DownloadString(gpuUrlBuild); + + Debug.LogToFile("INFO Downloaded driver page URL: " + gpuUrl); + if (gpuUrl.Contains("https://") || gpuUrl.Contains("http://")) { //absolute url @@ -476,6 +479,7 @@ namespace EnvyUpdate else { //panic. + Debug.LogToFile("FATAL Unexpected web response: " + gpuUrl); MessageBox.Show("ERROR: Invalid API response from Nvidia - unexpected web response. Please file an issue on GitHub."); Environment.Exit(10); } From ae9f50354e10c55daf4877bc72b89cb492f9678d Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 3 Apr 2023 20:57:46 +0200 Subject: [PATCH 06/76] set verbose default for debug --- EnvyUpdate/Debug.cs | 6 ++++++ EnvyUpdate/MainWindow.xaml.cs | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/EnvyUpdate/Debug.cs b/EnvyUpdate/Debug.cs index c0b6a5f..01e98b9 100644 --- a/EnvyUpdate/Debug.cs +++ b/EnvyUpdate/Debug.cs @@ -9,7 +9,13 @@ namespace EnvyUpdate class Debug { public static bool isFake = false; +#if DEBUG + public static bool isVerbose = true; +#else public static bool isVerbose = false; +#endif + + public static string debugFile = Path.Combine(GlobalVars.exedirectory, "envyupdate.log"); public static int LoadFakeIDs(string idType) diff --git a/EnvyUpdate/MainWindow.xaml.cs b/EnvyUpdate/MainWindow.xaml.cs index b10aa50..d7f1ab3 100644 --- a/EnvyUpdate/MainWindow.xaml.cs +++ b/EnvyUpdate/MainWindow.xaml.cs @@ -37,9 +37,8 @@ namespace EnvyUpdate // This is necessary, since .NET throws an exception if you check for a non-existant arg. } - if (arguments.Contains("/verbose")) + if (Debug.isVerbose) { - Debug.isVerbose = true; if (!File.Exists(Debug.debugFile)) File.CreateText(Debug.debugFile); File.AppendAllText(Debug.debugFile, "INFO Starting EnvyUpdate, version " + System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion); From 645c9e72d24c0a6959d2e181c70bc2552eb79a5b Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 3 Apr 2023 21:01:32 +0200 Subject: [PATCH 07/76] fix debug log --- EnvyUpdate/Debug.cs | 2 +- EnvyUpdate/MainWindow.xaml.cs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/EnvyUpdate/Debug.cs b/EnvyUpdate/Debug.cs index 01e98b9..a239764 100644 --- a/EnvyUpdate/Debug.cs +++ b/EnvyUpdate/Debug.cs @@ -53,7 +53,7 @@ namespace EnvyUpdate public static void LogToFile(string content) { if (isVerbose) - System.IO.File.AppendAllText(Debug.debugFile, content); + System.IO.File.AppendAllText(Debug.debugFile, content + "\n"); } } } diff --git a/EnvyUpdate/MainWindow.xaml.cs b/EnvyUpdate/MainWindow.xaml.cs index d7f1ab3..cd86d62 100644 --- a/EnvyUpdate/MainWindow.xaml.cs +++ b/EnvyUpdate/MainWindow.xaml.cs @@ -39,8 +39,6 @@ namespace EnvyUpdate if (Debug.isVerbose) { - if (!File.Exists(Debug.debugFile)) - File.CreateText(Debug.debugFile); File.AppendAllText(Debug.debugFile, "INFO Starting EnvyUpdate, version " + System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion); } From 90fc53ce0081a265edd0b86645e6b3f6f274a03b Mon Sep 17 00:00:00 2001 From: Jakob Date: Sat, 8 Jul 2023 23:18:53 +0200 Subject: [PATCH 08/76] potential solution for #31? --- EnvyUpdate/Properties/AssemblyInfo.cs | 4 +-- EnvyUpdate/Util.cs | 52 ++++++++++++++++----------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/EnvyUpdate/Properties/AssemblyInfo.cs b/EnvyUpdate/Properties/AssemblyInfo.cs index a45fa1a..2659d08 100644 --- a/EnvyUpdate/Properties/AssemblyInfo.cs +++ b/EnvyUpdate/Properties/AssemblyInfo.cs @@ -49,5 +49,5 @@ using System.Windows; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.18")] -[assembly: AssemblyFileVersion("2.18")] +[assembly: AssemblyVersion("2.19")] +[assembly: AssemblyFileVersion("2.19")] diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index dd84e97..328a24d 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -153,28 +153,38 @@ namespace EnvyUpdate Debug.LogToFile("INFO Got Nvidia GPU list."); if (xmlcontent == null) - Debug.LogToFile("WARN GPU list is NULL! This is a possible error source."); - - XDocument xDoc = XDocument.Parse(xmlcontent); - string gpuName = GetGPUName(true); - - switch (IDtype) { - case "psid": - id = GetValueFromName(xDoc, gpuName, true); - Debug.LogToFile("INFO Got psid: " + id); - break; - case "pfid": - id = GetValueFromName(xDoc, gpuName, false); - Debug.LogToFile("INFO Got pfid: " + id); - break; - case "osid": - id = GetOSID(); - Debug.LogToFile("INFO Got osid: " + id); - break; - default: - Debug.LogToFile("WARN GetIDs was called, but nothing was specified."); - break; + Debug.LogToFile("WARN GPU list is NULL! This is a possible error source."); + switch (IDtype) + { + case "osid": + id = GetOSID(); + Debug.LogToFile("WARN Ignore previous warning, just getting osid."); + Debug.LogToFile("INFO Got osid: " + id); + break; + default: + Debug.LogToFile("WARN GetIDs was called, but nothing was specified."); + break; + } + } + else + { + XDocument xDoc = XDocument.Parse(xmlcontent); + string gpuName = GetGPUName(true); + switch (IDtype) + { + case "psid": + id = GetValueFromName(xDoc, gpuName, true); + Debug.LogToFile("INFO Got psid: " + id); + break; + case "pfid": + id = GetValueFromName(xDoc, gpuName, false); + Debug.LogToFile("INFO Got pfid: " + id); + break; + default: + Debug.LogToFile("WARN GetIDs was called, but nothing was specified."); + break; + } } return id; From d347ccefdf59b7b0fb72cbba87d368f0b33d2096 Mon Sep 17 00:00:00 2001 From: Jakob Date: Sun, 9 Jul 2023 18:19:33 +0200 Subject: [PATCH 09/76] improve logging --- EnvyUpdate/Debug.cs | 1 - EnvyUpdate/InfoWindow.xaml | 26 ++++++++++++++++---------- EnvyUpdate/InfoWindow.xaml.cs | 18 ++++++++++++++++++ EnvyUpdate/MainWindow.xaml.cs | 18 ++++++++++++++---- EnvyUpdate/Util.cs | 21 +++++++++++++++++---- 5 files changed, 65 insertions(+), 19 deletions(-) diff --git a/EnvyUpdate/Debug.cs b/EnvyUpdate/Debug.cs index a239764..43e54b9 100644 --- a/EnvyUpdate/Debug.cs +++ b/EnvyUpdate/Debug.cs @@ -49,7 +49,6 @@ namespace EnvyUpdate return "Nvidia GeForce RTX 4080 (debug)"; } - [ConditionalAttribute("DEBUG")] public static void LogToFile(string content) { if (isVerbose) diff --git a/EnvyUpdate/InfoWindow.xaml b/EnvyUpdate/InfoWindow.xaml index bdc6ca6..e9d09c5 100644 --- a/EnvyUpdate/InfoWindow.xaml +++ b/EnvyUpdate/InfoWindow.xaml @@ -7,14 +7,20 @@ xmlns:p="clr-namespace:EnvyUpdate.Properties" mc:Ignorable="d" Title="" Height="400" Width="600" ResizeMode="NoResize" WindowStyle="ToolWindow" SizeToContent="WidthAndHeight"> - - -