Skip to content

Commit

Permalink
save + save_back methods for resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Jul 9, 2024
1 parent 542c724 commit a619639
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
11 changes: 11 additions & 0 deletions fyrox-resource/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
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<dyn Error>> {
self.untyped.save_back()
}
}

impl<T> Default for Resource<T>
Expand Down
35 changes: 26 additions & 9 deletions fyrox-resource/src/untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<dyn Error>> {
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<dyn Error>> {
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<T>(&self) -> Option<Resource<T>>
where
Expand Down

0 comments on commit a619639

Please sign in to comment.