195 lines
6.2 KiB
C#

using System.IO;
using Syroot.IO;
using Syroot.Maths;
using Syroot.Worms.Core;
namespace Syroot.Worms.Gen2
{
/// <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, ILoadableFile, ISaveableFile
{
// ---- CONSTANTS ----------------------------------------------------------------------------------------------
private const short _version = 0x0300;
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the <see cref="Palette"/> class.
/// </summary>
public Palette()
{
Version = _version;
}
/// <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)
{
Load(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)
{
Load(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 (PUBLIC) ---------------------------------------------------------------------------------------
/// <summary>
/// Loads the data from the given <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to load the data from.</param>
public void Load(Stream stream)
{
LoadRiff(stream);
}
/// <summary>
/// Loads the data from the given file.
/// </summary>
/// <param name="fileName">The name of the file to load the data from.</param>
public void Load(string fileName)
{
using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Load(stream);
}
}
/// <summary>
/// Saves the data into the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream)
{
SaveRiff(stream);
}
/// <summary>
/// Saves the data in the given file.
/// </summary>
/// <param name="fileName">The name of the file to save the data in.</param>
public void Save(string fileName)
{
using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
Save(stream);
}
}
// ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------
[RiffChunkLoad("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 WA.
}
}
[RiffChunkLoad("offl")]
private void LoadOfflChunk(BinaryDataReader reader, int length)
{
OfflData = reader.ReadBytes(length);
}
[RiffChunkLoad("tran")]
private void LoadTranChunk(BinaryDataReader reader, int length)
{
TranData = reader.ReadBytes(length);
}
[RiffChunkLoad("unde")]
private void LoadUndeChunk(BinaryDataReader reader, int length)
{
UndeData = reader.ReadBytes(length);
}
[RiffChunkSave("data")]
private void SaveDataChunk(BinaryDataWriter writer)
{
// Write the PAL version.
writer.Write(_version);
// Write the colors.
writer.Write((short)Colors.Length);
for (int i = 0; i < Colors.Length; i++)
{
Color color = Colors[i];
writer.Write(color.R);
writer.Write(color.G);
writer.Write(color.B);
writer.Write((byte)0); // Dismiss alpha, as it is not used in WA.
}
}
[RiffChunkSave("offl")]
private void SaveOfflChunk(BinaryDataWriter writer)
{
writer.Write(OfflData);
}
[RiffChunkSave("tran")]
private void SaveTranChunk(BinaryDataWriter writer)
{
writer.Write(TranData);
}
[RiffChunkSave("unde")]
private void SaveUndeChunk(BinaryDataWriter writer)
{
writer.Write(UndeData);
}
}
}