using System.IO; using Syroot.IO; using Syroot.Maths; using Syroot.Worms.Core; 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, ILoadableFile, ISaveableFile { // ---- CONSTANTS ---------------------------------------------------------------------------------------------- private const short _version = 0x0300; // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------ /// /// Initializes a new instance of the class. /// public Palette() { Version = _version; } /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. public Palette(Stream stream) { Load(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) { Load(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 (PUBLIC) --------------------------------------------------------------------------------------- /// /// Loads the data from the given . /// /// The to load the data from. public void Load(Stream stream) { LoadRiff(stream); } /// /// Loads the data from the given file. /// /// The name of the file to load the data from. public void Load(string fileName) { using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { Load(stream); } } /// /// Saves the data into the given . /// /// The to save the data to. public void Save(Stream stream) { SaveRiff(stream); } /// /// Saves the data in the given file. /// /// The name of the file to save the data in. 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); } } }