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
use insim_core::binrw::{self, binrw, BinRead, BinWrite};

use crate::identifiers::{ConnectionId, RequestId};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// Used within the [Cim] packet to indicate the mode.
pub enum CimMode {
    /// Not in a special mode
    Normal(CimSubModeNormal),

    /// Options screen
    Options,

    /// Host options screen
    HostOptions,

    /// Garage screen
    Garage(CimSubModeGarage),

    /// Vehicle select screen
    CarSelect,

    /// Track select screen
    TrackSelect,

    /// Shift+U mode
    ShiftU {
        /// ShiftU submode
        submode: CimSubModeShiftU,

        /// SelType is the selected object type or zero if unselected
        /// It may be an AXO_x as in ObjectInfo or one of these:
        /// const int MARSH_IS_CP = 252; // insim checkpoint
        /// const int MARSH_IS_AREA = 253; // insim circle
        /// const int MARSH_MARSHAL = 254; // restricted area
        /// const int MARSH_ROUTE = 255; // route checker
        seltype: u8,
    },
}

impl Default for CimMode {
    fn default() -> Self {
        Self::Normal(CimSubModeNormal::Normal)
    }
}

#[repr(u8)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// CimMode::Normal, submode
pub enum CimSubModeNormal {
    #[default]
    /// Not in a special mode
    Normal = 0,

    /// Showing wheel temperature
    WheelTemps = 1,

    /// Showing wheel damaage
    WheelDamage = 2,

    /// Showing live settings
    LiveSettings = 3,

    /// Show pit instructions
    PitInstructions = 4,
}

impl From<u8> for CimSubModeNormal {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Normal,
            1 => Self::WheelTemps,
            2 => Self::WheelDamage,
            3 => Self::LiveSettings,
            4 => Self::PitInstructions,
            _ => {
                unreachable!(
                    "Unhandled CimSubModeNormal. Perhaps a programming error or protocol update?"
                )
            },
        }
    }
}

#[repr(u8)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// CimMode::Garage, submode
pub enum CimSubModeGarage {
    #[default]
    /// Info tab of setup screen
    Info = 0,

    /// Colours tab of setup screen
    Colours = 1,

    /// Braking and traction control tab of setup screen
    BrakeTC = 2,

    /// Suspension tab of setup screen
    Susp = 3,

    /// Steering tab of setup screen
    Steer = 4,

    /// Drive / gear tab of setup screen
    Drive = 5,

    /// Tyres
    Tyres = 6,

    /// Aero tab of setup screen
    Aero = 7,

    /// Passengers tab of setup screen
    Pass = 8,
}

impl From<u8> for CimSubModeGarage {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Info,
            1 => Self::Colours,
            2 => Self::BrakeTC,
            3 => Self::Susp,
            4 => Self::Steer,
            5 => Self::Drive,
            6 => Self::Tyres,
            7 => Self::Aero,
            8 => Self::Pass,
            _ => {
                unreachable!(
                    "Unhandled CimSubModeGarage. Perhaps a programming error or protocol update?"
                )
            },
        }
    }
}

#[repr(u8)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// CimMode::ShiftU, submode
pub enum CimSubModeShiftU {
    #[default]
    /// No buttons displayed
    Plain = 0,

    /// Buttons displayed, but not editing
    Buttons = 1,

    /// Editing mode
    Edit = 2,
}

impl From<u8> for CimSubModeShiftU {
    fn from(value: u8) -> Self {
        match value {
            1 => Self::Buttons,
            2 => Self::Edit,
            _ => Self::Plain,
        }
    }
}

impl BinRead for CimMode {
    type Args<'a> = ();

    fn read_options<R: std::io::Read + std::io::Seek>(
        reader: &mut R,
        endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<Self> {
        let pos = reader.stream_position()?;
        let discrim = u8::read_options(reader, endian, ())?;
        let submode = u8::read_options(reader, endian, ())?;
        let seltype = u8::read_options(reader, endian, ())?;

        let res = match discrim {
            0 => Self::Normal(submode.into()),
            1 => Self::Options,
            2 => Self::HostOptions,
            3 => Self::Garage(submode.into()),
            4 => Self::CarSelect,
            5 => Self::TrackSelect,
            6 => Self::ShiftU {
                submode: submode.into(),
                seltype,
            },
            _ => {
                return Err(binrw::Error::BadMagic {
                    pos,
                    found: Box::new(submode),
                })
            },
        };

        Ok(res)
    }
}

impl BinWrite for CimMode {
    type Args<'a> = ();

    fn write_options<W: std::io::Write + std::io::Seek>(
        &self,
        writer: &mut W,
        endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<()> {
        let (discrim, submode, seltype) = match self {
            CimMode::Normal(submode) => (0u8, *submode as u8, 0u8),
            CimMode::Options => (1u8, 0u8, 0u8),
            CimMode::HostOptions => (2u8, 0u8, 0u8),
            CimMode::Garage(submode) => (3u8, *submode as u8, 0u8),
            CimMode::CarSelect => (4u8, 0u8, 0u8),
            CimMode::TrackSelect => (5u8, 0u8, 0u8),
            CimMode::ShiftU {
                submode: mode,
                seltype,
            } => (6u8, *mode as u8, *seltype),
        };

        discrim.write_options(writer, endian, ())?;
        submode.write_options(writer, endian, ())?;
        seltype.write_options(writer, endian, ())?;
        Ok(())
    }
}

#[binrw]
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// Connection Interface Mode
pub struct Cim {
    /// Non-zero if the packet is a packet request or a reply to a request
    pub reqi: RequestId,

    /// connection's unique id (0 = local)
    pub ucid: ConnectionId,

    /// Mode & submode
    #[brw(pad_after = 1)]
    pub mode: CimMode,
}