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
use base64;
use serde::Deserializer;
use serde::de::{Error, Visitor};
use std::fmt;
#[derive(Debug, Deserialize, Serialize)]
pub struct MapData {
pub config: Config,
pub model: Vec<Model>,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Config {
pub tilesize_x: u16,
pub tilesize_y: u16,
#[serde(default)]
#[serde(deserialize_with = "decode_base64_opt")]
pub skybox: Option<Vec<u8>>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Model {
#[serde(deserialize_with = "decode_base64")]
pub texture: Vec<u8>,
pub object: Vec<Object>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Object {
pub position: Vertex,
pub vertices: Vec<Vertex>,
pub faces: Vec<Triangle>,
pub uvs: Vec<UV>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Vertex {
pub x: f32,
pub y: f32,
pub z: f32,
}
pub type Triangle = [u8; 3];
pub type UV = [UVCoord; 3];
#[derive(Debug, Deserialize, Serialize)]
pub struct UVCoord {
pub x: f32,
pub y: f32,
}
fn decode_base64_opt<'de, D>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error>
where
D: Deserializer<'de>,
{
deserializer
.deserialize_bytes(Base64Visitor)
.map(|data| Some(data))
}
fn decode_base64<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_bytes(Base64Visitor)
}
struct Base64Visitor;
impl<'de> Visitor<'de> for Base64Visitor {
type Value = Vec<u8>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a base64 byte array")
}
fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
base64::decode_config(&v[22..], base64::MIME).map_err(Error::custom)
}
}