author | unC0Rr |
Tue, 20 Nov 2018 16:18:42 +0100 | |
changeset 14259 | 9f4d75fa4a2c |
parent 14219 | 77b95406631e |
child 14260 | a239e4243cf9 |
permissions | -rw-r--r-- |
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
1 |
use nom::{Err::Error, *}; |
14214 | 2 |
use std::str; |
3 |
||
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
4 |
use super::messages::{ |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
5 |
ConfigEngineMessage::*, EngineMessage::*, KeystrokeAction::*, SyncedEngineMessage::*, |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
6 |
UnorderedEngineMessage::*, UnsyncedEngineMessage::*, *, |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
7 |
}; |
14215 | 8 |
|
14216 | 9 |
macro_rules! eof_slice ( |
10 |
($i:expr,) => ( |
|
11 |
{ |
|
12 |
if ($i).input_len() == 0 { |
|
13 |
Ok(($i, $i)) |
|
14 |
} else { |
|
15 |
Err(Error(error_position!($i, ErrorKind::Eof::<u32>))) |
|
16 |
} |
|
17 |
} |
|
18 |
); |
|
19 |
); |
|
14214 | 20 |
|
21 |
named!(unrecognized_message<&[u8], EngineMessage>, |
|
22 |
do_parse!(rest >> (Unknown)) |
|
23 |
); |
|
24 |
||
25 |
named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from)); |
|
26 |
||
14215 | 27 |
named!(length_without_timestamp<&[u8], usize>, |
28 |
map_opt!(rest_len, |l| if l > 2 { Some(l - 2) } else { None } ) |
|
29 |
); |
|
30 |
||
14214 | 31 |
named!(synced_message<&[u8], SyncedEngineMessage>, alt!( |
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
32 |
do_parse!(tag!("L") >> (Left(Press))) |
14216 | 33 |
| do_parse!(tag!("l") >> ( Left(Release) )) |
34 |
| do_parse!(tag!("R") >> ( Right(Press) )) |
|
35 |
| do_parse!(tag!("r") >> ( Right(Release) )) |
|
36 |
| do_parse!(tag!("U") >> ( Up(Press) )) |
|
37 |
| do_parse!(tag!("u") >> ( Up(Release) )) |
|
38 |
| do_parse!(tag!("D") >> ( Down(Press) )) |
|
39 |
| do_parse!(tag!("d") >> ( Down(Release) )) |
|
40 |
| do_parse!(tag!("Z") >> ( Precise(Press) )) |
|
41 |
| do_parse!(tag!("z") >> ( Precise(Release) )) |
|
42 |
| do_parse!(tag!("A") >> ( Attack(Press) )) |
|
43 |
| do_parse!(tag!("a") >> ( Attack(Release) )) |
|
44 |
| do_parse!(tag!("N") >> ( NextTurn )) |
|
45 |
| do_parse!(tag!("j") >> ( LongJump )) |
|
46 |
| do_parse!(tag!("J") >> ( HighJump )) |
|
47 |
| do_parse!(tag!("S") >> ( Switch )) |
|
48 |
| do_parse!(tag!(",") >> ( Skip )) |
|
49 |
| do_parse!(tag!("1") >> ( Timer(1) )) |
|
50 |
| do_parse!(tag!("2") >> ( Timer(2) )) |
|
51 |
| do_parse!(tag!("3") >> ( Timer(3) )) |
|
52 |
| do_parse!(tag!("4") >> ( Timer(4) )) |
|
53 |
| do_parse!(tag!("5") >> ( Timer(5) )) |
|
54 |
| do_parse!(tag!("p") >> x: be_i24 >> y: be_i24 >> ( Put(x, y) )) |
|
55 |
| do_parse!(tag!("P") >> x: be_i24 >> y: be_i24 >> ( CursorMove(x, y) )) |
|
56 |
| do_parse!(tag!("f") >> s: string_tail >> ( SyncedEngineMessage::TeamControlLost(s) )) |
|
57 |
| do_parse!(tag!("g") >> s: string_tail >> ( SyncedEngineMessage::TeamControlGained(s) )) |
|
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
58 |
| do_parse!(tag!("h") >> s: string_tail >> ( HogSay(s) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
59 |
| do_parse!(tag!("t") >> t: be_u8 >> ( Taunt(t) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
60 |
| do_parse!(tag!("w") >> w: be_u8 >> ( SetWeapon(w) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
61 |
| do_parse!(tag!("~") >> s: be_u8 >> ( Slot(s) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
62 |
| do_parse!(tag!("+") >> ( Heartbeat )) |
14214 | 63 |
)); |
64 |
||
65 |
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!( |
|
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
66 |
do_parse!(tag!("F") >> s: string_tail >> ( UnsyncedEngineMessage::TeamControlLost(s) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
67 |
| do_parse!(tag!("G") >> s: string_tail >> ( UnsyncedEngineMessage::TeamControlGained(s) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
68 |
)); |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
69 |
|
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
70 |
named!(unordered_message<&[u8], UnorderedEngineMessage>, alt!( |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
71 |
do_parse!(tag!("?") >> ( Ping )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
72 |
| do_parse!(tag!("!") >> ( Ping )) |
14259 | 73 |
| do_parse!(tag!("E") >> s: string_tail >> ( Error(s)) ) |
74 |
| do_parse!(tag!("W") >> s: string_tail >> ( Warning(s)) ) |
|
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
75 |
| do_parse!(tag!("s") >> s: string_tail >> ( ChatMessage(s)) ) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
76 |
| do_parse!(tag!("b") >> s: string_tail >> ( TeamMessage(s)) ) // TODO: wtf is the format |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
77 |
| do_parse!(tag!("M") >> s: string_tail >> ( GameSetupChecksum(s)) ) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
78 |
| do_parse!(tag!("o") >> ( StopSyncing )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
79 |
| do_parse!(tag!("I") >> ( PauseToggled )) |
14214 | 80 |
)); |
81 |
||
82 |
named!(config_message<&[u8], ConfigEngineMessage>, alt!( |
|
83 |
do_parse!(tag!("C") >> (ConfigRequest)) |
|
84 |
)); |
|
85 |
||
14216 | 86 |
named!(timestamped_message<&[u8], (SyncedEngineMessage, u16)>, |
87 |
do_parse!(msg: length_value!(length_without_timestamp, terminated!(synced_message, eof_slice!())) |
|
88 |
>> timestamp: be_u16 |
|
89 |
>> ((msg, timestamp)) |
|
90 |
) |
|
91 |
); |
|
92 |
||
14215 | 93 |
named!(unwrapped_message<&[u8], EngineMessage>, |
94 |
alt!( |
|
95 |
map!(timestamped_message, |(m, t)| Synced(m, t as u32)) |
|
96 |
| do_parse!(tag!("#") >> (Synced(TimeWrap, 65535))) |
|
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
97 |
| map!(unordered_message, |m| Unordered(m)) |
14215 | 98 |
| map!(unsynced_message, |m| Unsynced(m)) |
99 |
| map!(config_message, |m| Config(m)) |
|
100 |
| unrecognized_message |
|
101 |
)); |
|
102 |
||
103 |
named!(length_specifier<&[u8], u16>, alt!( |
|
104 |
verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64) |
|
105 |
| map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64) |
|
106 |
) |
|
107 |
); |
|
108 |
||
14214 | 109 |
named!(empty_message<&[u8], EngineMessage>, |
110 |
do_parse!(tag!("\0") >> (Empty)) |
|
111 |
); |
|
112 |
||
14215 | 113 |
named!(non_empty_message<&[u8], EngineMessage>, |
14216 | 114 |
length_value!(length_specifier, terminated!(unwrapped_message, eof_slice!()))); |
14214 | 115 |
|
116 |
named!(message<&[u8], EngineMessage>, alt!( |
|
117 |
empty_message |
|
118 |
| non_empty_message |
|
119 |
) |
|
120 |
); |
|
121 |
||
122 |
named!(pub extract_messages<&[u8], Vec<EngineMessage> >, many0!(complete!(message))); |
|
123 |
||
124 |
#[test] |
|
125 |
fn parse_length() { |
|
126 |
assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1))); |
|
127 |
assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0))); |
|
128 |
assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63))); |
|
129 |
assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64))); |
|
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
130 |
assert_eq!( |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
131 |
length_specifier(b"\xff\xff"), |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
132 |
Ok((&b""[..], EngineMessage::MAX_LEN)) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
133 |
); |
14214 | 134 |
} |
135 |
||
136 |
#[test] |
|
137 |
fn parse_synced_messages() { |
|
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
138 |
assert_eq!( |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
139 |
message(b"\x03L\x01\x02"), |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
140 |
Ok((&b""[..], Synced(Left(Press), 258))) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
141 |
); |
14216 | 142 |
assert_eq!(message(b"\x01#"), Ok((&b""[..], Synced(TimeWrap, 65535)))); |
14259 | 143 |
|
144 |
assert_eq!(message(&vec![9, b'p', 255, 133, 151, 1, 0, 2, 0, 0]), Ok((&b""[..], Synced(Put(-31337, 65538), 0)))); |
|
14214 | 145 |
} |
146 |
||
147 |
#[test] |
|
148 |
fn parse_unsynced_messages() { |
|
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
149 |
assert_eq!( |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
150 |
message(b"\x06shello"), |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
151 |
Ok((&b""[..], Unordered(ChatMessage(String::from("hello"))))) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
152 |
); |
14214 | 153 |
} |
154 |
||
155 |
#[test] |
|
156 |
fn parse_incorrect_messages() { |
|
157 |
assert_eq!(message(b"\x00"), Ok((&b""[..], Empty))); |
|
158 |
assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown))); |
|
14216 | 159 |
|
160 |
// garbage after correct message |
|
161 |
assert_eq!(message(b"\x04La\x01\x02"), Ok((&b""[..], Unknown))); |
|
14214 | 162 |
} |
163 |
||
164 |
#[test] |
|
165 |
fn parse_config_messages() { |
|
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
166 |
assert_eq!(message(b"\x01C"), Ok((&b""[..], Config(ConfigRequest)))); |
14214 | 167 |
} |
14219
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14216
diff
changeset
|
168 |
|
14214 | 169 |
#[test] |
170 |
fn parse_test_general() { |
|
171 |
assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc")))); |
|
172 |
} |