2025-07-08 21:37:03 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use crate::ByteBuffer;
|
|
|
|
|
|
|
|
use super::Resource;
|
|
|
|
|
|
|
|
/// Used to read unpacked files from a directory.
|
|
|
|
pub struct UnpackedResource {
|
|
|
|
base_directory: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnpackedResource {
|
|
|
|
pub fn from_existing(base_directory: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
base_directory: base_directory.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for UnpackedResource {
|
|
|
|
fn read(&mut self, path: &str) -> Option<ByteBuffer> {
|
|
|
|
let mut new_path = PathBuf::from(&self.base_directory);
|
2025-07-08 22:23:16 -04:00
|
|
|
new_path.push(path.to_lowercase());
|
2025-07-08 21:37:03 -04:00
|
|
|
|
|
|
|
std::fs::read(new_path).ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exists(&mut self, path: &str) -> bool {
|
|
|
|
let mut new_path = PathBuf::from(&self.base_directory);
|
2025-07-08 22:23:16 -04:00
|
|
|
new_path.push(path.to_lowercase());
|
2025-07-08 21:37:03 -04:00
|
|
|
|
|
|
|
std::fs::exists(new_path).unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn common_setup_data() -> UnpackedResource {
|
|
|
|
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
d.push("resources/tests");
|
|
|
|
|
|
|
|
UnpackedResource::from_existing(d.to_str().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_files() {
|
|
|
|
let mut data = common_setup_data();
|
|
|
|
|
|
|
|
assert!(data.read("empty_planlive.lgb").is_some());
|
|
|
|
assert!(data.read("non_existent.lgb").is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exist_files() {
|
|
|
|
let mut data = common_setup_data();
|
|
|
|
|
|
|
|
assert_eq!(data.exists("empty_planlive.lgb"), true);
|
|
|
|
assert_eq!(data.exists("non_existent.lgb"), false);
|
|
|
|
}
|
|
|
|
}
|