Skip to content

Commit d12f758

Browse files
committed
Move to .NET 4.8.1 and cleanup code
Migrated to 4.8.1 and cleaned up various functions for the .NET version, could be simplified, were more secure, or were unused.
1 parent c1d6fed commit d12f758

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1677
-1684
lines changed

nspector/Common/Cache/CachedSettingValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal CachedSettingValue(byte[] ValueBin, string ProfileNames)
3131

3232
internal string ValueStr = "";
3333
internal uint Value = 0;
34-
internal byte[] ValueBin = new byte[0];
34+
internal byte[] ValueBin = [];
3535
internal StringBuilder ProfileNames;
3636
internal uint ValueProfileCount;
3737
}

nspector/Common/Cache/CachedSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal CachedSettings(uint settingId, NVDRS_SETTING_TYPE settingType)
1616

1717
internal uint SettingId;
1818

19-
internal List<CachedSettingValue> SettingValues = new List<CachedSettingValue>();
19+
internal List<CachedSettingValue> SettingValues = [];
2020

2121
internal uint ProfileCount = 0;
2222

nspector/Common/CustomSettings/CustomSettingNames.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace nspector.Common.CustomSettings
88
[Serializable]
99
public class CustomSettingNames
1010
{
11-
public List<CustomSetting> Settings = new List<CustomSetting>();
11+
public List<CustomSetting> Settings = [];
1212

1313
public void StoreToFile(string filename)
1414
{

nspector/Common/DrsDecrypterService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace nspector.Common
1111
internal class DrsDecrypterService : DrsSettingsServiceBase
1212
{
1313

14-
private static readonly byte[] _InternalSettingsKey = new byte[] {
14+
private static readonly byte[] _InternalSettingsKey = [
1515
0x2f, 0x7c, 0x4f, 0x8b, 0x20, 0x24, 0x52, 0x8d, 0x26, 0x3c, 0x94, 0x77, 0xf3, 0x7c, 0x98, 0xa5,
1616
0xfa, 0x71, 0xb6, 0x80, 0xdd, 0x35, 0x84, 0xba, 0xfd, 0xb6, 0xa6, 0x1b, 0x39, 0xc4, 0xcc, 0xb0,
1717
0x7e, 0x95, 0xd9, 0xee, 0x18, 0x4b, 0x9c, 0xf5, 0x2d, 0x4e, 0xd0, 0xc1, 0x55, 0x17, 0xdf, 0x18,
@@ -27,7 +27,7 @@ internal class DrsDecrypterService : DrsSettingsServiceBase
2727
0x1c, 0x4b, 0xf8, 0x26, 0x03, 0x6e, 0xb2, 0x3f, 0x1e, 0xe6, 0xca, 0x3d, 0x61, 0x44, 0xb0, 0x92,
2828
0xaf, 0xf0, 0x88, 0xca, 0xe0, 0x5f, 0x5d, 0xf4, 0xdf, 0xc6, 0x4c, 0xa4, 0xe0, 0xca, 0xb0, 0x20,
2929
0x5d, 0xc0, 0xfa, 0xdd, 0x9a, 0x34, 0x8f, 0x50, 0x79, 0x5a, 0x5f, 0x7c, 0x19, 0x9e, 0x40, 0x70,
30-
0x71, 0xb5, 0x45, 0x19, 0xb8, 0x53, 0xfc, 0xdf, 0x24, 0xbe, 0x22, 0x1c, 0x79, 0xbf, 0x42, 0x89 };
30+
0x71, 0xb5, 0x45, 0x19, 0xb8, 0x53, 0xfc, 0xdf, 0x24, 0xbe, 0x22, 0x1c, 0x79, 0xbf, 0x42, 0x89 ];
3131

3232
public DrsDecrypterService(DrsSettingsMetaService metaService) : base(metaService)
3333
{
@@ -97,7 +97,7 @@ public bool IsInternalSetting(string profileName, uint settingId)
9797
return _InternalSettings.Contains(FormatInternalSettingKey(profileName, settingId));
9898
}
9999

100-
private HashSet<string> _InternalSettings = new HashSet<string>();
100+
private HashSet<string> _InternalSettings = [];
101101

102102
private void CreateInternalSettingMap()
103103
{
@@ -114,7 +114,7 @@ private void CreateInternalSettingMap()
114114
{
115115
var lines = File.ReadAllLines(tmpfile);
116116

117-
_InternalSettings = new HashSet<string>();
117+
_InternalSettings = [];
118118

119119
var paProfile = "Profile\\s\\\"(?<profileName>.*?)\\\"";
120120
var rxProfile = new Regex(paProfile, RegexOptions.Compiled);

nspector/Common/DrsImportService.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,16 @@
99

1010
namespace nspector.Common
1111
{
12-
internal class DrsImportService : DrsSettingsServiceBase
12+
internal class DrsImportService(
13+
DrsSettingsMetaService metaService,
14+
DrsSettingsService settingService,
15+
DrsScannerService scannerService,
16+
DrsDecrypterService decrypterService) : DrsSettingsServiceBase(metaService)
1317
{
1418

15-
private readonly DrsSettingsService _SettingService;
16-
private readonly DrsScannerService _ScannerService;
17-
private readonly DrsDecrypterService _DecrypterService;
18-
19-
public DrsImportService(
20-
DrsSettingsMetaService metaService,
21-
DrsSettingsService settingService,
22-
DrsScannerService scannerService,
23-
DrsDecrypterService decrypterService)
24-
: base(metaService)
25-
{
26-
_SettingService = settingService;
27-
_ScannerService = scannerService;
28-
_DecrypterService = decrypterService;
29-
}
19+
private readonly DrsSettingsService _SettingService = settingService;
20+
private readonly DrsScannerService _ScannerService = scannerService;
21+
private readonly DrsDecrypterService _DecrypterService = decrypterService;
3022

3123
internal void ExportAllProfilesToNvidiaTextFile(string filename)
3224
{
@@ -122,8 +114,7 @@ internal string ImportProfiles(string filename)
122114

123115
if (hProfile != IntPtr.Zero)
124116
{
125-
var modified = false;
126-
_SettingService.ResetProfile(profile.ProfileName, out modified);
117+
_SettingService.ResetProfile(profile.ProfileName, out bool modified);
127118
try
128119
{
129120
UpdateApplications(hSession, hProfile, profile);
@@ -137,8 +128,7 @@ internal string ImportProfiles(string filename)
137128
}
138129

139130
sbFailedProfilesMessage.AppendLine(string.Format("Failed to import profile '{0}'", profile.ProfileName));
140-
var appEx = nex as NvapiAddApplicationException;
141-
if (appEx != null)
131+
if (nex is NvapiAddApplicationException appEx)
142132
{
143133
var profilesWithThisApp = _ScannerService.FindProfilesUsingApplication(appEx.ApplicationName);
144134
sbFailedProfilesMessage.AppendLine(string.Format("- application '{0}' is already in use by profile '{1}'", appEx.ApplicationName, profilesWithThisApp));
@@ -197,6 +187,7 @@ private void UpdateApplications(IntPtr hSession, IntPtr hProfile, Profile import
197187
}
198188
}
199189

190+
/*
200191
private uint GetImportValue(uint settingId, Profile importProfile)
201192
{
202193
var setting = importProfile.Settings
@@ -207,6 +198,7 @@ private uint GetImportValue(uint settingId, Profile importProfile)
207198
208199
return 0;
209200
}
201+
*/
210202

211203
private ProfileSetting GetImportProfileSetting(uint settingId, Profile importProfile)
212204
{

nspector/Common/DrsScannerService.cs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,17 @@ namespace nspector.Common
1313
{
1414

1515

16-
internal class DrsScannerService : DrsSettingsServiceBase
16+
internal class DrsScannerService(DrsSettingsMetaService metaService, DrsDecrypterService decrpterService) : DrsSettingsServiceBase(metaService, decrpterService)
1717
{
18-
19-
public DrsScannerService(DrsSettingsMetaService metaService, DrsDecrypterService decrpterService)
20-
: base(metaService, decrpterService)
21-
{ }
22-
23-
24-
internal List<CachedSettings> CachedSettings = new List<CachedSettings>();
25-
internal List<string> ModifiedProfiles = new List<string>();
26-
internal HashSet<string> UserProfiles = new HashSet<string>();
18+
internal List<CachedSettings> CachedSettings = [];
19+
internal List<string> ModifiedProfiles = [];
20+
internal HashSet<string> UserProfiles = [];
2721

2822
// most common setting ids as start pattern for the heuristic scan
29-
private readonly uint[] _commonSettingIds = new uint[] { 0x1095DEF8, 0x1033DCD2, 0x1033CEC1,
23+
private readonly uint[] _commonSettingIds = [ 0x1095DEF8, 0x1033DCD2, 0x1033CEC1,
3024
0x10930F46, 0x00A06946, 0x10ECDB82, 0x20EBD7B8, 0x0095DEF9, 0x00D55F7D,
3125
0x1033DCD3, 0x1033CEC2, 0x2072F036, 0x00664339, 0x002C7F45, 0x209746C1,
32-
0x0076E164, 0x20FF7493, 0x204CFF7B };
26+
0x0076E164, 0x20FF7493, 0x204CFF7B ];
3327

3428

3529
private bool CheckCommonSetting(IntPtr hSession, IntPtr hProfile, NVDRS_PROFILE profile,
@@ -40,8 +34,10 @@ private bool CheckCommonSetting(IntPtr hSession, IntPtr hProfile, NVDRS_PROFILE
4034
if (checkedSettingsCount >= profile.numOfSettings)
4135
return false;
4236

43-
var setting = new NVDRS_SETTING();
44-
setting.version = nvw.NVDRS_SETTING_VER;
37+
var setting = new NVDRS_SETTING
38+
{
39+
version = nvw.NVDRS_SETTING_VER
40+
};
4541

4642
if (nvw.DRS_GetSetting(hSession, hProfile, checkSettingId, ref setting) != NvAPI_Status.NVAPI_OK)
4743
return false;
@@ -55,10 +51,7 @@ private bool CheckCommonSetting(IntPtr hSession, IntPtr hProfile, NVDRS_PROFILE
5551
}
5652
else if (addToScanResult)
5753
{
58-
if (decrypter != null)
59-
{
60-
decrypter.DecryptSettingIfNeeded(profile.profileName, ref setting);
61-
}
54+
decrypter?.DecryptSettingIfNeeded(profile.profileName, ref setting);
6255

6356
checkedSettingsCount++;
6457
AddScannedSettingToCache(profile, setting);
@@ -79,12 +72,12 @@ private int CalcPercent(int current, int max)
7972
return (current > 0) ? (int)Math.Round((current * 100f) / max) : 0; ;
8073
}
8174

82-
public async Task ScanProfileSettingsAsync(bool justModified, IProgress<int> progress, CancellationToken token = default(CancellationToken))
75+
public async Task ScanProfileSettingsAsync(bool justModified, IProgress<int> progress, CancellationToken token = default)
8376
{
8477
await Task.Run(() =>
8578
{
86-
ModifiedProfiles = new List<string>();
87-
UserProfiles = new HashSet<string>();
79+
ModifiedProfiles = [];
80+
UserProfiles = [];
8881
var knownPredefines = new List<uint>(_commonSettingIds);
8982

9083
DrsSession((hSession) =>
@@ -162,7 +155,7 @@ await Task.Run(() =>
162155
private void AddScannedSettingToCache(NVDRS_PROFILE profile, NVDRS_SETTING setting)
163156
{
164157
// 3D Vision is dead so dont bother scanning those values for improved scan performance
165-
bool allowAddValue = !((setting.settingId & 0x70000000) == 0x70000000);
158+
bool allowAddValue = !((setting.settingId & 0x70000000) == 0x70000000);
166159
//bool allowAddValue = true;
167160

168161
var cachedSetting = CachedSettings
@@ -185,7 +178,7 @@ private void AddScannedSettingToCache(NVDRS_PROFILE profile, NVDRS_SETTING setti
185178
cachedSetting.AddDwordValue(setting.predefinedValue.dwordValue, profile.profileName);
186179
else if (setting.settingType == NVDRS_SETTING_TYPE.NVDRS_BINARY_TYPE)
187180
cachedSetting.AddBinaryValue(setting.predefinedValue.binaryValue, profile.profileName);
188-
181+
189182
}
190183
else
191184
cachedSetting.ProfileCount++;

nspector/Common/DrsSessionScope.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class DrsSessionScope
1111

1212
public static volatile bool HoldSession = true;
1313

14-
private static object _Sync = new object();
14+
private static readonly object _Sync = new();
1515

1616

1717
public static T DrsSession<T>(Func<IntPtr, T> action, bool forceNonGlobalSession = false, bool preventLoadSettings = false)
@@ -55,7 +55,7 @@ public static void DestroyGlobalSession()
5555
{
5656
if (GlobalSession != IntPtr.Zero)
5757
{
58-
var csRes = nvw.DRS_DestroySession(GlobalSession);
58+
_ = nvw.DRS_DestroySession(GlobalSession);
5959
GlobalSession = IntPtr.Zero;
6060
}
6161
}

nspector/Common/DrsSettingsMetaService.cs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ internal class DrsSettingsMetaService
1919
private readonly CustomSettingNames _customSettings;
2020
private readonly CustomSettingNames _referenceSettings;
2121

22-
private List<MetaServiceItem> MetaServices = new List<MetaServiceItem>();
22+
private List<MetaServiceItem> MetaServices = [];
2323

24-
private Dictionary<uint, SettingMeta> settingMetaCache = new Dictionary<uint, SettingMeta>();
24+
private Dictionary<uint, SettingMeta> settingMetaCache = [];
2525

2626
public DrsSettingsMetaService(CustomSettingNames customSettings, CustomSettingNames referenceSettings = null)
2727
{
@@ -33,8 +33,8 @@ public DrsSettingsMetaService(CustomSettingNames customSettings, CustomSettingNa
3333

3434
public void ResetMetaCache(bool initOnly = false)
3535
{
36-
settingMetaCache = new Dictionary<uint, SettingMeta>();
37-
MetaServices = new List<MetaServiceItem>();
36+
settingMetaCache = [];
37+
MetaServices = [];
3838

3939
CustomMeta = new CustomSettingMetaService(_customSettings);
4040
MetaServices.Add(new MetaServiceItem() { ValueNamePrio = 1, Service = CustomMeta });
@@ -174,9 +174,9 @@ private List<SettingValue<T>> MergeSettingValues<T>(List<SettingValue<T>> a, Lis
174174

175175
var atmp = a.FirstOrDefault();
176176
if (atmp != null && atmp is IComparable)
177-
return a.OrderBy(x => x.Value).ToList();
177+
return [.. a.OrderBy(x => x.Value)];
178178
else
179-
return a.ToList();
179+
return [.. a];
180180
}
181181

182182
private List<SettingValue<byte[]>> GetBinaryValues(uint settingId)
@@ -214,7 +214,7 @@ private List<SettingValue<uint>> GetDwordValues(uint settingId)
214214

215215
if (result != null)
216216
{
217-
result = (from v in result.Where(x => 1 == 1
217+
result = [.. (from v in result.Where(x => 1 == 1
218218
&& !x.ValueName.EndsWith("_NUM")
219219
&& !x.ValueName.EndsWith("_MASK")
220220
&& (!x.ValueName.EndsWith("_MIN") || x.ValueName.Equals("PREFERRED_PSTATE_PREFER_MIN"))
@@ -225,7 +225,7 @@ group v by v.ValueName into g
225225
select g.First(t => t.ValueName == g.Key))
226226
.OrderBy(v => v.ValueSource)
227227
.ThenBy(v => v.ValuePos)
228-
.ThenBy(v => v.ValueName).ToList();
228+
.ThenBy(v => v.ValueName)];
229229

230230
}
231231

@@ -249,47 +249,42 @@ public List<uint> GetSettingIds(SettingViewMode viewMode)
249249

250250
private SettingMetaSource[] GetAllowedSettingIdMetaSourcesForViewMode(SettingViewMode viewMode)
251251
{
252-
switch (viewMode)
252+
return viewMode switch
253253
{
254-
case SettingViewMode.CustomSettingsOnly:
255-
return new[] {
256-
SettingMetaSource.CustomSettings
257-
};
258-
case SettingViewMode.IncludeScannedSetttings:
259-
return new[] {
260-
SettingMetaSource.ConstantSettings,
254+
SettingViewMode.CustomSettingsOnly => [
255+
SettingMetaSource.CustomSettings
256+
],
257+
SettingViewMode.IncludeScannedSetttings => [
258+
SettingMetaSource.ConstantSettings,
261259
SettingMetaSource.ScannedSettings,
262260
SettingMetaSource.CustomSettings,
263261
SettingMetaSource.DriverSettings,
264262
SettingMetaSource.ReferenceSettings,
265-
};
266-
default:
267-
return new[] {
263+
],
264+
_ => [
268265
SettingMetaSource.CustomSettings,
269266
SettingMetaSource.DriverSettings,
270-
};
271-
}
267+
],
268+
};
272269
}
273270

274271
private SettingMetaSource[] GetAllowedSettingValueMetaSourcesForViewMode(SettingViewMode viewMode)
275272
{
276-
switch (viewMode)
273+
return viewMode switch
277274
{
278-
case SettingViewMode.CustomSettingsOnly:
279-
return new[] {
280-
SettingMetaSource.CustomSettings,
275+
SettingViewMode.CustomSettingsOnly => [
276+
SettingMetaSource.CustomSettings,
281277
SettingMetaSource.ScannedSettings,
282-
};
283-
default:
284-
return new[] {
278+
],
279+
_ => [
285280
SettingMetaSource.ConstantSettings,
286281
SettingMetaSource.ScannedSettings,
287282
SettingMetaSource.CustomSettings,
288283
SettingMetaSource.DriverSettings,
289284
SettingMetaSource.ReferenceSettings,
290285

291-
};
292-
}
286+
],
287+
};
293288
}
294289

295290
private SettingMeta CreateSettingMeta(uint settingId)
@@ -298,8 +293,7 @@ private SettingMeta CreateSettingMeta(uint settingId)
298293
var settingName = GetSettingName(settingId);
299294
var groupName = GetGroupName(settingId);
300295

301-
if (groupName == null)
302-
groupName = GetLegacyGroupName(settingId, settingName);
296+
groupName ??= GetLegacyGroupName(settingId, settingName);
303297

304298

305299

@@ -397,7 +391,9 @@ public SettingMeta GetSettingMeta(uint settingId, SettingViewMode viewMode = Set
397391
}
398392
}
399393

394+
#pragma warning disable IDE0060 // Remove unused parameter
400395
private string GetLegacyGroupName(uint settingId, string settingName)
396+
#pragma warning restore IDE0060 // Remove unused parameter
401397
{
402398
if (settingName == null)
403399
return null;
@@ -428,7 +424,7 @@ private string GetDescription(uint settingId)
428424
{
429425
var csn = MetaServices.FirstOrDefault(m => m.Service.Source == SettingMetaSource.CustomSettings);
430426
var csnDescription = csn != null ? ((CustomSettingMetaService)csn.Service).GetDescription(settingId) ?? "" : "";
431-
427+
432428
var refs = MetaServices.FirstOrDefault(m => m.Service.Source == SettingMetaSource.ReferenceSettings);
433429
var refsDescription = refs != null ? ((CustomSettingMetaService)refs.Service).GetDescription(settingId) ?? "" : "";
434430

0 commit comments

Comments
 (0)