From ebb172df7e558fb12268a48246e0fee7197fd573 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Fri, 24 Apr 2020 18:07:32 +0100 Subject: [PATCH] handle room formats --- README.md | 4 +--- src/game.rs | 2 +- src/room.rs | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e738a1f..68b88c6 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,7 @@ some more practical uses would be things like: * check multi-line endings/dialogues * handle old out-of-bounds editor errors (extra image pixels, exits/endings with position -1,-0 etc.) * it's ok if the output of these does not match the input as we're fixing it -* "unquote" function for dialogues, game title, anything else that supports """ -* if old room format, export game in old room format -* handle windows line endings (cr+lf) - https://crates.io/crates/loe +* "unquote" function for dialogues, game title, anything else that supports """ * add Bitsy HD and Bitsy 7.0 test data ### tidy up diff --git a/src/game.rs b/src/game.rs index 8576f21..cd1fe81 100644 --- a/src/game.rs +++ b/src/game.rs @@ -242,7 +242,7 @@ impl ToString for Game { } for room in &self.rooms { - segments.push(room.to_string()); + segments.push(room.to_string(self.room_format(), self.room_type)); } for tile in &self.tiles { diff --git a/src/room.rs b/src/room.rs index 00d163f..34adf09 100644 --- a/src/room.rs +++ b/src/room.rs @@ -100,10 +100,14 @@ impl From for Room { let mut tiles: Vec = Vec::new(); for line in lines.into_iter() { - let line: Vec<&str> = line - .split(if line.contains(",") {","} else {""}) // old room format? + let comma_separated = line.contains(","); // old room format? + let mut line: Vec<&str> = line + .split(if comma_separated {","} else {""}) .collect(); + if ! comma_separated { line = line[1..].to_owned(); } + let line = line[..16].to_owned(); + for tile_id in line { tiles.push(tile_id.to_string()); } @@ -136,9 +140,16 @@ impl Room { let sqrt = (self.tiles.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD for line in self.tiles.chunks(sqrt) { for tile in line { - tiles.push_str(&format!("{},", tile)); + tiles.push_str(tile); + if room_format == RoomFormat::CommaSeparated { + tiles.push(','); + } } - tiles.pop(); // remove trailing comma + + if room_format == RoomFormat::CommaSeparated { + tiles.pop(); // remove trailing comma + } + tiles.push_str("\n"); } tiles.pop(); // remove trailing newline