Add custom fields to object export

stinkhead7ds
Ashley N. 2023-09-28 21:30:04 -04:00
parent 8ae402d037
commit 90cee2d998
2 changed files with 16 additions and 7 deletions

View File

@ -94,10 +94,9 @@ pub fn run_command() -> Result<(), Box<dyn Error>> {
let symbol_ids = all_symbols; let symbol_ids = all_symbols;
let sprite_ids = all_sprites; 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 // Get tile and palette files
let ( all_tiles, palettes ) = get_tiles( &tiled_file )?; let ( all_tiles, palettes ) = get_tiles( &tiled_file )?;

View File

@ -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 } ) Ok( TiledTileset { first_gid, image, palettes, tile_order, sprite_metadata } )
} }
pub fn get_objs( node: &Node ) -> Result<Vec<Object>, Box<dyn Error>> { pub fn get_objs( node: &Node, object_fields: &Vec<&str> ) -> Result<Vec<Object>, Box<dyn Error>> {
let mut result = Vec::new(); let mut result = Vec::new();
let objects = node.descendants().filter( | node | node.tag_name() == "object".into() ); let objects = node.descendants().filter( | node | node.tag_name() == "object".into() );
@ -233,7 +233,17 @@ pub fn get_objs( node: &Node ) -> Result<Vec<Object>, Box<dyn Error>> {
attributes.insert( "x".trim().to_owned(), x ); attributes.insert( "x".trim().to_owned(), x );
attributes.insert( "y".trim().to_owned(), y ); 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 } ); result.push( Object { id, attributes } );
} else { } else {
@ -247,7 +257,7 @@ pub fn get_objs( node: &Node ) -> Result<Vec<Object>, Box<dyn Error>> {
Ok( result ) Ok( result )
} }
pub fn get_tiled_tilemap( path: &str ) -> Result<TiledTilemap, Box<dyn Error>> { pub fn get_tiled_tilemap( path: &str, object_fields: &Vec<&str> ) -> Result<TiledTilemap, Box<dyn Error>> {
let file = read_to_string( path )?; let file = read_to_string( path )?;
let document = roxmltree::Document::parse( &file )?; let document = roxmltree::Document::parse( &file )?;
let working_directory = { let working_directory = {
@ -341,7 +351,7 @@ pub fn get_tiled_tilemap( path: &str ) -> Result<TiledTilemap, Box<dyn Error>> {
// Get the entity-component system // Get the entity-component system
let object_group = map.descendants().find( | node | node.tag_name() == "objectgroup".into() ); let object_group = map.descendants().find( | node | node.tag_name() == "objectgroup".into() );
let objects = if let Some( object_group ) = object_group { let objects = if let Some( object_group ) = object_group {
get_objs( &object_group )? get_objs( &object_group, object_fields )?
} else { } else {
Vec::new() Vec::new()
}; };