2020-04-18 15:58:30 +00:00
|
|
|
use crate::{from_base36, optional_data_line, AnimationFrames, Image, ToBase36};
|
2020-04-29 07:27:35 +00:00
|
|
|
use crate::image::animation_frames_from_string;
|
2020-04-12 12:46:55 +00:00
|
|
|
|
2020-05-31 15:12:23 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-04-12 12:46:55 +00:00
|
|
|
pub struct Item {
|
2020-04-13 13:47:15 +00:00
|
|
|
pub id: u64,
|
2020-04-12 16:13:08 +00:00
|
|
|
pub animation_frames: Vec<Image>,
|
|
|
|
pub name: Option<String>,
|
2020-04-13 18:11:51 +00:00
|
|
|
pub dialogue_id: Option<String>,
|
2020-04-13 23:36:31 +00:00
|
|
|
pub colour_id: Option<u64>,
|
2020-04-12 12:46:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:12:21 +00:00
|
|
|
impl Item {
|
2020-04-28 17:00:31 +00:00
|
|
|
#[inline]
|
2020-04-13 18:12:21 +00:00
|
|
|
fn name_line(&self) -> String {
|
|
|
|
optional_data_line("NAME", self.name.as_ref())
|
|
|
|
}
|
2020-04-13 23:36:31 +00:00
|
|
|
|
2020-04-28 17:00:31 +00:00
|
|
|
#[inline]
|
2020-04-13 18:12:21 +00:00
|
|
|
fn dialogue_line(&self) -> String {
|
|
|
|
optional_data_line("DLG", self.dialogue_id.as_ref())
|
|
|
|
}
|
2020-04-13 23:36:31 +00:00
|
|
|
|
2020-04-28 17:00:31 +00:00
|
|
|
#[inline]
|
2020-04-13 23:36:31 +00:00
|
|
|
fn colour_line(&self) -> String {
|
|
|
|
optional_data_line("COL", self.colour_id.as_ref())
|
|
|
|
}
|
2020-04-13 18:12:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 12:46:55 +00:00
|
|
|
impl From<String> for Item {
|
2020-04-28 17:00:31 +00:00
|
|
|
#[inline]
|
2020-04-12 12:46:55 +00:00
|
|
|
fn from(string: String) -> Item {
|
|
|
|
let mut lines: Vec<&str> = string.lines().collect();
|
|
|
|
|
2020-04-13 13:47:15 +00:00
|
|
|
let id = from_base36(&lines[0].replace("ITM ", ""));
|
2020-04-12 12:46:55 +00:00
|
|
|
let mut name = None;
|
2020-04-13 13:47:15 +00:00
|
|
|
let mut dialogue_id = None;
|
2020-04-13 23:36:31 +00:00
|
|
|
let mut colour_id: Option<u64> = None;
|
2020-04-12 12:46:55 +00:00
|
|
|
|
2020-04-13 23:36:31 +00:00
|
|
|
loop {
|
2020-04-12 12:46:55 +00:00
|
|
|
let last_line = lines.pop().unwrap();
|
|
|
|
|
|
|
|
if last_line.starts_with("NAME") {
|
|
|
|
name = Some(last_line.replace("NAME ", "").to_string());
|
|
|
|
} else if last_line.starts_with("DLG") {
|
2020-04-13 13:47:15 +00:00
|
|
|
dialogue_id = Some(last_line.replace("DLG ", "").to_string());
|
2020-04-13 23:36:31 +00:00
|
|
|
} else if last_line.starts_with("COL") {
|
|
|
|
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
|
2020-04-12 12:46:55 +00:00
|
|
|
} else {
|
|
|
|
lines.push(last_line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 07:27:35 +00:00
|
|
|
let animation_frames = animation_frames_from_string(
|
|
|
|
lines[1..].join("\n")
|
|
|
|
);
|
2020-04-12 12:46:55 +00:00
|
|
|
|
2020-04-18 15:58:30 +00:00
|
|
|
Item {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
animation_frames,
|
|
|
|
dialogue_id,
|
|
|
|
colour_id,
|
|
|
|
}
|
2020-04-12 12:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Item {
|
|
|
|
#[inline]
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
format!(
|
2020-04-13 23:36:31 +00:00
|
|
|
"ITM {}\n{}{}{}{}",
|
2020-04-13 13:47:15 +00:00
|
|
|
self.id.to_base36(),
|
2020-04-12 12:46:55 +00:00
|
|
|
self.animation_frames.to_string(),
|
2020-04-13 18:12:21 +00:00
|
|
|
self.name_line(),
|
|
|
|
self.dialogue_line(),
|
2020-04-13 23:36:31 +00:00
|
|
|
self.colour_line(),
|
2020-04-12 12:46:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::item::Item;
|
|
|
|
use crate::mock;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_item_from_string() {
|
|
|
|
let output = Item::from(include_str!("test-resources/item").to_string());
|
|
|
|
let expected = mock::item();
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-12 12:46:55 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_item_to_string() {
|
|
|
|
let output = mock::item().to_string();
|
|
|
|
let expected = include_str!("test-resources/item").to_string();
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-12 12:46:55 +00:00
|
|
|
}
|