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_resource::state::LoadError;
37use std::{path::PathBuf, sync::Arc};
38
39/// Default implementation for model loading.
40pub struct ModelLoader {
41    /// Resource manager to allow complex model loading.
42    pub resource_manager: ResourceManager,
43    /// Node constructors contains a set of constructors that allows to build a node using its
44    /// type UUID.
45    pub serialization_context: Arc<SerializationContext>,
46    /// Default import options for model resources.
47    pub default_import_options: ModelImportOptions,
48}
49
50impl ResourceLoader for ModelLoader {
51    fn extensions(&self) -> &[&str] {
52        &["rgs", "fbx"]
53    }
54
55    fn is_native_extension(&self, ext: &str) -> bool {
56        fyrox_core::cmp_strings_case_insensitive("rgs", ext)
57    }
58
59    fn data_type_uuid(&self) -> Uuid {
60        Model::type_uuid()
61    }
62
63    fn load(&self, path: PathBuf, io: Arc<dyn ResourceIo>) -> BoxedLoaderFuture {
64        let resource_manager = self.resource_manager.clone();
65        let node_constructors = self.serialization_context.clone();
66        let default_import_options = self.default_import_options.clone();
67
68        Box::pin(async move {
69            let io = io.as_ref();
70
71            let import_options = try_get_import_settings(&path, io)
72                .await
73                .unwrap_or(default_import_options);
74
75            let model = Model::load(
76                path,
77                io,
78                node_constructors,
79                resource_manager,
80                import_options,
81            )
82            .await
83            .map_err(LoadError::new)?;
84
85            Ok(LoaderPayload::new(model))
86        })
87    }
88
89    fn try_load_import_settings(
90        &self,
91        resource_path: PathBuf,
92        io: Arc<dyn ResourceIo>,
93    ) -> BoxedImportOptionsLoaderFuture {
94        Box::pin(async move {
95            try_get_import_settings_opaque::<ModelImportOptions>(&resource_path, &*io).await
96        })
97    }
98
99    fn default_import_options(&self) -> Option<Box<dyn BaseImportOptions>> {
100        Some(Box::<ModelImportOptions>::default())
101    }
102}