@@ -107,6 +107,7 @@ pub fn parse_inp<R: Read>(mut data: R) -> Result<Model, ParseInpError> {
107
107
vendors,
108
108
} )
109
109
}
110
+
110
111
/// Parse `.inp` and `.inx` files.
111
112
pub fn dump_inp < R : Read > ( mut data : R , directory : & Path ) -> Result < ( ) , ParseInpError > {
112
113
// check magic bytes
@@ -186,3 +187,59 @@ pub fn dump_inp<R: Read>(mut data: R, directory: &Path) -> Result<(), ParseInpEr
186
187
187
188
Ok ( ( ) )
188
189
}
190
+
191
+ pub fn dump_to_inp < W : Write > ( directory : & Path , w : & mut W ) -> io:: Result < ( ) > {
192
+ let mut payload_file = File :: open ( directory. join ( "payload.json" ) ) ?;
193
+
194
+ w. write_all ( MAGIC ) ?;
195
+ w. write_all ( & ( payload_file. metadata ( ) ?. len ( ) as u32 ) . to_be_bytes ( ) ) ?;
196
+ io:: copy ( & mut payload_file, w) ?;
197
+
198
+ let mut texture_files = Vec :: new ( ) ;
199
+ for tex_file in fs:: read_dir ( directory. join ( "textures" ) ) ? {
200
+ let tex_file = tex_file?;
201
+ let path = tex_file. path ( ) ;
202
+ let Some ( ext) = path. extension ( ) else {
203
+ eprintln ! ( "File {:?} has no extension, ignoring" , tex_file. file_name( ) ) ;
204
+ continue ;
205
+ } ;
206
+
207
+ let Some ( ext) = ext. to_str ( ) else {
208
+ eprintln ! ( "File {:?} has unrecognized extension, ignoring" , tex_file. file_name( ) ) ;
209
+ continue ;
210
+ } ;
211
+
212
+ let tex_encoding: u8 = match ext {
213
+ "png" => 0 ,
214
+ "tga" => 1 ,
215
+ "bc7" => 2 ,
216
+ ext => {
217
+ eprintln ! (
218
+ "File {:?} has unsupported extension {:?}, ignoring" ,
219
+ tex_file. file_name( ) ,
220
+ ext
221
+ ) ;
222
+ continue ;
223
+ }
224
+ } ;
225
+
226
+ texture_files. push ( ( tex_encoding, path) ) ;
227
+ }
228
+
229
+ w. write_all ( TEX_SECT ) ?;
230
+ w. write_all ( & ( texture_files. len ( ) as u32 ) . to_be_bytes ( ) ) ?;
231
+ for ( tex_encoding, tex_path) in texture_files {
232
+ let mut tex_file = File :: open ( tex_path) ?;
233
+
234
+ let file_len = tex_file. metadata ( ) ?. len ( ) as u32 ;
235
+ println ! ( "t {} | {:?} {} B" , tex_encoding, file_len. to_be_bytes( ) , file_len) ;
236
+
237
+ w. write_all ( & file_len. to_be_bytes ( ) ) ?;
238
+ w. write_all ( & [ tex_encoding] ) ?;
239
+
240
+ io:: copy ( & mut tex_file, w) ?;
241
+ }
242
+
243
+ w. flush ( ) . unwrap ( ) ;
244
+ Ok ( ( ) )
245
+ }
0 commit comments