author | S-D- |
Thu, 19 Sep 2019 23:39:40 +0300 | |
changeset 15438 | 5fcdc072d706 |
parent 15305 | ae8e14d14596 |
child 15831 | c3971b38bbfa |
permissions | -rw-r--r-- |
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
1 |
use crate::messages::{ |
14235
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
2 |
ConfigEngineMessage::*, EngineMessage::*, KeystrokeAction::*, SyncedEngineMessage::*, |
14281 | 3 |
UnorderedEngineMessage::*, *, |
14235
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
4 |
}; |
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
5 |
use nom::{Err::Error, *}; |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
6 |
use std::str; |
14231 | 7 |
|
14232 | 8 |
macro_rules! eof_slice ( |
9 |
($i:expr,) => ( |
|
10 |
{ |
|
11 |
if ($i).input_len() == 0 { |
|
12 |
Ok(($i, $i)) |
|
13 |
} else { |
|
14 |
Err(Error(error_position!($i, ErrorKind::Eof::<u32>))) |
|
15 |
} |
|
16 |
} |
|
17 |
); |
|
18 |
); |
|
14230 | 19 |
|
20 |
named!(unrecognized_message<&[u8], EngineMessage>, |
|
21 |
do_parse!(rest >> (Unknown)) |
|
22 |
); |
|
23 |
||
24 |
named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from)); |
|
25 |
||
14231 | 26 |
named!(length_without_timestamp<&[u8], usize>, |
27 |
map_opt!(rest_len, |l| if l > 2 { Some(l - 2) } else { None } ) |
|
28 |
); |
|
29 |
||
14230 | 30 |
named!(synced_message<&[u8], SyncedEngineMessage>, alt!( |
14235
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
31 |
do_parse!(tag!("L") >> (Left(Press))) |
14232 | 32 |
| do_parse!(tag!("l") >> ( Left(Release) )) |
33 |
| do_parse!(tag!("R") >> ( Right(Press) )) |
|
34 |
| do_parse!(tag!("r") >> ( Right(Release) )) |
|
35 |
| do_parse!(tag!("U") >> ( Up(Press) )) |
|
36 |
| do_parse!(tag!("u") >> ( Up(Release) )) |
|
37 |
| do_parse!(tag!("D") >> ( Down(Press) )) |
|
38 |
| do_parse!(tag!("d") >> ( Down(Release) )) |
|
39 |
| do_parse!(tag!("Z") >> ( Precise(Press) )) |
|
40 |
| do_parse!(tag!("z") >> ( Precise(Release) )) |
|
41 |
| do_parse!(tag!("A") >> ( Attack(Press) )) |
|
42 |
| do_parse!(tag!("a") >> ( Attack(Release) )) |
|
43 |
| do_parse!(tag!("N") >> ( NextTurn )) |
|
44 |
| do_parse!(tag!("j") >> ( LongJump )) |
|
45 |
| do_parse!(tag!("J") >> ( HighJump )) |
|
46 |
| do_parse!(tag!("S") >> ( Switch )) |
|
47 |
| do_parse!(tag!(",") >> ( Skip )) |
|
48 |
| do_parse!(tag!("1") >> ( Timer(1) )) |
|
49 |
| do_parse!(tag!("2") >> ( Timer(2) )) |
|
50 |
| do_parse!(tag!("3") >> ( Timer(3) )) |
|
51 |
| do_parse!(tag!("4") >> ( Timer(4) )) |
|
52 |
| do_parse!(tag!("5") >> ( Timer(5) )) |
|
53 |
| do_parse!(tag!("p") >> x: be_i24 >> y: be_i24 >> ( Put(x, y) )) |
|
54 |
| do_parse!(tag!("P") >> x: be_i24 >> y: be_i24 >> ( CursorMove(x, y) )) |
|
55 |
| do_parse!(tag!("f") >> s: string_tail >> ( SyncedEngineMessage::TeamControlLost(s) )) |
|
56 |
| do_parse!(tag!("g") >> s: string_tail >> ( SyncedEngineMessage::TeamControlGained(s) )) |
|
14235
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
57 |
| do_parse!(tag!("t") >> t: be_u8 >> ( Taunt(t) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
58 |
| do_parse!(tag!("w") >> w: be_u8 >> ( SetWeapon(w) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
59 |
| do_parse!(tag!("~") >> s: be_u8 >> ( Slot(s) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
60 |
| do_parse!(tag!("+") >> ( Heartbeat )) |
14230 | 61 |
)); |
62 |
||
63 |
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!( |
|
14235
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
64 |
do_parse!(tag!("F") >> s: string_tail >> ( UnsyncedEngineMessage::TeamControlLost(s) )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
65 |
| do_parse!(tag!("G") >> s: string_tail >> ( UnsyncedEngineMessage::TeamControlGained(s) )) |
15285 | 66 |
| do_parse!(tag!("h") >> s: string_tail >> ( UnsyncedEngineMessage::HogSay(s) )) |
67 |
| do_parse!(tag!("s") >> s: string_tail >> ( UnsyncedEngineMessage::ChatMessage(s)) ) |
|
68 |
| do_parse!(tag!("b") >> s: string_tail >> ( UnsyncedEngineMessage::TeamMessage(s)) ) // TODO: wtf is the format |
|
14235
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
69 |
)); |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
70 |
|
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
71 |
named!(unordered_message<&[u8], UnorderedEngineMessage>, alt!( |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
72 |
do_parse!(tag!("?") >> ( Ping )) |
14294
645e4591361f
Send seed to engine to receive random map previews
unC0Rr
parents:
14281
diff
changeset
|
73 |
| do_parse!(tag!("!") >> ( Pong )) |
14276 | 74 |
| do_parse!(tag!("E") >> s: string_tail >> ( UnorderedEngineMessage::Error(s)) ) |
14275 | 75 |
| do_parse!(tag!("W") >> s: string_tail >> ( Warning(s)) ) |
14235
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
76 |
| do_parse!(tag!("M") >> s: string_tail >> ( GameSetupChecksum(s)) ) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
77 |
| do_parse!(tag!("o") >> ( StopSyncing )) |
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
78 |
| do_parse!(tag!("I") >> ( PauseToggled )) |
14230 | 79 |
)); |
80 |
||
81 |
named!(config_message<&[u8], ConfigEngineMessage>, alt!( |
|
82 |
do_parse!(tag!("C") >> (ConfigRequest)) |
|
14294
645e4591361f
Send seed to engine to receive random map previews
unC0Rr
parents:
14281
diff
changeset
|
83 |
| do_parse!(tag!("eseed ") >> s: string_tail >> ( SetSeed(s)) ) |
14230 | 84 |
)); |
85 |
||
14232 | 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 |
||
14231 | 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))) |
|
14235
77b95406631e
Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents:
14232
diff
changeset
|
97 |
| map!(unordered_message, |m| Unordered(m)) |
14231 | 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 |
||
14230 | 109 |
named!(empty_message<&[u8], EngineMessage>, |
110 |
do_parse!(tag!("\0") >> (Empty)) |
|
111 |
); |
|
112 |
||
14231 | 113 |
named!(non_empty_message<&[u8], EngineMessage>, |
14232 | 114 |
length_value!(length_specifier, terminated!(unwrapped_message, eof_slice!()))); |
14230 | 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 |
||
14281 | 124 |
pub fn extract_message(buf: &[u8]) -> Option<(usize, EngineMessage)> { |
125 |
let parse_result = message(buf); |
|
126 |
match parse_result { |
|
127 |
Ok((tail, msg)) => { |
|
128 |
let consumed = buf.len() - tail.len(); |
|
129 |
||
130 |
Some((consumed, msg)) |
|
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
131 |
} |
14281 | 132 |
Err(Err::Incomplete(_)) => None, |
133 |
Err(Err::Error(_)) | Err(Err::Failure(_)) => unreachable!(), |
|
134 |
} |
|
135 |
} |
|
136 |
||
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
137 |
#[cfg(test)] |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
138 |
mod tests { |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
139 |
use crate::messages::UnsyncedEngineMessage::*; |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
140 |
use crate::parser::*; |
14230 | 141 |
|
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
142 |
#[test] |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
143 |
fn parse_length() { |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
144 |
assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
145 |
assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
146 |
assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
147 |
assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
148 |
assert_eq!( |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
149 |
length_specifier(b"\xff\xff"), |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
150 |
Ok((&b""[..], EngineMessage::MAX_LEN)) |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
151 |
); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
152 |
} |
14281 | 153 |
|
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
154 |
#[test] |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
155 |
fn parse_synced_messages() { |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
156 |
assert_eq!( |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
157 |
message(b"\x03L\x01\x02"), |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
158 |
Ok((&b""[..], Synced(Left(Press), 258))) |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
159 |
); |
14275 | 160 |
|
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
161 |
assert_eq!(message(b"\x01#"), Ok((&b""[..], Synced(TimeWrap, 65535)))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
162 |
|
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
163 |
assert_eq!( |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
164 |
message(&vec![9, b'p', 255, 133, 151, 1, 0, 2, 0, 0]), |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
165 |
Ok((&b""[..], Synced(Put(-31337, 65538), 0))) |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
166 |
); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
167 |
} |
14230 | 168 |
|
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
169 |
#[test] |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
170 |
fn parse_unsynced_messages() { |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
171 |
assert_eq!( |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
172 |
message(b"\x06shello"), |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
173 |
Ok((&b""[..], Unsynced(ChatMessage(String::from("hello"))))) |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
174 |
); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
175 |
} |
14230 | 176 |
|
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
177 |
#[test] |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
178 |
fn parse_incorrect_messages() { |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
179 |
assert_eq!(message(b"\x00"), Ok((&b""[..], Empty))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
180 |
assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
181 |
|
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
182 |
// garbage after correct message |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
183 |
assert_eq!(message(b"\x04La\x01\x02"), Ok((&b""[..], Unknown))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
184 |
} |
14232 | 185 |
|
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
186 |
#[test] |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
187 |
fn parse_config_messages() { |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
188 |
assert_eq!(message(b"\x01C"), Ok((&b""[..], Config(ConfigRequest)))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
189 |
} |
14230 | 190 |
|
15305
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
191 |
#[test] |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
192 |
fn parse_test_general() { |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
193 |
assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc")))); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
194 |
|
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
195 |
assert_eq!(extract_message(b"\x02#"), None); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
196 |
assert_eq!( |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
197 |
extract_message(b"\x01#"), |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
198 |
Some((2, Synced(TimeWrap, 65535))) |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
199 |
); |
ae8e14d14596
Move messages queue to hedgewars-engine-messages lib
unc0rr
parents:
15285
diff
changeset
|
200 |
} |
14230 | 201 |
} |