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
pub mod buf;
pub mod compression;
mod anim;
mod bitmap;
mod bitmap_font;
mod model;
mod scene;
pub use self::{
anim::{AnimationBuf, Channel},
bitmap::{BitmapBuf, BitmapColor, BitmapFormat},
bitmap_font::BitmapFontBuf,
model::ModelBuf,
scene::{Instance, SceneBuf},
};
use {
paste::paste,
serde::{Deserialize, Serialize},
std::io::{Error, ErrorKind},
};
macro_rules! handle_struct {
($name: ident) => {
paste! {
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, PartialOrd, Ord,
Serialize)]
pub struct [<$name Handle>](usize);
}
};
}
handle_struct!(Animation);
handle_struct!(Bitmap);
handle_struct!(BitmapFont);
handle_struct!(Blob);
handle_struct!(Material);
handle_struct!(Model);
handle_struct!(Scene);
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
pub struct MaterialInfo {
pub color: BitmapHandle,
pub normal: BitmapHandle,
pub params: BitmapHandle,
}
pub trait Pak {
fn material_handle(&self, key: impl AsRef<str>) -> Option<MaterialHandle>;
fn material(&self, handle: MaterialHandle) -> Option<MaterialInfo>;
fn animation_handle(&self, key: impl AsRef<str>) -> Option<AnimationHandle>;
fn bitmap_font_handle(&self, key: impl AsRef<str>) -> Option<BitmapFontHandle>;
fn bitmap_handle(&self, key: impl AsRef<str>) -> Option<BitmapHandle>;
fn blob_handle(&self, key: impl AsRef<str>) -> Option<BlobHandle>;
fn model_handle(&self, key: impl AsRef<str>) -> Option<ModelHandle>;
fn scene_handle(&mut self, key: impl AsRef<str>) -> Option<SceneHandle>;
fn read_animation(&mut self, handle: AnimationHandle) -> Result<AnimationBuf, Error>;
fn read_bitmap_font(&mut self, handle: BitmapFontHandle) -> Result<BitmapFontBuf, Error>;
fn read_bitmap(&mut self, handle: BitmapHandle) -> Result<BitmapBuf, Error>;
fn read_blob(&mut self, handle: BlobHandle) -> Result<Vec<u8>, Error>;
fn read_model(&mut self, handle: ModelHandle) -> Result<ModelBuf, Error>;
fn read_scene(&mut self, handle: SceneHandle) -> Result<SceneBuf, Error>;
fn read_animation_key(&mut self, key: impl AsRef<str>) -> Result<AnimationBuf, Error> {
if let Some(h) = self.animation_handle(key) {
self.read_animation(h)
} else {
Err(Error::from(ErrorKind::InvalidInput))
}
}
fn read_bitmap_font_key(&mut self, key: impl AsRef<str>) -> Result<BitmapFontBuf, Error> {
if let Some(h) = self.bitmap_font_handle(key) {
self.read_bitmap_font(h)
} else {
Err(Error::from(ErrorKind::InvalidInput))
}
}
fn read_bitmap_key(&mut self, key: impl AsRef<str>) -> Result<BitmapBuf, Error> {
if let Some(h) = self.bitmap_handle(key) {
self.read_bitmap(h)
} else {
Err(Error::from(ErrorKind::InvalidInput))
}
}
fn read_blob_key(&mut self, key: impl AsRef<str>) -> Result<Vec<u8>, Error> {
if let Some(h) = self.blob_handle(key) {
self.read_blob(h)
} else {
Err(Error::from(ErrorKind::InvalidInput))
}
}
fn read_model_key(&mut self, key: impl AsRef<str>) -> Result<ModelBuf, Error> {
if let Some(h) = self.model_handle(key) {
self.read_model(h)
} else {
Err(Error::from(ErrorKind::InvalidInput))
}
}
fn read_scene_key(&mut self, key: impl AsRef<str>) -> Result<SceneBuf, Error> {
if let Some(h) = self.scene_handle(key) {
self.read_scene(h)
} else {
Err(Error::from(ErrorKind::InvalidInput))
}
}
}