use std::borrow::Cow;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use crate::mesh::Image;
pub trait Assets {
fn find(&self, reference: &str, model_dir: &Path) -> Option<PathBuf>;
#[cfg(feature = "fs")]
fn read(&self, path: &Path) -> Option<Cow<'_, [u8]>> {
std::fs::read(path).ok().map(Cow::Owned)
}
#[cfg(not(feature = "fs"))]
fn read(&self, path: &Path) -> Option<Cow<'_, [u8]>>;
fn image(&self, bytes: &[u8], name: &Path) -> Option<Image> {
let _ = name;
Image::png_or_jpeg(bytes).or_else(|| Image::webp(bytes))
}
fn load_image(&self, path: &Path) -> Option<Image> {
self.image(&self.read(path)?, path)
}
fn color_texture_for(&self, model: &Path) -> Option<PathBuf> {
let _ = model;
None
}
}
#[cfg(feature = "fs")]
#[derive(Debug, Clone)]
pub struct Folder {
pub root: PathBuf,
}
#[cfg(feature = "fs")]
impl Folder {
pub fn new(root: impl Into<PathBuf>) -> Self {
Self { root: root.into() }
}
}
#[cfg(feature = "fs")]
impl Assets for Folder {
fn find(&self, reference: &str, model_dir: &Path) -> Option<PathBuf> {
find_texture(reference, model_dir, &self.root)
}
fn color_texture_for(&self, model: &Path) -> Option<PathBuf> {
let home = model
.parent()
.and_then(Path::parent)
.filter(|h| h.starts_with(&self.root))
.unwrap_or(&self.root);
color_texture_by_name(model, |visit| walk_images(home, visit))
}
}
#[derive(Debug, Clone, Default)]
pub struct Memory {
files: BTreeMap<String, Vec<u8>>,
}
impl Memory {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, path: &str, bytes: impl Into<Vec<u8>>) -> &mut Self {
self.files.insert(memory_key(path), bytes.into());
self
}
#[must_use]
pub fn with(mut self, path: &str, bytes: impl Into<Vec<u8>>) -> Self {
self.insert(path, bytes);
self
}
pub fn get(&self, path: &str) -> Option<&[u8]> {
self.files.get(&memory_key(path)).map(Vec::as_slice)
}
pub fn paths(&self) -> impl Iterator<Item = &str> {
self.files.keys().map(String::as_str)
}
fn images(&self, visit: &mut dyn FnMut(&Path, &str) -> bool) {
for k in self.files.keys() {
let p = Path::new(k);
if let Some(stem) = image_stem(p)
&& visit(p, &stem)
{
return;
}
}
}
}
impl Assets for Memory {
fn find(&self, reference: &str, model_dir: &Path) -> Option<PathBuf> {
let cleaned = reference.trim().replace('\\', "/");
if cleaned.is_empty() {
return None;
}
if !cleaned.starts_with('/') && !cleaned.contains(':') {
let rel = memory_key(&format!("{}/{cleaned}", model_dir.to_string_lossy()));
if self.files.contains_key(&rel) {
return Some(rel.into());
}
}
let name = cleaned.rsplit('/').next()?.to_lowercase();
let by_name = self
.files
.keys()
.find(|k| k.rsplit('/').next().is_some_and(|n| n.to_lowercase() == name));
if let Some(k) = by_name {
return Some(k.into());
}
same_name_other_format(&name, |visit| self.images(visit))
}
fn read(&self, path: &Path) -> Option<Cow<'_, [u8]>> {
self.files
.get(&memory_key(&path.to_string_lossy()))
.map(|b| Cow::Borrowed(b.as_slice()))
}
fn color_texture_for(&self, model: &Path) -> Option<PathBuf> {
let key = memory_key(&model.to_string_lossy());
let home = key.rsplitn(3, '/').nth(2).map(|h| format!("{h}/")).unwrap_or_default();
color_texture_by_name(model, |visit| {
self.images(&mut |p, stem| p.to_string_lossy().starts_with(&home) && visit(p, stem));
})
}
}
pub(crate) fn memory_key(path: &str) -> String {
let mut parts: Vec<&str> = Vec::new();
for part in path.split(['/', '\\']) {
match part {
"" | "." => {}
".." => {
parts.pop();
}
p => parts.push(p),
}
}
parts.join("/")
}
fn color_texture_by_name(model: &Path, walk: impl FnOnce(&mut dyn FnMut(&Path, &str) -> bool)) -> Option<PathBuf> {
let stem = model.file_stem()?.to_string_lossy().to_lowercase();
if stem.len() < 3 || stem.contains('@') {
return None;
}
let mut plain = None;
let mut color = None;
walk(&mut |p, file_stem| {
let Some(rest) = file_stem.strip_prefix(&stem) else {
return false;
};
if !(rest.is_empty() || rest.starts_with(['_', '-', ' ', '.'])) {
return false;
}
match texture_role(file_stem) {
Some(Role::Color) if color.is_none() => color = Some(p.to_path_buf()),
None if plain.is_none() => plain = Some(p.to_path_buf()),
_ => {}
}
color.is_some()
});
color.or(plain)
}
fn same_name_other_format(name: &str, walk: impl FnOnce(&mut dyn FnMut(&Path, &str) -> bool)) -> Option<PathBuf> {
let (stem, ext) = name.rsplit_once('.')?;
if !IMAGE_EXTS.contains(&ext) {
return None;
}
let mut best: Option<(u8, PathBuf)> = None;
walk(&mut |p, s| {
if s == stem {
let ext = p
.extension()
.map(|x| x.to_string_lossy().to_lowercase())
.unwrap_or_default();
let rank = u8::from(!matches!(ext.as_str(), "png" | "jpg" | "jpeg"));
if best.as_ref().is_none_or(|(r, _)| rank < *r) {
best = Some((rank, p.to_path_buf()));
}
}
best.as_ref().is_some_and(|(r, _)| *r == 0)
});
best.map(|(_, p)| p)
}
fn image_stem(p: &Path) -> Option<String> {
let ext = p.extension()?.to_string_lossy().to_lowercase();
IMAGE_EXTS.contains(&ext.as_str()).then(|| {
p.file_stem()
.map(|x| x.to_string_lossy().to_lowercase())
.unwrap_or_default()
})
}
#[derive(Debug, PartialEq, Eq)]
enum Role {
Color,
Other,
}
fn texture_role(stem: &str) -> Option<Role> {
let tail = stem.rsplit(['-', '_', '.', ' ']).next().unwrap_or(stem);
let split = tail.len() < stem.len();
let ends = |list: &[&str]| split && list.contains(&tail);
let has = |list: &[&str]| list.iter().any(|w| stem.contains(w));
if has(&[
"normal",
"nrm",
"emissi",
"emit",
"glow",
"rough",
"metal",
"occlusion",
"height",
"disp",
"bump",
]) || has(&["mask", "opacity", "alpha", "spec", "gloss", "smooth"])
|| ends(&[
"n", "nor", "norm", "e", "r", "m", "mt", "ao", "occ", "h", "orm", "arm", "mra", "rma", "s", "g",
])
{
return Some(Role::Other);
}
if has(&[
"albedo",
"basecolor",
"base_color",
"base color",
"basecolour",
"diffuse",
]) || ends(&["diff", "dif", "col", "color", "colour", "d", "bc", "a", "c"])
{
return Some(Role::Color);
}
None
}
const IMAGE_EXTS: &[&str] = &[
"png", "jpg", "jpeg", "tga", "bmp", "webp", "gif", "tif", "tiff", "dds", "psd",
];
#[cfg(feature = "fs")]
fn walk_images(root: &Path, visit: &mut dyn FnMut(&Path, &str) -> bool) {
let mut stack = vec![root.to_path_buf()];
let mut seen = 0;
while let Some(dir) = stack.pop() {
let Ok(entries) = std::fs::read_dir(&dir) else {
continue;
};
for e in entries.flatten() {
seen += 1;
if seen > 20_000 {
return;
}
let p = e.path();
if p.is_dir() {
stack.push(p);
continue;
}
if let Some(stem) = image_stem(&p)
&& visit(&p, &stem)
{
return;
}
}
}
}
#[cfg(feature = "fs")]
fn find_texture(reference: &str, model_dir: &Path, root: &Path) -> Option<PathBuf> {
let cleaned = reference.trim().replace('\\', "/");
if cleaned.is_empty() {
return None;
}
let rel = model_dir.join(&cleaned);
if !cleaned.starts_with('/') && !cleaned.contains(':') && !cleaned.split('/').any(|c| c == "..") && rel.is_file() {
return Some(rel);
}
let name = cleaned.rsplit('/').next()?.to_lowercase();
let mut stack = vec![root.to_path_buf()];
let mut seen = 0;
while let Some(dir) = stack.pop() {
let Ok(entries) = std::fs::read_dir(&dir) else {
continue;
};
for e in entries.flatten() {
seen += 1;
if seen > 20_000 {
return None;
}
let p = e.path();
if p.is_dir() {
stack.push(p);
} else if p
.file_name()
.is_some_and(|n| n.to_string_lossy().to_lowercase() == name)
{
return Some(p);
}
}
}
same_name_other_format(&name, |visit| walk_images(root, visit))
}
#[cfg(all(test, feature = "fs"))]
mod tests {
use super::*;
#[test]
fn texture_roles_by_name() {
assert_eq!(texture_role("barrel_normal"), Some(Role::Other));
assert_eq!(texture_role("barrel_d"), Some(Role::Color));
assert_eq!(texture_role("barrel_albedo"), Some(Role::Color));
assert_eq!(texture_role("barrel"), None);
}
#[test]
fn memory_finds_like_a_folder() {
let m = Memory::new()
.with("Models/crate.obj", "")
.with("Models\\textures\\Crate.PNG", "png")
.with("textures/rock.jpg", "jpg")
.with("Barrel_Albedo.png", "a")
.with("Barrel_Normal.png", "n");
let found = |r: &str| m.find(r, Path::new("Models")).map(|p| p.to_string_lossy().into_owned());
let krate = Some("Models/textures/Crate.PNG");
assert_eq!(found("textures/Crate.PNG").as_deref(), krate);
assert_eq!(found("./textures\\Crate.PNG").as_deref(), krate);
assert_eq!(found("D:\\Work\\crate.png").as_deref(), krate);
assert_eq!(found("../textures/rock.jpg").as_deref(), Some("textures/rock.jpg"));
assert_eq!(found("rock.tga").as_deref(), Some("textures/rock.jpg"));
assert_eq!(found("missing.png"), None);
assert_eq!(
m.read(Path::new("./Models/textures/Crate.PNG")).as_deref(),
Some(&b"png"[..])
);
assert_eq!(
m.color_texture_for(Path::new("Barrel.obj")),
Some(PathBuf::from("Barrel_Albedo.png"))
);
}
#[test]
fn color_textures_by_name_come_from_the_models_part_of_the_pack() {
let m = Memory::new()
.with("Bundle/Kit2D/Sprites/Stairs.png", "2d")
.with("Bundle/Kit3D/Textures/Crate.png", "3d");
let guess = |model: &str| m.color_texture_for(Path::new(model));
assert_eq!(guess("Bundle/Kit3D/Models/Stairs.fbx"), None);
assert_eq!(
guess("Bundle/Kit3D/Models/Crate.fbx"),
Some(PathBuf::from("Bundle/Kit3D/Textures/Crate.png"))
);
assert_eq!(
guess("Models/Stairs.fbx"),
Some(PathBuf::from("Bundle/Kit2D/Sprites/Stairs.png")),
"a model near the top searches the whole pack"
);
}
}