godot_binary_serialization/types/
structures.rs

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
use indexmap::IndexMap;

use super::{variant::GodotVariant, TYPE_PADDING};

/// A Vector 2 from godot
#[derive(PartialEq, Debug, Clone, Copy)]
pub struct GodotVector2 {
    pub x: f32,
    pub y: f32,
}

impl GodotVector2 {
    const BIT_SIZE: usize = 8;
    pub fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }
}

impl GodotVariant for GodotVector2 {
    fn byte_length(&self) -> usize {
        TYPE_PADDING as usize + Self::BIT_SIZE
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    fn variant_eq(&self, other: &dyn GodotVariant) -> bool {
        if let Some(other) = other.as_any().downcast_ref::<GodotVector2>() {
            self.x == other.x && self.y == other.y
        } else {
            false
        }
    }

    fn bytes(&self) -> Vec<u8> {
        format!("{}{}", self.x, self.y).as_bytes().to_vec()
    }
}

/// A Vector 3 from godot
#[derive(PartialEq, Debug, Clone, Copy)]
pub struct GodotVector3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

impl GodotVector3 {
    const BIT_SIZE: usize = 12;

    pub fn new(x: f32, y: f32, z: f32) -> Self {
        Self { x, y, z }
    }
}

impl GodotVariant for GodotVector3 {
    fn byte_length(&self) -> usize {
        TYPE_PADDING as usize + Self::BIT_SIZE
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn variant_eq(&self, other: &dyn GodotVariant) -> bool {
        if let Some(other) = other.as_any().downcast_ref::<GodotVector3>() {
            self.x == other.x && self.y == other.y && self.z == other.z
        } else {
            false
        }
    }

    fn bytes(&self) -> Vec<u8> {
        format!("{}{}{}", self.x, self.y, self.y)
            .as_bytes()
            .to_vec()
    }
}

/// A Dictionary from godot, similar to an object in javascript, it consists of key:value pairs
#[derive(Debug)]
pub struct GodotDictionary {
    pub map: IndexMap<Box<dyn GodotVariant>, Box<dyn GodotVariant>>,
    pub byte_size: usize,
}

impl GodotDictionary {
    pub fn new_from_map(map: IndexMap<Box<dyn GodotVariant>, Box<dyn GodotVariant>>) -> Self {
        Self { map, byte_size: 0 }
    }
}

impl GodotVariant for GodotDictionary {
    fn byte_length(&self) -> usize {
        self.byte_size
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn variant_eq(&self, other: &dyn GodotVariant) -> bool {
        if let Some(other) = other.as_any().downcast_ref::<GodotDictionary>() {
            for (key, value) in self.map.iter() {
                for (okey, ovalue) in other.map.iter() {
                    if key != okey || value == ovalue {
                        return false;
                    }
                }
            }

            true
        } else {
            false
        }
    }

    fn bytes(&self) -> Vec<u8> {
        format!("{:?}", self.map).as_bytes().to_vec()
    }
}