turn room format and room type into enums; transform line endings
This commit is contained in:
72
src/game.rs
72
src/game.rs
@@ -1,8 +1,51 @@
|
||||
use crate::{
|
||||
optional_data_line, Avatar, Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection,
|
||||
Tile, ToBase36, Variable,
|
||||
};
|
||||
use crate::{Avatar, Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, ToBase36, Variable, transform_line_endings};
|
||||
use std::error::Error;
|
||||
use loe::TransformMode;
|
||||
|
||||
/// in very early versions of Bitsy, room tiles were defined as single characters
|
||||
/// so, only 36 tiles total. later versions are comma-separated
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
||||
pub enum RoomFormat {Contiguous, CommaSeparated}
|
||||
|
||||
impl RoomFormat {
|
||||
fn from(str: &str) -> Result<RoomFormat, &'static dyn Error> {
|
||||
match str {
|
||||
"0" => Ok(RoomFormat::Contiguous),
|
||||
"1" => Ok(RoomFormat::CommaSeparated),
|
||||
_ => panic!(format!("Invalid room format: {}", str)),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_string(&self) -> String {
|
||||
match &self {
|
||||
RoomFormat::Contiguous => "0",
|
||||
RoomFormat::CommaSeparated => "1",
|
||||
}.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// in very early versions of Bitsy, a room was called a "set"
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
||||
pub enum RoomType {Room, Set}
|
||||
|
||||
impl From<&str> for RoomType {
|
||||
fn from(string: &str) -> RoomType {
|
||||
match string {
|
||||
"ROOM" => RoomType::Room,
|
||||
"SET" => RoomType::Set,
|
||||
_ => panic!("Unrecognised room type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for RoomType {
|
||||
fn to_string(&self) -> String {
|
||||
match &self {
|
||||
RoomType::Set => "SET",
|
||||
RoomType::Room => "ROOM",
|
||||
}.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
||||
pub struct Version {
|
||||
@@ -25,7 +68,8 @@ impl Version {
|
||||
pub struct Game {
|
||||
pub name: String,
|
||||
pub version: Option<Version>,
|
||||
pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently
|
||||
pub room_format: Option<RoomFormat>,
|
||||
pub(crate) room_type: RoomType,
|
||||
pub font: Font,
|
||||
pub custom_font: Option<String>, // used if font is Font::Custom
|
||||
pub text_direction: TextDirection,
|
||||
@@ -44,6 +88,12 @@ pub struct Game {
|
||||
|
||||
impl Game {
|
||||
pub fn from(string: String) -> Result<Game, &'static dyn Error> {
|
||||
let line_endings_crlf = string.contains("\r\n");
|
||||
let mut string = string;
|
||||
if line_endings_crlf {
|
||||
string = transform_line_endings(string, TransformMode::LF)
|
||||
}
|
||||
|
||||
let mut string = format!("{}\n\n", string.trim_matches('\n'));
|
||||
|
||||
if string.starts_with("# BITSY VERSION") {
|
||||
@@ -105,7 +155,8 @@ impl Game {
|
||||
|
||||
let name = segments[0].to_string();
|
||||
let mut version = None;
|
||||
let mut room_format: u8 = 1;
|
||||
let mut room_format = None;
|
||||
let mut room_type = RoomType::Room;
|
||||
let mut font = Font::AsciiSmall;
|
||||
let mut custom_font = None;
|
||||
let mut text_direction = TextDirection::LeftToRight;
|
||||
@@ -123,7 +174,8 @@ impl Game {
|
||||
let segment = segment.replace("# BITSY VERSION ", "");
|
||||
version = Some(Version::from(&segment));
|
||||
} else if segment.starts_with("! ROOM_FORMAT") {
|
||||
room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap();
|
||||
let segment = segment.replace("! ROOM_FORMAT ", "");
|
||||
room_format = Some(RoomFormat::from(&segment).unwrap());
|
||||
} else if segment.starts_with("DEFAULT_FONT") {
|
||||
let segment = segment.replace("DEFAULT_FONT ", "");
|
||||
|
||||
@@ -137,6 +189,9 @@ impl Game {
|
||||
} else if segment.starts_with("PAL") {
|
||||
palettes.push(Palette::from(segment));
|
||||
} else if segment.starts_with("ROOM") || segment.starts_with("SET") {
|
||||
if segment.starts_with("SET") {
|
||||
room_type = RoomType::Set;
|
||||
}
|
||||
rooms.push(Room::from(segment));
|
||||
} else if segment.starts_with("TIL") {
|
||||
tiles.push(Tile::from(segment));
|
||||
@@ -156,6 +211,7 @@ impl Game {
|
||||
name,
|
||||
version,
|
||||
room_format,
|
||||
room_type,
|
||||
font,
|
||||
custom_font,
|
||||
text_direction,
|
||||
@@ -169,7 +225,7 @@ impl Game {
|
||||
endings,
|
||||
variables,
|
||||
font_data,
|
||||
line_endings_crlf: false
|
||||
line_endings_crlf
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user