-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathentity.rs
143 lines (129 loc) · 4.25 KB
/
entity.rs
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
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fs;
use test_case::test_case;
use fnv::FnvHashMap;
use std::collections::HashMap;
use tf_demo_parser::demo::data::DemoTick;
use tf_demo_parser::demo::message::packetentities::{EntityId, PacketEntity, UpdateType};
use tf_demo_parser::demo::message::Message;
use tf_demo_parser::demo::packet::datatable::{
ParseSendTable, SendTableName, ServerClass, ServerClassName,
};
use tf_demo_parser::demo::parser::MessageHandler;
use tf_demo_parser::demo::sendprop::{SendPropIdentifier, SendPropName, SendPropValue};
use tf_demo_parser::{Demo, DemoParser, MessageType, ParserState};
/// Compatible serialization with the js parser entity dumps
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum PVSCompat {
Preserve = 0,
Leave = 2,
Enter = 1,
Delete = 6,
}
impl From<UpdateType> for PVSCompat {
fn from(pvs: UpdateType) -> Self {
match pvs {
UpdateType::Preserve => PVSCompat::Preserve,
UpdateType::Leave => PVSCompat::Leave,
UpdateType::Enter => PVSCompat::Enter,
UpdateType::Delete => PVSCompat::Delete,
}
}
}
#[derive(PartialEq, Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct EntityDump {
tick: DemoTick,
server_class: ServerClassName,
id: EntityId,
props: HashMap<String, SendPropValue>,
pvs: PVSCompat,
}
impl EntityDump {
pub fn from_entity(
entity: PacketEntity,
tick: DemoTick,
classes: &[ServerClass],
prop_names: &FnvHashMap<SendPropIdentifier, (SendTableName, SendPropName)>,
state: &ParserState,
) -> Self {
EntityDump {
tick,
server_class: classes[usize::from(entity.server_class)].name.clone(),
id: entity.entity_index,
pvs: entity.update_type.into(),
props: entity
.props(state)
.map(|prop| {
let (table_name, prop_name) = &prop_names[&prop.identifier];
(format!("{}.{}", table_name, prop_name), prop.value)
})
.collect(),
}
}
}
struct EntityDumper {
entities: Vec<(DemoTick, PacketEntity)>,
prop_names: FnvHashMap<SendPropIdentifier, (SendTableName, SendPropName)>,
}
impl EntityDumper {
pub fn new() -> Self {
EntityDumper {
entities: Vec::with_capacity(128),
prop_names: FnvHashMap::default(),
}
}
}
impl MessageHandler for EntityDumper {
type Output = Vec<EntityDump>;
fn does_handle(message_type: MessageType) -> bool {
matches!(message_type, MessageType::PacketEntities)
}
fn handle_message(&mut self, message: &Message, tick: DemoTick, _parser_state: &ParserState) {
if let Message::PacketEntities(entity_message) = message {
self.entities.extend(
entity_message
.entities
.iter()
.map(|entity| (tick, entity.clone())),
)
}
}
fn handle_data_tables(
&mut self,
tables: &[ParseSendTable],
_server_classes: &[ServerClass],
_parser_state: &ParserState,
) {
for table in tables {
for prop_def in &table.props {
self.prop_names.insert(
prop_def.identifier(),
(table.name.clone(), prop_def.name.clone()),
);
}
}
}
fn into_output(self, state: &ParserState) -> Self::Output {
let prop_names = self.prop_names;
self.entities
.into_iter()
.map(|(tick, entity)| {
EntityDump::from_entity(entity, tick, &state.server_classes, &prop_names, state)
})
.collect()
}
}
#[test_case("test_data/small.dem")]
fn entity_test(input_file: &str) {
let file = fs::read(input_file).expect("Unable to read file");
let demo = Demo::new(&file);
let (_, entities) = DemoParser::new_with_analyser(demo.get_stream(), EntityDumper::new())
.parse()
.unwrap();
insta::with_settings!({sort_maps =>true}, {
insta::assert_json_snapshot!(entities);
});
}