Skip to content

Instantly share code, notes, and snippets.

@LordNed
Created June 23, 2015 04:33
Show Gist options
  • Save LordNed/4f22b231215c8bdd0dbf to your computer and use it in GitHub Desktop.
Save LordNed/4f22b231215c8bdd0dbf to your computer and use it in GitHub Desktop.
/// <summary> Delegate defines a function that decodes one instance of type T.</summary>
/// <param name="stream">The stream to decode the instance from</param>
private delegate T LoadFromStream<T>(EndianBinaryReader stream);
private static List<T> Collect<T>(EndianBinaryReader stream, LoadFromStream<T> function, int count)
{
List<T> values = new List<T>();
for(int i = 0; i < count; i++)
{
values.Add(function(stream));
}
return values;
}
private static List<T> ReadSection<T>(EndianBinaryReader stream, long offset, long nextOffset, LoadFromStream<T> function, int itemSize)
{
stream.BaseStream.Position = offset;
return Collect<T>(stream, function, (int)(nextOffset - offset) / itemSize);
}
#region Stream Decoding Functions
private static Color ReadColor32(EndianBinaryReader stream)
{
return new Color(stream.ReadByte() / 255f, stream.ReadByte() / 255f, stream.ReadByte() / 255f, stream.ReadByte() / 255f);
}
private static Color ReadColorShort(EndianBinaryReader stream)
{
// ToDo: Are these actually just divided by 255f? Wouldn't they be divided by short.MaxValue?
return new Color(stream.ReadInt16() / 255f, stream.ReadInt16() / 255f, stream.ReadInt16() / 255f, stream.ReadInt16() / 255f);
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment