use crate::misc::resource_id::ResourceID;
use crate::resource::runtime_resource_id::RuntimeResourceID;
use rayon::iter::ParallelIterator;
use rayon::prelude::IntoParallelIterator;
use std::collections::HashMap;
use std::fs::read_to_string;
use std::path::Path;
use std::str::FromStr;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum PathListError {
#[error("{0}")]
IoError(#[from] std::io::Error),
#[error("Invalid RuntimeResourceID entry")]
InvalidRuntimeResourceID,
}
#[derive(Default, Debug)]
pub struct PathList {
entries: HashMap<RuntimeResourceID, Option<ResourceID>>,
}
impl PathList {
pub fn new() -> Self {
Self::default()
}
pub fn parse_into(&mut self, path: &Path) -> Result<&Self, PathListError> {
let file_as_string = read_to_string(path).map_err(PathListError::IoError)?;
let lines: Vec<_> = file_as_string.lines().map(String::from).collect();
let lines_par = lines.into_par_iter();
self.entries = lines_par
.filter_map(|line_res| {
if line_res.starts_with('#') {
return None;
};
let (hash, path) = match line_res.split_once(',') {
Some((h, p)) => (h.split_once('.').unwrap().0, Some(p)),
None => (line_res.as_str(), None),
};
if let Ok(id) = u64::from_str_radix(hash, 16) {
if let Some(path) = path {
if let Ok(rid) = ResourceID::from_str(path) {
if rid.is_valid() {
return Some((RuntimeResourceID::from(id), Some(rid)));
}
}
}
Some((RuntimeResourceID::from(id), None))
} else {
None
}
})
.collect::<Vec<_>>()
.into_iter()
.collect();
Ok(self)
}
pub fn get(&self, key: &RuntimeResourceID) -> Option<&ResourceID> {
if let Some(value) = self.entries.get(key) {
if let Some(path) = value {
return Some(path);
}
return None;
}
None
}
}