107 lines
3.5 KiB
C#
Raw Normal View History

2017-04-18 16:08:29 +02:00
using System.IO;
using Syroot.IO;
using Syroot.Maths;
using Syroot.Worms.Core.Riff;
2017-04-17 22:01:27 +02:00
namespace Syroot.Worms.Gen2
{
2017-04-18 16:08:29 +02:00
/// <summary>
/// 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.
/// </summary>
[RiffFile("PAL ")]
public class Palette : RiffFile
2017-04-17 22:01:27 +02:00
{
2017-04-18 16:08:29 +02:00
// ---- CONSTANTS ----------------------------------------------------------------------------------------------
private const int _version = 0x0300;
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the <see cref="Palette"/> class, loading the data from the given
/// <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to load the data from.</param>
public Palette(Stream stream)
: base(stream)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Palette"/> class, loading the data from the given file.
/// </summary>
/// <param name="fileName">The name of the file to load the data from.</param>
public Palette(string fileName)
: base(fileName)
{
}
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
/// <summary>
/// Gets the version of the palette data.
/// </summary>
public int Version { get; set; }
/// <summary>
/// Gets the <see cref="Color"/> instances stored in this palette.
/// </summary>
public Color[] Colors { get; set; }
/// <summary>
/// Gets the unknown data in the offl chunk.
/// </summary>
public byte[] OfflData { get; set; }
/// <summary>
/// Gets the unknown data in the tran chunk.
/// </summary>
public byte[] TranData { get; set; }
/// <summary>
/// Gets the unknown data in the unde chunk.
/// </summary>
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);
}
2017-04-17 22:01:27 +02:00
}
}