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
|
use arrayvec::ArrayVec;
use event_loop::{Chunk, Loop, PeerId};
use gamenet::msg::{Game, System};
use hexdump::hexdump_iter;
use itertools::Itertools;
use log::LogLevel;
use log::{log, log_enabled, warn};
use packer::with_packer;
use snapshot::format::Item as SnapItem;
use std::{fmt, path::PathBuf};
pub trait LoopExt: Loop {
fn send_system<'a, S: Into<System<'a>>>(&mut self, pid: PeerId, msg: S) {
fn inner<L: Loop + ?Sized>(msg: System, pid: PeerId, evloop: &mut L) {
let mut buf: ArrayVec<[u8; 2048]> = ArrayVec::new();
with_packer(&mut buf, |p| msg.encode(p).unwrap());
evloop.send(Chunk {
pid,
vital: true,
data: &buf,
})
}
inner(msg.into(), pid, self)
}
fn send_game<'a, G: Into<Game<'a>>>(&mut self, pid: PeerId, msg: G) {
fn inner<L: Loop + ?Sized>(msg: Game, pid: PeerId, evloop: &mut L) {
let mut buf: ArrayVec<[u8; 2048]> = ArrayVec::new();
with_packer(&mut buf, |p| msg.encode(p).unwrap());
evloop.send(Chunk {
pid,
vital: true,
data: &buf,
})
}
inner(msg.into(), pid, self)
}
}
impl<L: Loop> LoopExt for L {}
pub fn need_file(crc: i32, name: &str) -> bool {
let mut path = PathBuf::new();
path.push("/tmp/maps");
path.push(format!("{}_{:08x}.map", name, crc));
!path.exists()
}
pub fn hexdump(level: LogLevel, data: &[u8]) {
if log_enabled!(level) {
hexdump_iter(data).foreach(|s| log!(level, "{}", s));
}
}
pub struct Warn<'a>(pub &'a [u8]);
impl<'a, W: fmt::Debug> warn::Warn<W> for Warn<'a> {
fn warn(&mut self, w: W) {
warn!("{:?}", w);
hexdump(LogLevel::Warn, self.0);
}
}
#[derive(Debug)]
pub struct WarnSnap<'a>(pub SnapItem<'a>);
impl<'a, W: fmt::Debug> warn::Warn<W> for WarnSnap<'a> {
fn warn(&mut self, w: W) {
warn!("{:?} for {:?}", w, self.0);
}
}
pub fn check_dummy_map(name: &[u8], crc: u32, size: i32) -> bool {
if name != b"dummy" {
return false;
}
match (crc, size) {
(0xbeae0b9f, 549) => {}
(0x6c760ac4, 306) => {}
_ => warn!("unknown dummy map, crc={}, size={}", crc, size),
}
true
}
pub fn get_map_path(name: &str, crc: i32) -> PathBuf {
let mut path = PathBuf::new();
path.push("/tmp/maps");
path.push(format!("{}_{:08x}.map", name, crc));
path
}
|