uorustlibs/map/
static_location.rs1use super::diff::StaticLocationDiffReader;
12use super::shared::{StaticLocation, read_block_statics};
13use crate::error::{MulReaderError, MulReaderResult};
14use crate::mul::MulReader;
15use std::fs::File;
16use std::io::{Read, Seek};
17use std::path::Path;
18
19#[derive(Debug)]
24pub struct StaticLocationReader<T: Read + Seek> {
25 mul_reader: MulReader<T>,
26 width: u32,
28 height: u32,
30}
31
32impl StaticLocationReader<File> {
33 pub fn new(
35 index_path: &Path,
36 mul_path: &Path,
37 width_blocks: u32,
38 height_blocks: u32,
39 ) -> MulReaderResult<StaticLocationReader<File>> {
40 let mul_reader = MulReader::new(index_path, mul_path)?;
41
42 Ok(StaticLocationReader {
43 mul_reader,
44 width: width_blocks,
45 height: height_blocks,
46 })
47 }
48}
49
50impl<T: Read + Seek> StaticLocationReader<T> {
51 pub fn from_mul(
53 mul_reader: MulReader<T>,
54 width_blocks: u32,
55 height_blocks: u32,
56 ) -> StaticLocationReader<T> {
57 StaticLocationReader {
58 mul_reader,
59 width: width_blocks,
60 height: height_blocks,
61 }
62 }
63
64 pub fn read_block(
67 &mut self,
68 id: u32,
69 patch: Option<&mut StaticLocationDiffReader<T>>,
70 ) -> MulReaderResult<Vec<StaticLocation>> {
71 match patch {
72 Some(reader) => reader
73 .read(id)
74 .unwrap_or_else(|| read_block_statics(&mut self.mul_reader, id)),
75 None => read_block_statics(&mut self.mul_reader, id),
76 }
77 }
78
79 pub fn read_block_from_coordinates(
81 &mut self,
82 x: u32,
83 y: u32,
84 patch: Option<&mut StaticLocationDiffReader<T>>,
85 ) -> MulReaderResult<Vec<StaticLocation>> {
86 let width = self.width;
87 let height = self.height;
88 if x < width && y < height {
89 self.read_block(y + (x * height), patch)
90 } else {
91 Err(MulReaderError::CoordinatesOutOfBounds { x, y })
92 }
93 }
94}