From 5d402221204fdae6d87bd0322e8ea721174eed39 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Thu, 2 Jul 2020 16:52:40 +0100 Subject: [PATCH] fix merge issues #1 & #2; minor version bump --- Cargo.toml | 2 +- src/game.rs | 28 +++++++++++++++------------- src/lib.rs | 6 +++--- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index de3fd2e..0d92887 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bitsy-parser" -version = "0.71.0" +version = "0.71.1" authors = ["Max Bradbury "] edition = "2018" description = "A parser and utilities for working with Bitsy game data" diff --git a/src/game.rs b/src/game.rs index 7a6600e..d47696e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -365,7 +365,7 @@ impl Game { for item in &game.items { let old_id = item.id.clone(); - let new_id = try_id(self.item_ids(), item.id.clone()); + let new_id = try_id(&self.item_ids(), &item.id); insert_if_different(item_id_changes.borrow_mut(), old_id, new_id) } @@ -419,12 +419,14 @@ impl Game { // calculate all of the new room IDs first // to insert any new room, we need to know the new IDs of every room // to maintain the integrity of exits and endings + + let mut all_room_ids = self.room_ids().clone(); + for room in &game.rooms { - insert_if_different( - room_id_changes.borrow_mut(), - room.id.clone(), - try_id(self.room_ids(), room.id.clone()) - ); + let old = room.id.clone(); + let new = try_id(&all_room_ids, &room.id); + insert_if_different(room_id_changes.borrow_mut(), old, new.clone()); + all_room_ids.push(new); } // needs to be handled after palettes, tiles, items, exits, endings @@ -679,7 +681,7 @@ impl Game { /// adds a palette safely and returns the ID #[inline] pub fn add_palette(&mut self, mut palette: Palette) -> String { - let new_id = try_id(self.palette_ids(), palette.id.clone()); + let new_id = try_id(&self.palette_ids(), &palette.id); if new_id != palette.id { palette.id = new_id.clone(); } @@ -704,7 +706,7 @@ impl Game { /// adds a sprite safely and returns the ID #[inline] pub fn add_sprite(&mut self, mut sprite: Sprite) -> String { - let new_id = try_id(self.sprite_ids(), sprite.id.clone()); + let new_id = try_id(&self.sprite_ids(), &sprite.id); if new_id != sprite.id { sprite.id = new_id.clone(); } @@ -715,7 +717,7 @@ impl Game { /// adds an item safely and returns the ID #[inline] pub fn add_item(&mut self, mut item: Item) -> String { - let new_id = try_id(self.item_ids(), item.id.clone()); + let new_id = try_id(&self.item_ids(), &item.id); if new_id != item.id { item.id = new_id.clone(); } @@ -726,7 +728,7 @@ impl Game { /// adds a dialogue safely and returns the ID #[inline] pub fn add_dialogue(&mut self, mut dialogue: Dialogue) -> String { - let new_id = try_id(self.dialogue_ids(), dialogue.id.clone()); + let new_id = try_id(&self.dialogue_ids(), &dialogue.id); if new_id != dialogue.id { dialogue.id = new_id.clone(); } @@ -736,7 +738,7 @@ impl Game { /// adds an ending safely and returns the ID pub fn add_ending(&mut self, mut ending: Ending) -> String { - let new_id = try_id(self.ending_ids(), ending.id.clone()); + let new_id = try_id(&self.ending_ids(), &ending.id); if new_id != ending.id { ending.id = new_id.clone(); } @@ -748,7 +750,7 @@ impl Game { /// You will need to be mindful that the room's palette, tile, exit and ending IDs /// will be valid after adding. pub fn add_room(&mut self, mut room: Room) -> String { - let new_id = try_id(self.room_ids(), room.id.clone()); + let new_id = try_id(&self.room_ids(), &room.id); if new_id != room.id { room.id = new_id.clone(); } @@ -757,7 +759,7 @@ impl Game { } pub fn add_variable(&mut self, mut variable: Variable) -> String { - let new_id = try_id(self.variable_ids(), variable.id.clone()); + let new_id = try_id(&self.variable_ids(), &variable.id); if new_id != variable.id { variable.id = new_id.clone(); } diff --git a/src/lib.rs b/src/lib.rs index 4044d67..2da3ba7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,7 +145,7 @@ fn segments_from_string(string: String) -> Vec { /// tries to use an existing ID - if it is already in use, generate a new one /// then return the ID (either original or new) /// todo refactor (unnecessary clones etc.) -fn try_id(ids: Vec, id: String) -> String { +fn try_id(ids: &Vec, id: &String) -> String { let id = id.clone(); let ids = ids.clone(); if is_id_available(&ids, &id) { @@ -242,12 +242,12 @@ mod test { fn test_try_id() { // does a conflict generate a new ID? assert_eq!( - try_id(vec!["0".to_string(), "1".to_string()], "1".to_string()), + try_id(&vec!["0".to_string(), "1".to_string()], &"1".to_string()), "2".to_string() ); // with no conflict, does the ID remain the same? assert_eq!( - try_id(vec!["0".to_string(), "1".to_string()], "3".to_string()), + try_id(&vec!["0".to_string(), "1".to_string()], &"3".to_string()), "3".to_string() ); }