feat(resource): enum instead of trait, parse fn
This commit is contained in:
@@ -1,37 +1,43 @@
|
|||||||
|
use anyhow::bail;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub type ResourceID = Box<dyn ResourceIDTrait + Send + Sync>;
|
pub enum ResourceID {
|
||||||
|
File(PathBuf),
|
||||||
pub trait ResourceIDTrait: Send + Sync {
|
Namespaced { namespace: String, path: PathBuf },
|
||||||
fn get_file(&self, prefixes: &[PathBuf]) -> Option<PathBuf>;
|
|
||||||
}
|
}
|
||||||
|
impl ResourceID {
|
||||||
|
pub fn get_file(&self, prefixes: &[PathBuf]) -> Option<PathBuf> {
|
||||||
|
match self {
|
||||||
|
ResourceID::File(file) => (file.is_absolute() && file.exists()).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());
|
||||||
|
|
||||||
impl ResourceIDTrait for PathBuf {
|
if test_path.as_path().exists() {
|
||||||
fn get_file(&self, _prefixes: &[PathBuf]) -> Option<PathBuf> {
|
return Some(test_path);
|
||||||
if self.is_absolute() && self.as_path().exists() {
|
}
|
||||||
Some(self.clone())
|
}
|
||||||
} else {
|
None
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct NamespacedResourceID {
|
|
||||||
pub namespace: String,
|
|
||||||
pub path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ResourceIDTrait for NamespacedResourceID {
|
|
||||||
fn get_file(&self, prefixes: &[PathBuf]) -> Option<PathBuf> {
|
|
||||||
for prefix in prefixes {
|
|
||||||
let mut path = prefix.clone();
|
|
||||||
path.push(self.namespace.clone());
|
|
||||||
path.push(self.path.clone());
|
|
||||||
|
|
||||||
if path.as_path().exists() {
|
|
||||||
return Some(path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_resource_id(reader: flexbuffers::Reader<&[u8]>) -> anyhow::Result<ResourceID> {
|
||||||
|
let string = reader.get_str()?;
|
||||||
|
|
||||||
|
Ok(if string.starts_with('/') {
|
||||||
|
let path = PathBuf::from(string);
|
||||||
|
path.metadata()?;
|
||||||
|
ResourceID::File(path)
|
||||||
|
} else if let Some((namespace, path)) = string.split_once(':') {
|
||||||
|
ResourceID::Namespaced {
|
||||||
|
namespace: namespace.to_string(),
|
||||||
|
path: PathBuf::from(path),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bail!("Invalid format for string");
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ use stereokit::{lifecycle::DrawContext, texture::Texture, StereoKit};
|
|||||||
|
|
||||||
pub fn create_interface(client: &Arc<Client>) {
|
pub fn create_interface(client: &Arc<Client>) {
|
||||||
let node = Node::create(client, "", "drawable", false);
|
let node = Node::create(client, "", "drawable", false);
|
||||||
node.add_local_signal("createModelFromFile", model::create_from_file);
|
node.add_local_signal("createModel", model::create);
|
||||||
node.add_local_signal("createModelFromResource", model::create_from_resource);
|
|
||||||
node.add_local_signal("setSkyFile", set_sky_file_flex);
|
node.add_local_signal("setSkyFile", set_sky_file_flex);
|
||||||
node.add_to_scenegraph();
|
node.add_to_scenegraph();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
use super::Node;
|
use super::Node;
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
|
use crate::core::destroy_queue;
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use crate::core::resource::{NamespacedResourceID, ResourceID};
|
use crate::core::resource::{parse_resource_id, ResourceID};
|
||||||
use crate::nodes::spatial::{get_spatial_parent_flex, Spatial};
|
use crate::nodes::spatial::{get_spatial_parent_flex, Spatial};
|
||||||
use anyhow::{anyhow, bail, ensure, Result};
|
use anyhow::{anyhow, bail, ensure, Result};
|
||||||
use flexbuffers::FlexBufferType;
|
use flexbuffers::FlexBufferType;
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use prisma::{Rgb, Rgba};
|
use prisma::{Rgb, Rgba};
|
||||||
@@ -24,9 +24,6 @@ use stereokit::texture::Texture;
|
|||||||
use stereokit::StereoKit;
|
use stereokit::StereoKit;
|
||||||
|
|
||||||
static MODEL_REGISTRY: Registry<Model> = Registry::new();
|
static MODEL_REGISTRY: Registry<Model> = Registry::new();
|
||||||
lazy_static! {
|
|
||||||
static ref MODELS_TO_DROP: Mutex<Vec<SendWrapper<SKModel>>> = Default::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum MaterialParameter {
|
pub enum MaterialParameter {
|
||||||
Texture(PathBuf),
|
Texture(PathBuf),
|
||||||
@@ -156,7 +153,7 @@ impl Model {
|
|||||||
impl Drop for Model {
|
impl Drop for Model {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(model) = self.sk_model.take() {
|
if let Some(model) = self.sk_model.take() {
|
||||||
MODELS_TO_DROP.lock().push(model);
|
destroy_queue::add(model);
|
||||||
}
|
}
|
||||||
MODEL_REGISTRY.remove(self);
|
MODEL_REGISTRY.remove(self);
|
||||||
}
|
}
|
||||||
@@ -168,7 +165,7 @@ pub fn draw_all(sk: &StereoKit, draw_ctx: &DrawContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_from_file(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
pub fn create(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
||||||
let flex_vec = flexbuffers::Reader::get_root(data)?.get_vector()?;
|
let flex_vec = flexbuffers::Reader::get_root(data)?.get_vector()?;
|
||||||
let node = Node::create(
|
let node = Node::create(
|
||||||
&calling_client,
|
&calling_client,
|
||||||
@@ -177,7 +174,7 @@ pub fn create_from_file(_node: &Node, calling_client: Arc<Client>, data: &[u8])
|
|||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
let parent = get_spatial_parent_flex(&calling_client, flex_vec.idx(1).get_str()?)?;
|
let parent = get_spatial_parent_flex(&calling_client, flex_vec.idx(1).get_str()?)?;
|
||||||
let path = PathBuf::from(flex_vec.idx(2).get_str()?);
|
let resource_id = parse_resource_id(flex_vec.idx(2))?;
|
||||||
let transform = Mat4::from_scale_rotation_translation(
|
let transform = Mat4::from_scale_rotation_translation(
|
||||||
flex_to_vec3!(flex_vec.idx(5))
|
flex_to_vec3!(flex_vec.idx(5))
|
||||||
.ok_or_else(|| anyhow!("Scale not found"))?
|
.ok_or_else(|| anyhow!("Scale not found"))?
|
||||||
@@ -191,35 +188,6 @@ pub fn create_from_file(_node: &Node, calling_client: Arc<Client>, data: &[u8])
|
|||||||
);
|
);
|
||||||
let node = node.add_to_scenegraph();
|
let node = node.add_to_scenegraph();
|
||||||
Spatial::add_to(&node, Some(parent), transform)?;
|
Spatial::add_to(&node, Some(parent), transform)?;
|
||||||
Model::add_to(&node, Box::new(path))?;
|
Model::add_to(&node, resource_id)?;
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
pub fn create_from_resource(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
|
||||||
let flex_vec = flexbuffers::Reader::get_root(data)?.get_vector()?;
|
|
||||||
let node = Node::create(
|
|
||||||
&calling_client,
|
|
||||||
"/drawable/model",
|
|
||||||
flex_vec.idx(0).get_str()?,
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
let parent = get_spatial_parent_flex(&calling_client, flex_vec.idx(1).get_str()?)?;
|
|
||||||
let resource_id = NamespacedResourceID {
|
|
||||||
namespace: flex_vec.idx(2).get_str()?.to_string(),
|
|
||||||
path: PathBuf::from(flex_vec.idx(3).get_str()?),
|
|
||||||
};
|
|
||||||
let transform = Mat4::from_scale_rotation_translation(
|
|
||||||
flex_to_vec3!(flex_vec.idx(6))
|
|
||||||
.ok_or_else(|| anyhow!("Scale not found"))?
|
|
||||||
.into(),
|
|
||||||
flex_to_quat!(flex_vec.idx(5))
|
|
||||||
.ok_or_else(|| anyhow!("Rotation not found"))?
|
|
||||||
.into(),
|
|
||||||
flex_to_vec3!(flex_vec.idx(4))
|
|
||||||
.ok_or_else(|| anyhow!("Position not found"))?
|
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
let node = node.add_to_scenegraph();
|
|
||||||
Spatial::add_to(&node, Some(parent), transform)?;
|
|
||||||
Model::add_to(&node, Box::new(resource_id))?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user