rscache/definition/
osrs.rs

1#[allow(clippy::too_many_lines)]
2mod item_def;
3mod loc_def;
4mod map_def;
5mod npc_def;
6#[allow(clippy::too_many_lines)]
7mod obj_def;
8
9pub use item_def::*;
10pub use loc_def::*;
11pub use map_def::*;
12pub use npc_def::*;
13pub use obj_def::*;
14
15use std::collections::HashMap;
16
17use crate::Cache;
18use runefs::{ArchiveFileGroup, IndexMetadata, REFERENCE_TABLE_ID};
19
20/// Marker trait for definitions.
21pub trait Definition: Sized {
22    fn new(id: u16, buffer: &[u8]) -> crate::Result<Self>;
23}
24
25/// Adds definition fetching from the cache to every struct that implements `Definition`.
26///
27/// The main difference between `fetch_from_index` and `fetch_from_archive`:
28/// - `fetch_from_index` will get only 1 definition from each archive making it a 1:1 relation.
29/// - `fetch_from_archive` will get multiple definitions from each archive making it a N:1 relation
30///   where N is atleast 1.
31pub trait FetchDefinition: Definition {
32    // TODO: finish documentation with example.
33    /// Fetches multiple definitions from every archive in the index.
34    ///
35    /// Note: every archive contains only one definition. (1:1)
36    ///
37    /// # Errors
38    ///
39    /// Can return multiple errors: if reading, decoding or parsing definition buffers fail.
40    fn fetch_from_index<D>(cache: &Cache, index_id: u8) -> crate::Result<HashMap<u16, D>>
41    where
42        D: Definition,
43    {
44        let buffer = cache.read(REFERENCE_TABLE_ID, index_id as u32)?.decode()?;
45        let archives = IndexMetadata::from_buffer(buffer)?;
46        let mut definitions = HashMap::new();
47        for archive in &archives {
48            let buffer = cache.read(index_id, archive.id)?.decode()?;
49
50            definitions.insert(archive.id as u16, D::new(archive.id as u16, &buffer)?);
51        }
52
53        Ok(definitions)
54    }
55
56    /// Fetches multiple definitions from a single archive.
57    ///
58    /// Note: every archive contains multiple definitions. (N:1)
59    ///
60    /// # Errors
61    ///
62    /// Can return multiple errors: if reading, decoding or parsing definition buffers fail.
63    ///
64    /// # Examples
65    ///
66    /// ```
67    /// # use std::collections::HashMap;
68    /// # use rscache::Cache;
69    /// use rscache::definition::osrs::{
70    ///     FetchDefinition,
71    ///     ItemDefinition,
72    /// };
73    ///
74    /// # fn main() -> Result<(), rscache::Error> {
75    /// # let cache = Cache::new("./data/osrs_cache")?;
76    /// let index_id = 2; // Config index.
77    /// let archive_id = 10; // Archive containing item definitions.
78    ///
79    /// let item_defs: HashMap<u16, ItemDefinition>
80    ///     = ItemDefinition::fetch_from_archive(&cache, index_id, archive_id)?;
81    /// # Ok(())
82    /// # }
83    /// ```
84    fn fetch_from_archive<D>(
85        cache: &Cache,
86        index_id: u8,
87        archive_id: u32,
88    ) -> crate::Result<HashMap<u16, D>>
89    where
90        D: Definition,
91    {
92        let buffer = cache.read(REFERENCE_TABLE_ID, index_id as u32)?.decode()?;
93        let archives = IndexMetadata::from_buffer(buffer)?;
94        let entry_count = archives[archive_id as usize - 1].entry_count;
95        let buffer = cache.read(index_id, archive_id)?.decode()?;
96
97        let archive_group = ArchiveFileGroup::from_buffer(&buffer, entry_count);
98
99        let mut definitions = HashMap::new();
100        for archive_file in archive_group {
101            definitions.insert(
102                archive_file.id as u16,
103                D::new(archive_file.id as u16, &archive_file.data)?,
104            );
105        }
106
107        Ok(definitions)
108    }
109}
110
111impl<D: Definition> FetchDefinition for D {}