|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +namespace nspector.Common.Helper |
| 9 | +{ |
| 10 | + public class IniParser |
| 11 | + { |
| 12 | + public Dictionary<string, Dictionary<string, string>> Data { get; } = new(); |
| 13 | + |
| 14 | + public void Load(string filePath) |
| 15 | + { |
| 16 | + using var reader = new StreamReader(filePath); |
| 17 | + string? line; |
| 18 | + string? currentSection = null; |
| 19 | + |
| 20 | + while ((line = reader.ReadLine()) != null) |
| 21 | + { |
| 22 | + line = line.Trim(); |
| 23 | + |
| 24 | + // Skip empty lines and comments |
| 25 | + if (string.IsNullOrEmpty(line) || line.StartsWith(";") || line.StartsWith("#")) |
| 26 | + continue; |
| 27 | + |
| 28 | + // Section |
| 29 | + if (line.StartsWith("[") && line.EndsWith("]")) |
| 30 | + { |
| 31 | + currentSection = line.Substring(1, line.Length - 2).Trim(); |
| 32 | + if (!Data.ContainsKey(currentSection)) |
| 33 | + Data[currentSection] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 34 | + } |
| 35 | + // Key=Value |
| 36 | + else if (currentSection != null && line.Contains('=')) |
| 37 | + { |
| 38 | + int idx = line.IndexOf('='); |
| 39 | + var key = line.Substring(0, idx).Trim(); |
| 40 | + var value = line.Substring(idx + 1).Trim(); |
| 41 | + Data[currentSection][key] = value; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public string? GetValue(string section, string key) |
| 47 | + { |
| 48 | + if (Data.TryGetValue(section, out var sectionDict) && |
| 49 | + sectionDict.TryGetValue(key, out var value)) |
| 50 | + { |
| 51 | + return value; |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + public List<string> GetSections() |
| 58 | + { |
| 59 | + return Data.Keys.ToList(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static class DlssHelper |
| 64 | + { |
| 65 | + private static Dictionary<string, Version> _ngxVersions = FetchVersions(); |
| 66 | + |
| 67 | + // Fetches latest versions installed in C:\ProgramData\NVIDIA\NGX\models\ folder |
| 68 | + private static Dictionary<string, Version> FetchVersions() |
| 69 | + { |
| 70 | + Dictionary<string, Version> versions = new Dictionary<string, Version>(); |
| 71 | + |
| 72 | + try |
| 73 | + { |
| 74 | + string ngxDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"NVIDIA\NGX\models\"); |
| 75 | + string ngxConfigPath = Path.Combine(ngxDataPath, "nvngx_config.txt"); |
| 76 | + if (!File.Exists(ngxConfigPath)) |
| 77 | + return versions; |
| 78 | + |
| 79 | + var ini = new IniParser(); |
| 80 | + ini.Load(ngxConfigPath); |
| 81 | + |
| 82 | + foreach (string section in ini.GetSections()) |
| 83 | + { |
| 84 | + string versionStr = ini.GetValue(section, "app_E658700"); |
| 85 | + if (string.IsNullOrEmpty(versionStr)) |
| 86 | + continue; |
| 87 | + |
| 88 | + Version ver = new Version(versionStr.Trim()); |
| 89 | + |
| 90 | + versions[section] = ver; |
| 91 | + } |
| 92 | + } |
| 93 | + catch |
| 94 | + { |
| 95 | + versions.Clear(); |
| 96 | + } |
| 97 | + |
| 98 | + return versions; |
| 99 | + } |
| 100 | + |
| 101 | + public static string GetSnippetLatestVersion(string snippet) |
| 102 | + { |
| 103 | + if (!_ngxVersions.ContainsKey(snippet)) |
| 104 | + return "unknown"; |
| 105 | + return "v" + _ngxVersions[snippet].ToString(); |
| 106 | + } |
| 107 | + |
| 108 | + public static string ReplaceDlssVersions(string str) |
| 109 | + { |
| 110 | + if (string.IsNullOrEmpty(str)) |
| 111 | + return str; |
| 112 | + |
| 113 | + if (str.Contains("${DlssVersion}")) |
| 114 | + str = str.Replace("${DlssVersion}", DlssHelper.GetSnippetLatestVersion("dlss").ToString()); |
| 115 | + |
| 116 | + if (str.Contains("${DlssgVersion}")) |
| 117 | + str = str.Replace("${DlssgVersion}", DlssHelper.GetSnippetLatestVersion("dlssg").ToString()); |
| 118 | + |
| 119 | + if (str.Contains("${DlssdVersion}")) |
| 120 | + str = str.Replace("${DlssdVersion}", DlssHelper.GetSnippetLatestVersion("dlssd").ToString()); |
| 121 | + |
| 122 | + return str; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments