lua_assembler/formats/luac/reader/
mod.rs

1use crate::formats::luac::{view::LuacView, LuacReadConfig};
2use gaia_types::GaiaError;
3use std::io::{Read, Seek, SeekFrom};
4
5/// 负责解析 .pyc 文件到 LuacFile 的读取器
6#[derive(Debug)]
7pub struct LuacReader<'config, R> {
8    pub(crate) reader: R,
9    pub(crate) view: LuacView,
10    // week errors
11    pub(crate) errors: Vec<GaiaError>,
12    pub(crate) config: &'config LuacReadConfig,
13    pub(crate) offset: u64,
14}
15
16impl LuacReadConfig {
17    pub fn as_reader<R: Read>(&self, reader: R) -> LuacReader<R> {
18        LuacReader::new(reader, self)
19    }
20}
21
22impl<'config, R> LuacReader<'config, R> {
23    pub fn new(reader: R, config: &'config LuacReadConfig) -> Self {
24        Self { reader, view: LuacView::default(), errors: vec![], config, offset: 0 }
25    }
26
27    pub fn get_offset(&self) -> u64 {
28        self.offset
29    }
30
31    pub fn set_offset(&mut self, offset: u64) -> Result<(), GaiaError>
32    where
33        R: Seek,
34    {
35        self.reader.seek(SeekFrom::Start(offset))?;
36        Ok(self.offset = offset)
37    }
38
39    fn check_magic_head(&mut self) -> Result<(), GaiaError> {
40        let expected = [0; 4];
41        if self.config.check_magic_head {
42            if self.view.magic_head != expected {
43                Err(GaiaError::invalid_magic_head(self.view.magic_head.to_vec(), expected.to_vec()))?;
44            }
45        }
46        Ok(())
47    }
48}
49
50impl<'config, R: Read + Seek> LuacReader<'config, R> {
51    pub fn read(mut self) -> Result<LuacView, GaiaError> {
52        self.set_offset(0)?;
53        self.read_to_end()?;
54        Ok(self.view)
55    }
56
57    pub fn read_to_end(&mut self) -> Result<(), GaiaError> {
58        todo!()
59    }
60}