euv-core 0.51.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Buffer and image registration helpers for `MemoryGltfResolver`.
impl MemoryGltfResolver {
    /// Registers an in-memory binary buffer mapped to `uri`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable resolver.
    /// - `&str` - Relative URI key.
    /// - `Vec<u8>` - Raw binary buffer payload.
    pub fn insert_buffer(&mut self, uri: &str, bytes: Vec<u8>) {
        let _: Option<Vec<u8>> = self.get_mut_buffers().insert(uri.to_string(), bytes);
    }

    /// Registers an in-memory encoded image mapped to `uri`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable resolver.
    /// - `&str` - Relative URI key.
    /// - `Vec<u8>` - Encoded PNG/JPEG/BMP bytes.
    pub fn insert_image(&mut self, uri: &str, bytes: Vec<u8>) {
        let _: Option<Vec<u8>> = self.get_mut_images().insert(uri.to_string(), bytes);
    }
}

/// `GltfBufferResolver` implementation backed by `MemoryGltfResolver`.
impl GltfBufferResolver for MemoryGltfResolver {
    /// Looks up a registered in-memory binary buffer by `uri`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Resolver reference.
    /// - `&str` - Relative buffer URI.
    ///
    /// # Returns
    ///
    /// - `Result<Vec<u8>, String>` - Cloned buffer bytes or error.
    fn resolve_buffer(&self, uri: &str) -> Result<Vec<u8>, String> {
        self.get_buffers()
            .get(uri)
            .cloned()
            .ok_or_else(|| ERR_GLTF_URI_RESOLVE.to_string())
    }

    /// Looks up a registered in-memory encoded image by `uri`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Resolver reference.
    /// - `&str` - Relative image URI.
    ///
    /// # Returns
    ///
    /// - `Result<Vec<u8>, String>` - Cloned image bytes or error.
    fn resolve_image(&self, uri: &str) -> Result<Vec<u8>, String> {
        self.get_images()
            .get(uri)
            .cloned()
            .ok_or_else(|| ERR_GLTF_URI_RESOLVE.to_string())
    }
}

/// Safe relative-path construction for `DirectoryGltfResolver`.
impl DirectoryGltfResolver {
    /// Creates a directory resolver rooted at `base_dir`.
    ///
    /// # Arguments
    ///
    /// - `PathBuf` - Directory containing the `.gltf` asset.
    ///
    /// # Returns
    ///
    /// - `Self` - Configured filesystem resolver.
    #[must_use]
    pub fn from_dir(base_dir: PathBuf) -> Self {
        Self { base_dir }
    }

    /// Validates that `uri` is a safe relative path without parent traversal and reads its bytes on native targets.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Resolver reference.
    /// - `&str` - Relative URI string.
    ///
    /// # Returns
    ///
    /// - `Result<Vec<u8>, String>` - Loaded file bytes or security/IO error.
    fn read_safe_relative(&self, uri: &str) -> Result<Vec<u8>, String> {
        if uri.is_empty()
            || uri.starts_with('/')
            || uri.starts_with('\\')
            || uri.contains("..")
            || uri.contains(':')
        {
            return Err(ERR_GLTF_URI_TRAVERSAL.to_string());
        }
        #[cfg(not(target_arch = "wasm32"))]
        {
            let full_path: PathBuf = self.get_base_dir().join(uri);
            fs::read(&full_path).map_err(|_: io::Error| ERR_GLTF_URI_RESOLVE.to_string())
        }
        #[cfg(target_arch = "wasm32")]
        {
            let _: &PathBuf = self.get_base_dir();
            Err(ERR_GLTF_URI_RESOLVE.to_string())
        }
    }
}

/// `GltfBufferResolver` implementation backed by `DirectoryGltfResolver`.
impl GltfBufferResolver for DirectoryGltfResolver {
    /// Loads a relative external `.bin` buffer from `base_dir`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Resolver reference.
    /// - `&str` - Relative buffer URI.
    ///
    /// # Returns
    ///
    /// - `Result<Vec<u8>, String>` - Loaded buffer bytes or error.
    fn resolve_buffer(&self, uri: &str) -> Result<Vec<u8>, String> {
        self.read_safe_relative(uri)
    }

    /// Loads a relative external image file from `base_dir`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Resolver reference.
    /// - `&str` - Relative image URI.
    ///
    /// # Returns
    ///
    /// - `Result<Vec<u8>, String>` - Loaded image bytes or error.
    fn resolve_image(&self, uri: &str) -> Result<Vec<u8>, String> {
        self.read_safe_relative(uri)
    }
}

/// Summary conversion helpers for `ImportedScene3D`.
impl ImportedScene3D {
    /// Converts this imported scene into a legacy `ImportedModel3D` LOD bundle for backwards-compatible `World3D::import_model` callers.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Imported scene reference.
    /// - `ModelFormat` - Recorded interchange format label.
    ///
    /// # Returns
    ///
    /// - `ImportedModel3D` - Legacy model summary containing LOD chains for every imported mesh.
    #[must_use]
    pub fn to_imported_model(&self, format: ModelFormat) -> ImportedModel3D {
        let mut chains: Vec<MeshLodChain> = Vec::with_capacity(self.get_meshes().len());
        let mut total_vertices: usize = 0;
        let mut total_triangles: usize = 0;
        for mesh in self.get_meshes().iter() {
            total_vertices += mesh.get_vertices().len();
            total_triangles += mesh.get_indices().len();
            chains.push(MeshLodChain::from_mesh(mesh.clone()));
        }
        ImportedModel3D {
            name: self.get_name().clone(),
            format,
            meshes: chains,
            total_vertices,
            total_triangles,
        }
    }
}