euv_core/game/engine3d/gltf/impl.rs
1use super::*;
2
3/// Buffer and image registration helpers for `MemoryGltfResolver`.
4impl MemoryGltfResolver {
5 /// Registers an in-memory binary buffer mapped to `uri`.
6 ///
7 /// # Arguments
8 ///
9 /// - `&Self` - Mutable resolver.
10 /// - `&str` - Relative URI key.
11 /// - `Vec<u8>` - Raw binary buffer payload.
12 pub fn insert_buffer(&mut self, uri: &str, bytes: Vec<u8>) {
13 let _: Option<Vec<u8>> = self.get_mut_buffers().insert(uri.to_string(), bytes);
14 }
15
16 /// Registers an in-memory encoded image mapped to `uri`.
17 ///
18 /// # Arguments
19 ///
20 /// - `&Self` - Mutable resolver.
21 /// - `&str` - Relative URI key.
22 /// - `Vec<u8>` - Encoded PNG/JPEG/BMP bytes.
23 pub fn insert_image(&mut self, uri: &str, bytes: Vec<u8>) {
24 let _: Option<Vec<u8>> = self.get_mut_images().insert(uri.to_string(), bytes);
25 }
26}
27
28/// `GltfBufferResolver` implementation backed by `MemoryGltfResolver`.
29impl GltfBufferResolver for MemoryGltfResolver {
30 /// Looks up a registered in-memory binary buffer by `uri`.
31 ///
32 /// # Arguments
33 ///
34 /// - `&Self` - Resolver reference.
35 /// - `&str` - Relative buffer URI.
36 ///
37 /// # Returns
38 ///
39 /// - `Result<Vec<u8>, String>` - Cloned buffer bytes or error.
40 fn resolve_buffer(&self, uri: &str) -> Result<Vec<u8>, String> {
41 self.get_buffers()
42 .get(uri)
43 .cloned()
44 .ok_or_else(|| ERR_GLTF_URI_RESOLVE.to_string())
45 }
46
47 /// Looks up a registered in-memory encoded image by `uri`.
48 ///
49 /// # Arguments
50 ///
51 /// - `&Self` - Resolver reference.
52 /// - `&str` - Relative image URI.
53 ///
54 /// # Returns
55 ///
56 /// - `Result<Vec<u8>, String>` - Cloned image bytes or error.
57 fn resolve_image(&self, uri: &str) -> Result<Vec<u8>, String> {
58 self.get_images()
59 .get(uri)
60 .cloned()
61 .ok_or_else(|| ERR_GLTF_URI_RESOLVE.to_string())
62 }
63}
64
65/// Safe relative-path construction for `DirectoryGltfResolver`.
66impl DirectoryGltfResolver {
67 /// Creates a directory resolver rooted at `base_dir`.
68 ///
69 /// # Arguments
70 ///
71 /// - `PathBuf` - Directory containing the `.gltf` asset.
72 ///
73 /// # Returns
74 ///
75 /// - `Self` - Configured filesystem resolver.
76 #[must_use]
77 pub fn from_dir(base_dir: PathBuf) -> Self {
78 Self { base_dir }
79 }
80
81 /// Validates that `uri` is a safe relative path without parent traversal and reads its bytes on native targets.
82 ///
83 /// # Arguments
84 ///
85 /// - `&Self` - Resolver reference.
86 /// - `&str` - Relative URI string.
87 ///
88 /// # Returns
89 ///
90 /// - `Result<Vec<u8>, String>` - Loaded file bytes or security/IO error.
91 fn read_safe_relative(&self, uri: &str) -> Result<Vec<u8>, String> {
92 if uri.is_empty()
93 || uri.starts_with('/')
94 || uri.starts_with('\\')
95 || uri.contains("..")
96 || uri.contains(':')
97 {
98 return Err(ERR_GLTF_URI_TRAVERSAL.to_string());
99 }
100 #[cfg(not(target_arch = "wasm32"))]
101 {
102 let full_path: PathBuf = self.get_base_dir().join(uri);
103 fs::read(&full_path).map_err(|_: io::Error| ERR_GLTF_URI_RESOLVE.to_string())
104 }
105 #[cfg(target_arch = "wasm32")]
106 {
107 let _: &PathBuf = self.get_base_dir();
108 Err(ERR_GLTF_URI_RESOLVE.to_string())
109 }
110 }
111}
112
113/// `GltfBufferResolver` implementation backed by `DirectoryGltfResolver`.
114impl GltfBufferResolver for DirectoryGltfResolver {
115 /// Loads a relative external `.bin` buffer from `base_dir`.
116 ///
117 /// # Arguments
118 ///
119 /// - `&Self` - Resolver reference.
120 /// - `&str` - Relative buffer URI.
121 ///
122 /// # Returns
123 ///
124 /// - `Result<Vec<u8>, String>` - Loaded buffer bytes or error.
125 fn resolve_buffer(&self, uri: &str) -> Result<Vec<u8>, String> {
126 self.read_safe_relative(uri)
127 }
128
129 /// Loads a relative external image file from `base_dir`.
130 ///
131 /// # Arguments
132 ///
133 /// - `&Self` - Resolver reference.
134 /// - `&str` - Relative image URI.
135 ///
136 /// # Returns
137 ///
138 /// - `Result<Vec<u8>, String>` - Loaded image bytes or error.
139 fn resolve_image(&self, uri: &str) -> Result<Vec<u8>, String> {
140 self.read_safe_relative(uri)
141 }
142}
143
144/// Summary conversion helpers for `ImportedScene3D`.
145impl ImportedScene3D {
146 /// Converts this imported scene into a legacy `ImportedModel3D` LOD bundle for backwards-compatible `World3D::import_model` callers.
147 ///
148 /// # Arguments
149 ///
150 /// - `&Self` - Imported scene reference.
151 /// - `ModelFormat` - Recorded interchange format label.
152 ///
153 /// # Returns
154 ///
155 /// - `ImportedModel3D` - Legacy model summary containing LOD chains for every imported mesh.
156 #[must_use]
157 pub fn to_imported_model(&self, format: ModelFormat) -> ImportedModel3D {
158 let mut chains: Vec<MeshLodChain> = Vec::with_capacity(self.get_meshes().len());
159 let mut total_vertices: usize = 0;
160 let mut total_triangles: usize = 0;
161 for mesh in self.get_meshes().iter() {
162 total_vertices += mesh.get_vertices().len();
163 total_triangles += mesh.get_indices().len();
164 chains.push(MeshLodChain::from_mesh(mesh.clone()));
165 }
166 ImportedModel3D {
167 name: self.get_name().clone(),
168 format,
169 meshes: chains,
170 total_vertices,
171 total_triangles,
172 }
173 }
174}