Skip to main content

fyrox_impl/resource/model/
loader.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Model loader.
22
23use crate::{
24    asset::{
25        io::ResourceIo,
26        loader::{
27            BoxedImportOptionsLoaderFuture, BoxedLoaderFuture, LoaderPayload, ResourceLoader,
28        },
29        manager::ResourceManager,
30        options::{try_get_import_settings, try_get_import_settings_opaque, BaseImportOptions},
31    },
32    core::{uuid::Uuid, TypeUuidProvider},
33    engine::SerializationContext,
34    resource::model::{Model, ModelImportOptions},
35};
36use fyrox_core::dyntype::DynTypeConstructorContainer;
37use fyrox_resource::state::LoadError;
38use std::{path::PathBuf, sync::Arc};
39
40/// Default implementation for model loading.
41pub struct ModelLoader {
42    /// Resource manager to allow complex model loading.
43    pub resource_manager: ResourceManager,
44    /// Node constructors contains a set of constructors that allows to build a node using its
45    /// type UUID.
46    pub serialization_context: Arc<SerializationContext>,
47    /// A container for dynamic types. See [`DynTypeConstructorContainer`] docs for more info.
48    pub dyn_type_constructors: Arc<DynTypeConstructorContainer>,
49    /// Default import options for model resources.
50    pub default_import_options: ModelImportOptions,
51}
52
53impl ResourceLoader for ModelLoader {
54    fn extensions(&self) -> &[&str] {
55        &["rgs", "fbx"]
56    }
57
58    fn is_native_extension(&self, ext: &str) -> bool {
59        fyrox_core::cmp_strings_case_insensitive("rgs", ext)
60    }
61
62    fn data_type_uuid(&self) -> Uuid {
63        Model::type_uuid()
64    }
65
66    fn load(&self, path: PathBuf, io: Arc<dyn ResourceIo>) -> BoxedLoaderFuture {
67        let resource_manager = self.resource_manager.clone();
68        let node_constructors = self.serialization_context.clone();
69        let dyn_type_constructors = self.dyn_type_constructors.clone();
70        let default_import_options = self.default_import_options.clone();
71
72        Box::pin(async move {
73            let io = io.as_ref();
74
75            let import_options = try_get_import_settings(&path, io)
76                .await
77                .unwrap_or(default_import_options);
78
79            let model = Model::load(
80                path,
81                io,
82                node_constructors,
83                dyn_type_constructors,
84                resource_manager,
85                import_options,
86            )
87            .await
88            .map_err(LoadError::new)?;
89
90            Ok(LoaderPayload::new(model))
91        })
92    }
93
94    fn try_load_import_settings(
95        &self,
96        resource_path: PathBuf,
97        io: Arc<dyn ResourceIo>,
98    ) -> BoxedImportOptionsLoaderFuture {
99        Box::pin(async move {
100            try_get_import_settings_opaque::<ModelImportOptions>(&resource_path, &*io).await
101        })
102    }
103
104    fn default_import_options(&self) -> Option<Box<dyn BaseImportOptions>> {
105        Some(Box::<ModelImportOptions>::default())
106    }
107}