audio-code 0.3.0

A simple, high level game engine for audio games: 3D sound, speech, virtual menus, physics, timing and online play. No graphics required.
Documentation
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
//! [`Packet`]: one message between players, and its binary encoding.

use glam::{Vec2, Vec3};

use super::PeerId;

/// One value inside a [`Packet`]. Made automatically by [`Packet::with`] from numbers,
/// `bool`, text, [`Vec2`], [`Vec3`], [`PeerId`] and byte vectors.
#[derive(Debug, Clone, PartialEq)]
pub enum Arg {
    /// A whole number (all integer types).
    Int(i64),
    /// A decimal number (`f32` and `f64`).
    Float(f64),
    /// `true` or `false`.
    Bool(bool),
    /// Text.
    Text(String),
    /// A 2D vector.
    Vec2(Vec2),
    /// A 3D vector, e.g. a position.
    Vec3(Vec3),
    /// Raw bytes for your own formats.
    Bytes(Vec<u8>),
}

macro_rules! int_args {
    ($($t:ty),*) => {$(
        impl From<$t> for Arg {
            fn from(v: $t) -> Self {
                Arg::Int(v as i64)
            }
        }
    )*};
}
int_args!(i8, i16, i32, i64, u8, u16, u32, u64, usize, isize);

impl From<PeerId> for Arg {
    fn from(v: PeerId) -> Self {
        Arg::Int(v.number() as i64)
    }
}

impl From<f32> for Arg {
    fn from(v: f32) -> Self {
        Arg::Float(v as f64)
    }
}

impl From<f64> for Arg {
    fn from(v: f64) -> Self {
        Arg::Float(v)
    }
}

impl From<bool> for Arg {
    fn from(v: bool) -> Self {
        Arg::Bool(v)
    }
}

impl From<&str> for Arg {
    fn from(v: &str) -> Self {
        Arg::Text(v.to_string())
    }
}

impl From<String> for Arg {
    fn from(v: String) -> Self {
        Arg::Text(v)
    }
}

impl From<&String> for Arg {
    fn from(v: &String) -> Self {
        Arg::Text(v.clone())
    }
}

impl From<Vec2> for Arg {
    fn from(v: Vec2) -> Self {
        Arg::Vec2(v)
    }
}

impl From<Vec3> for Arg {
    fn from(v: Vec3) -> Self {
        Arg::Vec3(v)
    }
}

impl From<Vec<u8>> for Arg {
    fn from(v: Vec<u8>) -> Self {
        Arg::Bytes(v)
    }
}

impl From<&[u8]> for Arg {
    fn from(v: &[u8]) -> Self {
        Arg::Bytes(v.to_vec())
    }
}

/// One message between players: a kind (what it is about) and a list of values.
///
/// ```
/// use audio_code::prelude::*;
///
/// let packet = Packet::new("move").with(vec3(3.0, 4.0, 0.0)).with(90.0).with("running");
/// assert_eq!(packet.kind(), "move");
/// assert_eq!(packet.vec3(0), vec3(3.0, 4.0, 0.0));
/// assert_eq!(packet.float(1), 90.0);
/// assert_eq!(packet.text(2), "running");
/// // Missing or other values give a neutral result instead of a crash:
/// assert_eq!(packet.int(7), 0);
/// ```
///
/// Text converts into a packet without values, so `ctx.net.send_all("ready")` works.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Packet {
    kind: String,
    args: Vec<Arg>,
}

impl Packet {
    /// A packet of the given kind without values.
    pub fn new(kind: impl Into<String>) -> Self {
        Self { kind: kind.into(), args: Vec::new() }
    }

    /// Adds a value (builder style).
    pub fn with(mut self, value: impl Into<Arg>) -> Self {
        self.args.push(value.into());
        self
    }

    /// Adds a value.
    pub fn push(&mut self, value: impl Into<Arg>) {
        self.args.push(value.into());
    }

    /// What the packet is about, e.g. `"move"` or `"chat"`.
    pub fn kind(&self) -> &str {
        &self.kind
    }

    /// Whether the packet is of this kind.
    pub fn is(&self, kind: &str) -> bool {
        self.kind == kind
    }

    /// All values.
    pub fn args(&self) -> &[Arg] {
        &self.args
    }

    /// Number of values.
    pub fn len(&self) -> usize {
        self.args.len()
    }

