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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use crate::{
error::{IoError, ParseError, ParseErrorType},
CyberGrindPattern, Prefab,
};
use std::{
fs::File,
io::{self, Read, Write},
path::Path,
};
const MAX_FILE_SIZE: usize = 1569;
impl CyberGrindPattern {
/// Creates a new file at path `path`. If one already exists,
/// it is truncated. Outputs a Cybergrind Pattern File to that
/// path.
pub fn write_to_path<P: AsRef<Path>>(&self, path: P) -> Result<(), io::Error> {
let mut file = File::create(path)?;
self.write(&mut file)
}
/// Takes in a file and writes a Cybergrind Pattern to it.
pub fn write(&self, file: &mut File) -> Result<(), io::Error> {
let mut buf = Box::new([0; MAX_FILE_SIZE]);
let mut buf_idx = 0;
let mut tile_idx = 0;
for _row in 0..16 {
for _column in 0..16 {
let tile = self.tiles[tile_idx];
let height = tile.height();
// Actually sobbing right now
if (0..9).contains(&height) {
buf[buf_idx] = (height + 48) as u8;
buf_idx += 1;
} else {
let dear_god_why = format!("({height})");
for byte in dear_god_why.as_bytes() {
buf[buf_idx] = *byte;
buf_idx += 1;
}
}
tile_idx += 1;
}
buf[buf_idx] = b'\n';
buf_idx += 1;
}
buf[buf_idx] = b'\n';
buf_idx += 1;
tile_idx = 0;
for _row in 0..16 {
for _column in 0..16 {
let tile = self.tiles[tile_idx];
let prefab = tile.prefab();
let char = match prefab {
Prefab::None => b'0',
Prefab::Melee => b'n',
Prefab::Projectile => b'p',
Prefab::HideousMass => b'H',
Prefab::Stairs => b's',
Prefab::JumpPad => b'J',
};
buf[buf_idx] = char;
buf_idx += 1;
tile_idx += 1;
}
buf[buf_idx] = b'\n';
buf_idx += 1;
}
file.write_all(&buf[..buf_idx])
}
/// Takes in a series of bytes and tries
/// to turn them into a Cybergrind Pattern.
pub fn parse(bytes: &[u8]) -> Result<CyberGrindPattern, ParseError> {
let mut pattern = CyberGrindPattern::new();
let mut pat_idx = 0;
let mut buf_idx = 0;
let mut char = 0;
let mut line = 1;
for _row in 0..16 {
let mut column = 1;
for _column in 0..16 {
char = bytes[buf_idx];
if char == b'(' {
let mut is_negative = false;
let mut height: i8 = 0;
column += 1;
buf_idx += 1;
char = bytes[buf_idx];
while char != b')' {
if char == b'-' {
if is_negative {
return Err(ParseError {
line,
column,
char,
kind: ParseErrorType::DuplicateNegative,
});
} else {
is_negative = true;
}
} else {
if !(48..=57).contains(&char) {
return Err(ParseError {
line,
column,
char,
kind: ParseErrorType::InvalidHeightChar,
});
}
if char == 48 && height == 0 {
return Err(ParseError {
line,
column,
char,
kind: ParseErrorType::LeadingZero,
});
}
height *= 10;
height += char as i8 - 48;
}
column += 1;
buf_idx += 1;
char = bytes[buf_idx];
}
if is_negative {
height *= -1;
}
if !(-50..=50).contains(&height) {
return Err(ParseError {
line,
column,
char,
kind: ParseErrorType::InvalidHeightValue,
});
}
pattern[pat_idx].set_height(height);
} else {
if !(48..=57).contains(&char) {
return Err(ParseError {
line,
column,
char,
kind: ParseErrorType::InvalidHeightChar,
});
}
pattern[pat_idx].set_height(char as i8 - 48);
}
column += 1;
pat_idx += 1;
buf_idx += 1;
}
if bytes[buf_idx] != b'\n' {
return Err(ParseError {
line,
column,
char,
kind: ParseErrorType::ExpectedNewline,
});
}
buf_idx += 1;
line += 1;
}
if bytes[buf_idx] != b'\n' {
return Err(ParseError {
line,
column: 1,
char,
kind: ParseErrorType::ExpectedNewline,
});
}
buf_idx += 1;
line += 1;
pat_idx = 0;
for _row in 0..16 {
for column in 1..17 {
char = bytes[buf_idx];
let prefab = match Prefab::try_from(char) {
Ok(prefab) => prefab,
Err(kind) => {
return Err(ParseError {
line,
column,
char,
kind,
})
}
};
pattern[pat_idx].set_prefab(prefab);
pat_idx += 1;
buf_idx += 1;
}
if bytes[buf_idx] != b'\n' {
return Err(ParseError {
line,
column: 17,
char,
kind: ParseErrorType::ExpectedNewline,
});
}
buf_idx += 1;
line += 1;
}
Ok(pattern)
}
/// Takes in a string and tries to turn it into
/// a Cybergrind pattern.
pub fn parse_str(string: &str) -> Result<CyberGrindPattern, ParseError> {
Self::parse(string.as_bytes())
}
/// Takes in a string and tries to read
/// it as a Cybergrind pattern.
pub fn parse_file(file: &mut File) -> Result<CyberGrindPattern, IoError> {
let mut buf = Box::new([0; MAX_FILE_SIZE]);
let bytes_read = match file.read(buf.as_mut()) {
Ok(bytes_read) => bytes_read,
Err(err) => return Err(IoError::Io(err)),
};
match Self::parse(&buf[..bytes_read]) {
Ok(pat) => Ok(pat),
Err(e) => Err(IoError::Parse(e)),
}
}
/// Tries to open a file at path `path` and reads
/// it as a Cybergrind Patter.
pub fn parse_path<P: AsRef<Path>>(path: P) -> Result<CyberGrindPattern, IoError> {
let mut file = match File::open(path) {
Ok(file) => file,
Err(err) => return Err(IoError::Io(err)),
};
Self::parse_file(&mut file)
}
}