rs2cache/
checksumtable.rs

1use crate::store::Store;
2use crc32fast::hash;
3use osrs_bytes::WriteExt;
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum ChecksumTableError {
8    #[error("io error: {0}")]
9    Io(#[from] std::io::Error),
10    #[error("store error: {0}")]
11    Store(#[from] crate::store::StoreError),
12}
13
14pub struct ChecksumTable {
15    entries: Vec<u32>,
16}
17
18impl ChecksumTable {
19    pub fn write(&self) -> Result<(), ChecksumTableError> {
20        let mut buf = Vec::new();
21
22        for entry in &self.entries {
23            buf.write_u32(*entry)?;
24        }
25
26        let mut checksum: u32 = 1234;
27        for entry in &self.entries {
28            checksum = (checksum << 1).wrapping_add(*entry)
29        }
30
31        Ok(())
32    }
33
34    pub fn create(store: Box<dyn Store>) -> Result<ChecksumTable, ChecksumTableError> {
35        let mut entries = Vec::new();
36        let mut next_archive = 0;
37
38        for archive in store.list(0)? {
39            let entry = hash(&store.read(0, archive)?);
40
41            for _ in next_archive..archive {
42                //entries.push(0);
43            }
44
45            entries.push(entry);
46            next_archive = archive + 1;
47        }
48
49        Ok(ChecksumTable { entries })
50    }
51}