use super::*;
impl MemoryGltfResolver {
pub fn insert_buffer(&mut self, uri: &str, bytes: Vec<u8>) {
let _: Option<Vec<u8>> = self.get_mut_buffers().insert(uri.to_string(), 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);
}
}
impl GltfBufferResolver for MemoryGltfResolver {
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())
}
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())
}
}
impl DirectoryGltfResolver {
#[must_use]
pub fn from_dir(base_dir: PathBuf) -> Self {
Self { base_dir }
}
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())
}
}
}
impl GltfBufferResolver for DirectoryGltfResolver {
fn resolve_buffer(&self, uri: &str) -> Result<Vec<u8>, String> {
self.read_safe_relative(uri)
}
fn resolve_image(&self, uri: &str) -> Result<Vec<u8>, String> {
self.read_safe_relative(uri)
}
}
impl ImportedScene3D {
#[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,
}
}
}