diff --git a/fyrox-resource/src/lib.rs b/fyrox-resource/src/lib.rs index 16232f2fe..daa764a27 100644 --- a/fyrox-resource/src/lib.rs +++ b/fyrox-resource/src/lib.rs @@ -310,6 +310,17 @@ where phantom: Default::default(), } } + + /// Tries to save the resource to the specified path. + pub fn save(&self, path: &Path) -> Result<(), Box> { + self.untyped.save(path) + } + + /// Tries to save the resource back to its external location. This method will fail on attempt + /// to save embedded resource, because embedded resources does not have external location. + pub fn save_back(&self) -> Result<(), Box> { + self.untyped.save_back() + } } impl Default for Resource diff --git a/fyrox-resource/src/untyped.rs b/fyrox-resource/src/untyped.rs index 5432223e8..cd7ba3f23 100644 --- a/fyrox-resource/src/untyped.rs +++ b/fyrox-resource/src/untyped.rs @@ -4,25 +4,22 @@ use crate::{ core::{ - parking_lot::Mutex, reflect::prelude::*, uuid::Uuid, visitor::prelude::*, TypeUuidProvider, + math::curve::Curve, parking_lot::Mutex, reflect::prelude::*, uuid, uuid::Uuid, + visitor::prelude::*, visitor::RegionGuard, TypeUuidProvider, }, manager::ResourceManager, state::{LoadError, ResourceState}, Resource, ResourceData, ResourceLoadError, TypedResourceData, CURVE_RESOURCE_UUID, MODEL_RESOURCE_UUID, SHADER_RESOURCE_UUID, SOUND_BUFFER_RESOURCE_UUID, TEXTURE_RESOURCE_UUID, }; -use fyrox_core::math::curve::Curve; -use fyrox_core::uuid; -use fyrox_core::visitor::RegionGuard; -use std::ffi::OsStr; -use std::fmt::Display; -use std::path::Path; use std::{ - fmt::{Debug, Formatter}, + error::Error, + ffi::OsStr, + fmt::{Debug, Display, Formatter}, future::Future, hash::{Hash, Hasher}, marker::PhantomData, - path::PathBuf, + path::{Path, PathBuf}, pin::Pin, sync::Arc, task::{Context, Poll}, @@ -369,6 +366,26 @@ impl UntypedResource { self.0.lock().kind = new_kind; } + /// Tries to save the resource to the specified path. + pub fn save(&self, path: &Path) -> Result<(), Box> { + let mut guard = self.0.lock(); + match guard.state { + ResourceState::Pending { .. } | ResourceState::LoadError { .. } => { + Err("Unable to save unloaded resource!".into()) + } + ResourceState::Ok(ref mut data) => data.save(path), + } + } + + /// Tries to save the resource back to its external location. This method will fail on attempt + /// to save embedded resource, because embedded resources does not have external location. + pub fn save_back(&self) -> Result<(), Box> { + match self.kind() { + ResourceKind::Embedded => Err("Embedded resource cannot be saved!".into()), + ResourceKind::External(path) => self.save(&path), + } + } + /// Tries to cast untyped resource to a particular type. pub fn try_cast(&self) -> Option> where