author | unc0rr |
Wed, 24 Oct 2018 00:08:23 +0200 | |
changeset 13953 | e98e2fc556a7 |
parent 13952 | d12ca66054aa |
child 13955 | a857cd1cc3f0 |
permissions | -rw-r--r-- |
13952 | 1 |
#[macro_use] |
2 |
extern crate log; |
|
3 |
extern crate argparse; |
|
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
4 |
extern crate base64; |
13952 | 5 |
extern crate dirs; |
6 |
extern crate ini; |
|
7 |
extern crate netbuf; |
|
8 |
extern crate stderrlog; |
|
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
9 |
extern crate tempfile; |
13952 | 10 |
|
11 |
use argparse::{ArgumentParser, Store}; |
|
12 |
use ini::Ini; |
|
13 |
use netbuf::Buf; |
|
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
14 |
use std::io::Write; |
13952 | 15 |
use std::net::TcpStream; |
16 |
use std::process::Command; |
|
17 |
use std::str::FromStr; |
|
18 |
||
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
19 |
type CheckError = Box<std::error::Error>; |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
20 |
|
13952 | 21 |
fn extract_packet(buf: &mut Buf) -> Option<netbuf::Buf> { |
22 |
let packet_end = (&buf[..]).windows(2).position(|window| window == b"\n\n")?; |
|
23 |
||
24 |
let mut tail = buf.split_off(packet_end); |
|
25 |
||
26 |
std::mem::swap(&mut tail, buf); |
|
27 |
||
28 |
buf.consume(2); |
|
29 |
||
30 |
Some(tail) |
|
31 |
} |
|
32 |
||
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
33 |
fn check(executable: &str, data_prefix: &str, buffer: &[u8]) -> Result<Vec<Vec<u8>>, CheckError> { |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
34 |
let mut replay = tempfile::NamedTempFile::new()?; |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
35 |
|
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
36 |
for line in buffer.split(|b| *b == '\n' as u8) { |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
37 |
replay.write(&base64::decode(line)?); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
38 |
} |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
39 |
|
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
40 |
let temp_file_path = replay.path(); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
41 |
|
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
42 |
let mut home_dir = dirs::home_dir().unwrap(); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
43 |
home_dir.push(".hedgewars"); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
44 |
|
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
45 |
let output = Command::new(executable) |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
46 |
.arg("--user-prefix") |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
47 |
.arg(&home_dir) |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
48 |
.arg("--prefix") |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
49 |
.arg(data_prefix) |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
50 |
.arg("--nomusic") |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
51 |
.arg("--nosound") |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
52 |
.arg("--stats-only") |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
53 |
.arg(temp_file_path) |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
54 |
.output()?; |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
55 |
|
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
56 |
let mut result = Vec::new(); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
57 |
for line in output.stdout.split(|b| *b == '\n' as u8) { |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
58 |
result.push(line.to_vec()); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
59 |
} |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
60 |
|
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
61 |
Ok(result) |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
62 |
} |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
63 |
|
13952 | 64 |
fn connect_and_run( |
65 |
username: &str, |
|
66 |
password: &str, |
|
67 |
protocol_number: u32, |
|
68 |
executable: &str, |
|
69 |
data_prefix: &str, |
|
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
70 |
) -> Result<(), CheckError> { |
13952 | 71 |
info!("Connecting..."); |
72 |
||
73 |
let mut stream = TcpStream::connect("hedgewars.org:46631")?; |
|
74 |
stream.set_nonblocking(false)?; |
|
75 |
||
76 |
let mut buf = Buf::new(); |
|
77 |
||
78 |
loop { |
|
79 |
buf.read_from(&mut stream)?; |
|
80 |
||
81 |
while let Some(msg) = extract_packet(&mut buf) { |
|
82 |
if msg[..].starts_with(b"CONNECTED") { |
|
83 |
info!("Connected"); |
|
84 |
let p = format!( |
|
85 |
"CHECKER\n{}\n{}\n{}\n\n", |
|
86 |
protocol_number, username, password |
|
87 |
); |
|
88 |
stream.write(p.as_bytes())?; |
|
89 |
} else if msg[..].starts_with(b"PING") { |
|
90 |
stream.write(b"PONG\n\n")?; |
|
91 |
} else if msg[..].starts_with(b"LOGONPASSED") { |
|
92 |
info!("Logged in"); |
|
93 |
stream.write(b"READY\n\n")?; |
|
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
94 |
} else if msg[..].starts_with(b"REPLAY") { |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
95 |
info!("Got a replay"); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
96 |
let result = check(executable, data_prefix, &msg[7..])?; |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
97 |
|
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
98 |
debug!( |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
99 |
"Check result: [{}]", |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
100 |
String::from_utf8_lossy(&result.join(&(',' as u8))) |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
101 |
); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
102 |
|
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
103 |
stream.write(&result.join(&('\n' as u8)))?; |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
104 |
stream.write(b"\n\n")?; |
13952 | 105 |
} else if msg[..].starts_with(b"BYE") { |
106 |
warn!("Received BYE: {}", String::from_utf8_lossy(&msg[..])); |
|
107 |
return Ok(()); |
|
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
108 |
} else if msg[..].starts_with(b"ERROR") { |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
109 |
warn!("Received ERROR: {}", String::from_utf8_lossy(&msg[..])); |
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
110 |
return Ok(()); |
13952 | 111 |
} else { |
112 |
warn!( |
|
113 |
"Unknown protocol command: {}", |
|
114 |
String::from_utf8_lossy(&msg[..]) |
|
115 |
) |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
||
13953
e98e2fc556a7
Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents:
13952
diff
changeset
|
121 |
fn get_protocol_number(executable: &str) -> std::io::Result<u32> { |
13952 | 122 |
let output = Command::new(executable).arg("--protocol").output()?; |
123 |
||
124 |
Ok(u32::from_str(&String::from_utf8(output.stdout).unwrap().as_str()).unwrap_or(55)) |
|
125 |
} |
|
126 |
||
127 |
fn main() { |
|
128 |
stderrlog::new() |
|
129 |
.verbosity(3) |
|
130 |
.timestamp(stderrlog::Timestamp::Second) |
|
131 |
.module(module_path!()) |
|
132 |
.init() |
|
133 |
.unwrap(); |
|
134 |
||
135 |
let mut frontend_settings = dirs::home_dir().unwrap(); |
|
136 |
frontend_settings.push(".hedgewars/settings.ini"); |
|
137 |
||
138 |
let i = Ini::load_from_file(frontend_settings.to_str().unwrap()).unwrap(); |
|
139 |
let username = i.get_from(Some("net"), "nick").unwrap(); |
|
140 |
let password = i.get_from(Some("net"), "passwordhash").unwrap(); |
|
141 |
||
142 |
let mut exe = "/usr/local/bin/hwengine".to_string(); |
|
143 |
let mut prefix = "/usr/local/share/hedgewars/Data".to_string(); |
|
144 |
{ |
|
145 |
let mut ap = ArgumentParser::new(); |
|
146 |
ap.set_description("Game replay checker for hedgewars."); |
|
147 |
ap.refer(&mut exe) |
|
148 |
.add_option(&["--exe"], Store, "Path to hwengine executable"); |
|
149 |
ap.refer(&mut prefix) |
|
150 |
.add_option(&["--prefix"], Store, "Path main Data dir"); |
|
151 |
ap.parse_args_or_exit(); |
|
152 |
} |
|
153 |
||
154 |
info!("Executable: {}", exe); |
|
155 |
info!("Data dir: {}", prefix); |
|
156 |
||
157 |
let protocol_number = get_protocol_number(&exe.as_str()).unwrap_or_default(); |
|
158 |
||
159 |
info!("Using protocol number {}", protocol_number); |
|
160 |
||
161 |
connect_and_run(&username, &password, protocol_number, &exe, &prefix); |
|
162 |
} |
|
163 |
||
164 |
#[cfg(test)] |
|
165 |
#[test] |
|
166 |
fn test() { |
|
167 |
let mut buf = Buf::new(); |
|
168 |
buf.extend(b"Hell"); |
|
169 |
if let Some(_) = extract_packet(&mut buf) { |
|
170 |
assert!(false) |
|
171 |
} |
|
172 |
||
173 |
buf.extend(b"o\n\nWorld"); |
|
174 |
||
175 |
let packet2 = extract_packet(&mut buf).unwrap(); |
|
176 |
assert_eq!(&buf[..], b"World"); |
|
177 |
assert_eq!(&packet2[..], b"Hello"); |
|
178 |
||
179 |
if let Some(_) = extract_packet(&mut buf) { |
|
180 |
assert!(false) |
|
181 |
} |
|
182 |
} |