@@ -13,6 +13,7 @@ use std::fs;
13
13
use std:: io:: Read ;
14
14
use std:: path:: PathBuf ;
15
15
use texture_packer:: { TexturePacker , TexturePackerConfig } ;
16
+ use crate :: item:: ItemType ;
16
17
17
18
#[ derive( Debug , Clone ) ]
18
19
pub struct Data {
@@ -92,48 +93,25 @@ pub fn load_data(data_directory: PathBuf) -> Result<Data> {
92
93
let model_knight = load_voxel_model ( "data/model/chr_knight.vox" ) . unwrap ( ) ;
93
94
models. register ( "knight" . to_owned ( ) , model_knight) ?;
94
95
95
- // Load blocks
96
- let mut block_datas: Vec < ( String , BlockType ) > = Vec :: new ( ) ;
97
- let blocks_directory = data_directory. join ( "blocks" ) ;
98
- info ! (
99
- "Loading blocks from directory {}" ,
100
- blocks_directory. display( )
101
- ) ;
102
- for dir_entry in fs:: read_dir ( blocks_directory) . context ( "couldn't read block directory" ) ? {
103
- let dir_entry = dir_entry. context ( "failed to read directory entry" ) ?;
104
- if dir_entry
105
- . file_type ( )
106
- . context ( "failed to get file type" ) ?
107
- . is_file ( )
108
- {
109
- let file_path = dir_entry. path ( ) ;
96
+ // Load items
97
+ let items_directory = data_directory. join ( "items" ) ;
98
+ let item_datas: Vec < ( String , ItemType ) > = load_files_from_folder ( items_directory) ;
110
99
111
- match file_path. extension ( ) {
112
- None => panic ! ( "No file extension" ) ,
113
- Some ( ext) => {
114
- if ext == "ron" {
115
- let mut file = fs:: File :: open ( file_path. clone ( ) )
116
- . context ( "couldn't open .ron block file" ) ?;
117
- let mut buffer = String :: new ( ) ;
118
- file. read_to_string ( & mut buffer) ?;
119
- block_datas. push ( (
120
- file_path
121
- . file_stem ( )
122
- . context ( "failed to get file stem" ) ?
123
- . to_str ( )
124
- . unwrap ( )
125
- . to_owned ( ) ,
126
- ron:: de:: from_str ( & buffer)
127
- . context ( "failed to parse .ron block file" ) ?,
128
- ) ) ;
129
- } else {
130
- panic ! ( "Unsupported file extension" ) ;
131
- }
132
- }
100
+ // Generate item models
101
+ for ( name, ty) in item_datas. into_iter ( ) {
102
+ match ty {
103
+ ItemType :: NormalItem { texture } => {
104
+ let texture_rect = texture_rects[ texture_registry. get_id_by_name ( & texture) . unwrap ( ) as usize ] ;
105
+ let model = self :: vox:: item:: generate_item_model ( texture_rect, & texture_atlas) ;
106
+ models. register ( format ! ( "item:{}" , name) , model) . expect ( "Failed to register item model" ) ;
133
107
}
134
108
}
135
109
}
136
110
111
+ // Load blocks
112
+ let blocks_directory = data_directory. join ( "blocks" ) ;
113
+ let block_datas: Vec < ( String , BlockType ) > = load_files_from_folder ( blocks_directory) ;
114
+
137
115
info ! ( "Processing collected block and texture data" ) ;
138
116
let mut blocks = Registry :: default ( ) ;
139
117
let mut meshes = Vec :: new ( ) ;
@@ -144,7 +122,7 @@ pub fn load_data(data_directory: PathBuf) -> Result<Data> {
144
122
name : "air" . to_owned ( ) ,
145
123
block_type : BlockType :: Air ,
146
124
} ,
147
- ) ? ;
125
+ ) . expect ( "Couldn't register air in the registry." ) ;
148
126
meshes. push ( BlockMesh :: Empty ) ;
149
127
150
128
for ( name, block_type) in block_datas. into_iter ( ) {
@@ -189,7 +167,7 @@ pub struct TextureRect {
189
167
pub height : f32 ,
190
168
}
191
169
192
- const MAX_TEXTURE_SIZE : u32 = 2048 ;
170
+ pub const MAX_TEXTURE_SIZE : u32 = 2048 ;
193
171
194
172
const TEXTURE_PACKER_CONFIG : TexturePackerConfig = TexturePackerConfig {
195
173
max_width : MAX_TEXTURE_SIZE ,
@@ -244,3 +222,65 @@ fn load_textures(
244
222
. collect ( ) ,
245
223
) )
246
224
}
225
+
226
+ /// Load all <name>.ron files from a given folder and parse them into type `T`.
227
+ fn load_files_from_folder < T : serde:: de:: DeserializeOwned > (
228
+ directory : PathBuf ,
229
+ ) -> Vec < ( String , T ) > {
230
+ let mut result = Vec :: new ( ) ;
231
+ info ! (
232
+ "Loading objects of type {} from directory {}" ,
233
+ std:: any:: type_name:: <T >( ) ,
234
+ directory. display( ) ,
235
+ ) ;
236
+ for dir_entry in fs:: read_dir ( directory) . expect ( "Failed to read from directory" ) {
237
+ let dir_entry = dir_entry. expect ( "Failed to read directory entry" ) ;
238
+ if dir_entry
239
+ . file_type ( )
240
+ . expect ( "Failed to get file type" )
241
+ . is_file ( )
242
+ {
243
+ let file_path = dir_entry. path ( ) ;
244
+
245
+ match file_path. extension ( ) {
246
+ None => log:: warn!( "No file extension for file {}, skipping..." , file_path. display( ) ) ,
247
+ Some ( ext) => {
248
+ if ext == "ron" {
249
+ log:: info!( "Attempting to read file {}" , file_path. display( ) ) ;
250
+ let mut file = fs:: File :: open ( file_path. clone ( ) )
251
+ . expect ( "Failed to open file" ) ;
252
+ let mut buffer = String :: new ( ) ;
253
+ file. read_to_string ( & mut buffer) . expect ( "Failed to read from file" ) ;
254
+ let file_stem = file_path
255
+ . file_stem ( )
256
+ . expect ( "Failed to get file stem" )
257
+ . to_str ( )
258
+ . unwrap ( )
259
+ . to_owned ( ) ;
260
+
261
+ let parsed_file = {
262
+ if ext == "ron" {
263
+ match ron:: de:: from_str ( & buffer) {
264
+ Ok ( x) => x,
265
+ Err ( e) => {
266
+ log:: error!( "Failed to parse RON: {}, skipping..." , e) ;
267
+ continue
268
+ }
269
+ }
270
+ } else {
271
+ unreachable ! ( "No parser for file format" ) ;
272
+ }
273
+ } ;
274
+ result. push ( (
275
+ file_stem,
276
+ parsed_file,
277
+ ) ) ;
278
+ } else {
279
+ log:: warn!( "Unsupported file extension {:?}, skipping..." , ext) ; // TODO: display instead of debug
280
+ }
281
+ }
282
+ }
283
+ }
284
+ }
285
+ result
286
+ }
0 commit comments