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
use encoding_rs_io::DecodeReaderBytesBuilder;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use crate::parser::raws::info_txt::DFInfoFile;
use crate::parser::raws::{graphics, RawObjectKind};
use crate::parser::refs::{DF_ENCODING, RAW_TOKEN_RE};
impl super::DFGraphic {
#[allow(clippy::too_many_lines)]
pub fn parse<P: AsRef<Path>>(
input_path: &P,
info_text: &DFInfoFile,
) -> Vec<graphics::DFGraphic> {
let caller = "Parse Simple Graphic Raw";
let mut results: Vec<graphics::DFGraphic> = Vec::new();
let file = match File::open(input_path) {
Ok(f) => f,
Err(e) => {
log::error!("{} - Error opening raw file for parsing!\n{:?}", caller, e);
return results;
}
};
let decoding_reader = DecodeReaderBytesBuilder::new()
.encoding(Some(*DF_ENCODING))
.build(file);
let reader = BufReader::new(decoding_reader);
let mut raw_filename = String::new();
let mut current_object = RawObjectKind::None;
let mut started = false;
let mut sprite_temp = graphics::DFGraphic::new(&raw_filename, "None:None", info_text);
for (index, line) in reader.lines().enumerate() {
if line.is_err() {
log::error!(
"{} - Error processing {}:{}",
caller,
input_path.as_ref().display(),
index
);
continue;
}
let line = match line {
Ok(l) => l,
Err(e) => {
log::error!("{} - Line-reading error\n{:?}", caller, e);
continue;
}
};
if index == 0 {
raw_filename = String::from(&line);
continue;
}
for cap in RAW_TOKEN_RE.captures_iter(&line) {
log::trace!("{} - Key: {} Value: {}", caller, &cap[2], &cap[3]);
match &cap[2] {
"OBJECT" => match &cap[3] {
"GRAPHICS" => {
// Discovered raws for plants.
current_object = RawObjectKind::Graphics;
}
&_ => {
log::debug!("{} - Wrong type of raw ({})", caller, &cap[3]);
return Vec::new();
// current_object = RawObjectKind::None;
}
},
"CREATURE_GRAPHICS"
| "CREATURE_CASTE_GRAPHICS"
| "TILE_GRAPHICS"
| "PLANT_GRAPHICS" => {
// We are starting a creature object capture
if let RawObjectKind::Graphics = current_object {
if started {
if !&sprite_temp.kind.eq(&graphics::Kind::Empty) {
results.push(sprite_temp);
}
} else {
started = true;
}
//Reset all temp values
log::trace!("Starting new graphic {}", &cap[3]);
//1. Make new sprite from its definition
sprite_temp = graphics::DFGraphic::new(
&raw_filename,
format!("{}:{}", &cap[2], &cap[3]).as_str(),
info_text,
);
}
}
"DEFAULT" | "SHRUB" | "PICKED" | "SEED" | "CROP" => {
sprite_temp.add_tile_from_token(&format!("{}:{}", &cap[2], &cap[3]));
}
&_ => {
// if sprite_temp.kind.eq(&graphics::Kind::Empty) {
// log::debug!("Skipping {}:{} because empty sprite", &cap[2], &cap[3]);
// } else {
// sprite_temp.add_tile_from_token(&format!("{}:{}", &cap[2], &cap[3]));
// }
}
}
}
}
if let RawObjectKind::Graphics = current_object {
if !&sprite_temp.kind.eq(&graphics::Kind::Empty) {
results.push(sprite_temp);
}
}
log::info!(
"{} sprite graphics objects defined in {} ({} {} in {:?})",
results.len(),
&raw_filename,
info_text.get_identifier(),
info_text.displayed_version,
info_text.get_location(),
);
results
}
}