euv_core/game/engine3d/blend/inspect/
impl.rs1use super::*;
2
3impl BlendInspector {
5 pub fn inspect_bytes(bytes: &[u8]) -> Result<BlendInspectionReport, String> {
15 let compression: BlendCompression = detect_blend_compression(bytes);
16 let owned_decompressed: Vec<u8>;
17 let data: &[u8] = match compression {
18 BlendCompression::None => bytes,
19 BlendCompression::Gzip => {
20 owned_decompressed = inflate_gzip(bytes)?;
21 &owned_decompressed
22 }
23 BlendCompression::Zstd => {
24 owned_decompressed = decompress_zstd_frame(bytes)?;
25 &owned_decompressed
26 }
27 };
28 let header: BlendHeader = parse_blend_header(data)?;
29 let is_le: bool = header.get_is_little_endian();
30 let ptr_size: usize = usize::from(header.get_pointer_size_bytes());
31 let bhead_size: usize =
32 if header.get_is_large_header_v5() && header.get_format_version() >= 1 {
33 BLEND_LARGE_BHEAD_BYTES
34 } else if ptr_size == 8 {
35 BLEND_CLASSIC_BHEAD8_BYTES
36 } else {
37 BLEND_CLASSIC_BHEAD4_BYTES
38 };
39
40 let mut cursor: usize = header.get_header_size_bytes();
41 let mut block_count: usize = 0;
42 let mut saw_endb: bool = false;
43 let mut warnings: Vec<String> = Vec::new();
44 let mut id_blocks: Vec<(BlendBlockKind, [u8; 4], usize, usize)> = Vec::new();
45 let mut dna_summary: Option<BlendDnaSchemaSummary> = None;
46
47 while cursor + bhead_size <= data.len() && block_count < BLEND_MAX_BLOCK_COUNT {
48 let code: [u8; 4] = [
49 data[cursor],
50 data[cursor + 1],
51 data[cursor + 2],
52 data[cursor + 3],
53 ];
54 let payload_len_u64: u64 =
55 if header.get_is_large_header_v5() && header.get_format_version() >= 1 {
56 let raw_len: i64 = read_endian_i64(data, cursor + 16, is_le).unwrap_or(-1);
57 if raw_len < 0 {
58 warnings.push(WARN_BLEND_TRUNCATED_BLOCK.to_string());
59 break;
60 }
61 raw_len as u64
62 } else {
63 let raw_len: i32 = read_endian_u32(data, cursor + 4, is_le).unwrap_or(0) as i32;
64 if raw_len < 0 {
65 warnings.push(WARN_BLEND_TRUNCATED_BLOCK.to_string());
66 break;
67 }
68 raw_len as u64
69 };
70 let payload_start: usize = cursor + bhead_size;
71 let payload_end: usize = match payload_start.checked_add(payload_len_u64 as usize) {
72 Some(end) if end <= data.len() => end,
73 _ => {
74 warnings.push(WARN_BLEND_TRUNCATED_BLOCK.to_string());
75 break;
76 }
77 };
78 block_count += 1;
79 let kind: BlendBlockKind = classify_blend_code(code);
80 match kind {
81 BlendBlockKind::EndOfFile => {
82 saw_endb = true;
83 break;
84 }
85 BlendBlockKind::DnaSchema => {
86 dna_summary =
87 parse_blend_dna1(&data[payload_start..payload_end], is_le, ptr_size);
88 }
89 BlendBlockKind::Scene
90 | BlendBlockKind::Collection
91 | BlendBlockKind::Object
92 | BlendBlockKind::Mesh
93 | BlendBlockKind::Material
94 | BlendBlockKind::Camera
95 | BlendBlockKind::Light
96 | BlendBlockKind::Armature
97 | BlendBlockKind::Action
98 | BlendBlockKind::ShapeKey
99 | BlendBlockKind::World
100 | BlendBlockKind::FreestyleLineStyle
101 | BlendBlockKind::Curve
102 | BlendBlockKind::ParticleSettings
103 | BlendBlockKind::Image
104 | BlendBlockKind::NodeTree => {
105 id_blocks.push((kind, code, payload_start, payload_end));
106 }
107 BlendBlockKind::Other => {}
108 }
109 cursor = payload_end;
110 }
111 if !saw_endb {
112 warnings.push(WARN_BLEND_MISSING_ENDB.to_string());
113 }
114 if dna_summary.is_none() {
115 warnings.push(WARN_BLEND_DNA_INCOMPLETE.to_string());
116 }
117
118 let default_offset: usize = if ptr_size == 8 {
119 BLEND_DEFAULT_ID_NAME_OFFSET_64
120 } else {
121 BLEND_DEFAULT_ID_NAME_OFFSET_32
122 };
123 let (id_name_offset, id_name_max_len): (usize, usize) = match &dna_summary {
124 Some(dna) => (
125 dna.try_get_id_name_offset().unwrap_or(default_offset),
126 dna.try_get_id_name_max_len().unwrap_or(258),
127 ),
128 None => (default_offset, 258),
129 };
130
131 let mut scenes: Vec<String> = Vec::new();
132 let mut collections: Vec<String> = Vec::new();
133 let mut objects: Vec<String> = Vec::new();
134 let mut meshes: Vec<String> = Vec::new();
135 let mut materials: Vec<String> = Vec::new();
136 let mut cameras: Vec<String> = Vec::new();
137 let mut lights: Vec<String> = Vec::new();
138 let mut armatures: Vec<String> = Vec::new();
139 let mut actions: Vec<String> = Vec::new();
140 let mut shape_keys: Vec<String> = Vec::new();
141 let mut worlds: Vec<String> = Vec::new();
142 let mut line_styles: Vec<String> = Vec::new();
143 let mut curves: Vec<String> = Vec::new();
144 let mut particle_settings: Vec<String> = Vec::new();
145 let mut images: Vec<String> = Vec::new();
146 let mut node_trees: Vec<String> = Vec::new();
147
148 for (kind, code, start, end) in id_blocks {
149 if let Some(name) =
150 extract_blend_id_name(&data[start..end], code, id_name_offset, id_name_max_len)
151 {
152 match kind {
153 BlendBlockKind::Scene => scenes.push(name),
154 BlendBlockKind::Collection => collections.push(name),
155 BlendBlockKind::Object => objects.push(name),
156 BlendBlockKind::Mesh => meshes.push(name),
157 BlendBlockKind::Material => materials.push(name),
158 BlendBlockKind::Camera => cameras.push(name),
159 BlendBlockKind::Light => lights.push(name),
160 BlendBlockKind::Armature => armatures.push(name),
161 BlendBlockKind::Action => actions.push(name),
162 BlendBlockKind::ShapeKey => shape_keys.push(name),
163 BlendBlockKind::World => worlds.push(name),
164 BlendBlockKind::FreestyleLineStyle => line_styles.push(name),
165 BlendBlockKind::Curve => curves.push(name),
166 BlendBlockKind::ParticleSettings => particle_settings.push(name),
167 BlendBlockKind::Image => images.push(name),
168 BlendBlockKind::NodeTree => node_trees.push(name),
169 BlendBlockKind::DnaSchema
170 | BlendBlockKind::EndOfFile
171 | BlendBlockKind::Other => {}
172 }
173 }
174 }
175
176 let (dna_struct_count, dna_type_count, dna_name_count): (usize, usize, usize) =
177 match &dna_summary {
178 Some(dna) => (
179 dna.get_struct_count(),
180 dna.get_type_count(),
181 dna.get_name_count(),
182 ),
183 None => (0, 0, 0),
184 };
185 let version_string: String = format!(
186 "{}.{}",
187 header.get_version_major(),
188 header.get_version_minor()
189 );
190 Ok(BlendInspectionReport {
191 blender_version_major: header.get_version_major(),
192 blender_version_minor: header.get_version_minor(),
193 version_string,
194 pointer_size_bytes: header.get_pointer_size_bytes(),
195 is_little_endian: header.get_is_little_endian(),
196 is_large_header_v5: header.get_is_large_header_v5(),
197 compression,
198 block_count,
199 dna_struct_count,
200 dna_type_count,
201 dna_name_count,
202 scenes,
203 collections,
204 objects,
205 meshes,
206 materials,
207 cameras,
208 lights,
209 armatures,
210 actions,
211 shape_keys,
212 worlds,
213 line_styles,
214 curves,
215 particle_settings,
216 images,
217 node_trees,
218 warnings,
219 })
220 }
221}
222
223impl BlendInspectionReport {
225 #[must_use]
231 pub fn capability_summary(&self) -> String {
232 format!(
233 "Blender v{} · SDNA {} structs · {} objs · {} meshes · {} armatures · {} actions · {} shape keys · {} Freestyle · {} worlds",
234 self.get_version_string(),
235 self.get_dna_struct_count(),
236 self.get_objects().len(),
237 self.get_meshes().len(),
238 self.get_armatures().len(),
239 self.get_actions().len(),
240 self.get_shape_keys().len(),
241 self.get_line_styles().len(),
242 self.get_worlds().len(),
243 )
244 }
245}