From 90cee2d998be0c7edb075bd1fe77c2880e7aad8f Mon Sep 17 00:00:00 2001 From: ashley Date: Thu, 28 Sep 2023 21:30:04 -0400 Subject: [PATCH] Add custom fields to object export --- src/reskit/cli/evaluator.rs | 5 ++--- src/reskit/level/converter.rs | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/reskit/cli/evaluator.rs b/src/reskit/cli/evaluator.rs index 1109ae6..4d598f3 100644 --- a/src/reskit/cli/evaluator.rs +++ b/src/reskit/cli/evaluator.rs @@ -94,10 +94,9 @@ pub fn run_command() -> Result<(), Box> { let symbol_ids = all_symbols; let sprite_ids = all_sprites; - // TODO: will be used when custom object fields are implemented - let _fields: Vec<&str> = fields.split( "," ).collect(); + let fields: Vec<&str> = fields.split( "," ).collect(); - let tiled_file = get_tiled_tilemap( &input_file )?; + let tiled_file = get_tiled_tilemap( &input_file, &fields )?; // Get tile and palette files let ( all_tiles, palettes ) = get_tiles( &tiled_file )?; diff --git a/src/reskit/level/converter.rs b/src/reskit/level/converter.rs index 754999f..ce2b3dd 100644 --- a/src/reskit/level/converter.rs +++ b/src/reskit/level/converter.rs @@ -211,7 +211,7 @@ fn get_tiles( tileset: Node, first_gid: usize, working_directory: &str ) -> Resu Ok( TiledTileset { first_gid, image, palettes, tile_order, sprite_metadata } ) } -pub fn get_objs( node: &Node ) -> Result, Box> { +pub fn get_objs( node: &Node, object_fields: &Vec<&str> ) -> Result, Box> { let mut result = Vec::new(); let objects = node.descendants().filter( | node | node.tag_name() == "object".into() ); @@ -233,7 +233,17 @@ pub fn get_objs( node: &Node ) -> Result, Box> { attributes.insert( "x".trim().to_owned(), x ); attributes.insert( "y".trim().to_owned(), y ); - // TODO: Custom object fields + // Custom object fields - push these in order into the LinkedHashMap + for field in object_fields { + let property = properties.descendants().find( | node | node.attribute( "name" ) == Some( &format!( "reskit-field[{}]", field ) ) ); + if let Some( property ) = property { + let value = property.attribute( "value" ).ok_or( "invalid file: property has no value attribute" )?.to_owned(); + attributes.insert( field.to_string(), value ); + } else { + print_warning( &format!( "object {} does not define a value for struct field \"{}\", this field will be filled in with 0x00 at export", id, field ) ); + attributes.insert( field.to_string(), "0".to_owned() ); + } + } result.push( Object { id, attributes } ); } else { @@ -247,7 +257,7 @@ pub fn get_objs( node: &Node ) -> Result, Box> { Ok( result ) } -pub fn get_tiled_tilemap( path: &str ) -> Result> { +pub fn get_tiled_tilemap( path: &str, object_fields: &Vec<&str> ) -> Result> { let file = read_to_string( path )?; let document = roxmltree::Document::parse( &file )?; let working_directory = { @@ -341,7 +351,7 @@ pub fn get_tiled_tilemap( path: &str ) -> Result> { // Get the entity-component system let object_group = map.descendants().find( | node | node.tag_name() == "objectgroup".into() ); let objects = if let Some( object_group ) = object_group { - get_objs( &object_group )? + get_objs( &object_group, object_fields )? } else { Vec::new() };