feat(resources): list of extensions to check

This commit is contained in:
Nova
2022-12-05 22:44:04 -05:00
parent 125ab4f74d
commit 48719c9862
3 changed files with 35 additions and 16 deletions

View File

@@ -1,29 +1,42 @@
use color_eyre::eyre::eyre;
use serde::{de::Visitor, Deserialize};
use std::path::PathBuf;
use std::{ffi::OsStr, path::PathBuf};
pub enum ResourceID {
File(PathBuf),
Namespaced { namespace: String, path: PathBuf },
}
impl ResourceID {
pub fn get_file(&self, prefixes: &[PathBuf]) -> Option<PathBuf> {
pub fn get_file(&self, prefixes: &[PathBuf], extensions: &[&OsStr]) -> Option<PathBuf> {
match self {
ResourceID::File(file) => (file.is_absolute() && file.exists()).then_some(file.clone()),
ResourceID::File(file) => (file.is_absolute()
&& file.exists() && Self::has_extension(file, extensions))
.then_some(file.clone()),
ResourceID::Namespaced { namespace, path } => {
for prefix in prefixes {
let mut test_path = prefix.clone();
test_path.push(namespace.clone());
test_path.push(path.clone());
if test_path.as_path().exists() {
return Some(test_path);
}
}
None
let file_name = path.file_name()?;
prefixes
.iter()
.filter_map(|prefix| {
let prefixed_path = prefix.clone().join(namespace).join(path);
let parent = prefixed_path.parent()?;
std::fs::read_dir(parent).ok()
})
.flatten()
.filter_map(|item| item.ok())
.map(|dir_entry| dir_entry.path())
.filter(|path| path.file_stem() == Some(file_name))
.find(|path| Self::has_extension(path, extensions))
}
}
}
fn has_extension(path: &PathBuf, extensions: &[&OsStr]) -> bool {
if let Some(path_extension) = path.extension() {
extensions.contains(&path_extension)
} else {
false
}
}
}
impl<'de> Deserialize<'de> for ResourceID {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>

View File

@@ -13,6 +13,7 @@ use send_wrapper::SendWrapper;
use serde::Deserialize;
use stardust_xr::schemas::flex::deserialize;
use stardust_xr::values::Transform;
use std::ffi::OsStr;
use std::fmt::Error;
use std::path::PathBuf;
use std::sync::Arc;
@@ -70,6 +71,7 @@ impl Model {
.base_resource_prefixes
.lock()
.clone(),
&[OsStr::new("glb"), OsStr::new("gltf")],
)
.ok_or_else(|| eyre!("Resource not found"))?,
);

View File

@@ -14,7 +14,7 @@ use prisma::{Flatten, Rgb, Rgba};
use send_wrapper::SendWrapper;
use serde::Deserialize;
use stardust_xr::{schemas::flex::deserialize, values::Transform};
use std::{path::PathBuf, sync::Arc};
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
use stereokit::{
font::Font,
lifecycle::DrawContext,
@@ -66,8 +66,12 @@ impl Text {
let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?;
let text = TEXT_REGISTRY.add(Text {
space: node.spatial.get().unwrap().clone(),
font_path: font_resource_id
.and_then(|res| res.get_file(&client.base_resource_prefixes.lock().clone())),
font_path: font_resource_id.and_then(|res| {
res.get_file(
&client.base_resource_prefixes.lock().clone(),
&[OsStr::new("ttf"), OsStr::new("otf")],
)
}),
style: OnceCell::new(),
data: Mutex::new(TextData {