Skip to main content

uorustlibs/map/
radarcol.rs

1//! Methods for reading radar colors from radarcol.mul
2//!
3//! This file is one of the simplest muls - a sequence of Color16s
4//!
5//! It's expected for the file to contain 65536 records.
6//! Each color matches a given tile in Art, and (offset by 16384 bytes) each static in there, too
7use crate::color::Color16;
8use crate::error::MulReaderResult;
9use byteorder::{LittleEndian, ReadBytesExt};
10use std::fs::File;
11use std::io::{Read, Seek, SeekFrom};
12use std::path::Path;
13
14/// A struct to help read colors out of RadarCol
15#[derive(Debug)]
16pub struct RadarColReader<T: Read + Seek> {
17    data_reader: T,
18    length: u32,
19}
20
21impl RadarColReader<File> {
22    /// Create a RadarColReader from a path
23    pub fn new(radar_col_path: &Path) -> MulReaderResult<RadarColReader<File>> {
24        let data_reader = File::open(radar_col_path)?;
25        let meta = data_reader.metadata()?;
26        let length = meta.len() as u32;
27
28        Ok(RadarColReader {
29            data_reader,
30            length,
31        })
32    }
33}
34
35impl<T: Read + Seek> RadarColReader<T> {
36    /// Create a RadarColReader from an existing readable
37    pub fn from_readable(data_reader: T, length: u32) -> RadarColReader<T> {
38        RadarColReader {
39            data_reader,
40            length,
41        }
42    }
43
44    /// Read the color at a specific index
45    pub fn read(&mut self, id: u32) -> MulReaderResult<Color16> {
46        self.data_reader.seek(SeekFrom::Start((id * 2) as u64))?;
47        let data = self.data_reader.read_u16::<LittleEndian>()?;
48        Ok(data)
49    }
50
51    /// Read all colors contained in the file
52    pub fn read_all(&mut self) -> MulReaderResult<Vec<Color16>> {
53        let mut output = vec![];
54        self.data_reader.seek(SeekFrom::Start(0))?;
55        for _i in 0..(self.length / 2) {
56            output.push(self.data_reader.read_u16::<LittleEndian>()?);
57        }
58        Ok(output)
59    }
60}