using System.IO;
using Microsoft.Xna.Framework;
using NIFLib.Enums;
using Color3 = Microsoft.Xna.Framework.Color;
using Color4 = Microsoft.Xna.Framework.Color;
namespace NIFLib
{
public static class ReaderExtensions
{
///
/// Read a from the stream depending on Nif Version
///
/// Reader
/// Nif Object Version
/// The from or
public static bool ReadBoolean(this BinaryReader reader, NifVersion version)
{
if (version < NifVersion.VER_4_1_0_1)
return reader.ReadUInt32() != 0;
return reader.ReadBoolean();
}
///
/// Reads a [] from the stream
///
/// The
/// The length.
/// The[]
public static float[] ReadFloatArray(this BinaryReader reader, int length)
{
var array = new float[length];
for (var i = 0; i < array.Length; i++)
{
array[i] = reader.ReadSingle();
}
return array;
}
///
/// Reads a [] from the stream
///
/// The
/// The length.
/// The[].
public static uint[] ReadUInt32Array(this BinaryReader reader, int length)
{
var array = new uint[length];
for (var i = 0; i < array.Length; i++)
{
array[i] = reader.ReadUInt32();
}
return array;
}
///
/// Reads a [] from the stream
///
/// The
/// The length.
/// The []
public static ushort[] ReadUInt16Array(this BinaryReader reader, int length)
{
var array = new ushort[length];
for (var i = 0; i < array.Length; i++)
{
array[i] = reader.ReadUInt16();
}
return array;
}
///
/// Reads a from the stream
///
/// The
/// The
public static Vector2 ReadVector2(this BinaryReader reader)
{
return new Vector2(reader.ReadSingle(), reader.ReadSingle());
}
///
/// Reads a from the stream
///
/// The
/// The
public static Vector3 ReadVector3(this BinaryReader reader)
{
return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
///
/// Reads from the stream
///
/// The
/// The
public static Vector4 ReadVector4(this BinaryReader reader)
{
return new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
///
/// Reads a from the stream
///
/// The
/// The
public static Color3 ReadColor3(this BinaryReader reader)
{
return new Color3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
///
/// Reads a from the stream
///
/// The
/// The
public static Color4 ReadColor4(this BinaryReader reader)
{
return new Color4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
///
/// Reads a in byte mode from the stream
///
/// The
/// The
public static Color4 ReadColor4Byte(this BinaryReader reader)
{
return new Color4((float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f);
}
///
/// Reads a from the stream
///
/// The
/// The
public static Matrix ReadMatrix(this BinaryReader reader)
{
var identity = Matrix.Identity;
for (var i = 0; i < 3; i++)
{
for (var j = 0; j < 3; j++)
{
identity[j, i] = reader.ReadSingle();
}
}
return identity;
}
}
}