// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Linq;
using System.Text;
namespace Epic.OnlineServices
{
public sealed partial class Helper
{
///
/// Converts an to a of the specified .
///
/// The type of to convert to.
/// The value to convert from.
/// The converted value.
private static void Convert(IntPtr from, out THandle to)
where THandle : Handle, new()
{
to = null;
if (from != IntPtr.Zero)
{
to = new THandle();
to.InnerHandle = from;
}
}
///
/// Converts a to an .
///
/// The value to convert from.
/// The converted value.
private static void Convert(Handle from, out IntPtr to)
{
to = IntPtr.Zero;
if (from != null)
{
to = from.InnerHandle;
}
}
///
/// Converts from a [] to a .
///
/// The value to convert from.
/// The converted value.
private static void Convert(byte[] from, out string to)
{
to = null;
if (from == null)
{
return;
}
to = Encoding.ASCII.GetString(from.Take(GetAnsiStringLength(from)).ToArray());
}
///
/// Converts from a of the specified length to a [].
///
/// The value to convert from.
/// The length to convert from.
/// The converted value.
private static void Convert(string from, out byte[] to, int fromLength)
{
if (from == null)
{
from = "";
}
to = Encoding.ASCII.GetBytes(new string(from.Take(fromLength).ToArray()).PadRight(fromLength, '\0'));
}
///
/// Converts from a [] to an .
/// Outputs the length of the [].
///
/// The type of to convert from.
/// The value to convert from.
/// The converted value; the length of the [].
private static void Convert(TArray[] from, out int to)
{
to = 0;
if (from != null)
{
to = from.Length;
}
}
///
/// Converts from a [] to an .
/// Outputs the length of the [].
///
/// The type of to convert from.
/// The value to convert from.
/// The converted value; the length of the [].
private static void Convert(TArray[] from, out uint to)
{
to = 0;
if (from != null)
{
to = (uint)from.Length;
}
}
///
/// Converts from an to an .
/// Outputs the length of the .
///
/// The type of the .
/// The value to convert from.
/// The converted value; the length of the .
private static void Convert(ArraySegment from, out int to)
{
to = from.Count;
}
///
/// Converts from an to an .
/// Outputs the length of the .
///
/// The type of the .
/// The value to convert from.
/// The converted value; the length of the .
private static void Convert(ArraySegment from, out uint to)
{
to = (uint)from.Count;
}
///
/// Converts from an to a .
///
/// The value to convert from.
/// The converted value.
private static void Convert(int from, out bool to)
{
to = from != 0;
}
///
/// Converts from an to an .
///
/// The value to convert from.
/// The converted value.
private static void Convert(bool from, out int to)
{
to = from ? 1 : 0;
}
///
/// Converts from a ? to a .
/// Outputs the number of seconds represented by the ? as a unix timestamp.
/// A ? equates to a value of -1, which means unset in the SDK.
///
/// The value to convert from.
/// The converted value.
private static void Convert(DateTimeOffset? from, out long to)
{
to = -1;
if (from.HasValue)
{
DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long unixTimestampTicks = (from.Value.UtcDateTime - unixStart).Ticks;
long unixTimestampSeconds = unixTimestampTicks / TimeSpan.TicksPerSecond;
to = unixTimestampSeconds;
}
}
///
/// Converts from a to a ?.
///
/// The value to convert from.
/// The converted value.
private static void Convert(long from, out DateTimeOffset? to)
{
to = null;
if (from >= 0)
{
DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
long unixTimeStampTicks = from * TimeSpan.TicksPerSecond;
to = new DateTimeOffset(unixStart.Ticks + unixTimeStampTicks, TimeSpan.Zero);
}
}
}
}