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
pub mod adj_data;
pub mod anim_data;
pub mod hlpb_data;
pub mod matl_data;
pub mod mesh_data;
pub mod meshex_data;
pub mod modl_data;
pub mod shdr_data;
pub mod skel_data;
use binrw::io::{Read, Seek, Write};
use ssbh_lib::prelude::*;
use std::convert::{TryFrom, TryInto};
use std::error::Error;
use std::path::Path;
pub use ssbh_lib::{CString, Color4f, Vector3, Vector4};
pub trait SsbhData: Sized {
type WriteError: Error;
fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>>;
fn read<R: Read + Seek>(reader: &mut R) -> Result<Self, Box<dyn std::error::Error>>;
fn write<W: Write + Seek>(&self, writer: &mut W) -> Result<(), Self::WriteError>;
fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Self::WriteError>;
}
pub mod prelude {
pub use crate::adj_data::AdjData;
pub use crate::anim_data::AnimData;
pub use crate::hlpb_data::HlpbData;
pub use crate::matl_data::MatlData;
pub use crate::mesh_data::MeshData;
pub use crate::meshex_data::MeshExData;
pub use crate::modl_data::ModlData;
pub use crate::shdr_data::ShdrData;
pub use crate::skel_data::SkelData;
pub use crate::SsbhData;
}
macro_rules! ssbh_data_impl {
($ssbh_data:ty, $ssbh_lib:ty, $error:ty) => {
impl SsbhData for $ssbh_data {
type WriteError = $error;
fn from_file<P: AsRef<std::path::Path>>(
path: P,
) -> Result<Self, Box<dyn std::error::Error>> {
<$ssbh_lib>::from_file(path)?.try_into().map_err(Into::into)
}
fn read<R: Read + Seek>(reader: &mut R) -> Result<Self, Box<dyn std::error::Error>> {
<$ssbh_lib>::read(reader)?.try_into().map_err(Into::into)
}
fn write<W: Write + Seek>(&self, writer: &mut W) -> Result<(), Self::WriteError> {
<$ssbh_lib>::try_from(self)?
.write(writer)
.map_err(Into::into)
}
fn write_to_file<P: AsRef<std::path::Path>>(
&self,
path: P,
) -> Result<(), Self::WriteError> {
<$ssbh_lib>::try_from(self)?
.write_to_file(path)
.map_err(Into::into)
}
}
};
}
macro_rules! ssbh_data_infallible_impl {
($ssbh_data:ty, $ssbh_lib:ty, $error:ty) => {
impl SsbhData for $ssbh_data {
type WriteError = $error;
fn from_file<P: AsRef<std::path::Path>>(
path: P,
) -> Result<Self, Box<dyn std::error::Error>> {
Ok(<$ssbh_lib>::from_file(path)?.into())
}
fn read<R: std::io::Read + std::io::Seek>(
reader: &mut R,
) -> Result<Self, Box<dyn std::error::Error>> {
Ok(<$ssbh_lib>::read(reader)?.into())
}
fn write<W: std::io::Write + std::io::Seek>(
&self,
writer: &mut W,
) -> Result<(), Self::WriteError> {
<$ssbh_lib>::from(self).write(writer)
}
fn write_to_file<P: AsRef<std::path::Path>>(
&self,
path: P,
) -> Result<(), Self::WriteError> {
<$ssbh_lib>::from(self).write_to_file(path)
}
}
};
}
ssbh_data_impl!(adj_data::AdjData, Adj, adj_data::error::Error);
ssbh_data_impl!(anim_data::AnimData, Anim, anim_data::error::Error);
ssbh_data_impl!(matl_data::MatlData, Matl, matl_data::error::Error);
ssbh_data_impl!(mesh_data::MeshData, Mesh, mesh_data::error::Error);
ssbh_data_infallible_impl!(meshex_data::MeshExData, MeshEx, std::io::Error);
ssbh_data_infallible_impl!(modl_data::ModlData, Modl, std::io::Error);
ssbh_data_infallible_impl!(hlpb_data::HlpbData, Hlpb, std::io::Error);
ssbh_data_impl!(skel_data::SkelData, Skel, skel_data::error::Error);
#[cfg(test)]
pub(crate) fn group_hex(a: &str, words_per_line: usize) -> String {
use itertools::Itertools;
let words = a
.chars()
.collect::<Vec<char>>()
.chunks(8)
.map(|c| c.iter().collect::<String>())
.collect::<Vec<String>>();
words.chunks(words_per_line).map(|c| c.join(" ")).join("\n")
}
#[cfg(test)]
macro_rules! assert_hex_eq {
($a:expr, $b:expr) => {
pretty_assertions::assert_str_eq!(
crate::group_hex(&hex::encode($a), 8),
crate::group_hex(&hex::encode($b), 8)
)
};
}
#[cfg(test)]
pub(crate) use assert_hex_eq;