feat(resources): list of extensions to check
This commit is contained in:
@@ -1,29 +1,42 @@
|
|||||||
use color_eyre::eyre::eyre;
|
use color_eyre::eyre::eyre;
|
||||||
use serde::{de::Visitor, Deserialize};
|
use serde::{de::Visitor, Deserialize};
|
||||||
use std::path::PathBuf;
|
use std::{ffi::OsStr, path::PathBuf};
|
||||||
|
|
||||||
pub enum ResourceID {
|
pub enum ResourceID {
|
||||||
File(PathBuf),
|
File(PathBuf),
|
||||||
Namespaced { namespace: String, path: PathBuf },
|
Namespaced { namespace: String, path: PathBuf },
|
||||||
}
|
}
|
||||||
impl ResourceID {
|
impl ResourceID {
|
||||||
pub fn get_file(&self, prefixes: &[PathBuf]) -> Option<PathBuf> {
|
pub fn get_file(&self, prefixes: &[PathBuf], extensions: &[&OsStr]) -> Option<PathBuf> {
|
||||||
match self {
|
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 } => {
|
ResourceID::Namespaced { namespace, path } => {
|
||||||
for prefix in prefixes {
|
let file_name = path.file_name()?;
|
||||||
let mut test_path = prefix.clone();
|
prefixes
|
||||||
test_path.push(namespace.clone());
|
.iter()
|
||||||
test_path.push(path.clone());
|
.filter_map(|prefix| {
|
||||||
|
let prefixed_path = prefix.clone().join(namespace).join(path);
|
||||||
if test_path.as_path().exists() {
|
let parent = prefixed_path.parent()?;
|
||||||
return Some(test_path);
|
std::fs::read_dir(parent).ok()
|
||||||
}
|
})
|
||||||
}
|
.flatten()
|
||||||
None
|
.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 {
|
impl<'de> Deserialize<'de> for ResourceID {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ use send_wrapper::SendWrapper;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use stardust_xr::schemas::flex::deserialize;
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
use stardust_xr::values::Transform;
|
use stardust_xr::values::Transform;
|
||||||
|
use std::ffi::OsStr;
|
||||||
use std::fmt::Error;
|
use std::fmt::Error;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -70,6 +71,7 @@ impl Model {
|
|||||||
.base_resource_prefixes
|
.base_resource_prefixes
|
||||||
.lock()
|
.lock()
|
||||||
.clone(),
|
.clone(),
|
||||||
|
&[OsStr::new("glb"), OsStr::new("gltf")],
|
||||||
)
|
)
|
||||||
.ok_or_else(|| eyre!("Resource not found"))?,
|
.ok_or_else(|| eyre!("Resource not found"))?,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ use prisma::{Flatten, Rgb, Rgba};
|
|||||||
use send_wrapper::SendWrapper;
|
use send_wrapper::SendWrapper;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use stardust_xr::{schemas::flex::deserialize, values::Transform};
|
use stardust_xr::{schemas::flex::deserialize, values::Transform};
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
|
||||||
use stereokit::{
|
use stereokit::{
|
||||||
font::Font,
|
font::Font,
|
||||||
lifecycle::DrawContext,
|
lifecycle::DrawContext,
|
||||||
@@ -66,8 +66,12 @@ impl Text {
|
|||||||
let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?;
|
let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?;
|
||||||
let text = TEXT_REGISTRY.add(Text {
|
let text = TEXT_REGISTRY.add(Text {
|
||||||
space: node.spatial.get().unwrap().clone(),
|
space: node.spatial.get().unwrap().clone(),
|
||||||
font_path: font_resource_id
|
font_path: font_resource_id.and_then(|res| {
|
||||||
.and_then(|res| res.get_file(&client.base_resource_prefixes.lock().clone())),
|
res.get_file(
|
||||||
|
&client.base_resource_prefixes.lock().clone(),
|
||||||
|
&[OsStr::new("ttf"), OsStr::new("otf")],
|
||||||
|
)
|
||||||
|
}),
|
||||||
style: OnceCell::new(),
|
style: OnceCell::new(),
|
||||||
|
|
||||||
data: Mutex::new(TextData {
|
data: Mutex::new(TextData {
|
||||||
|
|||||||
Reference in New Issue
Block a user