using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using NvAPIWrapper.Native; using NvAPIWrapper.Native.DRS; namespace NvAPIWrapper.DRS { /// /// Contains information about a setting /// public class SettingInfo { private static uint[] _availableSettingIds; private SettingInfo(uint settingId) { SettingId = settingId; } /// /// Gets an array of available possible valid values. /// public object[] AvailableValues { get { if (!IsAvailable) { return null; } return DRSApi.EnumAvailableSettingValues(SettingId).Values; } } /// /// Gets the default value of this setting /// public object DefaultValue { get { if (!IsAvailable) { return null; } var values = DRSApi.EnumAvailableSettingValues(SettingId); return values.DefaultValue; } } /// /// Gets a boolean value indicating if this setting is available on this machine and with the current version of NVIDIA /// driver /// public bool IsAvailable { get => GetAvailableSetting().Any(info => info.SettingId == SettingId); } /// /// Gets a boolean value indicating if this setting is know by this library /// public bool IsKnown { get => IsSettingKnown(SettingId); } /// /// Gets the description of this setting from the library or if this setting is not known by /// the library. /// public string KnownDescription { get { if (!IsKnown || KnownSettingId == null) { return null; } return GetSettingDescription(KnownSettingId.Value); } } /// /// Gets the known identification number of this setting from the library or if this setting is /// not known by the library. /// public KnownSettingId? KnownSettingId { get { if (!IsKnown) { return null; } return GetKnownSettingId(SettingId); } } /// /// Gets the type of a static class or an enum containing possible known values for this setting from the library or /// if this setting is not known by the library /// public Type KnownValueType { get { if (!IsKnown || !IsAvailable) { return null; } var name = KnownSettingId.ToString(); var nameSpace = typeof(SettingInfo).Namespace + ".SettingValues"; if (SettingType == DRSSettingType.Integer) { return Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(type => type.IsEnum && type.Namespace?.Equals(nameSpace, StringComparison.InvariantCultureIgnoreCase) == true && type.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); } if (SettingType == DRSSettingType.String || SettingType == DRSSettingType.UnicodeString) { return Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(type => type.IsClass && type.Namespace?.Equals(nameSpace, StringComparison.InvariantCultureIgnoreCase) == true && type.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); } return null; } } /// /// Gets the name of the setting from NVIDIA driver or if the setting is not available on this /// machine. /// public string Name { get { if (!IsAvailable) { return null; } return DRSApi.GetSettingNameFromId(SettingId); } } /// /// Gets the setting identification number /// public uint SettingId { get; } /// /// Gets the value type of the setting from NVIDIA driver or if the setting is not available on /// this machine. /// public DRSSettingType? SettingType { get { if (!IsAvailable) { return null; } var values = DRSApi.EnumAvailableSettingValues(SettingId); return values.SettingType; } } /// /// Gets information regarding a setting from its identification number. /// /// The identification number of the setting to get information about. /// An instance of containing information about the setting. public static SettingInfo FromId(uint settingId) { return new SettingInfo(settingId); } /// /// Gets information regarding a setting from its known identification number. /// /// The known identification number of the setting to get information about. /// An instance of containing information about the setting. public static SettingInfo FromKnownSettingId(KnownSettingId settingId) { return FromId(GetSettingId(settingId)); } /// /// Gets information regarding a setting from its name. /// /// The name of the setting to get information about. /// An instance of containing information about the setting. public static SettingInfo FromName(string settingName) { var settingId = DRSApi.GetSettingIdFromName(settingName); return FromId(settingId); } /// /// Gets a list of all available setting on this machine /// /// Instances of each representing a available setting on this machine. public static SettingInfo[] GetAvailableSetting() { if (_availableSettingIds == null) { _availableSettingIds = DRSApi.EnumAvailableSettingIds(); } return _availableSettingIds.Select(FromId).ToArray(); } /// /// Gets the known identification number of a setting from its identification number /// /// The setting identification number. /// The known setting identification number if the setting is known; otherwise . public static KnownSettingId? GetKnownSettingId(uint settingId) { if (!IsSettingKnown(settingId)) { return null; } return (KnownSettingId) settingId; } /// /// Gets the known setting description from its identification number /// /// The known setting identification number. /// The known setting description if available; otherwise . public static string GetSettingDescription(KnownSettingId knownSettingId) { var enumName = Enum.GetName(typeof(KnownSettingId), knownSettingId); if (enumName == null) { return null; } var enumField = typeof(KnownSettingId).GetField(enumName); if (enumField == null) { return null; } var descriptionAttribute = enumField .GetCustomAttributes(typeof(DescriptionAttribute), false) .OfType() .FirstOrDefault(); if (string.IsNullOrWhiteSpace(descriptionAttribute?.Description)) { return null; } return descriptionAttribute.Description; } /// /// Gets the identification number of a setting from its known identification number /// /// The known setting identification number. /// The setting identification number. public static uint GetSettingId(KnownSettingId knownSettingId) { return (uint) knownSettingId; } /// /// Checks if a setting is known by this library. /// /// The setting identification number. /// true if setting is known by this library; otherwise false. public static bool IsSettingKnown(uint settingId) { return Enum.IsDefined(typeof(KnownSettingId), settingId); } /// public override string ToString() { try { var settingName = Name; if (!string.IsNullOrWhiteSpace(settingName)) { return settingName; } } catch { // ignore; } return $"#{SettingId:X}"; } /// /// Tries to resolve the name of a known value using its actual value /// /// The actual value /// The name of the known value member. public string ResolveKnownValueName(object value) { if (!IsKnown) { return null; } var valueType = KnownValueType; if (valueType == null) { return null; } if (valueType.IsEnum) { return Enum.GetName(valueType, value); } var comparerType = typeof(EqualityComparer<>).MakeGenericType(value.GetType()); var comparer = comparerType.GetProperty(nameof(EqualityComparer.Default))?.GetValue(null); if (!(comparer is IEqualityComparer equalityComparer)) { return null; } return valueType.GetFields() .FirstOrDefault(info => info.IsStatic && equalityComparer.Equals(info.GetValue(null), value) )?.Name; } } }