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
243
244
245
246
247
248
249
250
251
use network::Frame;
use serde::ser::{SerializeMap, SerializeSeq};
use serde::{Serialize, Serializer};
use std::borrow::Cow;
use std::collections::HashMap;
#[derive(Serialize, PartialEq, Debug, Clone)]
pub struct Replay<'a> {
pub header_size: i32,
pub header_crc: i32,
pub major_version: i32,
pub minor_version: i32,
pub net_version: Option<i32>,
pub game_type: Cow<'a, str>,
#[serde(serialize_with = "pair_vec")]
pub properties: Vec<(&'a str, HeaderProp<'a>)>,
pub content_size: i32,
pub content_crc: i32,
pub network_frames: Option<NetworkFrames>,
pub levels: Vec<Cow<'a, str>>,
pub keyframes: Vec<KeyFrame>,
pub debug_info: Vec<DebugInfo<'a>>,
pub tick_marks: Vec<TickMark<'a>>,
pub packages: Vec<Cow<'a, str>>,
pub objects: Vec<Cow<'a, str>>,
pub names: Vec<Cow<'a, str>>,
pub class_indices: Vec<ClassIndex<'a>>,
pub net_cache: Vec<ClassNetCache>,
}
#[derive(Serialize, PartialEq, Debug, Clone)]
pub struct NetworkFrames {
pub frames: Vec<Frame>,
}
#[derive(Serialize, PartialEq, Debug, Clone)]
pub struct TickMark<'a> {
pub description: Cow<'a, str>,
pub frame: i32,
}
#[derive(Serialize, PartialEq, Debug, Clone, Copy)]
pub struct KeyFrame {
pub time: f32,
pub frame: i32,
pub position: i32,
}
#[derive(PartialEq, Debug, Clone)]
pub enum HeaderProp<'a> {
Array(Vec<Vec<(&'a str, HeaderProp<'a>)>>),
Bool(bool),
Byte,
Float(f32),
Int(i32),
Name(Cow<'a, str>),
QWord(i64),
Str(Cow<'a, str>),
}
#[derive(Serialize, PartialEq, Debug, Clone)]
pub struct DebugInfo<'a> {
pub frame: i32,
pub user: Cow<'a, str>,
pub text: Cow<'a, str>,
}
#[derive(Serialize, PartialEq, Debug, Clone)]
pub struct ClassIndex<'a> {
pub class: &'a str,
pub index: i32,
}
#[derive(Serialize, PartialEq, Debug, Clone, Copy)]
pub struct CacheProp {
pub object_ind: i32,
pub stream_id: i32,
}
#[derive(Serialize, PartialEq, Debug, Clone)]
pub struct ClassNetCache {
pub object_ind: i32,
pub parent_id: i32,
pub cache_id: i32,
pub properties: Vec<CacheProp>,
}
fn pair_vec<K, V, S>(inp: &[(K, V)], serializer: S) -> Result<S::Ok, S::Error>
where
K: Serialize,
V: Serialize,
S: Serializer,
{
let mut state = serializer.serialize_map(Some(inp.len()))?;
for &(ref key, ref val) in inp.iter() {
state.serialize_key(key)?;
state.serialize_value(val)?;
}
state.end()
}
impl<'a> Serialize for HeaderProp<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
HeaderProp::Array(ref x) => {
let mut state = serializer.serialize_seq(Some(x.len()))?;
for inner in x {
let mut els = HashMap::new();
for &(key, ref val) in inner.iter() {
els.insert(key, val);
}
state.serialize_element(&els)?;
}
state.end()
}
HeaderProp::Bool(ref x) => serializer.serialize_bool(*x),
HeaderProp::Byte => serializer.serialize_u8(0),
HeaderProp::Float(ref x) => serializer.serialize_f32(*x),
HeaderProp::Int(ref x) => serializer.serialize_i32(*x),
HeaderProp::QWord(ref x) => serializer.serialize_i64(*x),
HeaderProp::Name(ref x) | HeaderProp::Str(ref x) => serializer.serialize_str(x),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde;
use serde_json;
use std;
use std::borrow::Cow;
fn to_json<T: serde::Serialize>(input: &T) -> std::string::String {
serde_json::to_string(input).unwrap()
}
#[test]
fn serialize_header_array() {
let data = vec![
vec![
("frame", HeaderProp::Int(441)),
(
"PlayerName",
HeaderProp::Str(Cow::Borrowed("rust is awesome")),
),
],
vec![
("frame", HeaderProp::Int(1738)),
("PlayerName", HeaderProp::Str(Cow::Borrowed("rusty"))),
],
];
let actual = to_json(&HeaderProp::Array(data));
assert!(actual.contains("\"PlayerName\":\"rust is awesome\""));
assert!(actual.contains("\"PlayerName\":\"rusty\""));
assert!(actual.contains("\"frame\":441"));
assert!(actual.contains("\"frame\":1738"));
}
#[test]
fn serialize_header_bool() {
assert_eq!(to_json(&HeaderProp::Bool(false)), "false");
assert_eq!(to_json(&HeaderProp::Bool(true)), "true");
}
#[test]
fn serialize_header_numbers() {
assert_eq!(to_json(&HeaderProp::Byte), "0");
assert_eq!(to_json(&HeaderProp::QWord(10)), "10");
assert_eq!(to_json(&HeaderProp::Float(10.2)), "10.2");
assert_eq!(to_json(&HeaderProp::Int(11)), "11");
}
#[test]
fn serialize_header_str() {
let val = "hello world";
assert_eq!(
to_json(&HeaderProp::Str(Cow::Borrowed(val))),
"\"hello world\""
);
assert_eq!(
to_json(&HeaderProp::Name(Cow::Borrowed(val))),
"\"hello world\""
);
}
}