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
pub use super::gameevent_gen::{GameEvent, GameEventType};
use crate::demo::data::MaybeUtf8String;
use crate::demo::message::gameevent::GameEventTypeId;
use crate::{GameEventError, Result, Stream};
use bitbuffer::{BitRead, BitWrite, BitWriteStream, LittleEndian};
use parse_display::Display;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GameEventDefinition {
    pub id: GameEventTypeId,
    pub event_type: GameEventType,
    pub entries: Vec<GameEventEntry>,
}

impl PartialEq<GameEventDefinition> for GameEventDefinition {
    fn eq(&self, other: &Self) -> bool {
        self.id.eq(&other.id)
    }
}

impl Eq for GameEventDefinition {}

impl PartialOrd for GameEventDefinition {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.id.partial_cmp(&other.id)
    }
}

impl Ord for GameEventDefinition {
    fn cmp(&self, other: &Self) -> Ordering {
        self.id.cmp(&other.id)
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GameEventEntry {
    pub name: String,
    pub kind: GameEventValueType,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(BitRead, BitWrite, Debug, Clone, Copy, PartialEq, Display, Serialize, Deserialize)]
#[discriminant_bits = 3]
pub enum GameEventValueType {
    None = 0,
    String = 1,
    Float = 2,
    Long = 3,
    Short = 4,
    Byte = 5,
    Boolean = 6,
    Local = 7,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum GameEventValue {
    String(MaybeUtf8String),
    Float(f32),
    Long(u32),
    Short(u16),
    Byte(u8),
    Boolean(bool),
    Local,
}

fn read_event_value(stream: &mut Stream, definition: &GameEventEntry) -> Result<GameEventValue> {
    Ok(match definition.kind {
        GameEventValueType::String => GameEventValue::String(stream.read()?),
        GameEventValueType::Float => GameEventValue::Float(stream.read()?),
        GameEventValueType::Long => GameEventValue::Long(stream.read()?),
        GameEventValueType::Short => GameEventValue::Short(stream.read()?),
        GameEventValueType::Byte => GameEventValue::Byte(stream.read()?),
        GameEventValueType::Boolean => GameEventValue::Boolean(stream.read()?),
        GameEventValueType::Local => GameEventValue::Local,
        GameEventValueType::None => return Err(GameEventError::NoneValue.into()),
    })
}

impl BitWrite<LittleEndian> for GameEventValue {
    fn write(&self, stream: &mut BitWriteStream<LittleEndian>) -> bitbuffer::Result<()> {
        match self {
            GameEventValue::String(value) => value.write(stream),
            GameEventValue::Float(value) => value.write(stream),
            GameEventValue::Long(value) => value.write(stream),
            GameEventValue::Short(value) => value.write(stream),
            GameEventValue::Byte(value) => value.write(stream),
            GameEventValue::Boolean(value) => value.write(stream),
            GameEventValue::Local => Ok(()),
        }
    }
}

impl GameEventValue {
    pub fn get_type(&self) -> GameEventValueType {
        match self {
            GameEventValue::String(_) => GameEventValueType::String,
            GameEventValue::Float(_) => GameEventValueType::Float,
            GameEventValue::Long(_) => GameEventValueType::Long,
            GameEventValue::Short(_) => GameEventValueType::Short,
            GameEventValue::Byte(_) => GameEventValueType::Byte,
            GameEventValue::Boolean(_) => GameEventValueType::Boolean,
            GameEventValue::Local => GameEventValueType::Local,
        }
    }
}

pub trait EventValue: Sized {
    fn value_type() -> GameEventValueType;
}

impl EventValue for String {
    fn value_type() -> GameEventValueType {
        GameEventValueType::String
    }
}

impl EventValue for MaybeUtf8String {
    fn value_type() -> GameEventValueType {
        GameEventValueType::String
    }
}

impl EventValue for f32 {
    fn value_type() -> GameEventValueType {
        GameEventValueType::Float
    }
}

impl EventValue for u32 {
    fn value_type() -> GameEventValueType {
        GameEventValueType::Long
    }
}

impl EventValue for u16 {
    fn value_type() -> GameEventValueType {
        GameEventValueType::Short
    }
}

impl EventValue for u8 {
    fn value_type() -> GameEventValueType {
        GameEventValueType::Byte
    }
}

impl EventValue for bool {
    fn value_type() -> GameEventValueType {
        GameEventValueType::Boolean
    }
}

impl EventValue for () {
    fn value_type() -> GameEventValueType {
        GameEventValueType::Local
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct RawGameEvent {
    pub event_type: GameEventType,
    pub values: Vec<GameEventValue>,
}

impl RawGameEvent {
    pub fn read(stream: &mut Stream, definition: &GameEventDefinition) -> Result<Self> {
        let mut values: Vec<GameEventValue> = Vec::with_capacity(definition.entries.len());
        for entry in &definition.entries {
            values.push(read_event_value(stream, entry)?);
        }

        Ok(RawGameEvent {
            event_type: definition.event_type.clone(),
            values,
        })
    }
}

impl BitWrite<LittleEndian> for RawGameEvent {
    fn write(&self, stream: &mut BitWriteStream<LittleEndian>) -> bitbuffer::Result<()> {
        for value in self.values.iter() {
            value.write(stream)?;
        }
        Ok(())
    }
}

pub trait FromRawGameEvent: Sized {
    fn from_raw_event(values: Vec<GameEventValue>) -> Result<Self>;
}

impl<T: FromRawGameEvent> FromRawGameEvent for Box<T> {
    fn from_raw_event(values: Vec<GameEventValue>) -> Result<Self> {
        Ok(Box::new(T::from_raw_event(values)?))
    }
}