    /// Whether the packet has no values.
    pub fn is_empty(&self) -> bool {
        self.args.is_empty()
    }

    /// The value at `index`, if there is one.
    pub fn get(&self, index: usize) -> Option<&Arg> {
        self.args.get(index)
    }

    /// The value at `index` as a whole number. Decimals are cut off, `bool` gives 0 or 1,
    /// text is parsed. Anything else gives 0.
    pub fn int(&self, index: usize) -> i64 {
        match self.get(index) {
            Some(Arg::Int(v)) => *v,
            Some(Arg::Float(v)) => *v as i64,
            Some(Arg::Bool(v)) => *v as i64,
            Some(Arg::Text(v)) => v.trim().parse().unwrap_or(0),
            _ => 0,
        }
    }

    /// The value at `index` as a decimal number. Whole numbers and `bool` convert, text is
    /// parsed. Anything else gives 0.0.
    pub fn float(&self, index: usize) -> f32 {
        match self.get(index) {
            Some(Arg::Int(v)) => *v as f32,
            Some(Arg::Float(v)) => *v as f32,
            Some(Arg::Bool(v)) => *v as u8 as f32,
            Some(Arg::Text(v)) => v.trim().parse().unwrap_or(0.0),
            _ => 0.0,
        }
    }

    /// The value at `index` as `bool`. Numbers are `true` when not zero, text when it is
    /// `"true"`. Anything else gives `false`.
    pub fn bool(&self, index: usize) -> bool {
        match self.get(index) {
            Some(Arg::Bool(v)) => *v,
            Some(Arg::Int(v)) => *v != 0,
            Some(Arg::Float(v)) => *v != 0.0,
            Some(Arg::Text(v)) => v.trim() == "true",
            _ => false,
        }
    }

    /// The value at `index` as text, or `""` if it is not text.
    pub fn text(&self, index: usize) -> &str {
        match self.get(index) {
            Some(Arg::Text(v)) => v,
            _ => "",
        }
    }

    /// The value at `index` as a 2D vector. A 3D vector loses its z. Anything else gives
    /// `Vec2::ZERO`.
    pub fn vec2(&self, index: usize) -> Vec2 {
        match self.get(index) {
            Some(Arg::Vec2(v)) => *v,
            Some(Arg::Vec3(v)) => v.truncate(),
            _ => Vec2::ZERO,
        }
    }

    /// The value at `index` as a 3D vector. A 2D vector gets z = 0. Anything else gives
    /// `Vec3::ZERO`.
    pub fn vec3(&self, index: usize) -> Vec3 {
        match self.get(index) {
            Some(Arg::Vec3(v)) => *v,
            Some(Arg::Vec2(v)) => v.extend(0.0),
            _ => Vec3::ZERO,
        }
    }

    /// The value at `index` as a player id (stored with `.with(peer)`). Anything that is not
    /// a valid number gives [`PeerId::SERVER`].
    pub fn peer(&self, index: usize) -> PeerId {
        PeerId::from_number(u32::try_from(self.int(index)).unwrap_or(0))
    }

    /// The value at `index` as bytes, or an empty slice if it is not bytes.
    pub fn bytes(&self, index: usize) -> &[u8] {
        match self.get(index) {
            Some(Arg::Bytes(v)) => v,
            _ => &[],
        }
    }

    pub(crate) fn encode(&self, out: &mut Vec<u8>) {
        put_str(out, &self.kind);
        out.extend_from_slice(&(self.args.len() as u32).to_le_bytes());
        for arg in &self.args {
            match arg {
                Arg::Int(v) => {
                    out.push(0);
                    out.extend_from_slice(&v.to_le_bytes());
                }
                Arg::Float(v) => {
                    out.push(1);
                    out.extend_from_slice(&v.to_le_bytes());
                }
                Arg::Bool(v) => {
                    out.push(2);
                    out.push(*v as u8);
                }
                Arg::Text(v) => {
                    out.push(3);
                    put_str(out, v);
                }
                Arg::Vec2(v) => {
                    out.push(4);
                    for c in v.to_array() {
                        out.extend_from_slice(&c.to_le_bytes());
                    }
                }
                Arg::Vec3(v) => {
                    out.push(5);
                    for c in v.to_array() {
                        out.extend_from_slice(&c.to_le_bytes());
                    }
                }
                Arg::Bytes(v) => {
                    out.push(6);
                    put_bytes(out, v);
                }
            }
        }
    }

