using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using NvAPIWrapper.Native.General.Structures;
using NvAPIWrapper.Native.Interfaces;
namespace NvAPIWrapper.Native.DRS.Structures
{
///
/// Represents a setting value
///
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct DRSSettingValue : IInitializable
{
private const int UnicodeStringLength = UnicodeString.UnicodeStringLength;
private const int BinaryDataMax = 4096;
// Math.Max(BinaryDataMax + sizeof(uint), UnicodeStringLength * sizeof(ushort))
private const int FullStructureSize = 4100;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = FullStructureSize, ArraySubType = UnmanagedType.U1)]
internal byte[] _BinaryValue;
///
/// Creates a new instance of containing the passed unicode string as the value
///
/// The unicode string value
public DRSSettingValue(string value)
{
if (value?.Length > UnicodeStringLength)
{
value = value.Substring(0, UnicodeStringLength);
}
_BinaryValue = new byte[FullStructureSize];
var stringBytes = Encoding.Unicode.GetBytes(value ?? string.Empty);
Array.Copy(stringBytes, 0, _BinaryValue, 0, Math.Min(stringBytes.Length, _BinaryValue.Length));
}
///
/// Creates a new instance of containing the passed byte array as the value
///
/// The byte array value
public DRSSettingValue(byte[] value)
{
_BinaryValue = new byte[FullStructureSize];
if (value?.Length > 0)
{
var arrayLength = Math.Min(value.Length, BinaryDataMax);
var arrayLengthBytes = BitConverter.GetBytes((uint) arrayLength);
Array.Copy(arrayLengthBytes, 0, _BinaryValue, 0, arrayLengthBytes.Length);
Array.Copy(value, 0, _BinaryValue, arrayLengthBytes.Length, arrayLength);
}
}
///
/// Creates a new instance of containing the passed integer as the value
///
/// The integer value
public DRSSettingValue(uint value)
{
_BinaryValue = new byte[FullStructureSize];
var arrayLengthBytes = BitConverter.GetBytes(value);
Array.Copy(arrayLengthBytes, 0, _BinaryValue, 0, arrayLengthBytes.Length);
}
///
/// Returns the value as an integer
///
/// An integer representing the value
public uint AsInteger()
{
return BitConverter.ToUInt32(_BinaryValue, 0);
}
///
/// Returns the value as an array of bytes
///
/// An array of bytes representing the value
public byte[] AsBinary()
{
return _BinaryValue.Skip(sizeof(uint)).Take((int) AsInteger()).ToArray();
}
///
/// Returns the value as an unicode string
///
/// An unicode string representing the value
public string AsUnicodeString()
{
return Encoding.Unicode.GetString(_BinaryValue).TrimEnd('\0');
}
}
}