using System; using System.Collections.Generic; using System.Linq; using NvAPIWrapper.Native; using NvAPIWrapper.Native.DRS.Structures; namespace NvAPIWrapper.DRS { /// /// Represents a driver settings session. This is the starting point for using DRS set of functionalities. /// public class DriverSettingsSession : IDisposable { internal DriverSettingsSession(DRSSessionHandle handle) { Handle = handle; } private DriverSettingsSession() : this(DRSApi.CreateSession()) { } /// /// Gets the base settings profile /// public DriverSettingsProfile BaseProfile { get { var profileHandle = DRSApi.GetBaseProfile(Handle); if (profileHandle.IsNull) { return null; } return new DriverSettingsProfile(profileHandle, this); } } /// /// Gets the global settings profile /// public DriverSettingsProfile CurrentGlobalProfile { get { var profileHandle = DRSApi.GetCurrentGlobalProfile(Handle); if (profileHandle.IsNull) { return null; } return new DriverSettingsProfile(profileHandle, this); } set { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (string.IsNullOrEmpty(value.Name)) { throw new ArgumentException("Profile name can not be empty.", nameof(value)); } DRSApi.SetCurrentGlobalProfile(Handle, value.Name); } } /// /// Gets the session handle /// public DRSSessionHandle Handle { get; } /// /// Gets the number of registered profiles /// public int NumberOfProfiles { get => DRSApi.GetNumberOfProfiles(Handle); } /// /// Gets the list of all registered profiles /// public IEnumerable Profiles { get { return DRSApi.EnumProfiles(Handle).Select(handle => new DriverSettingsProfile(handle, this)); } } /// public void Dispose() { ReleaseUnmanagedResources(); GC.SuppressFinalize(this); } /// /// Creates a new session and load the settings /// /// A new instance of representing a session. public static DriverSettingsSession CreateAndLoad() { var session = new DriverSettingsSession(); session.Load(); return session; } /// /// Creates a new session and load the settings from a file /// /// The full path of file to load settings from. /// A new instance of representing a session. public static DriverSettingsSession CreateAndLoad(string fileName) { var session = new DriverSettingsSession(); session.Load(fileName); return session; } /// public override string ToString() { return $"{Handle} ({NumberOfProfiles} Profiles)"; } /// /// Finds an application by name. This method is useful when passed a full path of a file as it does return an /// application almost always describing the NVIDIA driver behavior regarding the passed executable file. /// /// The name of the application (with extension) or the full path of an executable file. /// An instance of class. public ProfileApplication FindApplication(string applicationName) { var application = DRSApi.FindApplicationByName(Handle, applicationName, out var profileHandle); if (application == null || !profileHandle.HasValue || profileHandle.Value.IsNull) { return null; } var profile = new DriverSettingsProfile(profileHandle.Value, this); return new ProfileApplication(application, profile); } /// /// Finds a profile based on the application named passed. This method is useful when passed a full path of a file as /// it does return a profile almost always describing the NVIDIA driver behavior regarding the passed executable file. /// /// The name of the application (with extension) or the full path of an executable file. /// /// An instance of class describing the NVIDIA driver behavior regarding the /// passed executable file. /// public DriverSettingsProfile FindApplicationProfile(string applicationName) { var application = DRSApi.FindApplicationByName(Handle, applicationName, out var profileHandle); if (application == null || !profileHandle.HasValue || profileHandle.Value.IsNull) { return null; } return new DriverSettingsProfile(profileHandle.Value, this); } /// /// Finds a profile based on its name. /// /// The profile name to search for. /// An instance of class. public DriverSettingsProfile FindProfileByName(string profileName) { var profileHandle = DRSApi.FindProfileByName(Handle, profileName); if (profileHandle.IsNull) { return null; } return new DriverSettingsProfile(profileHandle, this); } /// /// Resets all settings to default. /// public void RestoreDefaults() { DRSApi.RestoreDefaults(Handle); } /// /// Saves the current session settings /// public void Save() { DRSApi.SaveSettings(Handle); } /// /// Saves the current session settings to a file /// /// The full path of file to save settings to. public void Save(string fileName) { DRSApi.SaveSettings(Handle, fileName); } private void Load() { DRSApi.LoadSettings(Handle); } private void Load(string fileName) { DRSApi.LoadSettings(Handle, fileName); } private void ReleaseUnmanagedResources() { DRSApi.DestroySession(Handle); } /// ~DriverSettingsSession() { ReleaseUnmanagedResources(); } } }