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
use crate::io::{Endian, ReadExt as _, WriteExt as _};
use std::io::{Read, Write};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceVersionCommand {
    pub cmd: u32,
    pub cmdsize: u32,
    pub version: Version,
}

impl SourceVersionCommand {
    pub const TYPE: u32 = 0x2a;

    pub const SIZE: u32 = 0x10;

    pub fn read_from_in<R: Read>(read: &mut R, endian: Endian) -> Self {
        let cmd = read.read_u32_in(endian);
        assert_eq!(cmd, Self::TYPE);

        let cmdsize = read.read_u32_in(endian);
        assert_eq!(cmdsize, Self::SIZE);

        let version = Version(read.read_u64_in(endian));

        SourceVersionCommand {
            cmd,
            cmdsize,
            version,
        }
    }

    pub fn write_into<W: Write>(&self, write: &mut W) {
        write.write_u32_native(self.cmd);
        write.write_u32_native(self.cmdsize);
        write.write_u64_native(self.version.0);
    }
}

/// A.B.C.D.E packed as a24.b10.c10.d10.e10
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Version(u64);

impl Version {
    /// 40 ~ 64bit
    pub fn a(&self) -> u64 {
        self.0 & 0xFFFF_FF00_0000_0000
    }

    /// 30 ~ 39bit
    pub fn b(&self) -> u64 {
        self.0 & 0x0000_00FF_C000_0000
    }

    /// 20 ~ 29bit
    pub fn c(&self) -> u64 {
        self.0 & 0x0000_0000_3FF0_0000
    }

    /// 10 ~ 19bit
    pub fn d(&self) -> u64 {
        self.0 & 0x0000_0000_000F_FC00
    }

    /// 0 ~ 9bit
    pub fn e(&self) -> u64 {
        self.0 & 0x0000_0000_0000_03FF
    }
}