Nvidia.cs (3614B)
1 using System.Globalization; 2 using System.Net.Http.Json; 3 using System.Text.Json; 4 5 namespace DriverUpdater; 6 7 // Checks NVIDIA for the latest Game Ready driver. This is the lookup the NVIDIA App makes. It takes the 8 // GPU's PCI IDs and returns the newest driver for that card. 9 static class Nvidia 10 { 11 const string LookupUrl = "https://gfwsl.geforce.com/nvidia_web_services/controller.gfeclientcontent.NG.php/" 12 + "com.nvidia.services.GFEClientContent_NG.getDispDrvrByDevid/"; 13 14 public static async Task<List<DriverUpdate>> CheckAsync(IReadOnlyList<InstalledDevice> installed) 15 { 16 var updates = new List<DriverUpdate>(); 17 foreach (var gpu in installed.Where(d => d.DeviceClass == "DISPLAY" && d.IsFromVendor("10DE"))) 18 updates.Add(await CheckGpuAsync(gpu)); 19 return updates; 20 } 21 22 static async Task<DriverUpdate> CheckGpuAsync(InstalledDevice gpu) 23 { 24 // NVIDIA wants "DEV_VEN_SUBSYSDEV_SUBSYSVEN", like "2C05_10DE_89F4_1043". 25 string subsystem = gpu.Field("SUBSYS") ?? ""; 26 string deviceIds = subsystem.Length == 8 27 ? $"{gpu.Field("DEV")}_10DE_{subsystem[..4]}_{subsystem[4..]}" 28 : $"{gpu.Field("DEV")}_10DE"; 29 30 var query = new 31 { 32 dIDa = new[] { deviceIds }, 33 osC = "10.0", // Windows 10 and 11 both 34 is6 = "1", // 64 bit 35 lg = "1033", // English 36 iLp = "0", // Must be 0. A laptop GPU still gets the notebook driver from its device ID. 37 dch = "1", 38 upCRD = "0", // Game Ready. 1 asks for the Studio driver. 39 }; 40 string url = LookupUrl + Uri.EscapeDataString(JsonSerializer.Serialize(query)); 41 var driver = (await Web.Client.GetFromJsonAsync<LookupResponse>(url))!.DriverAttributes; 42 43 string installedVersion = ToNvidiaVersion(gpu.DriverVersion); 44 bool isNewer = decimal.Parse(driver.Version, CultureInfo.InvariantCulture) 45 > decimal.Parse(installedVersion, CultureInfo.InvariantCulture); 46 var packageUrl = new Uri(driver.DownloadURLAdmin); 47 48 return new DriverUpdate 49 { 50 Device = gpu.Name, 51 Source = "NVIDIA", 52 InstalledVersion = installedVersion, 53 AvailableVersion = driver.Version, 54 // Dates look like "2026-10-21T00:00:00+0000", which DateTime won't parse whole. 55 ReleaseDate = DateTime.ParseExact(driver.ReleaseDateTime[..10], "yyyy-MM-dd", CultureInfo.InvariantCulture), 56 NotesUrl = new Uri(driver.DetailsURL), 57 Package = isNewer 58 ? new VendorPackage(packageUrl, null, "NVIDIA Corporation", VendorPackage.TempFolderFor(packageUrl), Unpack.WithTarAsync) 59 : null, 60 TargetDevice = gpu, 61 Status = isNewer ? "Update available" : "Up to date", 62 }; 63 } 64 65 // Windows shows NVIDIA driver 610.88 as 32.0.16.1088. Join the last two fields, keep the last five 66 // digits, and put the decimal point before the final two. 67 static string ToNvidiaVersion(string windowsVersion) 68 { 69 string[] fields = windowsVersion.Split('.'); 70 string digits = (fields[2] + fields[3].PadLeft(4, '0'))[^5..]; 71 return $"{digits[..3]}.{digits[3..]}"; 72 } 73 74 // The parts of the response the app reads. DownloadURLAdmin is the standard installer. 75 // DownloadURL is a smaller build meant for the NVIDIA App. 76 record LookupResponse(DriverAttributes DriverAttributes); 77 record DriverAttributes(string Version, string ReleaseDateTime, string DetailsURL, string DownloadURLAdmin); 78 }