1mod accessors;
19mod compile;
20mod load;
21mod names;
22mod outputs;
23mod placeholders;
24
25pub use compile::compile;
26pub use load::{ResFile, Set};
27pub use names::{
28 accessor_name, default_key, name_problem, valid_locale, valid_name, valid_set_name,
29};
30pub use outputs::write_sources;
31
32use std::collections::{BTreeMap, HashMap};
33use std::path::{Path, PathBuf};
34
35pub const NEUTRAL_LOCALE: &str = "en_us";
37pub const RESOURCES_DIR: &str = yog_ids::project::RESOURCES;
39pub const GENERATED_FILE: &str = ".yog-build/generated/resources.g.rs";
43pub const GENERATED_DIR: &str = ".yog-build/generated/resources";
45
46pub type Output = (String, Vec<u8>);
48
49pub use yog_formats::yres::{Entry, EntryObject, Kind, Value, parse, write};
50
51#[derive(Debug, Clone, Default)]
53pub struct Refs {
54 pub texts: HashMap<String, (String, String)>,
56 pub images: HashMap<String, String>,
58 pub colors: HashMap<String, u32>,
60 pub pictures: HashMap<String, Picture>,
62}
63
64impl Refs {
65 pub fn text(&self, r: &yog_data::Res) -> Result<&(String, String), String> {
67 self.texts
68 .get(&r.name())
69 .ok_or_else(|| format!("`{r}`: no text `{}` in resources/*.yres", r.name()))
70 }
71
72 pub fn color(&self, r: &yog_data::Res) -> Result<u32, String> {
74 self.colors
75 .get(&r.name())
76 .copied()
77 .ok_or_else(|| format!("`{r}`: no color `{}` in resources/*.yres", r.name()))
78 }
79
80 pub fn image(&self, r: &yog_data::Res) -> Result<&str, String> {
82 self.images
83 .get(&r.name())
84 .map(String::as_str)
85 .ok_or_else(|| format!("`{r}`: no image `{}` in resources/*.yres", r.name()))
86 }
87
88 pub fn key_of(&self, t: &yog_data::Text) -> Result<Option<String>, String> {
91 use yog_data::Text;
92 Ok(match t {
93 Text::Literal(_) => None,
94 Text::Lang(k) => Some(k.clone()),
95 Text::NameOf(r) => Some(yog_ids::lang_key(
96 &r.kind,
97 yog_formats::book::refs::bare(&r.id),
98 )),
99 Text::Res(r) => Some(self.text(r)?.0.clone()),
100 })
101 }
102}
103
104#[derive(Debug, Clone, Default, PartialEq)]
107pub struct Picture {
108 pub png: Vec<u8>,
109 pub output: String,
110}
111
112#[derive(Debug, Default)]
114pub struct Compiled {
115 pub outputs: Vec<Output>,
118 pub sources: BTreeMap<String, String>,
120 pub refs: Refs,
121 pub warnings: Vec<String>,
122 pub files: usize,
124}
125
126pub fn find(root: &Path) -> Vec<PathBuf> {
128 crate::files::under(&root.join(RESOURCES_DIR), &|p| yog_ids::Format::Res.is(p))
129}
130
131pub fn value_problem(e: &Entry) -> Option<String> {
134 let media = |what: &str| format!("{:?}: {what} are bytes (`@bytes:…`)", e.name);
135 match (e.kind, &e.value) {
136 (Kind::Image | Kind::File | Kind::Sound, Value::Bytes(_))
137 if !yog_ids::valid_path(&e.name.replace('.', yog_ids::sep::PATH)) =>
138 {
139 Some(format!(
140 "{:?}: Minecraft resource paths are lower case — only a-z, 0-9, `_`, `-`, `.`",
141 e.name
142 ))
143 }
144 (Kind::Sound, Value::Bytes(b)) if !b.starts_with(yog_formats::yres::OGG_MAGIC) => {
145 Some(format!("{:?}: sounds are Ogg Vorbis", e.name))
146 }
147 (Kind::Image | Kind::File | Kind::Sound, Value::Bytes(_)) => None,
148 (Kind::Image, _) => Some(media("images")),
149 (Kind::File, _) => Some(media("files")),
150 (Kind::Sound, _) => Some(media("sounds")),
151 (Kind::Color, _) => parse_color(e.text()).is_none().then(|| {
152 format!(
153 "{:?}: `{}` isn't a color — #RRGGBB or #AARRGGBB",
154 e.name,
155 e.text()
156 )
157 }),
158 (Kind::Number, Value::Num(_))
159 | (Kind::Bool, Value::Bool(_))
160 | (Kind::String, Value::Str(_)) => None,
161 (Kind::Number, _) => Some(format!("{:?}: a number", e.name)),
162 (Kind::Bool, _) => Some(format!("{:?}: true or false", e.name)),
163 (Kind::String, _) => Some(format!("{:?}: a text", e.name)),
164 }
165}
166
167pub fn color_of(e: &Entry) -> Option<u32> {
169 (e.kind == Kind::Color)
170 .then(|| parse_color(e.text()))
171 .flatten()
172}
173
174fn parse_color(s: &str) -> Option<u32> {
176 crate::yui::color(s).filter(|_| s.trim().starts_with(crate::yui::HEX_MARK))
177}
178
179pub fn resolve_text(refs: &Refs, value: &str) -> Result<(String, Option<String>), String> {
182 match value.strip_prefix('@') {
183 Some(rest) if rest.starts_with('@') => Ok((rest.to_string(), None)),
184 Some(rest) => match refs.texts.get(rest) {
185 Some((key, text)) => Ok((text.clone(), Some(key.clone()))),
186 None => Err(format!("`{value}`: no text `{rest}` in resources/*.yres")),
187 },
188 None => Ok((value.to_string(), None)),
189 }
190}
191
192pub fn resolve_image(refs: &Refs, value: &str) -> Result<String, String> {
194 match value.strip_prefix('@') {
195 Some(rest) => refs
196 .images
197 .get(rest)
198 .cloned()
199 .ok_or_else(|| format!("`{value}`: no image `{rest}` in resources/*.yres")),
200 None => Ok(value.to_string()),
201 }
202}
203
204#[cfg(test)]
205mod tests;