use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use crate::crc::CRC;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackFileRef {
pub path: String,
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[repr(C)]
pub struct FileRef {
pub category: u32,
pub pack_file: u32,
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct PackIndexFile {
pub archives: Vec<PackFileRef>,
pub files: BTreeMap<CRC, FileRef>,
}
impl PackIndexFile {
pub fn add_pack(&mut self, path: String, compressed: bool) -> PackIndexHandle<'_> {
let index = self.archives.len() as u32;
self.archives.push(PackFileRef { path });
PackIndexHandle {
pki: self,
index,
compressed,
}
}
}
pub struct PackIndexHandle<'a> {
pki: &'a mut PackIndexFile,
index: u32,
compressed: bool,
}
impl<'a> PackIndexHandle<'a> {
pub fn add_files<I: Iterator<Item = CRC>>(&mut self, crcs: I) {
for crc in crcs {
self.add_file(crc)
}
}
pub fn add_file(&mut self, crc: CRC) {
self.pki.files.entry(crc).or_insert(FileRef {
category: u32::from(self.compressed),
pack_file: self.index,
});
}
}