using System;
using System.Collections.Generic;
using System.Linq;
using NvAPIWrapper.Native;
using NvAPIWrapper.Native.DRS;
using NvAPIWrapper.Native.DRS.Structures;
namespace NvAPIWrapper.DRS
{
///
/// Represents a NVIDIA driver settings profile
///
public class DriverSettingsProfile
{
internal DriverSettingsProfile(DRSProfileHandle handle, DriverSettingsSession parentSession)
{
Handle = handle;
Session = parentSession;
}
///
/// Gets a list of applications under this profile
///
public IEnumerable Applications
{
get
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
return DRSApi.EnumApplications(Session.Handle, Handle)
.Select(application => new ProfileApplication(application, this));
}
}
///
/// Gets or sets the profile support value for GPU series
///
public DRSGPUSupport GPUSupport
{
get
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var profileInfo = DRSApi.GetProfileInfo(Session.Handle, Handle);
return profileInfo.GPUSupport;
}
set
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var profileInfo = DRSApi.GetProfileInfo(Session.Handle, Handle);
profileInfo.GPUSupport = value;
DRSApi.SetProfileInfo(Session.Handle, Handle, profileInfo);
}
}
///
/// Gets the profile handle
///
public DRSProfileHandle Handle { get; private set; }
///
/// Gets a boolean value indicating if this profile is predefined
///
public bool IsPredefined
{
get
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var profileInfo = DRSApi.GetProfileInfo(Session.Handle, Handle);
return profileInfo.IsPredefined;
}
}
///
/// Gets a boolean value indicating if this profile is valid and contains a non-zero handle
///
public bool IsValid
{
get => !Handle.IsNull;
}
///
/// Gets the name of the profile
///
public string Name
{
get
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var profileInfo = DRSApi.GetProfileInfo(Session.Handle, Handle);
return profileInfo.Name;
}
}
///
/// Gets the number of application registered under this profile
///
public int NumberOfApplications
{
get
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var profileInfo = DRSApi.GetProfileInfo(Session.Handle, Handle);
return profileInfo.NumberOfApplications;
}
}
///
/// Gets the number of settings under this profile
///
public int NumberOfSettings
{
get
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var profileInfo = DRSApi.GetProfileInfo(Session.Handle, Handle);
return profileInfo.NumberOfSettings;
}
}
///
/// Gets the session that had queried this profile
///
public DriverSettingsSession Session { get; }
///
/// Gets a list of settings under this profile
///
public IEnumerable Settings
{
get
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
return DRSApi.EnumSettings(Session.Handle, Handle).Select(setting => new ProfileSetting(setting));
}
}
///
/// Creates a new profile
///
/// The session to create this profile in.
/// The name of the profile.
/// The supported GPU series for this profile.
/// An instance of representing this newly created profile.
public static DriverSettingsProfile CreateProfile(
DriverSettingsSession session,
string profileName,
DRSGPUSupport? gpuSupport = null)
{
gpuSupport = gpuSupport ?? new DRSGPUSupport();
var profileInfo = new DRSProfileV1(profileName, gpuSupport.Value);
var profileHandle = DRSApi.CreateProfile(session.Handle, profileInfo);
return new DriverSettingsProfile(profileHandle, session);
}
///
public override string ToString()
{
if (!IsValid)
{
return "[Invalid]";
}
if (IsPredefined)
{
return $"{Name} (Predefined)";
}
return Name;
}
///
/// Deletes this profile and makes this instance invalid.
///
public void Delete()
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
DRSApi.DeleteProfile(Session.Handle, Handle);
Handle = DRSProfileHandle.DefaultHandle;
}
///
/// Deletes an application by its name.
///
/// The name of the application to be deleted.
public void DeleteApplicationByName(string applicationName)
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
DRSApi.DeleteApplication(Session.Handle, Handle, applicationName);
}
///
/// Deletes a setting by its identification number
///
/// The identification number of the setting to be deleted.
public void DeleteSetting(uint settingId)
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
DRSApi.DeleteProfileSetting(Session.Handle, Handle, settingId);
}
///
/// Deletes a setting by its known identification number.
///
/// The known identification number of the setting to be deleted.
public void DeleteSetting(KnownSettingId settingId)
{
DeleteSetting(SettingInfo.GetSettingId(settingId));
}
///
/// Finds an application by its name.
///
/// The name of the application to search for.
///
/// An instance of if an application is found; otherwise
/// .
///
public ProfileApplication GetApplicationByName(string applicationName)
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var application = DRSApi.GetApplicationInfo(Session.Handle, Handle, applicationName);
if (application == null)
{
return null;
}
return new ProfileApplication(application, this);
}
///
/// Searches for a setting using its identification number.
///
/// The identification number of the setting to search for.
/// An instance of if a setting is found; otherwise .
public ProfileSetting GetSetting(uint settingId)
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var setting = DRSApi.GetSetting(Session.Handle, Handle, settingId);
if (setting == null)
{
return null;
}
return new ProfileSetting(setting.Value);
}
///
/// Searches for a setting using its known identification number.
///
/// The known identification number of the setting to search for.
/// An instance of if a setting is found; otherwise .
public ProfileSetting GetSetting(KnownSettingId settingId)
{
return GetSetting(SettingInfo.GetSettingId(settingId));
}
///
/// Restores applications and settings of this profile to their default. This also deletes custom profiles resulting in
/// their handles becoming invalid.
///
public void RestoreDefaults()
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var isPredefined = IsPredefined;
DRSApi.RestoreDefaults(Session.Handle, Handle);
if (!isPredefined)
{
Handle = DRSProfileHandle.DefaultHandle;
}
}
///
/// Restores a setting to its default value.
///
/// The identification number of the setting.
public void RestoreSettingToDefault(uint settingId)
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
DRSApi.RestoreDefaults(Session.Handle, Handle, settingId);
}
///
/// Restores a setting to its default value.
///
/// The known identification number of the setting.
public void RestoreSettingToDefault(KnownSettingId settingId)
{
RestoreSettingToDefault(SettingInfo.GetSettingId(settingId));
}
///
/// Sets a new value for a setting or creates a new setting and sets its value
///
/// The known identification number of the setting to change its value.
/// The type of the setting value.
/// The new value for the setting.
public void SetSetting(KnownSettingId settingId, DRSSettingType settingType, object value)
{
SetSetting(SettingInfo.GetSettingId(settingId), settingType, value);
}
///
/// Sets a new value for a setting or creates a new setting and sets its value
///
/// The known identification number of the setting to change its value.
/// The new value for the setting.
public void SetSetting(KnownSettingId settingId, string value)
{
SetSetting(SettingInfo.GetSettingId(settingId), value);
}
///
/// Sets a new value for a setting or creates a new setting and sets its value
///
/// The known identification number of the setting to change its value.
/// The new value for the setting.
public void SetSetting(KnownSettingId settingId, byte[] value)
{
SetSetting(SettingInfo.GetSettingId(settingId), value);
}
///
/// Sets a new value for a setting or creates a new setting and sets its value
///
/// The known identification number of the setting to change its value.
/// The new value for the setting.
public void SetSetting(KnownSettingId settingId, uint value)
{
SetSetting(SettingInfo.GetSettingId(settingId), value);
}
///
/// Sets a new value for a setting or creates a new setting and sets its value
///
/// The identification number of the setting to change its value.
/// The type of the setting value.
/// The new value for the setting.
public void SetSetting(uint settingId, DRSSettingType settingType, object value)
{
if (!IsValid)
{
throw new InvalidOperationException(
"Can not perform this operation with an invalid profile instance."
);
}
var setting = new DRSSettingV1(settingId, settingType, value);
DRSApi.SetSetting(Session.Handle, Handle, setting);
}
///
/// Sets a new value for a setting or creates a new setting and sets its value
///
/// The identification number of the setting to change its value.
/// The new value for the setting.
public void SetSetting(uint settingId, string value)
{
SetSetting(settingId, DRSSettingType.UnicodeString, value);
}
///
/// Sets a new value for a setting or creates a new setting and sets its value
///
/// The identification number of the setting to change its value.
/// The new value for the setting.
public void SetSetting(uint settingId, byte[] value)
{
SetSetting(settingId, DRSSettingType.Binary, value);
}
///
/// Sets a new value for a setting or creates a new setting and sets its value
///
/// The identification number of the setting to change its value.
/// The new value for the setting.
public void SetSetting(uint settingId, uint value)
{
SetSetting(settingId, DRSSettingType.Integer, value);
}
}
}