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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use crate::data::ATTRIBUTES;
use crate::network::{ActorId, Frame, NewActor, ObjectId, StreamId, UpdatedAttribute};
use fnv::FnvHashMap;
use std::error::Error;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::ops::Deref;
use std::str;

#[derive(PartialEq, Debug, Clone)]
pub enum ParseError {
    ParseError(&'static str, i32, Box<ParseError>),
    ZeroSize,
    Utf8Error(str::Utf8Error),
    TextTooLarge(i32),
    InsufficientData(i32, i32),
    UnexpectedProperty(String),
    CrcMismatch(u32, u32),
    CorruptReplay(String, Box<ParseError>),
    ListTooLarge(usize),
    NetworkError(Box<NetworkError>),
}

impl Display for ParseError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            ParseError::ZeroSize => write!(f, "A size of zero is not valid"),
            ParseError::Utf8Error(utf8_error) => {
                write!(f, "Unable decode data as utf8: {}", utf8_error)
            }
            ParseError::TextTooLarge(size) => write!(f, "Text of size {} is too large", size),
            ParseError::InsufficientData(expected, left) => write!(
                f,
                "Insufficient data. Expected {} bytes, but only {} left",
                expected, left
            ),
            ParseError::UnexpectedProperty(property) => {
                write!(f, "Did not expect a property of: {}", property)
            }
            ParseError::CrcMismatch(expected, found) => write!(
                f,
                "Crc mismatch. Expected {} but received {}",
                expected, found
            ),
            ParseError::CorruptReplay(section, _) => write!(
                f,
                "Failed to parse {} and crc check failed. Replay is corrupt",
                section
            ),
            ParseError::ListTooLarge(size) => write!(f, "list of size {} is too large", size),
            ParseError::ParseError(section, bytes_read, parse_error) => write!(
                f,
                "Could not decode replay {} at offset ({}): {}",
                section, bytes_read, parse_error
            ),
            ParseError::NetworkError(network_error) => write!(f, "{}", network_error),
        }
    }
}

impl Error for ParseError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ParseError::Utf8Error(utf8_error) => Some(utf8_error),
            ParseError::CorruptReplay(_, error) => Some(error),
            ParseError::ParseError(_, _, error) => Some(error),
            ParseError::NetworkError(error) => Some(error),
            _ => None,
        }
    }
}

impl From<str::Utf8Error> for ParseError {
    fn from(error: str::Utf8Error) -> Self {
        ParseError::Utf8Error(error)
    }
}

#[derive(PartialEq, Debug, Clone)]
pub enum AttributeError {
    NotEnoughDataFor(&'static str),
    UnrecognizedRemoteId(u8),
    Unimplemented,
    TooBigString(i32),
}

impl Error for AttributeError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}

impl Display for AttributeError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            AttributeError::NotEnoughDataFor(message) => {
                write!(f, "Not enough data to decode attribute {}", message)
            }
            AttributeError::UnrecognizedRemoteId(id) => {
                write!(f, "Unrecognized remote id of {}", id)
            }
            AttributeError::Unimplemented => write!(f, "Does not have an attribute implementation"),
            AttributeError::TooBigString(size) => write!(f, "Unexpected size for string: {}", size),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ContextObjectAttribute {
    obj_id: ObjectId,
    obj_name: String,
    prop_id: ObjectId,
    prop_name: String,
}

#[derive(PartialEq, Debug, Clone)]
pub struct FrameContext {
    pub objects: Vec<String>,
    pub object_attributes: FnvHashMap<ObjectId, FnvHashMap<StreamId, ObjectId>>,
    pub frames: Vec<Frame>,
    pub actors: FnvHashMap<ActorId, ObjectId>,
    pub new_actors: Vec<NewActor>,
    pub updated_actors: Vec<UpdatedAttribute>,
}

impl FrameContext {
    fn object_ind_to_string(&self, object_id: ObjectId) -> String {
        String::from(
            self.objects
                .get(usize::from(object_id))
                .map(Deref::deref)
                .unwrap_or("Out of bounds"),
        )
    }

    fn display_new_actor(&self, f: &mut fmt::Formatter<'_>, actor: &NewActor) -> fmt::Result {
        write!(
            f,
            "(id: {}, nameId: {}, objId: {}, objName: {}, initial trajectory: {:?})",
            actor.actor_id,
            actor
                .name_id
                .map(|x| x.to_string())
                .unwrap_or_else(|| String::from("<none>")),
            actor.object_id,
            self.object_ind_to_string(actor.object_id),
            actor.initial_trajectory
        )
    }

