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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use crate::schemas::models::{Display, Element, GuiLightMode, Model, Texture, Textures};

/// Methods for resolving the properties of a [`Model`] with respect to its
/// parents.
pub struct ModelResolver;

impl ModelResolver {
    /// Iterates through a [`Model`] and all of its parents to resolve all of
    /// the model's properties in a way that reflects the intended inheritance
    /// and/or override behavior of the Minecraft model format.
    ///
    /// The method takes in an iterator of [`Model`]s where the first element is
    /// the model being resolved, and the subsequent elements (if any) are the
    /// chain of parents of that model.
    ///
    /// # Example
    ///
    /// ```
    /// # use minecraft_assets::api::{ModelResolver};
    /// use maplit::hashmap;
    ///
    /// use minecraft_assets::schemas::models::*;
    ///
    /// let parent = Model {
    ///     textures: Some(Textures::from(hashmap! {
    ///         "up" => "#side",
    ///         "down" => "#side"
    ///     })),
    ///     elements: Some(vec![
    ///         Element {
    ///             faces: hashmap! {
    ///                 BlockFace::Up => ElementFace {
    ///                     texture: Texture::from("#up"),
    ///                     ..Default::default()
    ///                 },
    ///                 BlockFace::Down => ElementFace {
    ///                     texture: Texture::from("#down"),
    ///                     ..Default::default()
    ///                 },
    ///                 BlockFace::East => ElementFace {
    ///                     texture: Texture::from("#side"),
    ///                     ..Default::default()
    ///                 },
    ///                 BlockFace::West => ElementFace {
    ///                     texture: Texture::from("#side"),
    ///                     ..Default::default()
    ///                 }
    ///             },
    ///             ..Default::default()
    ///         }
    ///     ]),
    ///     ..Default::default()
    /// };
    ///
    /// let child = Model {
    ///     textures: Some(Textures::from(hashmap! {
    ///         "up" => "textures/up",
    ///         "side" => "textures/side"
    ///     })),
    ///     ..Default::default()
    /// };
    ///
    /// let expected = Model {
    ///     textures: Some(Textures::from(hashmap! {
    ///         "up" => "textures/up",
    ///         "down" => "textures/side",
    ///         "side" => "textures/side"
    ///     })),
    ///     elements: Some(vec![
    ///         Element {
    ///             faces: hashmap! {
    ///                 BlockFace::Up => ElementFace {
    ///                     texture: Texture::from("textures/up"),
    ///                     ..Default::default()
    ///                 },
    ///                 BlockFace::Down => ElementFace {
    ///                     texture: Texture::from("textures/side"),
    ///                     ..Default::default()
    ///                 },
    ///                 BlockFace::East => ElementFace {
    ///                     texture: Texture::from("textures/side"),
    ///                     ..Default::default()
    ///                 },
    ///                 BlockFace::West => ElementFace {
    ///                     texture: Texture::from("textures/side"),
    ///                     ..Default::default()
    ///                 }
    ///             },
    ///             ..Default::default()
    ///         }
    ///     ]),
    ///     ..Default::default()
    /// };
    ///
    /// let resolved = ModelResolver::resolve_model([&child, &parent].into_iter());
    ///
    /// assert_eq!(resolved, expected);
    /// ```
    pub fn resolve_model<'a>(models: impl IntoIterator<Item = &'a Model> + Clone) -> Model {
        let textures = Self::resolve_textures(models.clone());
        let mut elements = Self::resolve_elements(models.clone());

        if let Some(ref mut elements) = elements {
            Self::resolve_element_textures(elements, &textures);
        }

        let display = Self::resolve_display(models.clone());
        let ambient_occlusion = Self::resolve_ambient_occlusion(models.clone());
        let gui_light_mode = Self::resolve_gui_light_mode(models.clone());
        let overrides = models.into_iter().next().unwrap().overrides.clone();

        Model {
            parent: None,
            display,
            textures: Some(textures),
            elements,
            ambient_occlusion,
            gui_light_mode,
            overrides,
        }
    }

    /// Iterates through a [`Model`] and all of its parents to resolve all of
    /// the model's [texture variables].
    ///
    /// This works by merging together the [`Textures`] maps from all models in
    /// the parent-child chain, and then substituting texture variables with
    /// concrete values where possible.
    ///
    /// [texture variables]: Textures#texture-variables
    ///
    /// # Example
    ///
    /// ```
    /// # use minecraft_assets::api::{ModelResolver};
    /// use maplit::hashmap;
    ///
    /// use minecraft_assets::schemas::models::{Model, Textures};
    ///
    /// let child = Model {
    ///     textures: Some(Textures::from(hashmap! {
    ///         "child_texture" => "textures/child",
    ///         "bar" => "#parent_texture"
    ///     })),
    ///     ..Default::default()
    /// };
    ///
    /// let parent = Model {
    ///     textures: Some(Textures::from(hashmap! {
    ///         "parent_texture" => "textures/parent",
    ///         "foo" => "#child_texture"
    ///     })),
    ///     ..Default::default()
    /// };
    ///
    /// // Provide models in increasing level of parenthood.
    /// let models = [child, parent];
    /// let resolved = ModelResolver::resolve_textures(models.iter());
    ///
    /// let expected = Textures::from(hashmap! {
    ///     "parent_texture" => "textures/parent",
    ///     "foo" => "textures/child",              // <------- resolved
    ///     "child_texture" => "textures/child",
    ///     "bar" => "textures/parent"              // <------- resolved    
    /// });
    ///
    /// assert_eq!(resolved, expected);
    /// ```
    pub fn resolve_textures<'a>(models: impl IntoIterator<Item = &'a Model>) -> Textures {
        let mut textures = Textures::default();

        for model in models.into_iter() {
            if let Some(mut parent_textures) = model.textures.clone() {
                // Resolve variables in the parent using the child textures first.
                parent_textures.resolve(&textures);

                // Then resolve variables in the child using the parent textures.
                textures.resolve(&parent_textures);

                // Merge the **child** into the parent.
                std::mem::swap(&mut textures, &mut parent_textures);
                textures.merge(parent_textures.clone());
            }
        }

        textures
    }

    /// Iterates through a [`Model`] and all of its parents to resolve the
    /// model's cuboid [`Element`]s.
    ///
    /// This works by taking the first set of elements present in the chain of
    /// parents. Unlike textures, child definitions for model elements
    /// completely override elements from the parent(s).
    ///
    /// # Example
    ///
    /// ```
    /// # use minecraft_assets::api::{ModelResolver};
    /// use minecraft_assets::schemas::models::{Model, Element};
    ///
    /// let element1 = Element {
    ///     from: [0.0, 0.0, 0.0],
    ///     to: [1.0, 1.0, 1.0],
    ///     ..Default::default()
    /// };
    ///
    /// let element2 = Element {
    ///     from: [5.0, 6.0, 7.0],
    ///     to: [4.0, 3.0, 2.0],
    ///     ..Default::default()
    /// };
    ///
    /// let model1 = Model {
    ///     elements: Some(vec![element1.clone()]),
    ///     ..Default::default()
    /// };
    ///
    /// let model2 = Model {
    ///     elements: Some(vec![element2.clone()]),
    ///     ..Default::default()
    /// };
    ///
    /// let empty = Model::default();
    ///
    /// let resolved = ModelResolver::resolve_elements([&empty, &model1].into_iter());
    /// assert_eq!(resolved, Some(vec![element1.clone()]));
    ///
    /// let resolved = ModelResolver::resolve_elements([&empty, &model2].into_iter());
    /// assert_eq!(resolved, Some(vec![element2.clone()]));
    ///
    /// let resolved = ModelResolver::resolve_elements([&model1, &model2].into_iter());
    /// assert_eq!(resolved, Some(vec![element1.clone()]));
    ///
    /// let resolved = ModelResolver::resolve_elements([&model2, &model1].into_iter());
    /// assert_eq!(resolved, Some(vec![element2.clone()]));
    ///
    /// let resolved = ModelResolver::resolve_elements([&empty, &empty].into_iter());
    /// assert_eq!(resolved, None);
    /// ```
    pub fn resolve_elements<'a>(
        models: impl IntoIterator<Item = &'a Model>,
    ) -> Option<Vec<Element>> {
        Self::first_model_where_some(models, |model| model.elements.as_ref()).cloned()
    }

    /// Iterates through each [`ElementFace`] in each [`Element`] and resolves
    /// any texture variables using the provided map.
    ///
    /// [`ElementFace`]: crate::schemas::models::ElementFace
    pub fn resolve_element_textures<'a>(
        elements: impl IntoIterator<Item = &'a mut Element>,
        textures: &Textures,
    ) {
        for element in elements.into_iter() {
            for face in element.faces.values_mut() {
                if let Some(substitution) = face.texture.resolve(textures) {
                    face.texture = Texture::from(substitution);
                }
            }
        }
    }

    /// Iterates through a [`Model`] and all of its parents to resolve the
    /// model's [`Display`] properties.
    ///
    /// Similar to [`elements`] works by taking the first set of properties
    /// present in the chain of parents.
    ///
    /// [`elements`]: Self::resolve_elements
    pub fn resolve_display<'a>(models: impl IntoIterator<Item = &'a Model>) -> Option<Display> {
        Self::first_model_where_some(models, |model| model.display.as_ref()).cloned()
    }

    /// Iterates through a [`Model`] and all of its parents to resolve the
    /// model's ambient occlusion setting.
    ///
    /// Similar to [`elements`] works by taking the first property value present
    /// in the chain of parents.
    ///
    /// [`elements`]: Self::resolve_elements
    pub fn resolve_ambient_occlusion<'a>(
        models: impl IntoIterator<Item = &'a Model>,
    ) -> Option<bool> {
        Self::first_model_where_some(models, |model| model.ambient_occlusion.as_ref()).copied()
    }

    /// Iterates through a [`Model`] and all of its parents to resolve the
    /// model's GUI light mode setting.
    ///
    /// Similar to [`elements`] works by taking the first property value present
    /// in the chain of parents.
    ///
    /// [`elements`]: Self::resolve_elements
    pub fn resolve_gui_light_mode<'a>(
        models: impl IntoIterator<Item = &'a Model>,
    ) -> Option<GuiLightMode> {
        Self::first_model_where_some(models, |model| model.gui_light_mode.as_ref()).copied()
    }

    fn first_model_where_some<'a, F, T>(
        models: impl IntoIterator<Item = &'a Model>,
        mut op: F,
    ) -> Option<&'a T>
    where
        F: FnMut(&'a Model) -> Option<&'a T>,
    {
        for model in models.into_iter() {
            if let Some(item) = op(model) {
                return Some(item);
            }
        }

        None
    }
}