Skip to main content

fyrox_resource/
metadata.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//! Resource metadata. See [`ResourceMetadata`] docs for more info.
22
23use crate::io::ResourceIo;
24use fyrox_core::{io::FileError, Uuid};
25use ron::ser::PrettyConfig;
26use serde::{Deserialize, Serialize};
27use std::path::Path;
28
29/// Resource metadata contains additional information that cannot be stored in the resource file
30/// itself. The most important bit of information is a resource uuid, this id is used to as reference
31/// that does not change when the resource moved or renamed.
32#[derive(Serialize, Deserialize)]
33pub struct ResourceMetadata {
34    /// A resource uuid, this id is used to as reference that does not change when the resource
35    /// moved or renamed.
36    pub resource_id: Uuid,
37}
38
39impl ResourceMetadata {
40    /// Standard extension of a metadata file.
41    pub const EXTENSION: &'static str = "meta";
42
43    /// Creates a new metadata with random uuid.
44    pub fn new_with_random_id() -> Self {
45        Self {
46            resource_id: Uuid::new_v4(),
47        }
48    }
49
50    /// Tries to load a metadata from the given path.
51    pub async fn load_from_file_async(
52        path: &Path,
53        resource_io: &dyn ResourceIo,
54    ) -> Result<Self, FileError> {
55        resource_io.load_file(path).await.and_then(|metadata| {
56            ron::de::from_bytes::<Self>(&metadata).map_err(|err| {
57                FileError::Custom(format!(
58                    "Unable to deserialize the resource metadata. Reason: {err:?}"
59                ))
60            })
61        })
62    }
63
64    fn serialize(&self, path: &Path) -> Result<String, FileError> {
65        ron::ser::to_string_pretty(self, PrettyConfig::default()).map_err(|err| {
66            FileError::Custom(format!(
67                "Unable to serialize resource metadata for {} resource! Reason: {}",
68                path.display(),
69                err
70            ))
71        })
72    }
73
74    /// Tries to write the metadata to the given path. This method is meant to be used in async
75    /// context. For sync version use [`Self::save_sync`].
76    pub async fn save_async(
77        &self,
78        path: &Path,
79        resource_io: &dyn ResourceIo,
80    ) -> Result<(), FileError> {
81        let string = self.serialize(path)?;
82        resource_io.write_file(path, string.into_bytes()).await
83    }
84
85    /// Tries to write the metadata to the given path.
86    pub fn save_sync(&self, path: &Path, resource_io: &dyn ResourceIo) -> Result<(), FileError> {
87        let string = self.serialize(path)?;
88        resource_io.write_file_sync(path, string.as_bytes())
89    }
90}