    pub(crate) fn decode(r: &mut Reader) -> Option<Self> {
        let kind = r.str()?;
        let count = r.u32()? as usize;
        // Every value takes at least two bytes, so a huge count is rejected early.
        if count > r.remaining() / 2 {
            return None;
        }
        let mut args = Vec::with_capacity(count);
        for _ in 0..count {
            args.push(match r.u8()? {
                0 => Arg::Int(i64::from_le_bytes(r.array()?)),
                1 => Arg::Float(f64::from_le_bytes(r.array()?)),
                2 => Arg::Bool(r.u8()? != 0),
                3 => Arg::Text(r.str()?),
                4 => Arg::Vec2(Vec2::new(r.f32()?, r.f32()?)),
                5 => Arg::Vec3(Vec3::new(r.f32()?, r.f32()?, r.f32()?)),
                6 => Arg::Bytes(r.bytes()?.to_vec()),
                _ => return None,
            });
        }
        Some(Self { kind, args })
    }
}

impl From<&str> for Packet {
    fn from(kind: &str) -> Self {
        Packet::new(kind)
    }
}

impl From<String> for Packet {
    fn from(kind: String) -> Self {
        Packet::new(kind)
    }
}

impl From<&Packet> for Packet {
    fn from(packet: &Packet) -> Self {
        packet.clone()
    }
}

pub(crate) fn put_bytes(out: &mut Vec<u8>, bytes: &[u8]) {
    out.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
    out.extend_from_slice(bytes);
}

pub(crate) fn put_str(out: &mut Vec<u8>, text: &str) {
    put_bytes(out, text.as_bytes());
}

/// Reads values from a received frame. Every method returns `None` when the data ends early.
pub(crate) struct Reader<'a> {
    data: &'a [u8],
}

impl<'a> Reader<'a> {
    pub(crate) fn new(data: &'a [u8]) -> Self {
        Self { data }
    }

    pub(crate) fn remaining(&self) -> usize {
        self.data.len()
    }

    fn take(&mut self, n: usize) -> Option<&'a [u8]> {
        if n > self.data.len() {
            return None;
        }
        let (head, tail) = self.data.split_at(n);
        self.data = tail;
        Some(head)
    }

    fn array<const N: usize>(&mut self) -> Option<[u8; N]> {
        self.take(N)?.try_into().ok()
    }

    pub(crate) fn u8(&mut self) -> Option<u8> {
        Some(self.take(1)?[0])
    }

    pub(crate) fn u16(&mut self) -> Option<u16> {
        Some(u16::from_le_bytes(self.array()?))
    }

    pub(crate) fn u32(&mut self) -> Option<u32> {
        Some(u32::from_le_bytes(self.array()?))
    }

    pub(crate) fn u64(&mut self) -> Option<u64> {
        Some(u64::from_le_bytes(self.array()?))
    }

    fn f32(&mut self) -> Option<f32> {
        Some(f32::from_le_bytes(self.array()?))
    }

    pub(crate) fn bytes(&mut self) -> Option<&'a [u8]> {
        let len = self.u32()? as usize;
        self.take(len)
    }

    pub(crate) fn str(&mut self) -> Option<String> {
        Some(String::from_utf8_lossy(self.bytes()?).into_owned())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn round_trip() {
        let packet = Packet::new("all")
            .with(-5)
            .with(2.5)
            .with(true)
            .with("hä?")
            .with(Vec2::new(1.0, 2.0))
            .with(Vec3::new(1.0, 2.0, 3.0))
            .with(vec![1u8, 2, 3]);
        let mut bytes = Vec::new();
        packet.encode(&mut bytes);
        let decoded = Packet::decode(&mut Reader::new(&bytes)).unwrap();
        assert_eq!(decoded, packet);
        assert!(Packet::decode(&mut Reader::new(&bytes[..bytes.len() - 1])).is_none());
    }

    #[test]
    fn lenient_getters() {
        let p = Packet::new("x").with(3).with("4.5").with(Vec2::new(1.0, 2.0));
        assert_eq!(p.float(0), 3.0);
        assert_eq!(p.float(1), 4.5);
        assert_eq!(p.int(1), 0);
        assert_eq!(p.text(0), "");
        assert_eq!(p.vec3(2), Vec3::new(1.0, 2.0, 0.0));
        assert!(p.bool(0));
        assert!(!p.bool(9));
    }
}