use core::{error, fmt};
use crate::basic_types::Vector2I16;
use crate::formats::binary::{BinaryBlockType, BinaryFile, BinaryFileError};
use crate::formats::tlv::{TlvEntries, TlvEntry};
use crate::palette::ColorPalette;
mod tags {
pub const COORDINATES: u8 = 0x00;
pub const PALETTE: u8 = 0x01;
pub const MATRIX_SIZE: u8 = 0x02;
}
const MAX_COMPONENT_VALUE: i16 = 4095;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum GcsLightControlSetupError {
Corrupted,
File(BinaryFileError),
}
impl fmt::Display for GcsLightControlSetupError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Corrupted => f.write_str("the GCS light control setup block is malformed"),
Self::File(_) => f.write_str("the show file could not be read"),
}
}
}
impl error::Error for GcsLightControlSetupError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::Corrupted => None,
Self::File(err) => Some(err),
}
}
}
impl From<BinaryFileError> for GcsLightControlSetupError {
fn from(err: BinaryFileError) -> Self {
Self::File(err)
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct GcsLightControlSetup<'a> {
coords: Vector2I16,
size: Vector2I16,
palette: ColorPalette<'a>,
}
impl GcsLightControlSetup<'static> {
#[must_use]
pub const fn new() -> Self {
Self {
coords: Vector2I16 { x: 0, y: 0 },
size: Vector2I16 { x: 0, y: 0 },
palette: ColorPalette::new(),
}
}
}
impl<'a> GcsLightControlSetup<'a> {
pub fn from_buffer(body: &'a [u8]) -> Result<Self, GcsLightControlSetupError> {
let mut result = GcsLightControlSetup::new();
for entry in TlvEntries::new(body) {
let entry = entry.map_err(|_| GcsLightControlSetupError::Corrupted)?;
match entry.tag {
tags::COORDINATES => result.coords = parse_vector2_i16(entry)?,
tags::MATRIX_SIZE => result.size = parse_vector2_i16(entry)?,
tags::PALETTE => {
result.palette = ColorPalette::from_buffer(entry.value)
.map_err(|_| GcsLightControlSetupError::Corrupted)?;
}
_ => {}
}
}
Ok(result)
}
pub fn from_binary_file(
file: &BinaryFile<'a>,
) -> Result<Option<Self>, GcsLightControlSetupError> {
file.find_first_block_by_type(BinaryBlockType::GCS_LIGHT_CONTROL_SETUP)?
.map(|block| Self::from_buffer(block.body))
.transpose()
}
#[must_use]
pub fn into_owned(self) -> GcsLightControlSetup<'static> {
GcsLightControlSetup {
coords: self.coords,
size: self.size,
palette: self.palette.into_owned(),
}
}
pub fn clear(&mut self) {
*self = GcsLightControlSetup::new();
}
#[must_use]
pub const fn coords(&self) -> Vector2I16 {
self.coords
}
#[must_use]
pub const fn size(&self) -> Vector2I16 {
self.size
}
#[must_use]
pub const fn palette(&self) -> &ColorPalette<'a> {
&self.palette
}
}
fn parse_vector2_i16(entry: TlvEntry<'_>) -> Result<Vector2I16, GcsLightControlSetupError> {
let &[x0, x1, y0, y1] = entry.value else {
return Err(GcsLightControlSetupError::Corrupted);
};
let result = Vector2I16 {
x: i16::from_le_bytes([x0, x1]),
y: i16::from_le_bytes([y0, y1]),
};
let valid = 0..=MAX_COMPONENT_VALUE;
if valid.contains(&result.x) && valid.contains(&result.y) {
Ok(result)
} else {
Err(GcsLightControlSetupError::Corrupted)
}
}