From 07a7ee97b761873474ec728ffb624f4dd91a1f0d Mon Sep 17 00:00:00 2001 From: ashley Date: Mon, 25 Sep 2023 21:31:32 -0400 Subject: [PATCH] Consistent ordering of components and attributes on export --- src/reskit/level/system.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/reskit/level/system.rs b/src/reskit/level/system.rs index 52e677e..78f1287 100644 --- a/src/reskit/level/system.rs +++ b/src/reskit/level/system.rs @@ -1,8 +1,7 @@ use std::{error::Error, convert::TryInto, cmp::max, num::ParseIntError, collections::HashMap}; use image::GenericImageView; -use linked_hash_set::LinkedHashSet; use crate::reskit::{tileset::image_to_tiles, utility::{symbol_to_pascal, print_info}, cli::settings::TileOrder}; -use super::converter::{TiledTilemap, Layer, SystemPlane, TiledTileset}; +use super::{converter::{TiledTilemap, Layer, SystemPlane, TiledTileset}, ecs::Component}; /** * Output the .bin and .pal file (using `tileset` tool to build it) containing each of the tiles @@ -227,14 +226,18 @@ pub fn get_ecs( tilemap: &TiledTilemap, component_ids: &HashMap, att let mut result: Vec = Vec::new(); // Build a complete set of types - let mut types: LinkedHashSet> = LinkedHashSet::new(); + let mut types: Vec> = Vec::new(); for entity in &tilemap.ecs { - // Get the list of components attached to this entity - let components: LinkedHashSet = entity.components.keys().map( | id | id.to_lowercase() ).collect(); + // Get the list of component IDs attached to this entity + let mut components: Vec = entity.components.keys() + .map( | id | Ok( *component_ids.get( id ).ok_or( format!( "invalid file: when building type table: undefined component \"{}\"", id ) )? ) ) + .collect::, Box>>()?; + components.sort(); - // Assign this unique combination of components a type id - // by inserting it into `types` - types.insert_if_absent( components ); + // Assign this unique combination of components a type id by inserting it into `types` + if let None = types.iter().find( | ecs_type | ecs_type == &&components ) { + types.push( components ); + } } let largest_type_size: u16 = types.iter() @@ -252,13 +255,7 @@ pub fn get_ecs( tilemap: &TiledTilemap, component_ids: &HashMap, att // For (Number of Types): for ecs_type in &types { // (Type Definition Union Size) bytes: List of Component IDs - let mut type_component_ids: Vec = vec![]; - - for component in ecs_type { - type_component_ids.push( - *component_ids.get( component ).ok_or( format!( "invalid file: when building type table: undefined component \"{}\"", component ) )? - ); - } + let mut type_component_ids: Vec = ecs_type.clone(); // Fill the remainder of the union with 0xFF, if needed let remainder = largest_type_size as usize - type_component_ids.len(); @@ -275,10 +272,10 @@ pub fn get_ecs( tilemap: &TiledTilemap, component_ids: &HashMap, att // For (Object Table Size) for entity in &tilemap.ecs { - // Do the thing again - let mut components: Vec = entity.components.keys().map( | id | id.to_lowercase() ).collect(); + let mut components: Vec = entity.components.keys() + .map( | id | Ok( *component_ids.get( id ).ok_or( format!( "invalid file: when building type table: undefined component \"{}\"", id ) )? ) ) + .collect::, Box>>()?; components.sort(); - let components: LinkedHashSet = components.into_iter().collect(); // What type id is this? let type_id: u8 = types.iter().position( | ecs_type | ecs_type == &components ).ok_or( "internal error: no type id" )? as u8; @@ -314,7 +311,10 @@ pub fn get_ecs( tilemap: &TiledTilemap, component_ids: &HashMap, att for entity in &tilemap.ecs { let mut written_attributes = 0; - for ( component_id, component ) in &entity.components { + let mut components: Vec<(&String, &Component)> = entity.components.iter().collect(); + components.sort_by_key( | ( component_id, _ ) | component_ids[ *component_id ] ); + + for ( component_id, component ) in components { let attributes_in_order = attribute_ids.get( component_id ); if let Some( attributes_in_order ) = attributes_in_order { for attribute_id in attributes_in_order {