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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use super::{BestTimes, ElmaError};
use byteorder::{WriteBytesExt, LE};
use constants::TOP10_SIZE;
use nom::{le_i32, le_u32, le_u8};
use shared::TimeEntry;
use std::fs;
use std::path::Path;
use std::str;
use utils::{string_null_pad, write_top10};
const PLAYER_STRUCT_SIZE: usize = 116;
const PLAYERENTRY_PADDING: usize = 38;
const NUM_INTERNALS: usize = 54;
const PLAYER_NAME_SIZE: usize = 15;
const PLAYERENTRY_NAME_SIZE: usize = 16;
const LEVEL_NAME_SIZE: usize = 20;
const NUM_PLAYERS: usize = 50;
const NUM_LEVELS: usize = 90;
const STATE_START: u32 = 200;
const STATE_END: u32 = 123432221;
const STATE_END_ALT: u32 = 123432112;
const TOP10_ENTRIES: usize = 10;
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum PlayMode {
Single = 1,
Multi = 0,
}
impl Default for PlayMode {
fn default() -> Self {
PlayMode::Single
}
}
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum SoundOptimization {
Compatibility = 1,
BestQuality = 0,
}
impl Default for SoundOptimization {
fn default() -> Self {
SoundOptimization::BestQuality
}
}
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum VideoDetail {
Low = 0,
High = 1,
}
impl Default for VideoDetail {
fn default() -> Self {
VideoDetail::High
}
}
#[derive(Default, Debug, Clone, PartialEq)]
pub struct PlayerEntry {
pub name: String,
pub skipped_internals: Vec<bool>,
pub last_internal: i32,
pub selected_internal: i32,
}
#[derive(Default, Debug, Clone, PartialEq)]
pub struct PlayerKeys {
pub throttle: u32,
pub brake: u32,
pub rotate_right: u32,
pub rotate_left: u32,
pub change_direction: u32,
pub toggle_navigator: u32,
pub toggle_timer: u32,
pub toggle_show_hide: u32,
}
#[derive(Default, Debug, Clone, PartialEq)]
pub struct State {
pub version: u32,
pub times: Vec<BestTimes>,
pub players: Vec<PlayerEntry>,
pub player_a_name: String,
pub player_b_name: String,
pub player_a_keys: PlayerKeys,
pub player_b_keys: PlayerKeys,
pub sound_enabled: bool,
pub sound_optimization: SoundOptimization,
pub play_mode: PlayMode,
pub flagtag: bool,
pub swap_bikes: bool,
pub video_detail: VideoDetail,
pub animated_objects: bool,
pub animated_menus: bool,
pub inc_screen_size_key: u32,
pub dec_screen_size_key: u32,
pub screenshot_key: u32,
pub last_edited_lev_name: String,
pub last_played_external: String,
}
#[cfg_attr(rustfmt, rustfmt_skip)]
named!(playerkeys<PlayerKeys>,
do_parse!(
throttle: le_u32 >>
brake: le_u32 >>
rotate_right: le_u32 >>
rotate_left: le_u32 >>
change_direction: le_u32 >>
toggle_navigator: le_u32 >>
toggle_timer: le_u32 >>
toggle_show_hide: le_u32 >>
(PlayerKeys {
throttle,
brake,
rotate_right,
rotate_left,
change_direction,
toggle_navigator,
toggle_timer,
toggle_show_hide,
})
)
);
#[cfg_attr(rustfmt, rustfmt_skip)]
named!(playerentry<PlayerEntry>,
do_parse!(
name: apply!(null_padded_string, PLAYERENTRY_NAME_SIZE) >>
cond_reduce!(!name.is_empty(), take!(0)) >>
skipped_internals: fold_many_m_n!(NUM_INTERNALS, NUM_INTERNALS, le_u8, Vec::new(), |mut acc: Vec<_>, item: u8| { acc.push(to_bool(item as i32)); acc }) >>
take!(PLAYERENTRY_PADDING) >>
last_internal: le_i32 >>
selected_internal: le_i32 >>
(PlayerEntry {
name: name.to_string(),
skipped_internals,
last_internal,
selected_internal,
})
)
);
#[cfg_attr(rustfmt, rustfmt_skip)]
named_args!(null_padded_string(n: usize)<&str>,
do_parse!(
s: map_res!(take_while!(is_nonzero), str::from_utf8) >>
cond_reduce!(n >= s.len(), take!(n - s.len())) >>
(s)
)
);
#[cfg_attr(rustfmt, rustfmt_skip)]
named_args!(playernames(num: usize)<Vec<&str>>,
do_parse!(
names: fold_many_m_n!(num, num, apply!(null_padded_string, PLAYER_NAME_SIZE), Vec::new(), |mut acc: Vec<_>, item| { acc.push(item); acc }) >>
cond_reduce!(num <= TOP10_ENTRIES, count!(apply!(null_padded_string, PLAYER_NAME_SIZE), TOP10_ENTRIES - num)) >>
(names)
)
);
#[cfg_attr(rustfmt, rustfmt_skip)]
named!(top10part<Vec<TimeEntry>>,
do_parse!(
num_times: map!(le_u32, |x| x as usize) >>
times: fold_many_m_n!(num_times, num_times, le_i32, Vec::new(), |mut acc: Vec<_>, item: i32| { acc.push(item.into()); acc }) >>
cond_reduce!(num_times <= TOP10_ENTRIES, count!(le_i32, TOP10_ENTRIES - num_times)) >>
player_a_names: apply!(playernames, num_times) >>
player_b_names: apply!(playernames, num_times) >>
(izip!(times, player_a_names, player_b_names).map(|(time, a, b)| TimeEntry {names: (a.to_string(),b.to_string()), time}).collect())
)
);
#[cfg_attr(rustfmt, rustfmt_skip)]
named!(top10<BestTimes>,
do_parse!(
single: top10part >>
multi: top10part >>
(BestTimes {
single,
multi,
})
)
);
#[cfg_attr(rustfmt, rustfmt_skip)]
named!(boolean<bool>,
map!(le_i32, to_bool)
);
#[cfg_attr(rustfmt, rustfmt_skip)]
named!(parse_state<State>,
do_parse!(
version: verify!(le_u32, |x| x == STATE_START) >>
times: fold_many_m_n!(NUM_LEVELS, NUM_LEVELS, top10, Vec::new(), |mut acc: Vec<_>, item| { acc.push(item); acc }) >>
players: map_opt!(opt!(fold_many_m_n!(1, NUM_PLAYERS, playerentry, Vec::new(), |mut acc: Vec<_>, item| { acc.push(item); acc })), |x: Option<_>| x.or_else(|| Some(vec![]))) >>
cond_reduce!(players.len() <= NUM_PLAYERS, take!((NUM_PLAYERS - players.len()) * PLAYER_STRUCT_SIZE)) >>
num_players: le_u32 >>
cond_reduce!(players.len() == num_players as usize, take!(0)) >>
player_a_name: apply!(null_padded_string, PLAYER_NAME_SIZE) >>
player_b_name: apply!(null_padded_string, PLAYER_NAME_SIZE) >>
sound_enabled: boolean >>
sound_optimization: map!(le_i32, |i| if i == 0 {SoundOptimization::BestQuality} else {SoundOptimization::Compatibility}) >>
play_mode: map!(le_i32, |i| if i == 0 {PlayMode::Multi} else {PlayMode::Single}) >>
flagtag: boolean >>
swap_bikes: map!(boolean, |x| !x) >>
video_detail: map!(le_i32, |i| if i == 0 {VideoDetail::Low} else {VideoDetail::High}) >>
animated_objects: boolean >>
animated_menus: boolean >>
player_a_keys: playerkeys >>
player_b_keys: playerkeys >>
inc_screen_size_key: le_u32 >>
dec_screen_size_key: le_u32 >>
screenshot_key: le_u32 >>
last_edited_lev_name: apply!(null_padded_string, LEVEL_NAME_SIZE) >>
last_played_external: apply!(null_padded_string, LEVEL_NAME_SIZE) >>
verify!(le_u32, |x| x == STATE_END || x == STATE_END_ALT) >>
(State {
version,
times,
players,
player_a_name: player_a_name.to_string(),
player_b_name: player_b_name.to_string(),
player_a_keys,
player_b_keys,
sound_enabled,
sound_optimization,
play_mode,
flagtag,
swap_bikes,
video_detail,
animated_objects,
animated_menus,
dec_screen_size_key,
inc_screen_size_key,
last_edited_lev_name: last_edited_lev_name.to_string(),
last_played_external: last_played_external.to_string(),
screenshot_key,
})
)
);
fn to_bool(i: i32) -> bool {
if i == 0 {
false
} else {
true
}
}
fn is_nonzero(u: u8) -> bool {
to_bool(u as i32)
}
impl State {
pub fn new() -> Self {
State {
version: 200,
times: vec![
BestTimes {
single: vec![],
multi: vec![],
};
90
],
players: vec![],
player_a_name: "".to_string(),
player_b_name: "".to_string(),
player_a_keys: PlayerKeys {
throttle: 200,
brake: 208,
rotate_right: 205,
rotate_left: 203,
change_direction: 57,
toggle_navigator: 47,
toggle_timer: 20,
toggle_show_hide: 2,
},
player_b_keys: PlayerKeys {
throttle: 76,
brake: 80,
rotate_right: 81,
rotate_left: 79,
change_direction: 82,
toggle_navigator: 48,
toggle_timer: 21,
toggle_show_hide: 3,
},
sound_enabled: true,
sound_optimization: SoundOptimization::BestQuality,
play_mode: PlayMode::Single,
flagtag: false,
swap_bikes: false,
video_detail: VideoDetail::High,
animated_objects: true,
animated_menus: true,
inc_screen_size_key: 13,
dec_screen_size_key: 12,
screenshot_key: 23,
last_edited_lev_name: "".to_string(),
last_played_external: "".to_string(),
}
}
pub fn load<P: AsRef<Path>>(filename: P) -> Result<Self, ElmaError> {
let buffer = fs::read(filename)?;
State::parse(&buffer)
}
pub fn from_bytes<B: AsRef<[u8]>>(buffer: B) -> Result<Self, ElmaError> {
State::parse(buffer.as_ref())
}
fn parse(buffer: &[u8]) -> Result<Self, ElmaError> {
let mut v = buffer.to_vec();
{
let mut buf = &mut v[..];
crypt_whole_state(&mut buf);
}
match parse_state(&v) {
Ok((_, state)) => Ok(state),
Err(_) => Err(ElmaError::InvalidStateFile),
}
}
pub fn to_bytes(&self) -> Result<Vec<u8>, ElmaError> {
let mut buffer = vec![];
buffer.write_u32::<LE>(STATE_START)?;
for level in self.times.iter() {
let top10_bytes = write_top10(&level)?;
buffer.extend(top10_bytes);
}
buffer.extend(vec![0; (NUM_LEVELS - self.times.len()) * TOP10_SIZE]);
for p in &self.players {
buffer.extend(string_null_pad(&p.name, PLAYERENTRY_NAME_SIZE)?);
p.skipped_internals
.iter()
.for_each(|&x| buffer.push(x as u8));
buffer.extend(vec![0; NUM_INTERNALS - p.skipped_internals.len()]);
buffer.extend_from_slice(&[0; PLAYERENTRY_PADDING]);
buffer.write_i32::<LE>(p.last_internal)?;
buffer.write_i32::<LE>(p.selected_internal)?;
}
buffer.extend(vec![
0;
(NUM_PLAYERS - self.players.len()) * PLAYER_STRUCT_SIZE
]);
buffer.write_u32::<LE>(self.players.len() as u32)?;
buffer.extend(string_null_pad(&self.player_a_name, PLAYER_NAME_SIZE)?);
buffer.extend(string_null_pad(&self.player_b_name, PLAYER_NAME_SIZE)?);
buffer.write_i32::<LE>(self.sound_enabled as i32)?;
buffer.write_i32::<LE>(self.sound_optimization as i32)?;
buffer.write_i32::<LE>(self.play_mode as i32)?;
buffer.write_i32::<LE>(self.flagtag as i32)?;
buffer.write_i32::<LE>(!self.swap_bikes as i32)?;
buffer.write_i32::<LE>(self.video_detail as i32)?;
buffer.write_i32::<LE>(self.animated_objects as i32)?;
buffer.write_i32::<LE>(self.animated_menus as i32)?;
for k in [&self.player_a_keys, &self.player_b_keys].iter() {
buffer.write_u32::<LE>(k.throttle)?;
buffer.write_u32::<LE>(k.brake)?;
buffer.write_u32::<LE>(k.rotate_right)?;
buffer.write_u32::<LE>(k.rotate_left)?;
buffer.write_u32::<LE>(k.change_direction)?;
buffer.write_u32::<LE>(k.toggle_navigator)?;
buffer.write_u32::<LE>(k.toggle_timer)?;
buffer.write_u32::<LE>(k.toggle_show_hide)?;
}
buffer.write_u32::<LE>(self.inc_screen_size_key)?;
buffer.write_u32::<LE>(self.dec_screen_size_key)?;
buffer.write_u32::<LE>(self.screenshot_key)?;
buffer.extend(string_null_pad(
&self.last_edited_lev_name,
LEVEL_NAME_SIZE,
)?);
buffer.extend(string_null_pad(
&self.last_played_external,
LEVEL_NAME_SIZE,
)?);
buffer.write_u32::<LE>(STATE_END)?;
crypt_whole_state(&mut buffer[..]);
Ok(buffer)
}
pub fn save<P: AsRef<Path>>(&mut self, filename: P) -> Result<(), ElmaError> {
let buffer = self.to_bytes()?;
fs::write(filename, &buffer)?;
Ok(())
}
}
fn crypt_whole_state(buf: &mut [u8]) {
let state_pieces = [
4,
61920,
5800,
4,
PLAYER_NAME_SIZE,
PLAYER_NAME_SIZE,
4,
4,
4,
4,
4,
4,
4,
4,
32,
32,
4,
4,
4,
LEVEL_NAME_SIZE,
LEVEL_NAME_SIZE,
];
let mut curr = 0;
for p in state_pieces.iter() {
crypt_state(&mut buf[curr..curr + p]);
curr += p;
}
}
fn crypt_state(buffer: &mut [u8]) {
let mut ebp8: i16 = 0x17;
let mut ebp10: i16 = 0x2636;
for mut t in buffer.iter_mut() {
*t ^= (ebp8 & 0xFF) as u8;
ebp10 = ebp10.wrapping_add((ebp8.wrapping_rem(0xD3F)).wrapping_mul(0xD3F));
ebp8 = ebp10.wrapping_mul(0x1F).wrapping_add(0xD3F);
}
}
#[cfg(test)]
mod tests {
use super::null_padded_string;
use nom::simple_errors::Context::Code;
use nom::Err::Error;
use nom::Err::Incomplete;
use nom::ErrorKind::CondReduce;
use nom::Needed::Size;
#[test]
fn null_pad_string() {
assert_eq!(
null_padded_string(b"Elma\0\0\0\0\0\0", 10),
Ok((&[][..], "Elma"))
);
assert_eq!(
null_padded_string(b"Elma\0\0\0\0\0\0\0\0", 10),
Ok((&[0, 0][..], "Elma"))
);
assert_eq!(
null_padded_string(b"\0\0\0\0\0\0\0\0\0\0", 10),
Ok((&[][..], ""))
);
assert_eq!(
null_padded_string(b"Elma\0\0\0\0\0", 10),
Err(Incomplete(Size(6)))
);
assert_eq!(
null_padded_string(b"\0\0\0\0\0\0\0\0\0", 10),
Err(Incomplete(Size(10)))
);
assert_eq!(
null_padded_string(b"ElastoMani", 10),
Err(Incomplete(Size(1)))
);
assert_eq!(
null_padded_string(b"ElastoMania", 10),
Err(Incomplete(Size(1)))
);
assert_eq!(
null_padded_string(b"ElastoMania\0", 10),
Err(Error(Code(&[0][..], CondReduce)))
);
}
}