1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::{Avatar, Dialogue, Ending, Item, Palette, Room, Sprite, Tile, Variable, mock};

#[derive(Debug, PartialEq)]
pub struct Game {
    pub(crate) name: String,
    pub(crate) version: f64,
    pub(crate) room_format: u8,
    pub(crate) palettes: Vec<Palette>,
    pub(crate) rooms: Vec<Room>,
    pub(crate) tiles: Vec<Tile>,
    pub(crate) avatar: Avatar,
    pub(crate) sprites: Vec<Sprite>,
    pub(crate) items: Vec<Item>,
    pub(crate) dialogues: Vec<Dialogue>,
    pub(crate) endings: Vec<Ending>,
    pub(crate) variables: Vec<Variable>,
}

impl From<String> for Game {
    fn from(string: String) -> Game {
        // dialogues and endings can have 2+ line breaks inside, so deal with these separately
        // otherwise, everything can be split on a double line break (\n\n)
        let mut dialogues: Vec<Dialogue> = Vec::new();
        let mut endings: Vec<Ending> = Vec::new();
        let mut variables: Vec<Variable> = Vec::new();
        let main_split: Vec<&str> = string.split("\n\nDLG").collect();
        let main = main_split[0].to_string();
        let mut dialogues_endings_variables: String = main_split[1..].join("\n\nDLG");

        let variable_segments = dialogues_endings_variables.clone();
        let variable_segments: Vec<&str> = variable_segments.split("\n\nVAR").collect();
        if variable_segments.len() > 0 {
            dialogues_endings_variables = variable_segments[0].to_string();
            let variable_segments = variable_segments[1..].to_owned();

            for segment in variable_segments {
                let segment = format!("VAR{}", segment);
                variables.push(Variable::from(segment));
            }
        }

        let ending_segments = dialogues_endings_variables.clone();
        let ending_segments: Vec<&str> = ending_segments.split("\n\nEND").collect();
        if ending_segments.len() > 0 {
            dialogues_endings_variables = ending_segments[0].to_string();
            let ending_segments = ending_segments[1..].to_owned();

            for segment in ending_segments {
                let segment = format!("END{}", segment);
                endings.push(Ending::from(segment));
            }
        }

        let dialogue_segments = format!("\n\nDLG {}", dialogues_endings_variables.trim());
        let dialogue_segments: Vec<&str> = dialogue_segments.split("\n\nDLG").collect();
        for segment in dialogue_segments[1..].to_owned() {
            let segment = format!("DLG{}", segment);
            dialogues.push(Dialogue::from(segment));
        }

        let segments: Vec<&str> = main.split("\n\n").collect();

        let name = segments[0].to_string();
        let mut version: f64 = 1.0;
        let mut room_format: u8 = 1;
        let mut palettes: Vec<Palette> = Vec::new();
        let mut rooms: Vec<Room> = Vec::new();
        let mut tiles: Vec<Tile> = Vec::new();
        let mut avatar: Option<Avatar> = None; // unwrap this later
        let mut sprites: Vec<Sprite> = Vec::new();
        let mut items: Vec<Item> = Vec::new();

        for segment in segments[1..].to_owned() {
            let segment = segment.to_string();

            if segment.starts_with("# BITSY VERSION") {
                version = segment.replace("# BITSY VERSION ", "").parse().unwrap();
            } else if segment.starts_with("! ROOM_FORMAT") {
                room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap();
            } else if segment.starts_with("PAL") {
                palettes.push(Palette::from(segment));
            } else if segment.starts_with("ROOM") {
                rooms.push(Room::from(segment));
            } else if segment.starts_with("TIL") {
                tiles.push(Tile::from(segment));
            } else if segment.starts_with("SPR A") {
                avatar = Some(Avatar::from(segment));
            } else if segment.starts_with("SPR") {
                sprites.push(Sprite::from(segment));
            } else if segment.starts_with("ITM") {
                items.push(Item::from(segment));
            }
        }

        assert!(avatar.is_some());
        let avatar = avatar.unwrap();

        Game {
            name,
            version,
            room_format,
            palettes,
            rooms,
            tiles,
            avatar,
            sprites,
            items,
            dialogues,
            endings,
            variables,
        }
    }
}

impl ToString for Game {
    #[inline]
    fn to_string(&self) -> String {
        let mut segments: Vec<String> = Vec::new();

        // todo refactor

        for palette in &self.palettes {
            segments.push(palette.to_string());
        }

        for room in &self.rooms {
            segments.push(room.to_string());
        }

        for tile in &self.tiles {
            segments.push(tile.to_string());
        }

        segments.push(self.avatar.to_string());

        for sprite in &self.sprites {
            segments.push(sprite.to_string());
        }

        for item in &self.items {
            segments.push(item.to_string());
        }

        for dialogue in &self.dialogues {
            segments.push(dialogue.to_string());
        }

        for ending in &self.endings {
            segments.push(ending.to_string());
        }

        for variable in &self.variables {
            segments.push(variable.to_string());
        }

        format!(
            "{}\n\n# BITSY VERSION {}\n\n! ROOM_FORMAT {}\n\n{}\n\n",
            &self.name,
            &self.version,
            &self.room_format,
            segments.join("\n\n"),
        )
    }
}

#[test]
fn test_game_from_string() {
    let output = Game::from(
        include_str!["../test/resources/default.bitsy"].to_string()
    );

    let expected = mock::game_default();

    assert_eq!(output, expected);
}

#[test]
fn test_game_to_string() {
    let output = mock::game_default().to_string();
    let expected = include_str!["../test/resources/default.bitsy"].to_string();
    assert_eq!(output, expected);
}