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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use bitbuffer::BitRead;
use crate::demo::parser::MalformedSendPropDefinitionError;
use crate::demo::sendprop::{
RawSendPropDefinition, SendPropDefinition, SendPropFlag, SendPropIdentifier, SendPropType,
};
use crate::{Parse, ParseError, ParserState, Result, Stream};
use parse_display::{Display, FromStr};
use serde::{Deserialize, Serialize};
use std::cmp::min;
use std::convert::TryFrom;
use std::rc::Rc;
#[derive(BitRead, Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd, Display, FromStr)]
pub struct ClassId(u16);
impl From<u16> for ClassId {
fn from(int: u16) -> Self {
ClassId(int)
}
}
impl From<ClassId> for usize {
fn from(class: ClassId) -> Self {
class.0 as usize
}
}
#[derive(BitRead, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, Clone, Display)]
pub struct ServerClassName(Rc<String>);
impl ServerClassName {
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl From<String> for ServerClassName {
fn from(value: String) -> Self {
Self(Rc::new(value))
}
}
#[derive(BitRead, Debug, Clone)]
pub struct ServerClass {
pub id: ClassId,
pub name: ServerClassName,
pub data_table: SendTableName,
}
#[derive(
BitRead,
PartialEq,
Eq,
Hash,
Debug,
Serialize,
Deserialize,
Clone,
Display,
PartialOrd,
Ord,
Default,
)]
pub struct SendTableName(Rc<String>);
impl SendTableName {
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl From<String> for SendTableName {
fn from(value: String) -> Self {
Self(Rc::new(value))
}
}
#[derive(Debug, Clone)]
pub struct ParseSendTable {
pub name: SendTableName,
pub props: Vec<RawSendPropDefinition>,
pub needs_decoder: bool,
}
impl ParseSendTable {
fn parse(stream: &mut Stream, _state: &ParserState) -> Result<Self> {
let needs_decoder = stream.read()?;
let raw_name: String = stream.read()?;
let name: SendTableName = raw_name.into();
let prop_count = stream.read_int(10)?;
let mut array_element_prop = None;
let mut props = Vec::with_capacity(min(prop_count, 128));
for _ in 0..prop_count {
let prop: RawSendPropDefinition = RawSendPropDefinition::read(stream, name.clone())?;
if prop.flags.contains(SendPropFlag::InsideArray) {
if array_element_prop.is_some() || prop.flags.contains(SendPropFlag::ChangesOften) {
return Err(MalformedSendPropDefinitionError::ArrayChangesOften.into());
}
array_element_prop = Some(prop);
} else if let Some(array_element) = array_element_prop {
if prop.prop_type != SendPropType::Array {
return Err(MalformedSendPropDefinitionError::UntypedArray.into());
}
array_element_prop = None;
props.push(prop.with_array_property(array_element));
} else {
props.push(prop);
}
}
Ok(ParseSendTable {
name,
needs_decoder,
props,
})
}
}
impl ParseSendTable {
pub fn flatten_props(&self, tables: &[ParseSendTable]) -> Result<Vec<SendPropDefinition>> {
let mut flat = Vec::with_capacity(32);
self.get_all_props(tables, &self.get_excludes(tables), &mut flat)?;
let mut start = 0;
for i in 0..flat.len() {
if flat[i].parse_definition.changes_often() {
if i != start {
flat.swap(i, start);
}
start += 1;
}
}
Ok(flat)
}
fn get_excludes<'a>(&'a self, tables: &'a [ParseSendTable]) -> Vec<SendPropIdentifier> {
let mut excludes = Vec::with_capacity(8);
for prop in self.props.iter() {
if let Some(exclude_table) = prop.get_exclude_table() {
excludes.push(SendPropIdentifier::new(
exclude_table.as_str(),
prop.name.as_str(),
))
} else if let Some(table) = prop.get_data_table(tables) {
excludes.extend_from_slice(&table.get_excludes(tables));
}
}
excludes
}
fn get_all_props(
&self,
tables: &[ParseSendTable],
excludes: &[SendPropIdentifier],
props: &mut Vec<SendPropDefinition>,
) -> Result<()> {
let mut local_props = Vec::new();
self.get_all_props_iterator_props(tables, excludes, &mut local_props, props)?;
props.extend_from_slice(&local_props);
Ok(())
}
fn get_all_props_iterator_props(
&self,
tables: &[ParseSendTable],
excludes: &[SendPropIdentifier],
local_props: &mut Vec<SendPropDefinition>,
props: &mut Vec<SendPropDefinition>,
) -> Result<()> {
self.props
.iter()
.filter(|prop| !prop.is_exclude())
.filter(|prop| !excludes.iter().any(|exclude| *exclude == prop.identifier()))
.map(|prop| {
if let Some(table) = prop.get_data_table(tables) {
if prop.flags.contains(SendPropFlag::Collapsible) {
table.get_all_props_iterator_props(tables, excludes, local_props, props)?;
} else {
table.get_all_props(tables, excludes, props)?;
}
} else {
local_props.push(SendPropDefinition::try_from(prop)?);
}
Ok(())
})
.collect::<Result<()>>()?;
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct SendTable {
pub name: SendTableName,
pub needs_decoder: bool,
pub raw_props: Vec<RawSendPropDefinition>,
pub flattened_props: Vec<SendPropDefinition>,
}
#[derive(Debug)]
pub struct DataTablePacket {
pub tick: u32,
pub tables: Vec<ParseSendTable>,
pub server_classes: Vec<ServerClass>,
}
impl Parse<'_> for DataTablePacket {
fn parse(stream: &mut Stream, state: &ParserState) -> Result<Self> {
let tick = stream.read()?;
let len = stream.read_int::<usize>(32)?;
let mut packet_data = stream.read_bits(len * 8)?;
let mut tables = Vec::new();
while packet_data.read_bool()? {
let table = ParseSendTable::parse(&mut packet_data, state)?;
tables.push(table);
}
let server_class_count = packet_data.read_int(16)?;
let server_classes = packet_data.read_sized(server_class_count)?;
if packet_data.bits_left() > 7 {
Err(ParseError::DataRemaining(packet_data.bits_left()))
} else {
Ok(DataTablePacket {
tick,
tables,
server_classes,
})
}
}
}