using System.IO;
using Syroot.IO;
using Syroot.Maths;
using Syroot.Worms.Core.Riff;
namespace Syroot.Worms.Gen2
{
///
/// Represents a color palette stored in PAL files, following the RIFF format. It is used to index colors in images.
/// Used by WA and WWP. S. http://worms2d.info/Palette_file.
///
[RiffFile("PAL ")]
public class Palette : RiffFile
{
// ---- CONSTANTS ----------------------------------------------------------------------------------------------
private const int _version = 0x0300;
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
///
/// Initializes a new instance of the class, loading the data from the given
/// .
///
/// The to load the data from.
public Palette(Stream stream)
: base(stream)
{
}
///
/// Initializes a new instance of the class, loading the data from the given file.
///
/// The name of the file to load the data from.
public Palette(string fileName)
: base(fileName)
{
}
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
///
/// Gets the version of the palette data.
///
public int Version { get; set; }
///
/// Gets the instances stored in this palette.
///
public Color[] Colors { get; set; }
///
/// Gets the unknown data in the offl chunk.
///
public byte[] OfflData { get; set; }
///
/// Gets the unknown data in the tran chunk.
///
public byte[] TranData { get; set; }
///
/// Gets the unknown data in the unde chunk.
///
public byte[] UndeData { get; set; }
// ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------
[RiffChunkLoader("data")]
private void LoadDataChunk(BinaryDataReader reader, int length)
{
// Read the PAL version.
Version = reader.ReadInt16();
if (Version != _version)
{
throw new InvalidDataException("Unknown PAL version.");
}
// Read the colors.
Colors = new Color[reader.ReadInt16()];
for (int i = 0; i < Colors.Length; i++)
{
Colors[i] = new Color(reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
int alpha = reader.ReadByte(); // Dismiss alpha, as it is not used in W:A.
}
}
[RiffChunkLoader("offl")]
private void LoadOfflChunk(BinaryDataReader reader, int length)
{
OfflData = reader.ReadBytes(length);
}
[RiffChunkLoader("tran")]
private void LoadTranChunk(BinaryDataReader reader, int length)
{
TranData = reader.ReadBytes(length);
}
[RiffChunkLoader("unde")]
private void LoadUndeChunk(BinaryDataReader reader, int length)
{
UndeData = reader.ReadBytes(length);
}
}
}