1mod accessors;
19mod compile;
20mod load;
21mod names;
22mod outputs;
23mod placeholders;
24mod resource_ids;
25
26pub use compile::compile;
27pub(crate) use names::{accessor_name, default_key, name_problem};
28pub use outputs::write_sources;
29
30use crate::diagnostics::Diagnostic;
31use std::collections::{BTreeMap, HashMap};
32use std::path::{Path, PathBuf};
33
34pub const NEUTRAL_LOCALE: &str = "en_us";
36pub(crate) const RESOURCES_DIR: &str = yog_ids::project::RESOURCES;
38pub const GENERATED_FILE: &str = ".yog-build/generated/resources.g.rs";
42pub const GENERATED_DIR: &str = ".yog-build/generated/resources";
44
45pub(crate) type Output = (String, Vec<u8>);
47
48pub use yog_formats::yres::{Entry, Kind, Value, parse, write};
49
50#[derive(Debug, Clone, Default)]
52pub struct Refs {
53 pub(crate) texts: HashMap<String, (String, String)>,
55 pub(crate) images: HashMap<String, String>,
57 pub(crate) colors: HashMap<String, u32>,
59 pub(crate) pictures: HashMap<String, Picture>,
61 pub(crate) sounds: HashMap<String, String>,
63}
64
65impl Refs {
66 pub(crate) fn text(&self, r: &yog_data::Res) -> Result<&(String, String), String> {
68 self.texts
69 .get(&r.name())
70 .ok_or_else(|| format!("`{r}`: no text `{}` in resources/*.yres", r.name()))
71 }
72
73 pub(crate) fn sound(&self, r: &yog_data::Res) -> Result<&str, String> {
75 self.sounds
76 .get(&r.name())
77 .map(String::as_str)
78 .ok_or_else(|| format!("`{r}`: no sound `{}` in resources/*.yres", r.name()))
79 }
80
81 pub fn color(&self, r: &yog_data::Res) -> Result<u32, String> {
83 self.colors
84 .get(&r.name())
85 .copied()
86 .ok_or_else(|| format!("`{r}`: no color `{}` in resources/*.yres", r.name()))
87 }
88
89 pub(crate) fn key_of(&self, t: &yog_data::Text) -> Result<Option<String>, String> {
92 use yog_data::Text;
93 Ok(match t {
94 Text::Literal(_) => None,
95 Text::Lang(k) => Some(k.clone()),
96 Text::NameOf(r) => Some(yog_ids::lang_key(
97 &r.kind,
98 yog_formats::book::refs::bare(&r.id),
99 )),
100 Text::Res(r) => Some(self.text(r)?.0.clone()),
101 })
102 }
103}
104
105#[derive(Debug, Clone, Default, PartialEq)]
108pub(crate) struct Picture {
109 pub(crate) png: Vec<u8>,
110 pub(crate) output: String,
111}
112
113#[derive(Debug, Default)]
115pub struct Compiled {
116 pub outputs: Vec<Output>,
120 pub sources: BTreeMap<String, String>,
122 pub refs: Refs,
124 pub warnings: Vec<Diagnostic>,
126 pub files: usize,
128}
129
130pub(crate) fn find(root: &Path) -> Vec<PathBuf> {
132 crate::files::under(&root.join(RESOURCES_DIR), &|p| yog_ids::Format::Res.is(p))
133}
134
135pub(crate) fn value_problem(e: &Entry) -> Option<String> {
138 match (e.kind, &e.value) {
139 (k, Value::Bytes(_))
140 if k.holds_bytes()
141 && !yog_ids::valid_path(&e.name.replace('.', yog_ids::sep::PATH)) =>
142 {
143 Some(format!(
144 "{:?}: Minecraft resource paths are lower case — only a-z, 0-9, `_`, `-`, `.`",
145 e.name
146 ))
147 }
148 (k, Value::Bytes(_)) if k.holds_bytes() => None,
149 (k, _) if k.holds_bytes() => Some(format!(
150 "{:?}: a {} entry is bytes (`@bytes:…`)",
151 e.name,
152 k.name()
153 )),
154 (Kind::Color, _) => parse_color(e.text()).is_none().then(|| {
155 format!(
156 "{:?}: `{}` isn't a color — #RRGGBB or #AARRGGBB",
157 e.name,
158 e.text()
159 )
160 }),
161 (Kind::Number, Value::Num(_))
162 | (Kind::Bool, Value::Bool(_))
163 | (Kind::String, Value::Str(_)) => None,
164 (Kind::Number, _) => Some(format!("{:?}: a number", e.name)),
165 (Kind::Bool, _) => Some(format!("{:?}: true or false", e.name)),
166 (Kind::String, _) => Some(format!("{:?}: a text", e.name)),
167 (Kind::Image | Kind::File | Kind::Sound | Kind::Video, _) => None,
168 }
169}
170
171pub(crate) fn color_of(e: &Entry) -> Option<u32> {
173 (e.kind == Kind::Color)
174 .then(|| parse_color(e.text()))
175 .flatten()
176}
177
178fn parse_color(s: &str) -> Option<u32> {
180 crate::yui::color(s).filter(|_| s.trim().starts_with(crate::yui::HEX_MARK))
181}
182
183pub fn resolve_text(refs: &Refs, value: &str) -> Result<(String, Option<String>), String> {
186 match value.strip_prefix('@') {
187 Some(rest) if rest.starts_with('@') => Ok((rest.to_string(), None)),
188 Some(rest) => match refs.texts.get(rest) {
189 Some((key, text)) => Ok((text.clone(), Some(key.clone()))),
190 None => Err(format!("`{value}`: no text `{rest}` in resources/*.yres")),
191 },
192 None => Ok((value.to_string(), None)),
193 }
194}
195
196pub fn resolve_image(refs: &Refs, value: &str) -> Result<String, String> {
198 match value.strip_prefix('@') {
199 Some(rest) => refs
200 .images
201 .get(rest)
202 .cloned()
203 .ok_or_else(|| format!("`{value}`: no image `{rest}` in resources/*.yres")),
204 None => Ok(value.to_string()),
205 }
206}
207
208#[cfg(test)]
209mod tests;
210
211pub(crate) fn problem(msg: impl std::fmt::Display) -> crate::diagnostics::Diagnostic {
213 crate::diagnostics::Diagnostic::error(yog_ids::build_problem::RESOURCES, msg)
214}
215
216pub(crate) fn caveat(msg: impl std::fmt::Display) -> crate::diagnostics::Diagnostic {
218 crate::diagnostics::Diagnostic::warning(yog_ids::build_problem::RESOURCES, msg)
219}