    fn display_update(&self, f: &mut fmt::Formatter<'_>, attr: &UpdatedAttribute) -> fmt::Result {
        let actor_entry = self.actors.get(&attr.actor_id);
        let actor_obj_name = actor_entry.and_then(|x| self.objects.get(usize::from(*x)));
        let stream_obj_name = self.objects.get(usize::from(attr.object_id));

        write!(
            f,
            "(actor stream id / object id / name: {} / ",
            attr.actor_id
        )?;
        if let Some(actor_id) = actor_entry {
            write!(f, "{} / ", actor_id)
        } else {
            write!(f, "<none> / ")
        }?;

        if let Some(name) = actor_obj_name {
            write!(f, "{}, ", name)
        } else {
            write!(f, "<none>, ")
        }?;

        write!(
            f,
            "attribute stream id / object id / name: {} / {} / ",
            attr.stream_id, attr.object_id
        )?;

        if let Some(name) = stream_obj_name {
            write!(f, "{}", name)
        } else {
            write!(f, "<none>")
        }?;

        write!(f, ", attribute: {:?})", attr.attribute)
    }

    fn most_recent_frame_with_data(&self) -> Option<(usize, &Frame)> {
        self.frames
            .iter()
            .enumerate()
            .rev()
            .find(|(_, frame)| !frame.updated_actors.is_empty() || !frame.new_actors.is_empty())
    }
}

impl fmt::Display for FrameContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "on frame: {}, ", self.frames.len())?;
        if let Some(updated) = self.updated_actors.last() {
            write!(f, "last updated actor: ")?;
            self.display_update(f, updated)
        } else if let Some(new) = self.new_actors.last() {
            write!(f, "last new actor: ")?;
            self.display_new_actor(f, new)
        } else if let Some((frame_idx, frame)) = self.most_recent_frame_with_data() {
            write!(f, "backtracking to frame {}, ", frame_idx)?;
            if let Some(updated) = frame.updated_actors.last() {
                write!(f, "last updated actor: ")?;
                self.display_update(f, updated)
            } else if let Some(new) = frame.new_actors.last() {
                write!(f, "last new actor: ")?;
                self.display_new_actor(f, new)
            } else {
                write!(f, "it didn't decode anything")
            }
        } else {
            write!(f, "it didn't decode anything")
        }
    }
}

#[derive(PartialEq, Debug, Clone)]
pub enum FrameError {
    NotEnoughDataFor(&'static str),
    TimeOutOfRange {
        time: f32,
    },
    DeltaOutOfRange {
        delta: f32,
    },
    ObjectIdOutOfRange {
        obj: ObjectId,
    },
    MissingActor {
        actor: ActorId,
    },
    MissingCache {
        actor: ActorId,
        actor_object: ObjectId,
    },
    MissingAttribute {
        actor: ActorId,
        actor_object: ObjectId,
        attribute_stream: StreamId,
    },
    AttributeError {
        actor: ActorId,
        actor_object: ObjectId,
        attribute_stream: StreamId,
        error: AttributeError,
    },
}

impl FrameError {
    fn contextualize(&self, f: &mut fmt::Formatter<'_>, context: &FrameContext) -> fmt::Result {
        match self {
            FrameError::MissingCache { actor_object, .. } => {
                if let Some(name) = context.objects.get(usize::from(*actor_object)) {
                    write!(f, "({})", name)
                } else {
                    Ok(())
                }
            }
            FrameError::MissingAttribute {
                actor_object,
                attribute_stream,
                ..
            }
            | FrameError::AttributeError {
                actor_object,
                attribute_stream,
                ..
            } => {
                let actor_obj_name = context
                    .objects
                    .get(usize::from(*actor_object))
                    .map(|x| x.to_string())
                    .unwrap_or_else(|| String::from("<none>"));

                let objs_with_attr = context
                    .object_attributes
                    .iter()
                    .flat_map(|(obj_id, attrs)| {
                        attrs
                            .iter()
                            .map(move |(attr_id, attr_obj_id)| (obj_id, attr_obj_id, attr_id))
                    })
                    .filter(|&(_, _, stream_id)| stream_id == attribute_stream)
                    .collect::<Vec<_>>();

                let obj = context
                    .object_attributes
                    .get(actor_object)
                    .and_then(|x| x.get(attribute_stream));

                if let Some(attr_obj_id) = obj {
                    if let Some(name) = context.objects.get(usize::from(*attr_obj_id)) {
                        if let Some(attr) = ATTRIBUTES.get(name.as_str()) {
                            write!(
                                f,
                                "found attribute {} ({:?}) on {} in network cache data. ",
                                name, attr, actor_obj_name
                            )?;
                        } else {
                            write!(f, "found attribute {} (unknown to boxcars) on {} in network cache data. This is likely due to a rocket league update or an atypical replay. File a bug report!", name, actor_obj_name)?;

                            // No need for further context so we return early.
                            return Ok(());
                        }
                    } else {
                        write!(f, "found attribute on {} in network cache data, but not in replay object data, ", actor_obj_name)?;
                    }
                } else {
                    write!(
                        f,
                        "did not find attribute id ({}) on {} in object hierarchy: ",
                        attribute_stream, actor_obj_name
                    )?;
                }

                let mut obj_attr_names = objs_with_attr
                    .iter()
                    .filter_map(|&(obj_id, attr_obj_id, _)| {
                        context
                            .objects
                            .get(usize::from(*obj_id))
                            .and_then(|obj_name| {
                                context
                                    .objects
                                    .get(usize::from(*attr_obj_id))
                                    .map(|attr_name| (obj_name, attr_name))
                            })
                    })
                    .collect::<Vec<_>>();

                obj_attr_names.sort();
                obj_attr_names.dedup();

                let mut unknown_attributes = obj_attr_names
                    .iter()
                    .map(|(_obj_name, attr_name)| attr_name)
                    .filter(|x| !ATTRIBUTES.contains_key(x.as_str()))
                    .cloned()
                    .cloned()
                    .collect::<Vec<_>>();

                unknown_attributes.sort();
                unknown_attributes.dedup();

                let stringify_names = obj_attr_names
                    .iter()
                    .map(|(obj_name, attr_name)| format!("({}: {})", obj_name, attr_name))
                    .collect::<Vec<_>>();

                write!(f, "searching all attributes with the same stream id, ")?;
                write!(
                    f,
                    "unknown attributes: [{}], ",
                    unknown_attributes.join(", ")
                )?;
                write!(
                    f,
                    "all attributes with that stream id: [{}]. ",
                    stringify_names.join(", ")
                )?;
                Ok(())
            }
            _ => Ok(()),
        }
    }
}

impl Error for FrameError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            FrameError::AttributeError { error, .. } => Some(error),
            _ => None,
        }
    }
}

