Skip to main content

steel_registry/loot_table/
registry.rs

1use super::{FxHashMap, Identifier, LootTable};
2
3pub type LootTableRef = &'static LootTable;
4
5/// Registry for loot tables.
6pub struct LootTableRegistry {
7    tables_by_id: Vec<LootTableRef>,
8    tables_by_key: FxHashMap<Identifier, usize>,
9    allows_registering: bool,
10}
11
12impl LootTableRegistry {
13    #[must_use]
14    pub fn new() -> Self {
15        Self {
16            tables_by_id: Vec::new(),
17            tables_by_key: FxHashMap::default(),
18            allows_registering: true,
19        }
20    }
21
22    pub fn register(&mut self, table: LootTableRef) -> usize {
23        assert!(
24            self.allows_registering,
25            "Cannot register loot tables after the registry has been frozen"
26        );
27
28        let id = self.tables_by_id.len();
29        self.tables_by_key.insert(table.key.clone(), id);
30        self.tables_by_id.push(table);
31        id
32    }
33
34    pub fn iter(&self) -> impl Iterator<Item = (usize, LootTableRef)> + '_ {
35        self.tables_by_id
36            .iter()
37            .enumerate()
38            .map(|(id, &table)| (id, table))
39    }
40}
41
42impl Default for LootTableRegistry {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48crate::impl_registry!(
49    LootTableRegistry,
50    LootTable,
51    tables_by_id,
52    tables_by_key,
53    loot_tables
54);