impl Display for FrameError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            FrameError::NotEnoughDataFor(message) => write!(f, "not enough data to decode {}", message),
            FrameError::TimeOutOfRange {time} => write!(f, "time is out of range: {}", time),
            FrameError::DeltaOutOfRange {delta} => write!(f, "delta is out of range: {}", delta),
            FrameError::ObjectIdOutOfRange {obj} => write!(f, "new actor object id out of range: {}", obj),
            FrameError::MissingActor {actor} => write!(f, "attribute update references unknown actor: {}", actor),
            FrameError::MissingCache {actor, actor_object} => write!(f, "no known attributes found for actor id / object id: {} / {}", actor, actor_object),
            FrameError::MissingAttribute {actor, actor_object, attribute_stream} => write!(f, "attribute unknown or not implemented: actor id / actor object id / attribute id: {} / {} / {}", actor, actor_object, attribute_stream),
            FrameError::AttributeError {actor, actor_object, attribute_stream, error} => write!(f, "attribute decoding error encountered: {} for actor id / actor object id / attribute id: {} / {} / {}", error, actor, actor_object, attribute_stream),
        }
    }
}

#[derive(PartialEq, Debug, Clone)]
pub enum NetworkError {
    NotEnoughDataFor(&'static str),
    ObjectIdOutOfRange(ObjectId),
    StreamTooLargeIndex(i32, i32),
    MissingParentClass(String, String),
    ParentHasNoAttributes(ObjectId, ObjectId),
    FrameError(FrameError, Box<FrameContext>),
    TooManyFrames(i32),
}

impl Error for NetworkError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            NetworkError::FrameError(err, _) => Some(err),
            _ => None,
        }
    }
}

impl Display for NetworkError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            NetworkError::NotEnoughDataFor(message) => {
                write!(f, "Not enough data to decode {}", message)
            }
            NetworkError::ObjectIdOutOfRange(id) => write!(f, "Object Id of {} exceeds range", id),
            NetworkError::StreamTooLargeIndex(steam_id, object_index) => write!(
                f,
                "Stream id of {} references out of range object index: {}",
                steam_id, object_index
            ),
            NetworkError::MissingParentClass(obj, parent) => write!(
                f,
                "Replay contained object: {} but not the parent class: {}",
                obj, parent
            ),
            NetworkError::ParentHasNoAttributes(parent_id, object_id) => write!(
                f,
                "Parent id of {} for object id of {} was not recognized to have attributes",
                parent_id, object_id
            ),
            NetworkError::TooManyFrames(size) => write!(f, "Too many frames to decode: {}", size),
            NetworkError::FrameError(err, context) => {
                write!(f, "Error decoding frame: {}. ", err)?;
                err.contextualize(f, context)?;
                write!(f, " Context: {}", context)
            }
        }
    }
}